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
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,17 @@ class ReplayWorkflowRunTaskHandler implements WorkflowRunTaskHandler {

private final GetSystemInfoResponse.Capabilities capabilities;

private final WorkflowRunStorage storage;

ReplayWorkflowRunTaskHandler(
String namespace,
ReplayWorkflow workflow,
PollWorkflowTaskQueueResponseOrBuilder workflowTask,
SingleWorkerOptions workerOptions,
Scope metricsScope,
LocalActivityDispatcher localActivityDispatcher,
GetSystemInfoResponse.Capabilities capabilities) {
GetSystemInfoResponse.Capabilities capabilities,
WorkflowRunStorage storage) {
HistoryEvent startedEvent = workflowTask.getHistory().getEvents(0);
if (!startedEvent.hasWorkflowExecutionStartedEventAttributes()) {
throw new IllegalArgumentException(
Expand Down Expand Up @@ -118,6 +121,12 @@ class ReplayWorkflowRunTaskHandler implements WorkflowRunTaskHandler {
this.localActivityCompletionSink = localActivityCompletionQueue::add;
this.localActivityMeteringHelper = new LocalActivityMeteringHelper();
this.capabilities = capabilities;
this.storage = storage;
}

@Override
public WorkflowRunStorage getStorage() {
return storage;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import io.temporal.common.converter.DataConverter;
import io.temporal.internal.common.ProtobufTimeUtils;
import io.temporal.internal.common.WorkflowExecutionUtils;
import io.temporal.internal.payload.storage.ExternalStorageRunner;
import io.temporal.internal.payload.storage.StorageOperationMetrics;
import io.temporal.internal.worker.*;
import io.temporal.payload.context.WorkflowSerializationContext;
Expand Down Expand Up @@ -97,21 +96,18 @@ private Result handleWorkflowTaskWithQuery(
boolean useCache = stickyTaskQueue != null;

try {
workflowTask = retrieveStoredPayloads(workflowTask, downloadMetrics);
workflowRunTaskHandler =
getOrCreateWorkflowExecutor(
useCache, workflowTask, metricsScope, createdNew, downloadMetrics);
WorkflowRunStorage storage = workflowRunTaskHandler.getStorage();
if (!createdNew.get()) {
storage.retrieveTask(workflowTask, downloadMetrics);
}
logWorkflowTaskToBeProcessed(workflowTask, createdNew);

ServiceWorkflowHistoryIterator historyIterator =
new ServiceWorkflowHistoryIterator(
service,
namespace,
workflowTask,
metricsScope,
options.getExternalStorageRunner(),
options.getStorageCancellation(),
downloadMetrics);
service, namespace, workflowTask, metricsScope, storage, downloadMetrics);
boolean finalCommand;
Result result;

Expand Down Expand Up @@ -187,19 +183,6 @@ private Result handleWorkflowTaskWithQuery(
}
}

private PollWorkflowTaskQueueResponse.Builder retrieveStoredPayloads(
PollWorkflowTaskQueueResponse.Builder workflowTask,
@Nullable StorageOperationMetrics downloadMetrics) {
ExternalStorageRunner externalStorageRunner = options.getExternalStorageRunner();
if (externalStorageRunner == null) {
ExternalStorageRunner.throwIfContainsReference(workflowTask.build());
return workflowTask;
}
return externalStorageRunner
.retrieve(workflowTask.build(), options.getStorageCancellation(), downloadMetrics)
.toBuilder();
}

private Result createCompletedWFTRequest(
String workflowType,
PollWorkflowTaskQueueResponseOrBuilder workflowTask,
Expand Down Expand Up @@ -430,18 +413,14 @@ private WorkflowRunTaskHandler createStatefulHandler(
.blockingStub()
.withOption(METRICS_TAGS_CALL_OPTIONS_KEY, metricsScope)
.getWorkflowExecutionHistory(getHistoryRequest);
ExternalStorageRunner externalStorageRunner = options.getExternalStorageRunner();
if (externalStorageRunner == null) {
ExternalStorageRunner.throwIfContainsReference(getHistoryResponse);
} else {
getHistoryResponse =
externalStorageRunner.retrieve(
getHistoryResponse, options.getStorageCancellation(), downloadMetrics);
}
workflowTask
.setHistory(getHistoryResponse.getHistory())
.setNextPageToken(getHistoryResponse.getNextPageToken());
}
// Retrieved once the full history is fetched, so a partial history isn't retrieved and then
// replaced.
WorkflowRunStorage storage = new WorkflowRunStorage(options);
storage.retrieveTask(workflowTask, downloadMetrics);
ReplayWorkflow workflow = workflowFactory.getWorkflow(workflowType, workflowExecution);
return new ReplayWorkflowRunTaskHandler(
namespace,
Expand All @@ -450,7 +429,8 @@ private WorkflowRunTaskHandler createStatefulHandler(
options,
metricsScope,
localActivityDispatcher,
service.getServerCapabilities().get());
service.getServerCapabilities().get(),
storage);
}

private void resetStickyTaskQueue(WorkflowExecution execution) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import io.temporal.api.workflowservice.v1.GetWorkflowExecutionHistoryRequest;
import io.temporal.api.workflowservice.v1.GetWorkflowExecutionHistoryResponse;
import io.temporal.api.workflowservice.v1.PollWorkflowTaskQueueResponseOrBuilder;
import io.temporal.common.CancellationToken;
import io.temporal.internal.payload.storage.ExternalStorageRunner;
import io.temporal.internal.payload.storage.StorageOperationMetrics;
import io.temporal.internal.retryer.GrpcRetryer;
Expand All @@ -21,7 +20,6 @@
import java.time.Duration;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.concurrent.CancellationException;
import javax.annotation.Nullable;

/** Supports iteration over history while loading new pages through calls to the service. */
Expand All @@ -34,8 +32,7 @@ class ServiceWorkflowHistoryIterator implements WorkflowHistoryIterator {
private final Scope metricsScope;
private final PollWorkflowTaskQueueResponseOrBuilder task;
private final GrpcRetryer grpcRetryer;
private final @Nullable ExternalStorageRunner externalStorageRunner;
private final CancellationToken<CancellationException> storageCancellation;
private final @Nullable WorkflowRunStorage storage;
private final @Nullable StorageOperationMetrics downloadMetrics;
private Deadline deadline;
private Iterator<HistoryEvent> current;
Expand All @@ -46,24 +43,22 @@ class ServiceWorkflowHistoryIterator implements WorkflowHistoryIterator {
String namespace,
PollWorkflowTaskQueueResponseOrBuilder task,
Scope metricsScope) {
this(service, namespace, task, metricsScope, null, CancellationToken.none(), null);
this(service, namespace, task, metricsScope, null, null);
}

ServiceWorkflowHistoryIterator(
WorkflowServiceStubs service,
String namespace,
PollWorkflowTaskQueueResponseOrBuilder task,
Scope metricsScope,
@Nullable ExternalStorageRunner externalStorageRunner,
CancellationToken<CancellationException> storageCancellation,
@Nullable WorkflowRunStorage storage,
@Nullable StorageOperationMetrics downloadMetrics) {
this.storageCancellation = storageCancellation;
this.downloadMetrics = downloadMetrics;
this.service = service;
this.namespace = namespace;
this.task = task;
this.metricsScope = metricsScope;
this.externalStorageRunner = externalStorageRunner;
this.storage = storage;
// TODO Refactor WorkflowHistoryIteratorTest or WorkflowHistoryIterator to remove this check.
// `service == null` shouldn't be allowed as it's needed for a normal functioning of this
// class.
Expand All @@ -87,10 +82,10 @@ public boolean hasNext() {
GetWorkflowExecutionHistoryResponse response = queryWorkflowExecutionHistory();

History history = response.getHistory();
if (externalStorageRunner == null) {
if (storage == null) {
ExternalStorageRunner.throwIfContainsReference(history);
} else {
history = externalStorageRunner.retrieve(history, storageCancellation, downloadMetrics);
history = storage.retrieve(history, downloadMetrics);
}
current = history.getEventsList().iterator();
nextPageToken = response.getNextPageToken();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package io.temporal.internal.replay;

import com.google.protobuf.Message;
import io.temporal.api.workflowservice.v1.PollWorkflowTaskQueueResponse;
import io.temporal.common.CancellationToken;
import io.temporal.internal.payload.storage.ExternalStorageRunner;
import io.temporal.internal.payload.storage.StorageOperationMetrics;
import io.temporal.internal.worker.SingleWorkerOptions;
import java.util.concurrent.CancellationException;
import javax.annotation.Nullable;

/**
* Retrieves the stored payloads of one workflow run's tasks and history pages. A run's tasks and
* pages are passed to it in history order.
*/
public final class WorkflowRunStorage {
private final @Nullable ExternalStorageRunner runner;
private final CancellationToken<CancellationException> cancellationToken;

WorkflowRunStorage(SingleWorkerOptions options) {
this.runner = options.getExternalStorageRunner();
this.cancellationToken = options.getStorageCancellation();
}

/**
* Retrieves the payloads of {@code task} in place. Called once for each task of the run, in
* order, before its later history pages.
*/
void retrieveTask(
PollWorkflowTaskQueueResponse.Builder task, @Nullable StorageOperationMetrics metrics) {
PollWorkflowTaskQueueResponse original = task.build();
PollWorkflowTaskQueueResponse retrieved = retrieve(original, metrics);
if (retrieved != original) {
task.clear().mergeFrom(retrieved);
}
}

/** Retrieves the payloads of {@code message}, a task or a history page of the run. */
<T extends Message> T retrieve(T message, @Nullable StorageOperationMetrics metrics) {
if (runner == null) {
ExternalStorageRunner.throwIfContainsReference(message);
return message;
}
return runner.retrieve(message, cancellationToken, metrics);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,8 @@ QueryResult handleDirectQueryWorkflowTask(
*/
void resetStartedEventId(Long eventId);

/** Retrieves the stored payloads of this run's tasks and history pages. */
WorkflowRunStorage getStorage();

void close();
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ public void queryIsOutdated() throws Throwable {
SingleWorkerOptions.newBuilder().build(),
new NoopScope(),
mock(LocalActivityDispatcher.class),
GetSystemInfoResponse.Capabilities.newBuilder().build());
GetSystemInfoResponse.Capabilities.newBuilder().build(),
new WorkflowRunStorage(SingleWorkerOptions.newBuilder().build()));

stateMachines = handler.getWorkflowStateMachines();
QueryResult queryResult =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ public WorkflowContext getWorkflowContext() {
SingleWorkerOptions.newBuilder().build(),
metricsScope,
(a, b, c) -> true,
GetSystemInfoResponse.Capabilities.newBuilder().build());
GetSystemInfoResponse.Capabilities.newBuilder().build(),
new WorkflowRunStorage(SingleWorkerOptions.newBuilder().build()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ public void outstandingLocalActivityForcesANewWorkflowTask() throws Throwable {
SingleWorkerOptions.newBuilder().build(),
new NoopScope(),
dispatcher,
GetSystemInfoResponse.Capabilities.newBuilder().build());
GetSystemInfoResponse.Capabilities.newBuilder().build(),
new WorkflowRunStorage(SingleWorkerOptions.newBuilder().build()));
stateMachines.set(handler.getWorkflowStateMachines());

WorkflowTaskResult result =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import io.temporal.internal.payload.storage.ExternalStorageNotConfiguredException;
import io.temporal.internal.payload.storage.ExternalStorageRunner;
import io.temporal.internal.payload.storage.TestStorageDriver;
import io.temporal.internal.worker.SingleWorkerOptions;
import io.temporal.payload.storage.ExternalStorage;
import io.temporal.testUtils.HistoryUtils;
import java.nio.charset.Charset;
Expand Down Expand Up @@ -152,8 +153,14 @@ private static ServiceWorkflowHistoryIterator fetchingIterator(
CancellationToken<CancellationException> storageCancellation) {
PollWorkflowTaskQueueResponse workflowTask =
PollWorkflowTaskQueueResponse.newBuilder().setNextPageToken(NEXT_PAGE_TOKEN).build();
WorkflowRunStorage runStorage =
new WorkflowRunStorage(
SingleWorkerOptions.newBuilder()
.setExternalStorageRunner(storage)
.setStorageCancellation(storageCancellation)
.build());
return new ServiceWorkflowHistoryIterator(
null, "default", workflowTask, null, storage, storageCancellation, null) {
null, "default", workflowTask, null, runStorage, null) {
@Override
GetWorkflowExecutionHistoryResponse queryWorkflowExecutionHistory() {
return GetWorkflowExecutionHistoryResponse.newBuilder().setHistory(page).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,11 @@ public QueryResult handleDirectQueryWorkflowTask(
@Override
public void resetStartedEventId(Long event) {}

@Override
public WorkflowRunStorage getStorage() {
throw new UnsupportedOperationException();
}

@Override
public void close() {
runner.close();
Expand Down
Loading