Repository navigation
[feat] PineconeGrpcFuture implements concurrent.futures.Future - #410
Conversation
| self.response = upsert_response | ||
|
|
||
| def result(self, timeout): | ||
| def result(self, timeout=None): |
There was a problem hiding this comment.
Just OOC, did you set timeout to None here b/c you automatically set it to 5s if it's None in future.py?
There was a problem hiding this comment.
This MockUpsertDelegate is a standin for the grpc future returned from the grpc runner. That object has this interface: https://grpc.github.io/grpc/python/_modules/grpc.html#Future.result
I adjusted the signature to match the result args in the interface.
aulorbe
left a comment
There was a problem hiding this comment.
lgtm, super clean and modular. Thanks for letting me take a look :)
austin-denoble
left a comment
There was a problem hiding this comment.
Really clean approach, LGTM. Thanks for all of the testing coverage too, very thorough.
And of course thanks for breaking these up into smaller prs.
| else: | ||
| return PineconeException("Unknown GRPC error") | ||
|
|
||
| # def __repr__(self): |
There was a problem hiding this comment.
Remove or uncomment? Assuming these were for debugging.
There was a problem hiding this comment.
Yeah, thanks for calling out. I'll clean these up.
| namespace: Optional[str] = None, | ||
| async_req: Optional[bool] = False, | ||
| **kwargs, | ||
| ) -> Union[FetchResponse, PineconeGrpcFuture]: |
There was a problem hiding this comment.
Just for my understanding... Union is a way to have an "Either" return type? Or is it stating more the future ultimately has a FetchResponse shape once the promise is resolved?
Other question... is this ultimately preferred in Python over having two functions? (e.g. fetch and fetch_async)?
There was a problem hiding this comment.
Just for my understanding... Union is a way to have an "Either" return type? Or is it stating more the future ultimately has a FetchResponse shape once the promise is resolved?
Yes, Union[FetchResponse, PineconeGrpcFuture] means you will get back either a FetchResponse or PineconeGrpcFuture.
Other question... is this ultimately preferred in Python over having two functions? (e.g. fetch and fetch_async)?
I would strongly prefer to have methods with clear return types that don't require any casting or checking to know what you're getting, but if you look elsewhere in this class you'll see this is the general pattern used elsewhere in this class that has been present since before my time. It's something I would like to address in the future, but can't do it right now without creating a breaking change.
Eventually I would like to have something more like this:
# Sync interface, implemented for REST and GRPC
class SyncIndex(abc.ABC):
def fetch(...) -> FetchResponse
class Index(SyncIndex):
def fetch(...) -> FetchResponse
class GRPCIndex(SyncIndex):
def fetch(...) -> FetchResponse
# Asyncio interface, implemented for REST and GRPC
class AsyncioIndex(abc.ABC):
async def fetch(...) -> Awaitable[FetchResponse]
class AsyncioIndex(AsyncioIndex):
async def fetch(...) -> Awaitable[FetchResponse]
class AsyncioIndexGRPC(AsyncioIndex):
async def fetch(...) -> Awaitable[FetchResponse]
# Plus legacy async approaches, currently accessed with async_req=True kwarg
# These return types differ, so can't easily be grouped under one interface. Maybe with
# generics, which have some limited support in Python.
# Async, REST, using multiprocessing
class IndexAsync:
def fetch(...) -> ApplyResult[FetchResponse]
# Async, GRPC, using futures
class GRPCIndexAsync:
def fetch(...) -> PineconeGrpcFuture|
@jhamon thanks for the explanation. As an aside:
The naming is a bit confusing. I would expect it to be some kind of set union. However, the entire concept of an "Either" response type as you mentioned is super odd. |
…strict-config stops killing them (pinecone-io#399) (pinecone-io#410) Both smoke jobs install test deps by name against a published wheel rather than syncing the dev group, so nothing tied them to the pytest ini block. `--strict-config` (pinecone-io#306) then made `timeout = 5` a usage error, and in pytest 9 that error is raised after collection — so the job emitted the suite's collect-time skips and exited 4 having run none of the 12 tests it collected. Names the two plugins explicitly, at the dev group's versions, and adds a source-scan guard that holds both install lines to [tool.pytest.ini_options] and to the markers tests/smoke/ applies.
Problem
GRPCIndexhas long had limited and poorly documented support for async operations via the futures interface of thegrpclibrary. I've recently been trying to implementquery_namespacesusing these futures, and discovered that unfortunately the grpc futures implementation is not compatible with theconcurrent.futurespackage in the standard library. This makes them pretty much useless for anything at all complicated because the grpc library doesn't provide any utils for synchronization or waiting.Solution
A class called
PineconeGrpcFuturewas added in the past as a minimal wrapper around the future that is emitted bygrpc. These futures objects are used to represent asynchronous computation, and allow you to regisiter callbacks withadd_done_callback. This is similar to callingthen()on a javascript promise.The original purpose of our
PineconeGrpcFuturewrapper class seems to have been to implement some basic (very basic) error mapping, but for this diff I decided to extend the class to implement theconcurrent.futures.Futureinterface. This allows the instances ofPineconeGrpcFutureto be used withconcurrent.futures.as_completedandconcurrent.futures.waitutilities, which makes them dramatically more ergonomic to deal with. Unfortunately the grpc future is not compatible with theconcurrent.futurepackage out of the box.For the unit tests of
PineconeGrpcFuture, I had to make heavy use of mocking because all the various grpc classes are tightly coupled and can't be simply setup without performing actual network calls. This doesn't give me huge confidence it's actually working as expected, so as a sanity check I added some additional integration test coverage forupsert,fetch, anddeleteusingconcurrent.futures.wait.Type of Change
Test Plan
Added unit and integration tests