-
-
Notifications
You must be signed in to change notification settings - Fork 7
feat: Add a BatchPushTaskWorker for batched updates to the broker #695
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
Merged
Merged
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| from .worker import PushTaskWorker, TaskWorker | ||
| from .worker import BatchPushTaskWorker, PushTaskWorker, TaskWorker | ||
|
|
||
| __all__ = ("TaskWorker", "PushTaskWorker") | ||
| __all__ = ("TaskWorker", "PushTaskWorker", "BatchPushTaskWorker") |
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
192 changes: 192 additions & 0 deletions
192
clients/python/src/taskbroker_client/worker/push_clients.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,192 @@ | ||
| import logging | ||
| import threading | ||
| import time | ||
| from typing import TYPE_CHECKING, Any | ||
|
|
||
| import grpc | ||
| from sentry_protos.taskbroker.v1.taskbroker_pb2 import ( | ||
| SetBatchActivationStatusRequest, | ||
| SetTaskStatusRequest, | ||
| ) | ||
| from sentry_protos.taskbroker.v1.taskbroker_pb2_grpc import ConsumerServiceStub | ||
|
|
||
| from taskbroker_client.metrics import MetricsBackend | ||
| from taskbroker_client.types import ProcessingResult | ||
| from taskbroker_client.worker.client import ( | ||
| MAX_ACTIVATION_SIZE, | ||
| HealthCheckSettings, | ||
| RequestSignatureInterceptor, | ||
| parse_rpc_secret_list, | ||
| ) | ||
|
|
||
| if TYPE_CHECKING: | ||
| ServerInterceptor = grpc.ServerInterceptor[Any, Any] | ||
| else: | ||
| ServerInterceptor = grpc.ServerInterceptor | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class PushTaskbrokerClient: | ||
| """ | ||
| Taskworker RPC client wrapper | ||
|
|
||
| Push brokers are a deployment so they don't need to be connected to individually. There is one service provided | ||
| that works for all the brokers. | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| service: str, | ||
| application: str, | ||
| metrics: MetricsBackend, | ||
| health_check_settings: HealthCheckSettings | None = None, | ||
| rpc_secret: str | None = None, | ||
| grpc_config: str | None = None, | ||
| ) -> None: | ||
| self._application = application | ||
| self._service = service | ||
| self._rpc_secret = rpc_secret | ||
| self._metrics = metrics | ||
|
|
||
| self._grpc_options: list[tuple[str, Any]] = [ | ||
| ("grpc.max_receive_message_length", MAX_ACTIVATION_SIZE) | ||
| ] | ||
| if grpc_config: | ||
| self._grpc_options.append(("grpc.service_config", grpc_config)) | ||
|
|
||
| logger.info( | ||
| "taskworker.push_client.start", | ||
| extra={"service": service, "options": self._grpc_options}, | ||
| ) | ||
|
|
||
| self._stub = self._connect_to_host(service) | ||
|
|
||
| self._health_check_settings = health_check_settings | ||
| self._timestamp_since_touch_lock = threading.Lock() | ||
| self._timestamp_since_touch = 0.0 | ||
|
|
||
| def _emit_health_check(self) -> None: | ||
| if self._health_check_settings is None: | ||
| return | ||
|
|
||
| with self._timestamp_since_touch_lock: | ||
| cur_time = time.time() | ||
| if ( | ||
| cur_time - self._timestamp_since_touch | ||
| < self._health_check_settings.touch_interval_sec | ||
| ): | ||
| return | ||
|
|
||
| self._health_check_settings.file_path.touch() | ||
| self._metrics.incr( | ||
| "taskworker.client.health_check.touched", | ||
| ) | ||
| self._timestamp_since_touch = cur_time | ||
|
|
||
| def _connect_to_host(self, host: str) -> ConsumerServiceStub: | ||
| logger.info("taskworker.push_client.connect", extra={"host": host}) | ||
| channel = grpc.insecure_channel(host, options=self._grpc_options) | ||
| secrets = parse_rpc_secret_list(self._rpc_secret) | ||
| if secrets: | ||
| channel = grpc.intercept_channel(channel, RequestSignatureInterceptor(secrets)) | ||
| return ConsumerServiceStub(channel) | ||
|
|
||
| def emit_health_check(self) -> None: | ||
| self._emit_health_check() | ||
|
|
||
| def update_tasks(self, processing_results: list[ProcessingResult]) -> None: | ||
| for processing_result in processing_results: | ||
| self._update_task_single(processing_result) | ||
|
|
||
| def _update_task_single( | ||
| self, | ||
| processing_result: ProcessingResult, | ||
| ) -> None: | ||
| """ | ||
| Update the status for a given task activation. | ||
| """ | ||
| self._emit_health_check() | ||
|
|
||
| request = SetTaskStatusRequest( | ||
| id=processing_result.task_id, | ||
| status=processing_result.status, | ||
| fetch_next_task=None, | ||
| max_attempts=processing_result.max_attempts, | ||
| delay_on_retry=processing_result.delay_on_retry, | ||
| ) | ||
|
|
||
| retries = 0 | ||
| exception = None | ||
| while retries < 3: | ||
| try: | ||
| with self._metrics.timer( | ||
| "taskworker.update_task.rpc", tags={"service": self._service} | ||
| ): | ||
| self._stub.SetTaskStatus(request) | ||
| exception = None | ||
| break | ||
| except grpc.RpcError as err: | ||
| exception = err | ||
| self._metrics.incr( | ||
| "taskworker.client.rpc_error", | ||
| tags={"method": "SetTaskStatus", "status": err.code().name}, | ||
| ) | ||
| finally: | ||
| retries += 1 | ||
|
|
||
| if exception: | ||
| raise exception | ||
|
|
||
|
|
||
| class BatchPushTaskbrokerClient(PushTaskbrokerClient): | ||
| """ | ||
| Taskworker RPC client wrapper | ||
|
|
||
| Push brokers are a deployment so they don't need to be connected to individually. There is one service provided | ||
| that works for all the brokers. This client pushes batches of activation updates. | ||
| """ | ||
|
|
||
| def update_tasks( | ||
| self, | ||
| processing_results: list[ProcessingResult], | ||
| ) -> None: | ||
| """ | ||
| Update the status for a given task activation. | ||
| """ | ||
| self._emit_health_check() | ||
|
|
||
| request = SetBatchActivationStatusRequest( | ||
| updates=[ | ||
| SetTaskStatusRequest( | ||
| id=processing_result.task_id, | ||
| status=processing_result.status, | ||
| fetch_next_task=None, | ||
| max_attempts=processing_result.max_attempts, | ||
| delay_on_retry=processing_result.delay_on_retry, | ||
| ) | ||
| for processing_result in processing_results | ||
| ] | ||
| ) | ||
|
|
||
| retries = 0 | ||
| exception = None | ||
| while retries < 3: | ||
| try: | ||
| with self._metrics.timer( | ||
| "taskworker.update_task_batch.rpc", tags={"service": self._service} | ||
| ): | ||
| self._stub.SetBatchActivationStatus(request) | ||
| exception = None | ||
| break | ||
| except grpc.RpcError as err: | ||
| exception = err | ||
| self._metrics.incr( | ||
| "taskworker.client.rpc_error", | ||
| tags={"method": "SetBatchActivationStatus", "status": err.code().name}, | ||
| ) | ||
| finally: | ||
| retries += 1 | ||
|
|
||
| if exception: | ||
| raise exception |
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.
Uh oh!
There was an error while loading. Please reload this page.