Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions clients/python/src/taskbroker_client/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,15 @@ def pull(
default=50052,
type=int,
)
@click.option(
"--health-port",
help=(
"Serve gRPC health checks on this port, separate from the push server. When set, pushes "
"beyond --concurrency are rejected with RESOURCE_EXHAUSTED instead of queueing."
),
default=None,
type=int,
)
@click.option(
"--grpc-max-message-size",
help="Maximum gRPC send/receive message size, in bytes.",
Expand Down Expand Up @@ -365,6 +374,7 @@ def push(
health_check_file_path: str | None,
health_check_sec_per_touch: float,
grpc_port: int,
health_port: int | None,
grpc_max_message_size: int,
push_task_timeout: float,
skip_awaiting_futures: bool,
Expand Down Expand Up @@ -395,6 +405,7 @@ def push(
health_check_file_path=health_check_file_path,
health_check_sec_per_touch=health_check_sec_per_touch,
grpc_port=grpc_port,
health_port=health_port,
grpc_max_message_size=grpc_max_message_size,
push_task_timeout=push_task_timeout,
skip_awaiting_futures=skip_awaiting_futures,
Expand Down
24 changes: 23 additions & 1 deletion clients/python/src/taskbroker_client/worker/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ def __init__(
health_check_file_path: str | None = None,
health_check_sec_per_touch: float = DEFAULT_WORKER_HEALTH_CHECK_SEC_PER_TOUCH,
grpc_port: int = 50052,
health_port: int | None = None,
grpc_max_message_size: int = -1,
push_task_timeout: float = 5,
skip_awaiting_futures: bool = True,
Expand Down Expand Up @@ -336,6 +337,7 @@ def __init__(
self._processing_pool_name: str = processing_pool_name or "unknown"

self._grpc_port = grpc_port
self._health_port = health_port
self._grpc_max_message_size = grpc_max_message_size
self._grpc_secrets = parse_rpc_secret_list(app.config["rpc_secret"])
self._push_task_timeout = push_task_timeout
Expand Down Expand Up @@ -521,6 +523,7 @@ def start(self) -> int:
self.worker_pool.start_spawn_children_thread()

server: grpc.Server | None = None
health_server: grpc.Server | None = None
server_started = False
health_servicer: health.HealthServicer | None = None

Expand Down Expand Up @@ -558,6 +561,11 @@ def signal_handler(*args: Any) -> None:
("grpc.max_receive_message_length", max_message_size),
("grpc.max_send_message_length", max_message_size),
],
# One RPC per handler thread, so excess pushes are rejected instead of queueing.
# The limit covers every service on the server, so it needs health on its own server.
maximum_concurrent_rpcs=(
self._concurrency if self._health_port is not None else None
),
)

taskbroker_pb2_grpc.add_WorkerServiceServicer_to_server(
Expand All @@ -573,10 +581,17 @@ def signal_handler(*args: Any) -> None:

server.add_insecure_port(f"[::]:{self._grpc_port}")

if self._health_port is not None:
health_server = grpc.server(ThreadPoolExecutor(max_workers=1))
health_pb2_grpc.add_HealthServicer_to_server(health_servicer, health_server)
health_server.add_insecure_port(f"[::]:{self._health_port}")

# Don't accept connections we are about to drop.
if self._shutdown_signal.is_set():
return 0

if health_server is not None:
health_server.start()
server.start()
server_started = True

Expand All @@ -593,7 +608,10 @@ def signal_handler(*args: Any) -> None:
health_servicer.set("", health_pb2.HealthCheckResponse.SERVING)
health_servicer.set(WORKER_SERVICE_NAME, health_pb2.HealthCheckResponse.SERVING)

logger.info("taskworker.grpc_server.started", extra={"port": self._grpc_port})
logger.info(
"taskworker.grpc_server.started",
extra={"port": self._grpc_port, "health_port": self._health_port},
)
self._start_health_check_thread()

# Poll so a signal handler that only flipped a bool still gets us
Expand All @@ -619,6 +637,10 @@ def signal_handler(*args: Any) -> None:
if server is not None and server_started:
server.stop(grace=5)

# Stopped after the push server so probes keep seeing NOT_SERVING during its grace period.
if health_server is not None and server_started:
health_server.stop(grace=None)
Comment thread
evanh marked this conversation as resolved.
Resolved

self.shutdown()

return 0
Expand Down
64 changes: 64 additions & 0 deletions clients/python/tests/worker/test_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1173,6 +1173,70 @@ def wait_for_termination(timeout: float | None = None) -> bool:
pool_shutdown.assert_called_once_with()


def test_push_start_without_health_port_has_no_rpc_limit() -> None:
taskworker = _make_push_worker(concurrency=2, warmup_timeout=5)
taskworker._shutdown_signal.request()

fake_health = mock.MagicMock()
fake_server = mock.MagicMock()

with (
_push_worker_grpc_mocks(taskworker, fake_server, fake_health),
mock.patch(
"taskbroker_client.worker.worker.grpc.server", return_value=fake_server
) as grpc_server,
):
taskworker.start()

grpc_server.assert_called_once()
assert grpc_server.call_args.kwargs["maximum_concurrent_rpcs"] is None


def test_push_start_serves_health_on_separate_server() -> None:
taskworker = _make_push_worker(concurrency=2, warmup_timeout=5, health_port=50098)
handlers: dict[int, Callable[..., None]] = {}

fake_health = mock.MagicMock()
fake_server = mock.MagicMock()
fake_health_server = mock.MagicMock()

def wait_for_termination(timeout: float | None = None) -> bool:
handlers[signal.SIGTERM](signal.SIGTERM, None)
return True

fake_server.wait_for_termination.side_effect = wait_for_termination

with (
_push_worker_grpc_mocks(taskworker, fake_server, fake_health, handlers),
mock.patch(
"taskbroker_client.worker.worker.grpc.server",
side_effect=[fake_server, fake_health_server],
) as grpc_server,
mock.patch(
"taskbroker_client.worker.worker.health_pb2_grpc.add_HealthServicer_to_server"
) as add_health,
mock.patch.object(
TaskWorkerProcessingPool, "ready_count", new_callable=mock.PropertyMock, return_value=2
),
):
exitcode = taskworker.start()

assert exitcode == 0
push_call, health_call = grpc_server.call_args_list
assert push_call.kwargs["maximum_concurrent_rpcs"] == 2
assert "maximum_concurrent_rpcs" not in health_call.kwargs

# Health stays on the push server too, so probes on the old port keep working during rollout.
assert add_health.call_args_list == [
mock.call(fake_health, fake_server),
mock.call(fake_health, fake_health_server),
]
fake_health_server.add_insecure_port.assert_called_once_with("[::]:50098")
fake_health_server.start.assert_called_once_with()
fake_server.stop.assert_called_once_with(grace=5)
fake_health_server.stop.assert_called_once_with(grace=None)


def test_push_start_keeps_serving_while_server_is_healthy() -> None:
"""
A healthy server must not end the serve loop.
Expand Down
Loading