-
Notifications
You must be signed in to change notification settings - Fork 84
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[feat] PineconeGrpcFuture implements concurrent.futures.Future #410
Merged
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
edfe31d
Implement concurrent.futures interface for PineconeGrpcFuture
jhamon a75805d
Add integration tests for async_req with upsert, fetch, delete
jhamon 54c2fa3
Fixes for mypy
jhamon b26f05e
Fix broken unit test
jhamon 8f3adcd
Test fixes
jhamon 28dbadd
Fix grpc test imports
jhamon 4312f8a
Remove commented code
jhamon ff9a9de
Update unit test with grpc.Future interface
jhamon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,86 @@ | ||
from grpc._channel import _MultiThreadedRendezvous | ||
from concurrent.futures import Future as ConcurrentFuture | ||
from typing import Optional | ||
from grpc import Future as GrpcFuture, RpcError | ||
from pinecone.exceptions.exceptions import PineconeException | ||
|
||
|
||
class PineconeGrpcFuture: | ||
def __init__(self, delegate): | ||
self._delegate = delegate | ||
class PineconeGrpcFuture(ConcurrentFuture): | ||
def __init__( | ||
self, grpc_future: GrpcFuture, timeout: Optional[int] = None, result_transformer=None | ||
): | ||
super().__init__() | ||
self._grpc_future = grpc_future | ||
self._result_transformer = result_transformer | ||
if timeout is not None: | ||
self._default_timeout = timeout # seconds | ||
else: | ||
self._default_timeout = 5 # seconds | ||
|
||
def cancel(self): | ||
return self._delegate.cancel() | ||
# Sync initial state, in case the gRPC future is already done | ||
self._sync_state(self._grpc_future) | ||
|
||
def cancelled(self): | ||
return self._delegate.cancelled() | ||
# Add callback to subscribe to updates from the gRPC future | ||
self._grpc_future.add_done_callback(self._sync_state) | ||
|
||
def running(self): | ||
return self._delegate.running() | ||
@property | ||
def grpc_future(self): | ||
return self._grpc_future | ||
|
||
def done(self): | ||
return self._delegate.done() | ||
def _sync_state(self, grpc_future): | ||
if self.done(): | ||
return | ||
|
||
def add_done_callback(self, fun): | ||
return self._delegate.add_done_callback(fun) | ||
if grpc_future.cancelled(): | ||
self.cancel() | ||
elif grpc_future.exception(timeout=self._default_timeout): | ||
self.set_exception(grpc_future.exception()) | ||
elif grpc_future.done(): | ||
try: | ||
result = grpc_future.result(timeout=self._default_timeout) | ||
self.set_result(result) | ||
except Exception as e: | ||
self.set_exception(e) | ||
elif grpc_future.running(): | ||
self.set_running_or_notify_cancel() | ||
|
||
def result(self, timeout=None): | ||
try: | ||
return self._delegate.result(timeout=timeout) | ||
except _MultiThreadedRendezvous as e: | ||
raise PineconeException(e._state.debug_error_string) from e | ||
def set_result(self, result): | ||
if self._result_transformer: | ||
result = self._result_transformer(result) | ||
return super().set_result(result) | ||
|
||
def cancel(self): | ||
self._grpc_future.cancel() | ||
return super().cancel() | ||
|
||
def exception(self, timeout=None): | ||
return self._delegate.exception(timeout=timeout) | ||
exception = super().exception(timeout=self._timeout(timeout)) | ||
if isinstance(exception, RpcError): | ||
return self._wrap_rpc_exception(exception) | ||
return exception | ||
|
||
def traceback(self, timeout=None): | ||
return self._delegate.traceback(timeout=timeout) | ||
# This is not part of the ConcurrentFuture interface, but keeping it for | ||
# backward compatibility | ||
return self._grpc_future.traceback(timeout=self._timeout(timeout)) | ||
|
||
def result(self, timeout=None): | ||
try: | ||
return super().result(timeout=self._timeout(timeout)) | ||
except RpcError as e: | ||
raise self._wrap_rpc_exception(e) from e | ||
|
||
def _timeout(self, timeout: Optional[int] = None) -> int: | ||
if timeout is not None: | ||
return timeout | ||
else: | ||
return self._default_timeout | ||
|
||
def _wrap_rpc_exception(self, e): | ||
if e._state and e._state.debug_error_string: | ||
return PineconeException(e._state.debug_error_string) | ||
else: | ||
return PineconeException("Unknown GRPC error") | ||
|
||
def __del__(self): | ||
self._grpc_future.cancel() | ||
self = None # release the reference to the grpc future |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import os | ||
import pytest | ||
from pinecone import Vector | ||
from ..helpers import poll_stats_for_namespace, random_string | ||
|
||
if os.environ.get("USE_GRPC") == "true": | ||
from pinecone.grpc import GRPCDeleteResponse | ||
|
||
|
||
class TestDeleteFuture: | ||
@pytest.mark.skipif( | ||
os.getenv("USE_GRPC") != "true", reason="PineconeGrpcFutures only returned from grpc client" | ||
) | ||
def test_delete_future(self, idx): | ||
namespace = random_string(10) | ||
|
||
idx.upsert( | ||
vectors=[ | ||
Vector(id="id1", values=[0.1, 0.2]), | ||
Vector(id="id2", values=[0.1, 0.2]), | ||
Vector(id="id3", values=[0.1, 0.2]), | ||
], | ||
namespace=namespace, | ||
) | ||
poll_stats_for_namespace(idx, namespace, 3) | ||
|
||
delete_one = idx.delete(ids=["id1"], namespace=namespace, async_req=True) | ||
delete_namespace = idx.delete(namespace=namespace, delete_all=True, async_req=True) | ||
|
||
from concurrent.futures import as_completed | ||
|
||
for future in as_completed([delete_one, delete_namespace], timeout=10): | ||
resp = future.result() | ||
assert isinstance(resp, GRPCDeleteResponse) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import os | ||
import pytest | ||
|
||
if os.environ.get("USE_GRPC") == "true": | ||
from pinecone.grpc import PineconeGrpcFuture | ||
|
||
|
||
@pytest.mark.skipif( | ||
os.getenv("USE_GRPC") != "true", reason="PineconeGrpcFutures only returned from grpc client" | ||
) | ||
class TestFetchFuture: | ||
def setup_method(self): | ||
self.expected_dimension = 2 | ||
|
||
def test_fetch_multiple_by_id(self, idx, namespace): | ||
target_namespace = namespace | ||
|
||
results = idx.fetch(ids=["1", "2", "4"], namespace=target_namespace, async_req=True) | ||
assert isinstance(results, PineconeGrpcFuture) | ||
|
||
from concurrent.futures import wait, FIRST_COMPLETED | ||
|
||
done, _ = wait([results], return_when=FIRST_COMPLETED) | ||
|
||
results = done.pop().result() | ||
assert results.usage is not None | ||
assert results.usage["read_units"] is not None | ||
assert results.usage["read_units"] > 0 | ||
|
||
assert results.namespace == target_namespace | ||
assert len(results.vectors) == 3 | ||
assert results.vectors["1"].id == "1" | ||
assert results.vectors["2"].id == "2" | ||
# Metadata included, if set | ||
assert results.vectors["1"].metadata is None | ||
assert results.vectors["2"].metadata is None | ||
assert results.vectors["4"].metadata is not None | ||
assert results.vectors["4"].metadata["genre"] == "action" | ||
assert results.vectors["4"].metadata["runtime"] == 120 | ||
# Values included | ||
assert results.vectors["1"].values is not None | ||
assert len(results.vectors["1"].values) == self.expected_dimension | ||
|
||
def test_fetch_single_by_id(self, idx, namespace): | ||
target_namespace = namespace | ||
|
||
future = idx.fetch(ids=["1"], namespace=target_namespace, async_req=True) | ||
|
||
from concurrent.futures import wait, FIRST_COMPLETED | ||
|
||
done, _ = wait([future], return_when=FIRST_COMPLETED) | ||
results = done.pop().result() | ||
|
||
assert results.namespace == target_namespace | ||
assert len(results.vectors) == 1 | ||
assert results.vectors["1"].id == "1" | ||
assert results.vectors["1"].metadata is None | ||
assert results.vectors["1"].values is not None | ||
assert len(results.vectors["1"].values) == self.expected_dimension | ||
|
||
def test_fetch_nonexistent_id(self, idx, namespace): | ||
target_namespace = namespace | ||
|
||
# Fetch id that is missing | ||
future = idx.fetch(ids=["100"], namespace=target_namespace, async_req=True) | ||
|
||
from concurrent.futures import wait, FIRST_COMPLETED | ||
|
||
done, _ = wait([future], return_when=FIRST_COMPLETED) | ||
results = done.pop().result() | ||
|
||
assert results.namespace == target_namespace | ||
assert len(results.vectors) == 0 | ||
|
||
def test_fetch_nonexistent_namespace(self, idx): | ||
target_namespace = "nonexistent-namespace" | ||
|
||
# Fetch from namespace with no vectors | ||
future = idx.fetch(ids=["1"], namespace=target_namespace, async_req=True) | ||
|
||
from concurrent.futures import wait, FIRST_COMPLETED | ||
|
||
done, _ = wait([future], return_when=FIRST_COMPLETED) | ||
results = done.pop().result() | ||
|
||
assert results.namespace == target_namespace | ||
assert len(results.vectors) == 0 | ||
|
||
def test_fetch_unspecified_namespace(self, idx): | ||
# Fetch without specifying namespace gives default namespace results | ||
future = idx.fetch(ids=["1", "4"], async_req=True) | ||
|
||
from concurrent.futures import wait, FIRST_COMPLETED | ||
|
||
done, _ = wait([future], return_when=FIRST_COMPLETED) | ||
results = done.pop().result() | ||
|
||
assert results.namespace == "" | ||
assert results.vectors["1"].id == "1" | ||
assert results.vectors["1"].values is not None | ||
assert results.vectors["4"].metadata is not None |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just for my understanding...
Union
is a way to have an "Either" return type? Or is it stating more the future ultimately has aFetchResponse
shape once the promise is resolved?Other question... is this ultimately preferred in Python over having two functions? (e.g.
fetch
andfetch_async
)?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes,
Union[FetchResponse, PineconeGrpcFuture]
means you will get back either aFetchResponse
orPineconeGrpcFuture
.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: