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
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import io.temporal.internal.payload.limits.PayloadLimitViolation;
import io.temporal.internal.payload.limits.PayloadLimitViolationException;
import io.temporal.internal.payload.storage.ActivityStorageTargets;
import io.temporal.internal.payload.storage.ExternalStorageOperations;
import io.temporal.internal.payload.storage.ExternalStorageRunner;
import io.temporal.internal.retryer.GrpcRetryer;
import io.temporal.payload.storage.StorageDriverTargetInfo;
Expand Down Expand Up @@ -441,8 +442,8 @@ private void offloadHeartbeat(RecordActivityTaskHeartbeatRequest.Builder builder
store.get();
// The interval bounds the storage calls, which can be cancelled. The details already went
// through this codec untimed on this thread, and so do their references.
externalStorage.encodeReferences(
builder, HeaderUtils.resolveHeaderCodec(heartbeatDataConverter));
ExternalStorageOperations.encodeReferences(
externalStorage, builder, HeaderUtils.resolveHeaderCodec(heartbeatDataConverter));
} catch (TimeoutException e) {
offloadCancel.cancel();
throw new CancellationException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import io.temporal.internal.nexus.NexusOperationMetadata;
import io.temporal.internal.nexus.PropagatedNexusSerializationContext;
import io.temporal.internal.payload.storage.ExternalStorageDataConverter;
import io.temporal.internal.payload.storage.ExternalStorageOperations;
import io.temporal.internal.payload.storage.ExternalStorageRunner;
import io.temporal.payload.codec.PayloadCodec;
import io.temporal.payload.context.ActivitySerializationContext;
Expand Down Expand Up @@ -178,8 +179,13 @@ public StartActivityOutput startActivity(StartActivityInput input) {
io.temporal.api.common.v1.Header.Builder grpcHeader =
HeaderUtils.toHeaderGrpc(input.getHeader(), null, contextHeaderCodec).toBuilder();
if (externalStorage != null && grpcHeader.getFieldsCount() > 0) {
externalStorage.store(
grpcHeader, storageTarget, null, contextHeaderCodec, CancellationToken.none());
ExternalStorageOperations.store(
externalStorage,
grpcHeader,
storageTarget,
null,
contextHeaderCodec,
CancellationToken.none());
}
request.setHeader(grpcHeader);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import io.temporal.internal.nexus.NexusOperationMetadata;
import io.temporal.internal.nexus.OperationTokenUtil;
import io.temporal.internal.nexus.PropagatedNexusSerializationContext;
import io.temporal.internal.payload.storage.ExternalStorageOperations;
import io.temporal.internal.payload.storage.ExternalStorageRunner;
import io.temporal.internal.worker.WorkerVersioningProtoUtils;
import io.temporal.payload.codec.PayloadCodec;
Expand Down Expand Up @@ -162,7 +163,8 @@ private void storeHeader(
if (externalStorage == null || header.getFieldsCount() == 0) {
return;
}
externalStorage.store(
ExternalStorageOperations.store(
externalStorage,
header,
new StorageDriverWorkflowInfo(
namespace,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import io.temporal.internal.common.ProtobufTimeUtils;
import io.temporal.internal.common.RetryOptionsUtils;
import io.temporal.internal.common.SearchAttributesUtil;
import io.temporal.internal.payload.storage.ExternalStorageOperations;
import io.temporal.internal.payload.storage.ExternalStorageRunner;
import io.temporal.payload.codec.PayloadCodec;
import io.temporal.payload.context.WorkflowSerializationContext;
Expand Down Expand Up @@ -555,7 +556,8 @@ private static Header storeHeader(
(key, value) ->
(value.equals(described.get(key)) ? unchanged : toStore).putFields(key, value));
if (toStore.getFieldsCount() > 0) {
externalStorage.store(toStore, storageTarget, null, codec, CancellationToken.none());
ExternalStorageOperations.store(
externalStorage, toStore, storageTarget, null, codec, CancellationToken.none());
}
return toStore.putAllFields(unchanged.getFieldsMap()).build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io.temporal.internal.common.HeaderUtils;
import io.temporal.internal.common.OptionsUtils;
import io.temporal.internal.payload.limits.PayloadErrorLimits;
import io.temporal.internal.payload.storage.ExternalStorageOperations;
import io.temporal.internal.payload.storage.ExternalStorageRunner;
import io.temporal.internal.retryer.GrpcRetryer;
import io.temporal.payload.context.SerializationContext;
Expand Down Expand Up @@ -92,7 +93,8 @@ private <T extends Message> T storeOutbound(T request) {
return request;
}
Message.Builder builder = request.toBuilder();
externalStorage.store(
ExternalStorageOperations.store(
externalStorage,
builder,
storageTarget,
null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,25 +144,26 @@ private Payloads store(Payloads payloads) {
return payloads;
}
Payloads.Builder builder = payloads.toBuilder();
externalStorage.store(
builder, storageTarget, null, getPayloadCodec(), CancellationToken.none());
ExternalStorageOperations.store(
externalStorage, builder, storageTarget, null, getPayloadCodec(), CancellationToken.none());
return builder.build();
}

private <T extends com.google.protobuf.Message> T retrieveMessage(T message) {
if (externalStorage == null) {
throw new ExternalStorageNotConfiguredException();
}
return externalStorage.retrieve(message, getPayloadCodec(), CancellationToken.none());
return ExternalStorageOperations.retrieve(
externalStorage, message, getPayloadCodec(), CancellationToken.none());
}

private Failure storeMessage(Failure failure) {
if (externalStorage == null) {
return failure;
}
Failure.Builder builder = failure.toBuilder();
externalStorage.store(
builder, storageTarget, null, getPayloadCodec(), CancellationToken.none());
ExternalStorageOperations.store(
externalStorage, builder, storageTarget, null, getPayloadCodec(), CancellationToken.none());
return builder.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package io.temporal.internal.payload.storage;

import com.google.protobuf.Message;
import io.temporal.common.CancellationToken;
import io.temporal.internal.payload.visitor.MessageVisitor;
import io.temporal.payload.codec.PayloadCodec;
import io.temporal.payload.storage.StorageDriverTargetInfo;
import java.util.concurrent.CancellationException;
import javax.annotation.Nullable;

/**
* The external storage passes over the payloads exchanged with the server: the runner's store and
* retrieve, combined with the payload codec passes over the references that replace stored
* payloads.
*/
public final class ExternalStorageOperations {

/**
* Stores the payloads of {@code builder}, then encodes and marks the references that replace them
* with {@code codec} if reference encoding is on.
*
* @param codec the codec that encoded the payloads of {@code builder}, or null if there is none
*/
public static void store(
ExternalStorageRunner runner,
Message.Builder builder,
@Nullable StorageDriverTargetInfo target,
@Nullable MessageVisitor<StorageDriverTargetInfo> targetVisitor,
@Nullable PayloadCodec codec,
CancellationToken<CancellationException> cancellationToken) {
runner.store(builder, target, targetVisitor, cancellationToken);
encodeReferences(runner, builder, codec);
}

/**
* Encodes and marks the plain references in {@code builder} with {@code codec}, if reference
* encoding is on. Call it once the store has completed. The codec runs on the calling thread, so
* that a slow codec never holds a driver's thread, and like any codec call it is not bounded by
* the store's cancellation.
*
* @param codec the codec that encoded the payloads of {@code builder}, or null if there is none
*/
public static void encodeReferences(
ExternalStorageRunner runner, Message.Builder builder, @Nullable PayloadCodec codec) {
if (runner.isEncodeReferences()) {
ExternalStorageRunner.join(ExternalStorageReferenceCodec.encode(builder, codec));
}
}

/**
* Decodes the references in {@code message} that were encoded with the payload codec, then
* retrieves every reference.
*
* @param codec the codec to decode the payloads of {@code message} with, or null if there is none
*/
public static <T extends Message> T retrieve(
ExternalStorageRunner runner,
T message,
@Nullable PayloadCodec codec,
CancellationToken<CancellationException> cancellationToken) {
Message.Builder builder = message.toBuilder();
if (!ExternalStorageRunner.getOrThrowIfCancelled(
ExternalStorageReferenceCodec.decode(builder, codec), cancellationToken)) {
return message;
}
@SuppressWarnings("unchecked")
T decoded = (T) builder.build();
return runner.retrieve(decoded, cancellationToken);
}

private ExternalStorageOperations() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import io.temporal.internal.payload.visitor.MessageVisitor;
import io.temporal.internal.payload.visitor.PayloadVisitorOptions;
import io.temporal.internal.payload.visitor.PayloadVisitors;
import io.temporal.payload.codec.PayloadCodec;
import io.temporal.payload.storage.ExternalStorage;
import io.temporal.payload.storage.StorageDriver;
import io.temporal.payload.storage.StorageDriverTargetInfo;
Expand All @@ -23,6 +22,9 @@
* {@link PayloadVisitors} transforming payloads to and from {@link ExternalStorageReference} using
* {@link ExternalStoragePayloadTransformer}. Use {@link ExternalStorage} via {@link #create} to
* configure external storage.
*
* <p>The runner only handles plain references. {@link ExternalStorageOperations} adds the payload
* codec passes over references.
*/
public final class ExternalStorageRunner {
private final ExternalStoragePayloadTransformer payloadTransformer;
Expand All @@ -49,43 +51,18 @@ public static ExternalStorageRunner create(ExternalStorage options, String names
this.encodeReferences = encodeReferences;
}

/** Stores the payloads of {@code builder}, leaving the references that replace them plain. */
public void store(
Message.Builder builder,
@Nullable StorageDriverTargetInfo target,
@Nullable MessageVisitor<StorageDriverTargetInfo> targetVisitor,
CancellationToken<CancellationException> cancellationToken) {
store(builder, target, targetVisitor, cancellationToken, null);
/** Whether the references that replace stored payloads are encoded with the payload codec. */
boolean isEncodeReferences() {
return encodeReferences;
}

/**
* Stores the payloads of {@code builder}, then encodes the references that replace them with
* {@code codec}.
*
* @param codec the codec that encoded the payloads of {@code builder}, or null if there is none
*/
/** Stores the payloads of {@code builder}, leaving the references that replace them plain. */
public void store(
Message.Builder builder,
@Nullable StorageDriverTargetInfo target,
@Nullable MessageVisitor<StorageDriverTargetInfo> targetVisitor,
@Nullable PayloadCodec codec,
CancellationToken<CancellationException> cancellationToken) {
store(builder, target, targetVisitor, cancellationToken);
encodeReferences(builder, codec);
}

/**
* Encodes the plain references in {@code builder} with {@code codec}, if reference encoding is
* on. Call it once the store has completed. The codec runs on the calling thread, so that a slow
* codec never holds a driver's thread, and like any codec call it is not bounded by the store's
* cancellation.
*
* @param codec the codec that encoded the payloads of {@code builder}, or null if there is none
*/
public void encodeReferences(Message.Builder builder, @Nullable PayloadCodec codec) {
if (encodeReferences) {
join(ExternalStorageReferenceCodec.encode(builder, codec));
}
store(builder, target, targetVisitor, cancellationToken, null);
}

public void store(
Expand Down Expand Up @@ -125,26 +102,6 @@ public <T extends Message> T retrieve(
return retrieve(message, cancellationToken, null);
}

/**
* Decodes the references in {@code message} that were encoded with the payload codec, then
* retrieves every reference.
*
* @param codec the codec to decode the payloads of {@code message} with, or null if there is none
*/
public <T extends Message> T retrieve(
T message,
@Nullable PayloadCodec codec,
CancellationToken<CancellationException> cancellationToken) {
Message.Builder builder = message.toBuilder();
if (!getOrThrowIfCancelled(
ExternalStorageReferenceCodec.decode(builder, codec), cancellationToken)) {
return message;
}
@SuppressWarnings("unchecked")
T decoded = (T) builder.build();
return retrieve(decoded, cancellationToken);
}

public <T extends Message> T retrieve(
T message,
CancellationToken<CancellationException> cancellationToken,
Expand Down Expand Up @@ -181,7 +138,7 @@ public static void throwIfContainsReference(Message message) {
join(PayloadVisitors.visit(message.toBuilder(), options));
}

private static void join(CompletableFuture<?> future) {
static void join(CompletableFuture<?> future) {
try {
future.join();
} catch (CompletionException e) {
Expand All @@ -191,7 +148,7 @@ private static void join(CompletableFuture<?> future) {
}
}

private static <T> T getOrThrowIfCancelled(
static <T> T getOrThrowIfCancelled(
CompletableFuture<T> future, CancellationToken<CancellationException> cancellationToken) {
CompletableFuture<Void> cancellation = cancellationToken.getCancellationFuture();
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import io.temporal.internal.payload.limits.PayloadLimitViolation;
import io.temporal.internal.payload.limits.PayloadLimitViolationException;
import io.temporal.internal.payload.storage.ActivityStorageTargets;
import io.temporal.internal.payload.storage.ExternalStorageOperations;
import io.temporal.internal.payload.storage.ExternalStorageRunner;
import io.temporal.internal.retryer.GrpcRetryer;
import io.temporal.internal.worker.ActivityTaskHandler.Result;
Expand Down Expand Up @@ -552,13 +553,15 @@ private ActivityTask retrieveInboundPayloads(ActivityTask task) {
PollActivityTaskQueueResponse.Builder builder = built.toBuilder();
if (built.hasHeartbeatDetails()) {
builder.setHeartbeatDetails(
externalStorageRunner.retrieve(
ExternalStorageOperations.retrieve(
externalStorageRunner,
built.getHeartbeatDetails(),
codec(ActivitySerializationContexts.result(namespace, info)),
storageCancellation.token()));
}
return new ActivityTask(
externalStorageRunner.retrieve(
ExternalStorageOperations.retrieve(
externalStorageRunner,
builder.build(),
codec(ActivitySerializationContexts.activity(namespace, info)),
storageCancellation.token()),
Expand All @@ -573,7 +576,8 @@ private void storeOutboundPayloads(
return;
}
try {
externalStorageRunner.store(
ExternalStorageOperations.store(
externalStorageRunner,
builder,
storageTargetForActivityTask(namespace, pollResponse),
null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import io.temporal.internal.payload.limits.PayloadLimitViolation;
import io.temporal.internal.payload.limits.PayloadLimitViolationException;
import io.temporal.internal.payload.storage.ExternalStorageNotConfiguredException;
import io.temporal.internal.payload.storage.ExternalStorageOperations;
import io.temporal.internal.payload.storage.ExternalStorageRunner;
import io.temporal.internal.retryer.GrpcRetryer;
import io.temporal.payload.codec.PayloadCodec;
Expand Down Expand Up @@ -703,7 +704,8 @@ private NexusTask retrieveInboundPayloads(NexusTask task) {
return task;
}
return new NexusTask(
externalStorageRunner.retrieve(
ExternalStorageOperations.retrieve(
externalStorageRunner,
built,
// The context the handler scopes its converter to once it runs.
codec(NexusSerializationContexts.forRequest(built.getRequest())),
Expand All @@ -724,8 +726,13 @@ private void storeOutbound(
return;
}
try {
externalStorageRunner.store(
builder, null, null, codec(serializationContext), storageCancellation.token());
ExternalStorageOperations.store(
externalStorageRunner,
builder,
null,
null,
codec(serializationContext),
storageCancellation.token());
} catch (CancellationException e) {
// A shutdown cancellation is not a task failure. Let it reach handle(), which abandons the
// task rather than telling the server the handler failed.
Expand Down
Loading