Skip to content
Open
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ to docs, or any other relevant information.
project-scoped custom roles, and caller rate limits.

### Fixed
- Graceful worker factory shutdown keeps the shared worker-command poller available while
activities drain and lets supported servers complete its outstanding poll.
- Autoscaling workflow pollers now prioritize meaningful sticky backlog up to the sticky pool's
target and use advertised slot capacity to balance sticky and non-sticky admission. Without an
advertised maximum, additional polls of one queue type wait until the other has an active poll.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import io.grpc.StatusRuntimeException;
import io.nexusrpc.OperationState;
import io.nexusrpc.handler.HandlerException;
import io.temporal.api.enums.v1.TaskQueueType;
import io.temporal.api.failure.v1.Failure;
import io.temporal.api.nexus.v1.*;
import io.temporal.api.workflowservice.v1.*;
Expand Down Expand Up @@ -204,8 +205,30 @@ public CompletableFuture<Void> shutdown(ShutdownManager shutdownManager, boolean
storageCancellation.cancel();
}
String supplierName = this + "#executorSlots";
return poller
.shutdown(shutdownManager, interruptTasks)
CompletableFuture<Void> pollerShutdown = poller.shutdown(shutdownManager, interruptTasks);
if (workerCommandsTaskQueue
&& !interruptTasks
&& namespaceCapabilities.isGracefulPollShutdown()) {
// Poller shutdown closes admission immediately. Wake any suspended poller so it can exit.
poller.resumePolling();
ShutdownWorkerRequest request =
ShutdownWorkerRequest.newBuilder()
.setNamespace(namespace)
.setIdentity(options.getIdentity())
.setWorkerInstanceKey(options.getWorkerInstanceKey())
.setTaskQueue(taskQueue)
.setReason("graceful shutdown")
.addTaskQueueTypes(TaskQueueType.TASK_QUEUE_TYPE_NEXUS)
.build();
CompletableFuture<Void> gracefulPollerShutdown = pollerShutdown;
pollerShutdown =
shutdownManager
.waitOnWorkerShutdownRequestResult(service.futureStub().shutdownWorker(request))
.thenCompose(
succeeded ->
succeeded ? gracefulPollerShutdown : poller.shutdown(shutdownManager, true));
}
return pollerShutdown
.thenCompose(
ignore ->
!interruptTasks
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,26 +127,32 @@ private CompletableFuture<Void> limitedWait(
*/
public CompletableFuture<Void> waitOnWorkerShutdownRequest(
ListenableFuture<ShutdownWorkerResponse> shutdownRequest) {
CompletableFuture<Void> future = new CompletableFuture<>();
return waitOnWorkerShutdownRequestResult(shutdownRequest).thenApply(ignored -> null);
}

/** Reports whether the best-effort shutdown request succeeded. */
CompletableFuture<Boolean> waitOnWorkerShutdownRequestResult(
ListenableFuture<ShutdownWorkerResponse> shutdownRequest) {
CompletableFuture<Boolean> future = new CompletableFuture<>();
shutdownRequest.addListener(
() -> {
boolean succeeded = false;
try {
shutdownRequest.get();
succeeded = true;
} catch (Exception e) {
if (e instanceof ExecutionException) {
e = (Exception) e.getCause();
}
if (e instanceof StatusRuntimeException) {
Throwable cause = e instanceof ExecutionException ? e.getCause() : e;
if (cause instanceof StatusRuntimeException) {
// If the server does not support shutdown, ignore the exception
if (Status.Code.UNIMPLEMENTED.equals(
((StatusRuntimeException) e).getStatus().getCode())
|| isChannelShutdownException((StatusRuntimeException) e)) {
((StatusRuntimeException) cause).getStatus().getCode())
|| isChannelShutdownException((StatusRuntimeException) cause)) {
return;
}
}
log.warn("failed to call shutdown worker", e);
log.warn("failed to call shutdown worker", cause);
} finally {
future.complete(null);
future.complete(succeeded);
}
},
this::executeOrRunDirect);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io.temporal.worker.tuning.NexusSlotInfo;
import io.temporal.worker.tuning.PollerBehaviorSimpleMaximum;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.TimeoutException;
import java.util.function.Function;
import javax.annotation.Nonnull;
Expand Down Expand Up @@ -56,7 +57,8 @@ public static SuspendableWorker newWorkerCommandWorker(
SingleWorkerOptions.newBuilder()
.setIdentity(identity)
.setBuildId(Version.LIBRARY_VERSION)
.setWorkerInstanceKey(workerGroupingKey)
// Factories sharing the command queue must shut down their pollers independently.
.setWorkerInstanceKey(UUID.randomUUID().toString())
.setDataConverter(dataConverter)
.setMetricsScope(metricsScope)
.setPollerOptions(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -472,13 +472,24 @@ private void doShutdown(boolean interruptUserTasks) {
shutdownFutures.add(futureHolder[0]);
}
}
if (workerCommandWorker != null) {
// TODO: Should be able to pass `interruptUserTasks` here when
// https://github.com/temporalio/api/pull/784 is in
boolean drainBeforeStoppingCommands =
workerCommandWorker != null
&& !interruptUserTasks
&& namespaceCapabilities.isGracefulPollShutdown();
if (workerCommandWorker != null && !drainBeforeStoppingCommands) {
shutdownFutures.add(workerCommandWorker.shutdown(shutdownManager, true));
}

CompletableFuture.allOf(shutdownFutures.toArray(new CompletableFuture[0]))
CompletableFuture<Void> workersShutdown =
CompletableFuture.allOf(shutdownFutures.toArray(new CompletableFuture[0]));
if (drainBeforeStoppingCommands) {
// Keep processing worker commands until the regular workers finish draining.
workersShutdown =
workersShutdown.thenCompose(
ignored -> workerCommandWorker.shutdown(shutdownManager, false));
}

workersShutdown
.thenApply(
r -> {
// Unregister workers from heartbeat manager only after full shutdown,
Expand Down
Loading
Loading