Skip to content
Draft
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
7 changes: 7 additions & 0 deletions sdk/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@
<version>${slf4j.version}</version>
<scope>test</scope>
</dependency>
<!-- The Lambda Java runtime's event serializer, the version aws-lambda-java-runtime-interface-client 2.12.2 uses -->
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-serialization</artifactId>
<version>1.4.1</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package software.amazon.lambda.durable.config;

import java.time.Duration;
import software.amazon.lambda.durable.serde.SerDes;

/** Optional configuration for a distributed map run. */
public class DistributedMapConfig {
private static final long MAX_TIMEOUT_SECONDS = 7776000; // 90 days

private final DistributedMapDestination destination;
private final CompletionConfig completionConfig;
private final Duration timeout;
private final SerDes resultSerDes;

private DistributedMapConfig(Builder builder) {
if (builder.timeout != null
&& (builder.timeout.toSeconds() <= 0 || builder.timeout.toSeconds() > MAX_TIMEOUT_SECONDS)) {
throw new IllegalArgumentException(
"timeout must be positive and at most 90 days, got: " + builder.timeout.toSeconds() + "s");
}
this.destination = builder.destination;
this.completionConfig = builder.completionConfig;
this.timeout = builder.timeout;
this.resultSerDes = builder.resultSerDes;
}

public DistributedMapDestination destination() {
return destination;
}

public CompletionConfig completionConfig() {
return completionConfig;
}

public Duration timeout() {
return timeout;
}

public SerDes resultSerDes() {
return resultSerDes;
}

public static Builder builder() {
return new Builder();
}

public Builder toBuilder() {
var builder = new Builder();
builder.destination = destination;
builder.completionConfig = completionConfig;
builder.timeout = timeout;
builder.resultSerDes = resultSerDes;
return builder;
}

/** Builder for DistributedMapConfig. */
public static class Builder {
private DistributedMapDestination destination;
private CompletionConfig completionConfig;
private Duration timeout;
private SerDes resultSerDes;

private Builder() {}

public Builder destination(DistributedMapDestination destination) {
this.destination = destination;
return this;
}

public Builder completionConfig(CompletionConfig completionConfig) {
this.completionConfig = completionConfig;
return this;
}

public Builder timeout(Duration timeout) {
this.timeout = timeout;
return this;
}

public Builder resultSerDes(SerDes resultSerDes) {
this.resultSerDes = resultSerDes;
return this;
}

public DistributedMapConfig build() {
return new DistributedMapConfig(this);
}
}

/**
* Failure-tolerance configuration for a distributed map run. {@code toleratedFailurePercentage} is a number from 0
* through 100 and may be fractional.
*/
public record CompletionConfig(
Integer toleratedFailureCount, Double toleratedFailurePercentage, Integer minimumSampleSize) {

public CompletionConfig {
if (toleratedFailureCount != null && toleratedFailurePercentage != null) {
throw new IllegalArgumentException(
"toleratedFailureCount and toleratedFailurePercentage are mutually exclusive");
}
if (minimumSampleSize != null && toleratedFailurePercentage == null) {
throw new IllegalArgumentException("minimumSampleSize is only valid with toleratedFailurePercentage");
}
// Omitting the config means zero tolerated failures, so an empty one is rejected rather than omitted.
if (toleratedFailureCount == null && toleratedFailurePercentage == null) {
throw new IllegalArgumentException(
"a completion config must set toleratedFailureCount or toleratedFailurePercentage");
}
if (toleratedFailureCount != null && toleratedFailureCount < 0) {
throw new IllegalArgumentException(
"toleratedFailureCount must be non-negative, got: " + toleratedFailureCount);
}
if (toleratedFailurePercentage != null && !Double.isFinite(toleratedFailurePercentage)) {
throw new IllegalArgumentException(
"toleratedFailurePercentage must be a finite number, got: " + toleratedFailurePercentage);
}
if (toleratedFailurePercentage != null
&& (toleratedFailurePercentage < 0 || toleratedFailurePercentage > 100)) {
throw new IllegalArgumentException(
"toleratedFailurePercentage must be between 0 and 100, got: " + toleratedFailurePercentage);
}
if (minimumSampleSize != null && minimumSampleSize < 1) {
throw new IllegalArgumentException("minimumSampleSize must be at least 1, got: " + minimumSampleSize);
}
}

/** Stops the run after more than this many items permanently fail. */
public static CompletionConfig failureCount(int count) {
return new CompletionConfig(count, null, null);
}

/** Abort once the failure rate exceeds this percentage, a number from 0 through 100 that may be fractional. */
public static CompletionConfig failurePercentage(double percentage) {
return new CompletionConfig(null, percentage, null);
}

/**
* Abort once the failure rate exceeds this percentage, a number from 0 through 100 that may be fractional,
* gated by a minimum sample size.
*/
public static CompletionConfig failurePercentage(double percentage, int minimumSampleSize) {
return new CompletionConfig(null, percentage, minimumSampleSize);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package software.amazon.lambda.durable.config;

import java.nio.charset.StandardCharsets;
import software.amazon.lambda.durable.util.DistributedMapValidation;

/** Destination routing for distributed map run results. */
public record DistributedMapDestination(Success onSuccess, Failure onFailure) {
private static final int MAX_DESTINATION_KEY_PREFIX_BYTES = 900;

/** Routes only succeeded item records. */
public static DistributedMapDestination of(Success onSuccess) {
return new DistributedMapDestination(onSuccess, null);
}

/** Routes only failed item records. */
public static DistributedMapDestination of(Failure onFailure) {
return new DistributedMapDestination(null, onFailure);
}

/** Routes both succeeded and failed item records. */
public static DistributedMapDestination of(Success onSuccess, Failure onFailure) {
return new DistributedMapDestination(onSuccess, onFailure);
}

/** Route succeeded item records to an S3 prefix (defaults to include output only). */
public static Success s3Successes(String prefixUri) {
return s3Successes(prefixUri, false, true, null);
}

/** Route succeeded item records to an S3 prefix. */
public static Success s3Successes(
String prefixUri, boolean includeInput, boolean includeOutput, String expectedBucketOwner) {
var parsed = DistributedMapValidation.parseS3Uri(prefixUri);
return new Success(
parsed.bucket(),
parsed.path() != null ? parsed.path() : "",
includeInput,
includeOutput,
expectedBucketOwner);
}

/** Route permanently-failed item records to an S3 prefix (defaults to include input and error). */
public static Failure s3Failures(String prefixUri) {
return s3Failures(prefixUri, true, true, null);
}

/** Route permanently-failed item records to an S3 prefix. */
public static Failure s3Failures(
String prefixUri, boolean includeInput, boolean includeError, String expectedBucketOwner) {
var parsed = DistributedMapValidation.parseS3Uri(prefixUri);
return new Failure(
parsed.bucket(),
parsed.path() != null ? parsed.path() : "",
includeInput,
includeError,
expectedBucketOwner);
}

/**
* Rejects a destination key prefix the service would reject. The limit is 900 UTF-8 bytes, including a trailing
* slash when the URI has one, and it is measured in bytes rather than characters, so a multi-byte prefix under 900
* characters can still exceed it. The service applies the same limit when the start checkpoint is written, so
* checking it here fails the call that built the destination instead of the operation.
*/
private static void validateDestinationPrefix(String prefix) {
if (prefix == null) {
return;
}
var bytes = prefix.getBytes(StandardCharsets.UTF_8).length;
if (bytes > MAX_DESTINATION_KEY_PREFIX_BYTES) {
throw new IllegalArgumentException("destination key prefix must be at most "
+ MAX_DESTINATION_KEY_PREFIX_BYTES + " UTF-8 bytes, got: " + bytes);
}
}

/** S3 destination for succeeded distributed map items. */
public record Success(
String bucket, String prefix, boolean includeInput, boolean includeOutput, String expectedBucketOwner) {

public Success {
if (bucket == null || bucket.isEmpty()) {
throw new IllegalArgumentException("success destination bucket cannot be empty");
}
DistributedMapValidation.validateBucketOwner(expectedBucketOwner);
validateDestinationPrefix(prefix);
if (!includeInput && !includeOutput) {
throw new IllegalArgumentException("success destination must include at least one of input or output");
}
}
}

/** S3 destination for permanently-failed distributed map items. */
public record Failure(
String bucket, String prefix, boolean includeInput, boolean includeError, String expectedBucketOwner) {

public Failure {
if (bucket == null || bucket.isEmpty()) {
throw new IllegalArgumentException("failure destination bucket cannot be empty");
}
DistributedMapValidation.validateBucketOwner(expectedBucketOwner);
validateDestinationPrefix(prefix);
if (!includeInput && !includeError) {
throw new IllegalArgumentException("failure destination must include at least one of input or error");
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package software.amazon.lambda.durable.config;

import software.amazon.lambda.durable.util.DistributedMapValidation;

/** Processor configuration for a distributed map run. */
public class DistributedMapProcessor {

/** Per-batch response mode reported by the processor. */
public enum ResponseMode {
BATCH,
ITEM_OUTCOMES
}

private static final int MIN_BATCH_SIZE = 1;
private static final int MAX_BATCH_SIZE = 10000;
private static final int MAX_NAME_PREFIX_LENGTH = 36;

private final String functionName;
private final ResponseMode responseMode;
private final Integer batchSize;
private final DistributedMapProcessorRetryConfig retry;
private final String durableExecutionNamePrefix;

private DistributedMapProcessor(Builder builder) {
DistributedMapValidation.validateFunctionName(builder.functionName);
if (builder.batchSize != null && (builder.batchSize < MIN_BATCH_SIZE || builder.batchSize > MAX_BATCH_SIZE)) {
throw new IllegalArgumentException("batchSize must be between 1 and 10000, got: " + builder.batchSize);
}
if (builder.durableExecutionNamePrefix != null
&& (builder.durableExecutionNamePrefix.isEmpty()
|| builder.durableExecutionNamePrefix.length() > MAX_NAME_PREFIX_LENGTH)) {
throw new IllegalArgumentException("durableExecutionNamePrefix must be between 1 and 36 characters, got: "
+ builder.durableExecutionNamePrefix.length());
}
this.functionName = builder.functionName;
this.responseMode = builder.responseMode;
this.batchSize = builder.batchSize;
this.retry = builder.retry;
this.durableExecutionNamePrefix = builder.durableExecutionNamePrefix;
}

public String functionName() {
return functionName;
}

public ResponseMode responseMode() {
return responseMode;
}

public Integer batchSize() {
return batchSize;
}

/** Returns the retry settings, or null for the service defaults. */
public DistributedMapProcessorRetryConfig retry() {
return retry;
}

public String durableExecutionNamePrefix() {
return durableExecutionNamePrefix;
}

/** Processor that reports a single pass/fail outcome for the whole batch, with no per-item results. */
public static Builder reportBatchOutcome(String functionName) {
return new Builder(functionName, ResponseMode.BATCH);
}

/**
* Processor that may report a failure or an output for each item. Every item it does not list succeeds with no
* output.
*/
public static Builder reportItemOutcomes(String functionName) {
return new Builder(functionName, ResponseMode.ITEM_OUTCOMES);
}

/** Builder for DistributedMapProcessor. */
public static class Builder {
private final String functionName;
private final ResponseMode responseMode;
private Integer batchSize;
private DistributedMapProcessorRetryConfig retry;
private String durableExecutionNamePrefix;

private Builder(String functionName, ResponseMode responseMode) {
this.functionName = functionName;
this.responseMode = responseMode;
}

public Builder batchSize(int batchSize) {
this.batchSize = batchSize;
return this;
}

/**
* Sets the per-item retry policy. Omit it to take the service defaults of 2 attempts within 6 hours.
*
* @param retry the retry settings
* @return this builder
*/
public Builder retry(DistributedMapProcessorRetryConfig retry) {
this.retry = retry;
return this;
}

public Builder durableExecutionNamePrefix(String durableExecutionNamePrefix) {
this.durableExecutionNamePrefix = durableExecutionNamePrefix;
return this;
}

public DistributedMapProcessor build() {
return new DistributedMapProcessor(this);
}
}
}
Loading
Loading