-
Notifications
You must be signed in to change notification settings - Fork 74
feat(model-engine): add GcpPubSubQueueEndpointResourceDelegate for async endpoints on GCP #827
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
Open
postevanus-scale
wants to merge
13
commits into
main
Choose a base branch
from
feat/gcp-pubsub-queue-delegate
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
5ad5016
fix(chart): align with minimal-base image and runtime istio flag
postevanus-scale e5d3050
feat(model-engine): add GcpPubSubQueueEndpointResourceDelegate for as…
postevanus-scale 5afce44
fix(model-engine): address greptile + codex review feedback on gcp pu…
postevanus-scale 6f5b1c9
fix(model-engine): read GCP_PROJECT_ID env first + apply black/isort
postevanus-scale 703fe32
fix(model-engine): prevent topic orphan + drop unused redis delegate …
postevanus-scale 59dbf85
fix(model-engine): narrow gcp_project_id at call site to satisfy mypy
postevanus-scale f67227c
chore(model-engine): regenerate requirements.txt to lock google-cloud…
postevanus-scale 5001650
fix(model-engine): pin opentelemetry-exporter-otlp-proto-grpc
postevanus-scale dcac955
fix(model-engine): lazy pubsub clients + restore newline in helm helper
postevanus-scale 59354e3
fix(clients/python): silence types-setuptools 82.0 package_data stub …
postevanus-scale d843f69
test(api): update test_gcp_provider_selects_gcp_implementations for t…
postevanus-scale 7f8033f
Merge branch 'main' into feat/gcp-pubsub-queue-delegate
postevanus-scale 3711540
ci: retrigger integration tests (sync endpoint readiness flake)
postevanus-scale 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
151 changes: 151 additions & 0 deletions
151
...del_engine_server/infra/gateways/resources/gcp_pubsub_queue_endpoint_resource_delegate.py
This file contains hidden or 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,151 @@ | ||
| from typing import Any, Dict, Optional | ||
|
|
||
| from google.api_core import exceptions as gcp_exceptions | ||
| from google.cloud import pubsub_v1 | ||
| from google.protobuf import field_mask_pb2 | ||
| from model_engine_server.core.loggers import logger_name, make_logger | ||
| from model_engine_server.domain.exceptions import EndpointResourceInfraException | ||
| from model_engine_server.infra.gateways.resources.queue_endpoint_resource_delegate import ( | ||
| QueueEndpointResourceDelegate, | ||
| QueueInfo, | ||
| ) | ||
|
|
||
| logger = make_logger(logger_name()) | ||
|
|
||
| GCP_PUBSUB_MAX_ACK_DEADLINE_SECONDS = 600 # Pub/Sub hard limit | ||
|
|
||
|
|
||
| class GcpPubSubQueueEndpointResourceDelegate(QueueEndpointResourceDelegate): | ||
| """ | ||
| Using GCP Pub/Sub (topic + subscription per endpoint). | ||
|
|
||
| topic_prefix and subscription_prefix control the GCP resource name prefix. | ||
| The logical queue_name returned to callers always uses the canonical | ||
| QueueEndpointResourceDelegate.endpoint_id_to_queue_name format, independent | ||
| of these prefixes. | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| project_id: str, | ||
| topic_prefix: str = "launch-endpoint-id-", | ||
| subscription_prefix: str = "launch-endpoint-id-", | ||
| ) -> None: | ||
| if not project_id: | ||
| raise ValueError( | ||
| "GcpPubSubQueueEndpointResourceDelegate requires a non-empty project_id; " | ||
| "set infra.gcp_project_id in the service config." | ||
| ) | ||
| self.project_id = project_id | ||
| self.topic_prefix = topic_prefix | ||
| self.subscription_prefix = subscription_prefix | ||
| # Lazily-initialized gRPC clients. Construction calls Google ADC which is | ||
| # unavailable in unit-test environments, so defer until first real use. | ||
| # The clients are then cached for the lifetime of the delegate. | ||
| self._publisher_client: Optional[pubsub_v1.PublisherClient] = None | ||
| self._subscriber_client: Optional[pubsub_v1.SubscriberClient] = None | ||
|
|
||
| @property | ||
| def _publisher(self) -> pubsub_v1.PublisherClient: | ||
| if self._publisher_client is None: | ||
| self._publisher_client = pubsub_v1.PublisherClient() | ||
| return self._publisher_client | ||
|
|
||
| @property | ||
| def _subscriber(self) -> pubsub_v1.SubscriberClient: | ||
| if self._subscriber_client is None: | ||
| self._subscriber_client = pubsub_v1.SubscriberClient() | ||
| return self._subscriber_client | ||
|
|
||
| def _topic_id(self, endpoint_id: str) -> str: | ||
| return f"{self.topic_prefix}{endpoint_id}" | ||
|
|
||
| def _subscription_id(self, endpoint_id: str) -> str: | ||
| return f"{self.subscription_prefix}{endpoint_id}" | ||
|
|
||
| async def create_queue_if_not_exists( | ||
| self, | ||
| endpoint_id: str, | ||
| endpoint_name: str, | ||
| endpoint_created_by: str, | ||
| endpoint_labels: Dict[str, Any], | ||
| queue_message_timeout_seconds: Optional[int] = None, | ||
| ) -> QueueInfo: | ||
| queue_name = QueueEndpointResourceDelegate.endpoint_id_to_queue_name(endpoint_id) | ||
| topic_path = f"projects/{self.project_id}/topics/{self._topic_id(endpoint_id)}" | ||
| subscription_path = ( | ||
| f"projects/{self.project_id}/subscriptions/{self._subscription_id(endpoint_id)}" | ||
| ) | ||
| ack_deadline = min(queue_message_timeout_seconds or 60, GCP_PUBSUB_MAX_ACK_DEADLINE_SECONDS) | ||
|
|
||
| try: | ||
| self._publisher.create_topic(name=topic_path) | ||
| except gcp_exceptions.AlreadyExists: | ||
| pass | ||
|
|
||
| try: | ||
| self._subscriber.create_subscription( | ||
| name=subscription_path, | ||
| topic=topic_path, | ||
| ack_deadline_seconds=ack_deadline, | ||
| ) | ||
| except gcp_exceptions.AlreadyExists: | ||
| try: | ||
| self._subscriber.update_subscription( | ||
| subscription=pubsub_v1.types.Subscription( | ||
| name=subscription_path, | ||
| ack_deadline_seconds=ack_deadline, | ||
| ), | ||
| update_mask=field_mask_pb2.FieldMask(paths=["ack_deadline_seconds"]), | ||
| ) | ||
| except gcp_exceptions.GoogleAPIError as e: | ||
| logger.warning( | ||
| f"Failed to update ack_deadline for Pub/Sub subscription {subscription_path}: {e}" | ||
| ) | ||
|
|
||
| # Pub/Sub has no URL concept analogous to SQS queue URLs | ||
| return QueueInfo(queue_name, queue_url=None) | ||
|
|
||
| async def delete_queue(self, endpoint_id: str) -> None: | ||
| subscription_path = ( | ||
| f"projects/{self.project_id}/subscriptions/{self._subscription_id(endpoint_id)}" | ||
| ) | ||
| topic_path = f"projects/{self.project_id}/topics/{self._topic_id(endpoint_id)}" | ||
|
|
||
| # Always attempt BOTH deletions so a failure on one doesn't leave the other resource | ||
| # orphaned (Greptile P1). NotFound is silent. Other GoogleAPIErrors are collected and | ||
| # surfaced together at the end so callers see every cleanup failure, not just the first. | ||
| errors: list[tuple[str, str, gcp_exceptions.GoogleAPIError]] = [] | ||
|
|
||
| try: | ||
| self._subscriber.delete_subscription(subscription=subscription_path) | ||
| except gcp_exceptions.NotFound: | ||
| logger.info( | ||
| f"Could not find Pub/Sub subscription {subscription_path} for endpoint {endpoint_id}" | ||
| ) | ||
| except gcp_exceptions.GoogleAPIError as e: | ||
| errors.append(("subscription", subscription_path, e)) | ||
|
|
||
| try: | ||
| self._publisher.delete_topic(topic=topic_path) | ||
| except gcp_exceptions.NotFound: | ||
| logger.info(f"Could not find Pub/Sub topic {topic_path} for endpoint {endpoint_id}") | ||
| except gcp_exceptions.GoogleAPIError as e: | ||
| errors.append(("topic", topic_path, e)) | ||
|
|
||
| if errors: | ||
| details = "; ".join( | ||
| f"Failed to delete Pub/Sub {kind} {path}: {err}" for kind, path, err in errors | ||
| ) | ||
| raise EndpointResourceInfraException( | ||
| f"Cleanup errors for endpoint {endpoint_id}: {details}" | ||
| ) from errors[0][2] | ||
|
|
||
| async def get_queue_attributes(self, endpoint_id: str) -> Dict[str, Any]: | ||
| queue_name = QueueEndpointResourceDelegate.endpoint_id_to_queue_name(endpoint_id) | ||
| return { | ||
| "name": queue_name, | ||
| # Pub/Sub does not expose a synchronous undelivered message count; | ||
| # real observability requires the Cloud Monitoring API as a separate concern. | ||
| "num_undelivered_messages": -1, | ||
| } | ||
This file contains hidden or 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 hidden or 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.
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.
ack_deadline_seconds. The current expression only clamps to the 600-second ceiling; values of 1–9 (a valid user-suppliedqueue_message_timeout_seconds) will be forwarded to the API and rejected withINVALID_ARGUMENT. Add a lower bound of 10 to mirror the ceiling clamp.Prompt To Fix With AI