-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Refactor] Extract GrpcRunner from GRPCIndexBase class (#395)
## Problem This is another extractive refactoring in preparation for grpc with asyncio. ## Solution The generated stub class, `VectorServiceStub`, is what knows how to call the Pinecone grpc service, but our wrapper code needs to do some work to make sure we have a consistent approach to "metadata" (grpc-speak for request headers) and handling other request params like `timeout`. Previously this work was accomplished in a private method of the `GRPCIndexBase` base class called `_wrap_grpc_call()`. Since we will need to perform almost identical marshaling of metadata for requests with asyncio, I pulled this logic out into a separate class `GrpcRunner` and renamed `_wrap_grpc_call` to `run`. You can see there is also a parallel method implementation called `run_asyncio`; currently this is unused and untested, but kind of illustrates why this refactor is useful. ## Type of Change - [ ] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) - [ ] This change requires a documentation update - [ ] Infrastructure change (CI configs, etc) - [ ] Non-code change (docs, etc) - [x] None of the above: Mechanical refactor, should have no net impact to functionality. ## Test Plan Tests should still be green
- Loading branch information
Showing
10 changed files
with
287 additions
and
144 deletions.
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 |
---|---|---|
@@ -0,0 +1,97 @@ | ||
from functools import wraps | ||
from typing import Dict, Tuple, Optional | ||
|
||
from grpc._channel import _InactiveRpcError | ||
|
||
from pinecone import Config | ||
from .utils import _generate_request_id | ||
from .config import GRPCClientConfig | ||
from pinecone.utils.constants import REQUEST_ID, CLIENT_VERSION | ||
from pinecone.exceptions.exceptions import PineconeException | ||
from grpc import CallCredentials, Compression | ||
from google.protobuf.message import Message | ||
|
||
|
||
class GrpcRunner: | ||
def __init__(self, index_name: str, config: Config, grpc_config: GRPCClientConfig): | ||
self.config = config | ||
self.grpc_client_config = grpc_config | ||
|
||
self.fixed_metadata = { | ||
"api-key": config.api_key, | ||
"service-name": index_name, | ||
"client-version": CLIENT_VERSION, | ||
} | ||
if self.grpc_client_config.additional_metadata: | ||
self.fixed_metadata.update(self.grpc_client_config.additional_metadata) | ||
|
||
def run( | ||
self, | ||
func, | ||
request: Message, | ||
timeout: Optional[int] = None, | ||
metadata: Optional[Dict[str, str]] = None, | ||
credentials: Optional[CallCredentials] = None, | ||
wait_for_ready: Optional[bool] = None, | ||
compression: Optional[Compression] = None, | ||
): | ||
@wraps(func) | ||
def wrapped(): | ||
user_provided_metadata = metadata or {} | ||
_metadata = self._prepare_metadata(user_provided_metadata) | ||
try: | ||
return func( | ||
request, | ||
timeout=timeout, | ||
metadata=_metadata, | ||
credentials=credentials, | ||
wait_for_ready=wait_for_ready, | ||
compression=compression, | ||
) | ||
except _InactiveRpcError as e: | ||
raise PineconeException(e._state.debug_error_string) from e | ||
|
||
return wrapped() | ||
|
||
async def run_asyncio( | ||
self, | ||
func, | ||
request: Message, | ||
timeout: Optional[int] = None, | ||
metadata: Optional[Dict[str, str]] = None, | ||
credentials: Optional[CallCredentials] = None, | ||
wait_for_ready: Optional[bool] = None, | ||
compression: Optional[Compression] = None, | ||
): | ||
@wraps(func) | ||
async def wrapped(): | ||
user_provided_metadata = metadata or {} | ||
_metadata = self._prepare_metadata(user_provided_metadata) | ||
try: | ||
return await func( | ||
request, | ||
timeout=timeout, | ||
metadata=_metadata, | ||
credentials=credentials, | ||
wait_for_ready=wait_for_ready, | ||
compression=compression, | ||
) | ||
except _InactiveRpcError as e: | ||
raise PineconeException(e._state.debug_error_string) from e | ||
|
||
return await wrapped() | ||
|
||
def _prepare_metadata( | ||
self, user_provided_metadata: Dict[str, str] | ||
) -> Tuple[Tuple[str, str], ...]: | ||
return tuple( | ||
(k, v) | ||
for k, v in { | ||
**self.fixed_metadata, | ||
**self._request_metadata(), | ||
**user_provided_metadata, | ||
}.items() | ||
) | ||
|
||
def _request_metadata(self) -> Dict[str, str]: | ||
return {REQUEST_ID: _generate_request_id()} |
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
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
Oops, something went wrong.