mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(dynamodb): add stream iterator expiry, 24-hour retention and shards with lineage (#8744)
The native engine's streams had one shard that never closed, iterators that never expired and records trimmed only by count, so the consumer code that handles an expired iterator, a stale checkpoint or a shard hand-off only ran for the first time against AWS. Four settings, each off by default, bring a stream closer to AWS: - stream-shard-count gives each stream that many open shards. An item's changes go to the shard its partition key picks, so they stay in order. - stream-shard-rollover-seconds closes an open shard once it has been open that long, and a child shard naming it as ParentShardId takes its keys. - stream-iterator-expiry-enabled expires an iterator 15 minutes after it is returned: GetRecords answers ExpiredIteratorException. - stream-retention-enabled trims records after 24 hours and drops a closed shard 24 hours after it closes. GetShardIterator then refuses a trimmed sequence number with TrimmedDataAccessException and one the shard never held with ValidationException "Invalid SequenceNumber for the shard". Disabling a stream closes its open shards, as AWS does, and a closed shard read to its end returns no NextShardIterator. DescribeStream pages with Limit and ExclusiveStartShardId. GetShardIterator refuses a shard the stream does not have, and a sequence iterator with no sequence number, with AWS's messages. The 1,000-record limit now holds for the whole stream, and a read is refused as trimmed exactly when its shard lost a record after the cursor. Closes #4522 Co-authored-by: Jason Pogozelski <jpogozelski@xoi.io>
This commit is contained in:
1 parent
f9e88a2215
commit
2624f0683a
8 files changed
+1054
-150
No files matched your search
@@ -1669,6 +1669,37 @@ public interface EmulatorConfig {
|
||||
@WithDefault("300")
|
||||
int latestRestorableLagSeconds();
|
||||
|
||||
/**
|
||||
* Open shards in each DynamoDB stream of the native engine. An item's changes always go to the shard its
|
||||
* partition key picks, so they stay in order. AWS gives each table partition a shard of its own.
|
||||
* Env: FLOCI_SERVICES_DYNAMODB_STREAM_SHARD_COUNT
|
||||
*/
|
||||
@WithDefault("1")
|
||||
int streamShardCount();
|
||||
|
||||
/**
|
||||
* Seconds a stream shard of the native engine stays open before it closes and a child shard, which names it
|
||||
* as its parent, takes its keys. 0 keeps shards open until the stream is disabled.
|
||||
* Env: FLOCI_SERVICES_DYNAMODB_STREAM_SHARD_ROLLOVER_SECONDS
|
||||
*/
|
||||
@WithDefault("0")
|
||||
long streamShardRolloverSeconds();
|
||||
|
||||
/**
|
||||
* Expire a stream shard iterator 15 minutes after it is returned, as AWS does.
|
||||
* Env: FLOCI_SERVICES_DYNAMODB_STREAM_ITERATOR_EXPIRY_ENABLED
|
||||
*/
|
||||
@WithDefault("false")
|
||||
boolean streamIteratorExpiryEnabled();
|
||||
|
||||
/**
|
||||
* Keep stream records, and shards after they close, for 24 hours as AWS does, and refuse a shard iterator
|
||||
* for a sequence number the shard no longer holds or never held.
|
||||
* Env: FLOCI_SERVICES_DYNAMODB_STREAM_RETENTION_ENABLED
|
||||
*/
|
||||
@WithDefault("false")
|
||||
boolean streamRetentionEnabled();
|
||||
|
||||
/** The engine behind the DynamoDB API: {@code native} or {@code local}. Env: FLOCI_SERVICES_DYNAMODB_BACKEND. */
|
||||
@WithDefault("native")
|
||||
String backend();
|
||||
|
||||
@@ -4,6 +4,7 @@ import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.floci.aws.config.EmulatorConfig;
|
||||
import io.floci.aws.core.common.AwsArnUtils;
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.core.storage.AccountAwareStorageBackend;
|
||||
@@ -17,16 +18,20 @@ import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.Clock;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneOffset;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentLinkedDeque;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
@ApplicationScoped
|
||||
@@ -34,35 +39,90 @@ public class DynamoDbStreamService {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(DynamoDbStreamService.class);
|
||||
|
||||
static final String SHARD_ID = "shardId-0000000001-00000000001";
|
||||
/** The first shard of every stream, which a stream with one shard that never rolls over keeps for good. */
|
||||
static final String SHARD_ID = shardId(1, 0);
|
||||
/** The most records a stream keeps across its shards; the oldest goes first. */
|
||||
static final int MAX_RECORDS = 1000;
|
||||
/** How long a shard iterator can be used when iterator expiry is on, as on AWS. */
|
||||
static final Duration ITERATOR_LIFETIME = Duration.ofMinutes(15);
|
||||
/** How long records, and shards once closed, are kept when retention is on, as on AWS. */
|
||||
static final Duration RETENTION = Duration.ofHours(24);
|
||||
/** The most shards one DescribeStream page lists, AWS's upper limit. */
|
||||
static final int MAX_SHARDS_PER_PAGE = 100;
|
||||
private static final String ZERO_SEQUENCE_NUMBER = "000000000000000000000";
|
||||
|
||||
private static final DateTimeFormatter STREAM_LABEL_FORMAT =
|
||||
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS").withZone(ZoneOffset.UTC);
|
||||
|
||||
/**
|
||||
* How the native engine shapes its streams: the open shards each has, how long a shard stays open before a child
|
||||
* takes its keys (null for good), and whether iterators expire and records age out as on AWS.
|
||||
*/
|
||||
record Settings(int shardCount, Duration shardRollover, boolean iteratorExpiry, boolean retention) {
|
||||
|
||||
static final Settings DEFAULTS = new Settings(1, null, false, false);
|
||||
|
||||
Settings {
|
||||
if (shardCount < 1) {
|
||||
throw new IllegalArgumentException("A DynamoDB stream needs at least one shard, not " + shardCount);
|
||||
}
|
||||
if (shardRollover != null && (shardRollover.isZero() || shardRollover.isNegative())) {
|
||||
shardRollover = null;
|
||||
}
|
||||
}
|
||||
|
||||
static Settings from(EmulatorConfig.DynamoDbServiceConfig config) {
|
||||
long rollover = config.streamShardRolloverSeconds();
|
||||
return new Settings(config.streamShardCount(), rollover > 0 ? Duration.ofSeconds(rollover) : null,
|
||||
config.streamIteratorExpiryEnabled(), config.streamRetentionEnabled());
|
||||
}
|
||||
}
|
||||
|
||||
/** A shard as DescribeStream lists it. Its ending sequence number is null while it is open. */
|
||||
record ShardDescription(String shardId, String parentShardId, String startingSequenceNumber,
|
||||
String endingSequenceNumber) {}
|
||||
|
||||
record ShardsPage(List<ShardDescription> shards, String lastEvaluatedShardId) {}
|
||||
|
||||
record GetRecordsResult(List<DynamoDbStreamRecord> records, String nextShardIterator) {}
|
||||
|
||||
/** Where an iterator reads from: at {@code sequence} when inclusive, else just after it. */
|
||||
private record Cursor(String sequence, boolean inclusive) {}
|
||||
|
||||
/** What a shard iterator carries: the shard it reads, where, and when it was returned. */
|
||||
private record IteratorToken(String streamArn, String shardId, Cursor cursor, Instant issuedAt) {}
|
||||
|
||||
/** Keyed by table ARN, which is unique per partition, region, account and table. */
|
||||
private final ConcurrentHashMap<String, StreamDescription> streams = new ConcurrentHashMap<>();
|
||||
private final ConcurrentHashMap<String, ConcurrentLinkedDeque<DynamoDbStreamRecord>> records =
|
||||
new ConcurrentHashMap<>();
|
||||
private final ConcurrentHashMap<String, AtomicLong> streamRecordCounts = new ConcurrentHashMap<>();
|
||||
/** Keyed by stream ARN. */
|
||||
private final ConcurrentHashMap<String, StreamState> states = new ConcurrentHashMap<>();
|
||||
/** Per stream (the stream ARN embeds account + region + table) so accounts never share a counter. */
|
||||
private final ConcurrentHashMap<String, AtomicLong> sequenceCounters = new ConcurrentHashMap<>();
|
||||
|
||||
private final ObjectMapper objectMapper;
|
||||
private final Settings settings;
|
||||
private final Clock clock;
|
||||
|
||||
@Inject
|
||||
public DynamoDbStreamService(ObjectMapper objectMapper, StorageFactory storageFactory) {
|
||||
public DynamoDbStreamService(ObjectMapper objectMapper, StorageFactory storageFactory, EmulatorConfig config) {
|
||||
this(objectMapper, storageFactory.create("dynamodb", "dynamodb-tables.json",
|
||||
new TypeReference<Map<String, TableDefinition>>() {}));
|
||||
new TypeReference<Map<String, TableDefinition>>() {}),
|
||||
Settings.from(config.services().dynamodb()), Clock.systemUTC());
|
||||
}
|
||||
|
||||
/** Package-private constructor for testing. */
|
||||
DynamoDbStreamService(ObjectMapper objectMapper, StorageBackend<String, TableDefinition> tableStore) {
|
||||
this.objectMapper = objectMapper;
|
||||
loadPersistedStreams(tableStore);
|
||||
this(objectMapper, tableStore, Settings.DEFAULTS, Clock.systemUTC());
|
||||
}
|
||||
|
||||
/** Package-private constructor for testing streams shaped by {@code settings}, on {@code clock}. */
|
||||
DynamoDbStreamService(ObjectMapper objectMapper, StorageBackend<String, TableDefinition> tableStore,
|
||||
Settings settings, Clock clock) {
|
||||
this.objectMapper = objectMapper;
|
||||
this.settings = settings;
|
||||
this.clock = clock;
|
||||
loadPersistedStreams(tableStore);
|
||||
}
|
||||
|
||||
private void loadPersistedStreams(StorageBackend<String, TableDefinition> tableStore) {
|
||||
if (tableStore == null) {
|
||||
@@ -104,7 +164,7 @@ public class DynamoDbStreamService {
|
||||
}
|
||||
|
||||
String streamArn = streamArnInput;
|
||||
Instant now = Instant.now();
|
||||
Instant now = clock.instant();
|
||||
String label;
|
||||
if (streamArn == null){
|
||||
label = STREAM_LABEL_FORMAT.format(now);
|
||||
@@ -123,17 +183,32 @@ public class DynamoDbStreamService {
|
||||
sd.setCreationDateTime(now);
|
||||
sd.setStartingSequenceNumber(String.format("%021d", 1));
|
||||
|
||||
StreamState state = new StreamState(counter(streamArn));
|
||||
synchronized (state) {
|
||||
for (int slot = 0; slot < settings.shardCount(); slot++) {
|
||||
state.open(1, slot, null, now);
|
||||
}
|
||||
}
|
||||
// Shards first, so a write that already sees the stream has somewhere to land.
|
||||
states.put(streamArn, state);
|
||||
streams.put(tableArn, sd);
|
||||
records.put(streamArn, new ConcurrentLinkedDeque<>());
|
||||
streamRecordCounts.put(streamArn, new AtomicLong());
|
||||
LOG.infov("Enabled stream for table {0} in region {1}: {2}", tableName, region, streamArn);
|
||||
LOG.infov("Enabled stream for table {0} in region {1}: {2} ({3} shards)",
|
||||
tableName, region, streamArn, settings.shardCount());
|
||||
return sd;
|
||||
}
|
||||
|
||||
/** Disabling a stream closes its open shards, as on AWS; what they hold stays readable. */
|
||||
public void disableStream(String tableArn) {
|
||||
StreamDescription sd = streams.get(tableArn);
|
||||
if (sd != null) {
|
||||
sd.setStreamStatus("DISABLED");
|
||||
StreamState state = states.get(sd.getStreamArn());
|
||||
if (state != null) {
|
||||
synchronized (state) {
|
||||
Instant now = clock.instant();
|
||||
state.shards.stream().filter(Shard::open).toList().forEach(shard -> state.close(shard, now));
|
||||
}
|
||||
}
|
||||
LOG.infov("Disabled stream for table {0}", tableArn);
|
||||
}
|
||||
}
|
||||
@@ -141,8 +216,7 @@ public class DynamoDbStreamService {
|
||||
public void deleteStream(String tableArn) {
|
||||
StreamDescription sd = streams.remove(tableArn);
|
||||
if (sd != null) {
|
||||
records.remove(sd.getStreamArn());
|
||||
streamRecordCounts.remove(sd.getStreamArn());
|
||||
states.remove(sd.getStreamArn());
|
||||
sequenceCounters.remove(sd.getStreamArn());
|
||||
LOG.infov("Deleted stream for table {0}", tableArn);
|
||||
}
|
||||
@@ -151,8 +225,7 @@ public class DynamoDbStreamService {
|
||||
/** Emulator reset: drops every stream and its records. Sequence numbers stay monotonic. */
|
||||
public void clear() {
|
||||
streams.clear();
|
||||
records.clear();
|
||||
streamRecordCounts.clear();
|
||||
states.clear();
|
||||
}
|
||||
|
||||
public void captureEvent(String eventName, JsonNode oldItem, JsonNode newItem,
|
||||
@@ -181,29 +254,52 @@ public class DynamoDbStreamService {
|
||||
record.setEventName(eventName);
|
||||
record.setEventSource("aws:dynamodb");
|
||||
record.setAwsRegion(region);
|
||||
record.setApproximateCreationDateTime(Instant.now().getEpochSecond());
|
||||
record.setApproximateCreationDateTime(clock.instant().getEpochSecond());
|
||||
record.setKeys(keys);
|
||||
record.setNewImage(newImage);
|
||||
record.setOldImage(oldImage);
|
||||
record.setStreamViewType(viewType);
|
||||
record.setTtlDeletion(ttlDeletion);
|
||||
|
||||
ConcurrentLinkedDeque<DynamoDbStreamRecord> deque = records.get(sd.getStreamArn());
|
||||
if (deque != null) {
|
||||
synchronized (deque) {
|
||||
long seq = sequenceCounters.computeIfAbsent(sd.getStreamArn(), k -> new AtomicLong())
|
||||
.incrementAndGet();
|
||||
record.setSequenceNumber(String.format("%021d", seq));
|
||||
streamRecordCounts.computeIfAbsent(sd.getStreamArn(), ignored -> new AtomicLong())
|
||||
.incrementAndGet();
|
||||
deque.addLast(record);
|
||||
while (deque.size() > MAX_RECORDS) {
|
||||
deque.pollFirst();
|
||||
}
|
||||
StreamState state = states.get(sd.getStreamArn());
|
||||
if (state == null) {
|
||||
return;
|
||||
}
|
||||
int slot = slotOf(keys, table);
|
||||
synchronized (state) {
|
||||
maintain(state, clock.instant());
|
||||
Shard shard = state.openShard(slot);
|
||||
if (shard == null) {
|
||||
// Disabled since the status check above, which closed every shard.
|
||||
return;
|
||||
}
|
||||
record.setSequenceNumber(sequence(state.counter.incrementAndGet()));
|
||||
shard.records.addLast(record);
|
||||
shard.lastSequence = record.getSequenceNumber();
|
||||
state.retained++;
|
||||
while (state.retained > MAX_RECORDS) {
|
||||
state.trimOldest();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The slot whose open shard takes an item's changes, from its partition key, so one item's changes always land
|
||||
* on one shard and stay in order.
|
||||
*/
|
||||
private int slotOf(ObjectNode keys, TableDefinition table) {
|
||||
if (settings.shardCount() == 1) {
|
||||
return 0;
|
||||
}
|
||||
for (KeySchemaElement element : table.getKeySchema()) {
|
||||
if ("HASH".equals(element.getKeyType()) && keys.has(element.getAttributeName())) {
|
||||
return Math.floorMod(keys.get(element.getAttributeName()).toString().hashCode(),
|
||||
settings.shardCount());
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
private ObjectNode buildKeys(JsonNode item, TableDefinition table) {
|
||||
ObjectNode keys = objectMapper.createObjectNode();
|
||||
if (item == null) {
|
||||
@@ -256,6 +352,34 @@ public class DynamoDbStreamService {
|
||||
"Stream not found: " + streamArn, 400);
|
||||
}
|
||||
|
||||
/** The stream's shards after {@code exclusiveStartShardId}, in shard ID order, a page of at most {@code limit}. */
|
||||
ShardsPage describeShards(String streamArn, String exclusiveStartShardId, Integer limit) {
|
||||
describeStream(streamArn);
|
||||
StreamState state = states.get(streamArn);
|
||||
if (state == null) {
|
||||
return new ShardsPage(List.of(), null);
|
||||
}
|
||||
requirePositiveLimit(limit);
|
||||
int pageSize = limit == null ? MAX_SHARDS_PER_PAGE : Math.min(limit, MAX_SHARDS_PER_PAGE);
|
||||
List<ShardDescription> page = new ArrayList<>();
|
||||
boolean more = false;
|
||||
synchronized (state) {
|
||||
maintain(state, clock.instant());
|
||||
for (Shard shard : state.shards) {
|
||||
if (exclusiveStartShardId != null && shard.id.compareTo(exclusiveStartShardId) <= 0) {
|
||||
continue;
|
||||
}
|
||||
if (page.size() == pageSize) {
|
||||
more = true;
|
||||
break;
|
||||
}
|
||||
page.add(new ShardDescription(shard.id, shard.parentId, shard.startingSequenceNumber,
|
||||
shard.endingSequenceNumber));
|
||||
}
|
||||
}
|
||||
return new ShardsPage(List.copyOf(page), more ? page.get(page.size() - 1).shardId() : null);
|
||||
}
|
||||
|
||||
String getShardIterator(String streamArn, String shardId,
|
||||
String iteratorType, String sequenceNumber) {
|
||||
StreamDescription sd = describeStream(streamArn);
|
||||
@@ -263,140 +387,336 @@ public class DynamoDbStreamService {
|
||||
throw new AwsException("ResourceNotFoundException",
|
||||
"Stream not found: " + streamArn, 400);
|
||||
}
|
||||
|
||||
ConcurrentLinkedDeque<DynamoDbStreamRecord> deque = records.get(streamArn);
|
||||
List<DynamoDbStreamRecord> snapshot;
|
||||
long recordCount;
|
||||
if (deque == null) {
|
||||
snapshot = List.of();
|
||||
recordCount = 0;
|
||||
} else {
|
||||
synchronized (deque) {
|
||||
snapshot = new ArrayList<>(deque);
|
||||
recordCount = streamRecordCounts.getOrDefault(streamArn, new AtomicLong()).get();
|
||||
}
|
||||
boolean bySequence = "AT_SEQUENCE_NUMBER".equals(iteratorType) || "AFTER_SEQUENCE_NUMBER".equals(iteratorType);
|
||||
if (!bySequence && !"TRIM_HORIZON".equals(iteratorType) && !"LATEST".equals(iteratorType)) {
|
||||
throw new AwsException("ValidationException", "Unknown iterator type: " + iteratorType, 400);
|
||||
}
|
||||
if (bySequence && (sequenceNumber == null || sequenceNumber.isBlank())) {
|
||||
throw new AwsException("ValidationException",
|
||||
"One or more parameter values were invalid: SequenceNumber cannot be null", 400);
|
||||
}
|
||||
|
||||
String cursorSequence = switch (iteratorType) {
|
||||
case "TRIM_HORIZON" -> snapshot.isEmpty() ? zeroSequence() : snapshot.get(0).getSequenceNumber();
|
||||
case "LATEST" -> snapshot.isEmpty() ? zeroSequence() : snapshot.get(snapshot.size() - 1).getSequenceNumber();
|
||||
case "AT_SEQUENCE_NUMBER", "AFTER_SEQUENCE_NUMBER" -> {
|
||||
if (sequenceNumber == null || sequenceNumber.isBlank()) {
|
||||
throw new AwsException("ValidationException",
|
||||
"Sequence number is required for this iterator type", 400);
|
||||
StreamState state = states.get(streamArn);
|
||||
if (state == null) {
|
||||
throw shardNotFound();
|
||||
}
|
||||
Instant now = clock.instant();
|
||||
Cursor cursor;
|
||||
synchronized (state) {
|
||||
maintain(state, now);
|
||||
Shard shard = state.find(shardId);
|
||||
if (shard == null) {
|
||||
throw shardNotFound();
|
||||
}
|
||||
cursor = switch (iteratorType) {
|
||||
case "TRIM_HORIZON" -> shard.records.isEmpty()
|
||||
? shard.afterTrimmed() : new Cursor(shard.records.getFirst().getSequenceNumber(), true);
|
||||
case "LATEST" -> shard.records.isEmpty()
|
||||
? shard.afterTrimmed() : new Cursor(shard.records.getLast().getSequenceNumber(), false);
|
||||
default -> {
|
||||
if (settings.retention()) {
|
||||
requireHeld(state, shard, sequenceNumber);
|
||||
}
|
||||
yield new Cursor(sequenceNumber, "AT_SEQUENCE_NUMBER".equals(iteratorType));
|
||||
}
|
||||
yield sequenceNumber;
|
||||
}
|
||||
default -> throw new AwsException("ValidationException",
|
||||
"Unknown iterator type: " + iteratorType, 400);
|
||||
};
|
||||
|
||||
boolean inclusive = "TRIM_HORIZON".equals(iteratorType) || "AT_SEQUENCE_NUMBER".equals(iteratorType);
|
||||
long cursorRecordCount = zeroSequence().equals(cursorSequence) ? recordCount : -1;
|
||||
return encodeIterator(streamArn, cursorSequence, inclusive, cursorRecordCount);
|
||||
};
|
||||
}
|
||||
return encodeIterator(new IteratorToken(streamArn, shardId, cursor, now));
|
||||
}
|
||||
|
||||
private int findSequencePosition(List<DynamoDbStreamRecord> records, String targetSeq, boolean inclusive) {
|
||||
for (int i = 0; i < records.size(); i++) {
|
||||
String seq = records.get(i).getSequenceNumber();
|
||||
int cmp = seq.compareTo(targetSeq);
|
||||
if (inclusive ? cmp >= 0 : cmp > 0) {
|
||||
return i;
|
||||
/**
|
||||
* Refuses a sequence number the shard never held, as AWS does with ValidationException, and one it held but has
|
||||
* trimmed, with TrimmedDataAccessException. A sequence number at or below the shard's trim point cannot be told
|
||||
* apart from another shard's, so it is taken as trimmed. A shard's starting sequence number is no exception: the
|
||||
* first shards of a stream all start at the same one, and only the shard whose record it is holds it.
|
||||
*/
|
||||
private static void requireHeld(StreamState state, Shard shard, String sequenceNumber) {
|
||||
BigInteger requested;
|
||||
try {
|
||||
requested = new BigInteger(sequenceNumber);
|
||||
} catch (NumberFormatException e) {
|
||||
throw invalidSequenceNumber();
|
||||
}
|
||||
if (requested.compareTo(new BigInteger(shard.startingSequenceNumber)) < 0
|
||||
|| requested.compareTo(BigInteger.valueOf(state.counter.get())) > 0
|
||||
|| (!shard.open() && requested.compareTo(new BigInteger(shard.endingSequenceNumber)) > 0)) {
|
||||
throw invalidSequenceNumber();
|
||||
}
|
||||
if (shard.trimmedThrough != null && requested.compareTo(new BigInteger(shard.trimmedThrough)) <= 0) {
|
||||
throw trimmed();
|
||||
}
|
||||
for (DynamoDbStreamRecord record : shard.records) {
|
||||
if (requested.compareTo(new BigInteger(record.getSequenceNumber())) == 0) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
return records.size();
|
||||
throw invalidSequenceNumber();
|
||||
}
|
||||
|
||||
record GetRecordsResult(List<DynamoDbStreamRecord> records, String nextShardIterator) {}
|
||||
|
||||
GetRecordsResult getRecords(String shardIterator, Integer limit) {
|
||||
String[] parts = decodeIterator(shardIterator);
|
||||
String streamArn = parts[0];
|
||||
String cursorSequence = parts[1];
|
||||
boolean inclusive = Boolean.parseBoolean(parts[2]);
|
||||
long cursorRecordCount = parseRecordCount(parts[3]);
|
||||
|
||||
ConcurrentLinkedDeque<DynamoDbStreamRecord> deque = records.get(streamArn);
|
||||
List<DynamoDbStreamRecord> snapshot;
|
||||
long currentRecordCount;
|
||||
if (deque == null) {
|
||||
snapshot = List.of();
|
||||
currentRecordCount = 0;
|
||||
} else {
|
||||
synchronized (deque) {
|
||||
snapshot = new ArrayList<>(deque);
|
||||
currentRecordCount = streamRecordCounts.getOrDefault(streamArn, new AtomicLong()).get();
|
||||
requirePositiveLimit(limit);
|
||||
IteratorToken iterator = decodeIterator(shardIterator);
|
||||
Instant now = clock.instant();
|
||||
if (settings.iteratorExpiry() && now.isAfter(iterator.issuedAt().plus(ITERATOR_LIFETIME))) {
|
||||
throw new AwsException("ExpiredIteratorException", "The provided iterator exceeds the maximum age allowed.",
|
||||
400);
|
||||
}
|
||||
StreamState state = states.get(iterator.streamArn());
|
||||
if (state == null) {
|
||||
return new GetRecordsResult(List.of(), encodeIterator(
|
||||
new IteratorToken(iterator.streamArn(), iterator.shardId(), iterator.cursor(), now)));
|
||||
}
|
||||
Cursor cursor = iterator.cursor();
|
||||
List<DynamoDbStreamRecord> page;
|
||||
Cursor next;
|
||||
boolean ended;
|
||||
synchronized (state) {
|
||||
maintain(state, now);
|
||||
Shard shard = state.find(iterator.shardId());
|
||||
if (shard == null) {
|
||||
throw shardNotFound();
|
||||
}
|
||||
if (shard.trimmedThrough != null) {
|
||||
int trimmedAgainstCursor = shard.trimmedThrough.compareTo(cursor.sequence());
|
||||
if (cursor.inclusive() ? trimmedAgainstCursor >= 0 : trimmedAgainstCursor > 0) {
|
||||
throw trimmed();
|
||||
}
|
||||
}
|
||||
List<DynamoDbStreamRecord> held = new ArrayList<>(shard.records);
|
||||
int position = held.size();
|
||||
for (int i = 0; i < held.size(); i++) {
|
||||
int againstCursor = held.get(i).getSequenceNumber().compareTo(cursor.sequence());
|
||||
if (cursor.inclusive() ? againstCursor >= 0 : againstCursor > 0) {
|
||||
position = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
int effectiveLimit = limit != null ? limit : 100;
|
||||
page = new ArrayList<>(held.subList(position, Math.min(position + effectiveLimit, held.size())));
|
||||
next = page.isEmpty() ? cursor : new Cursor(page.getLast().getSequenceNumber(), false);
|
||||
// A closed shard read to its end has no next iterator, which is how a reader learns it is done.
|
||||
ended = !shard.open() && position >= held.size();
|
||||
}
|
||||
int position = findSequencePosition(snapshot, cursorSequence, inclusive);
|
||||
if (zeroSequence().equals(cursorSequence) && cursorRecordCount >= 0
|
||||
&& currentRecordCount - cursorRecordCount > MAX_RECORDS) {
|
||||
throw new AwsException("TrimmedDataAccessException",
|
||||
"The requested sequence number has been trimmed", 400);
|
||||
}
|
||||
if (!snapshot.isEmpty() && !zeroSequence().equals(cursorSequence)
|
||||
&& cursorSequence.compareTo(snapshot.get(0).getSequenceNumber()) < 0) {
|
||||
throw new AwsException("TrimmedDataAccessException",
|
||||
"The requested sequence number has been trimmed", 400);
|
||||
}
|
||||
|
||||
int effectiveLimit = limit != null ? limit : 100;
|
||||
int end = Math.min(position + effectiveLimit, snapshot.size());
|
||||
List<DynamoDbStreamRecord> page = snapshot.subList(position, end);
|
||||
|
||||
String nextSequence = page.isEmpty()
|
||||
? cursorSequence
|
||||
: page.get(page.size() - 1).getSequenceNumber();
|
||||
long nextRecordCount = zeroSequence().equals(nextSequence) ? cursorRecordCount : -1;
|
||||
String nextIterator = encodeIterator(streamArn, nextSequence, page.isEmpty() && inclusive, nextRecordCount);
|
||||
return new GetRecordsResult(new ArrayList<>(page), nextIterator);
|
||||
return new GetRecordsResult(page, ended ? null
|
||||
: encodeIterator(new IteratorToken(iterator.streamArn(), iterator.shardId(), next, now)));
|
||||
}
|
||||
|
||||
private String encodeIterator(String streamArn, String sequenceNumber, boolean inclusive, long recordCount) {
|
||||
String raw = streamArn + "|" + sequenceNumber + "|" + inclusive + "|" + recordCount;
|
||||
return Base64.getEncoder().encodeToString(raw.getBytes());
|
||||
/**
|
||||
* Applies the passage of time to a stream: with retention on, records past 24 hours are trimmed and shards closed
|
||||
* longer ago are dropped; with a rollover, each open shard that has been open that long closes and a child takes
|
||||
* its keys. Done on every use of the stream rather than on a timer, so it costs nothing while idle. A closed shard
|
||||
* that holds no records is dropped once the shard that replaced it has closed too: nothing is left to read in it,
|
||||
* and a reader has had a whole rollover to reach its end. That keeps an idle stream at two generations of shards,
|
||||
* and a busy one at the shards that still hold records.
|
||||
*/
|
||||
private void maintain(StreamState state, Instant now) {
|
||||
if (settings.retention()) {
|
||||
long oldestKept = now.minus(RETENTION).getEpochSecond();
|
||||
for (Shard shard : state.shards) {
|
||||
while (!shard.records.isEmpty() && shard.records.getFirst().getApproximateCreationDateTime() < oldestKept) {
|
||||
state.trimFirst(shard);
|
||||
}
|
||||
}
|
||||
Instant closedBefore = now.minus(RETENTION);
|
||||
state.shards.removeIf(shard -> {
|
||||
boolean expired = !shard.open() && shard.closedAt.isBefore(closedBefore);
|
||||
if (expired) {
|
||||
state.retained -= shard.records.size();
|
||||
}
|
||||
return expired;
|
||||
});
|
||||
}
|
||||
if (settings.shardRollover() != null) {
|
||||
for (int i = 0, existing = state.shards.size(); i < existing; i++) {
|
||||
Shard shard = state.shards.get(i);
|
||||
if (shard.open() && !now.isBefore(shard.openedAt.plus(settings.shardRollover()))) {
|
||||
state.close(shard, now);
|
||||
state.open(shard.generation + 1, shard.slot, shard.id, now);
|
||||
}
|
||||
}
|
||||
}
|
||||
state.shards.removeIf(shard -> !shard.open() && shard.records.isEmpty() && shard.replacementClosed);
|
||||
}
|
||||
|
||||
private String encodeIterator(IteratorToken iterator) {
|
||||
String raw = iterator.streamArn() + "|" + iterator.shardId() + "|" + iterator.cursor().sequence() + "|"
|
||||
+ iterator.cursor().inclusive() + "|" + iterator.issuedAt().toEpochMilli();
|
||||
return Base64.getEncoder().encodeToString(raw.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
/** The stream an iterator reads, rejecting a malformed iterator as GetRecords does. */
|
||||
String streamArnOf(String shardIterator) {
|
||||
return decodeIterator(shardIterator)[0];
|
||||
return decodeIterator(shardIterator).streamArn();
|
||||
}
|
||||
|
||||
private String[] decodeIterator(String iterator) {
|
||||
/** Fields are read from the right, so the stream ARN keeps whatever it holds. */
|
||||
private IteratorToken decodeIterator(String token) {
|
||||
String raw;
|
||||
try {
|
||||
String raw = new String(Base64.getDecoder().decode(iterator));
|
||||
int lastPipe = raw.lastIndexOf('|');
|
||||
int sequencePipe = raw.lastIndexOf('|', lastPipe - 1);
|
||||
int inclusivePipe = raw.lastIndexOf('|', sequencePipe - 1);
|
||||
if (inclusivePipe < 0 || sequencePipe < 0 || lastPipe < 0 || lastPipe == raw.length() - 1) {
|
||||
throw new AwsException("ValidationException", "Invalid shard iterator", 400);
|
||||
}
|
||||
String inclusive = raw.substring(sequencePipe + 1, lastPipe);
|
||||
String recordCount = raw.substring(lastPipe + 1);
|
||||
if (!"true".equals(inclusive) && !"false".equals(inclusive)) {
|
||||
throw new AwsException("ValidationException", "Invalid shard iterator", 400);
|
||||
}
|
||||
try {
|
||||
Long.parseLong(recordCount);
|
||||
} catch (NumberFormatException e) {
|
||||
throw new AwsException("ValidationException", "Invalid shard iterator", 400);
|
||||
}
|
||||
return new String[]{raw.substring(0, inclusivePipe), raw.substring(inclusivePipe + 1, sequencePipe),
|
||||
inclusive, recordCount};
|
||||
raw = new String(Base64.getDecoder().decode(token), StandardCharsets.UTF_8);
|
||||
} catch (IllegalArgumentException e) {
|
||||
throw new AwsException("ValidationException", "Invalid shard iterator", 400);
|
||||
throw invalidIterator();
|
||||
}
|
||||
String[] fields = new String[5];
|
||||
int end = raw.length();
|
||||
for (int field = 4; field > 0; field--) {
|
||||
int pipe = raw.lastIndexOf('|', end - 1);
|
||||
if (pipe < 0) {
|
||||
throw invalidIterator();
|
||||
}
|
||||
fields[field] = raw.substring(pipe + 1, end);
|
||||
end = pipe;
|
||||
}
|
||||
fields[0] = raw.substring(0, end);
|
||||
if (fields[0].isEmpty() || fields[1].isEmpty() || fields[2].isEmpty()
|
||||
|| !("true".equals(fields[3]) || "false".equals(fields[3]))) {
|
||||
throw invalidIterator();
|
||||
}
|
||||
}
|
||||
|
||||
private long parseRecordCount(String recordCount) {
|
||||
try {
|
||||
return Long.parseLong(recordCount);
|
||||
return new IteratorToken(fields[0], fields[1], new Cursor(fields[2], Boolean.parseBoolean(fields[3])),
|
||||
Instant.ofEpochMilli(Long.parseLong(fields[4])));
|
||||
} catch (NumberFormatException e) {
|
||||
throw new AwsException("ValidationException", "Invalid shard iterator", 400);
|
||||
throw invalidIterator();
|
||||
}
|
||||
}
|
||||
|
||||
private String zeroSequence() {
|
||||
return ZERO_SEQUENCE_NUMBER;
|
||||
private AtomicLong counter(String streamArn) {
|
||||
return sequenceCounters.computeIfAbsent(streamArn, ignored -> new AtomicLong());
|
||||
}
|
||||
|
||||
/** Generation 1, slot 0 is {@code shardId-0000000001-00000000001}; IDs sort in generation, then slot, order. */
|
||||
private static String shardId(int generation, int slot) {
|
||||
return String.format("shardId-%010d-%011d", generation, slot + 1);
|
||||
}
|
||||
|
||||
private static String sequence(long value) {
|
||||
return String.format("%021d", value);
|
||||
}
|
||||
|
||||
/** {@code Limit} of DescribeStream and GetRecords is at least 1; AWS refuses less before reading anything. */
|
||||
private static void requirePositiveLimit(Integer limit) {
|
||||
if (limit != null && limit < 1) {
|
||||
throw new AwsException("ValidationException", "1 validation error detected: Value at 'limit' failed to "
|
||||
+ "satisfy constraint: Member must have value greater than or equal to 1", 400);
|
||||
}
|
||||
}
|
||||
|
||||
private static AwsException shardNotFound() {
|
||||
return new AwsException("ResourceNotFoundException", "Requested resource not found: Shard does not exist", 400);
|
||||
}
|
||||
|
||||
private static AwsException trimmed() {
|
||||
return new AwsException("TrimmedDataAccessException", "The data you are trying to access has been trimmed.", 400);
|
||||
}
|
||||
|
||||
private static AwsException invalidSequenceNumber() {
|
||||
return new AwsException("ValidationException", "Invalid SequenceNumber for the shard", 400);
|
||||
}
|
||||
|
||||
private static AwsException invalidIterator() {
|
||||
return new AwsException("ValidationException", "Invalid shard iterator", 400);
|
||||
}
|
||||
|
||||
/** A stream's shards, oldest first, and the records they keep. Guarded by its own monitor. */
|
||||
private static final class StreamState {
|
||||
final List<Shard> shards = new ArrayList<>();
|
||||
final AtomicLong counter;
|
||||
/** Records kept across every shard, held to {@link #MAX_RECORDS}. */
|
||||
int retained;
|
||||
|
||||
StreamState(AtomicLong counter) {
|
||||
this.counter = counter;
|
||||
}
|
||||
|
||||
void open(int generation, int slot, String parentId, Instant now) {
|
||||
shards.add(new Shard(generation, slot, parentId, sequence(counter.get() + 1), now));
|
||||
}
|
||||
|
||||
Shard find(String shardId) {
|
||||
for (Shard shard : shards) {
|
||||
if (shard.id.equals(shardId)) {
|
||||
return shard;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Closes {@code shard} and tells its parent, which no reader needs once it is empty and this has closed. */
|
||||
void close(Shard shard, Instant now) {
|
||||
shard.close(now);
|
||||
Shard parent = shard.parentId == null ? null : find(shard.parentId);
|
||||
if (parent != null) {
|
||||
parent.replacementClosed = true;
|
||||
}
|
||||
}
|
||||
|
||||
Shard openShard(int slot) {
|
||||
for (Shard shard : shards) {
|
||||
if (shard.slot == slot && shard.open()) {
|
||||
return shard;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Trims the stream's oldest record, whichever shard holds it. */
|
||||
void trimOldest() {
|
||||
Shard oldest = null;
|
||||
for (Shard shard : shards) {
|
||||
if (!shard.records.isEmpty() && (oldest == null || shard.records.getFirst().getSequenceNumber()
|
||||
.compareTo(oldest.records.getFirst().getSequenceNumber()) < 0)) {
|
||||
oldest = shard;
|
||||
}
|
||||
}
|
||||
if (oldest != null) {
|
||||
trimFirst(oldest);
|
||||
}
|
||||
}
|
||||
|
||||
void trimFirst(Shard shard) {
|
||||
shard.trimmedThrough = shard.records.removeFirst().getSequenceNumber();
|
||||
retained--;
|
||||
}
|
||||
}
|
||||
|
||||
private static final class Shard {
|
||||
final String id;
|
||||
final String parentId;
|
||||
final int generation;
|
||||
final int slot;
|
||||
final String startingSequenceNumber;
|
||||
final Instant openedAt;
|
||||
final ArrayDeque<DynamoDbStreamRecord> records = new ArrayDeque<>();
|
||||
/** The sequence number of the newest record ever added, or null. */
|
||||
String lastSequence;
|
||||
/** The sequence number of the newest record trimmed away, or null: a cursor at or before it lost records. */
|
||||
String trimmedThrough;
|
||||
String endingSequenceNumber;
|
||||
Instant closedAt;
|
||||
/** Whether the child shard that took this one's keys has closed in turn. */
|
||||
boolean replacementClosed;
|
||||
|
||||
Shard(int generation, int slot, String parentId, String startingSequenceNumber, Instant openedAt) {
|
||||
this.id = shardId(generation, slot);
|
||||
this.parentId = parentId;
|
||||
this.generation = generation;
|
||||
this.slot = slot;
|
||||
this.startingSequenceNumber = startingSequenceNumber;
|
||||
this.openedAt = openedAt;
|
||||
}
|
||||
|
||||
boolean open() {
|
||||
return closedAt == null;
|
||||
}
|
||||
|
||||
void close(Instant now) {
|
||||
closedAt = now;
|
||||
endingSequenceNumber = lastSequence != null ? lastSequence : startingSequenceNumber;
|
||||
}
|
||||
|
||||
/** Where TRIM_HORIZON and LATEST read an empty shard: after anything it trimmed, so no trim is reported. */
|
||||
Cursor afterTrimmed() {
|
||||
return new Cursor(trimmedThrough != null ? trimmedThrough : ZERO_SEQUENCE_NUMBER, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
+32
-11
@@ -92,12 +92,18 @@ public class NativeDynamoDbStreamsJsonHandler {
|
||||
sd.getTableName(), streamArn);
|
||||
}
|
||||
|
||||
String exclusiveStartShardId = request.hasNonNull("ExclusiveStartShardId")
|
||||
? request.get("ExclusiveStartShardId").asText() : null;
|
||||
Integer limit = request.hasNonNull("Limit") ? request.get("Limit").asInt() : null;
|
||||
DynamoDbStreamService.ShardsPage shards = streamService.describeShards(streamArn, exclusiveStartShardId, limit);
|
||||
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
response.set("StreamDescription", describeStreamToNode(sd, keySchema));
|
||||
response.set("StreamDescription", describeStreamToNode(sd, keySchema, shards));
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
private ObjectNode describeStreamToNode(StreamDescription sd, List<KeySchemaElement> keySchema) {
|
||||
private ObjectNode describeStreamToNode(StreamDescription sd, List<KeySchemaElement> keySchema,
|
||||
DynamoDbStreamService.ShardsPage page) {
|
||||
ObjectNode node = objectMapper.createObjectNode();
|
||||
node.put("StreamArn", sd.getStreamArn());
|
||||
node.put("StreamLabel", sd.getStreamLabel());
|
||||
@@ -116,15 +122,27 @@ public class NativeDynamoDbStreamsJsonHandler {
|
||||
node.set("KeySchema", keySchemaArray);
|
||||
|
||||
ArrayNode shards = objectMapper.createArrayNode();
|
||||
ObjectNode shard = objectMapper.createObjectNode();
|
||||
shard.put("ShardId", DynamoDbStreamService.SHARD_ID);
|
||||
ObjectNode seqRange = objectMapper.createObjectNode();
|
||||
seqRange.put("StartingSequenceNumber", sd.getStartingSequenceNumber());
|
||||
shard.set("SequenceNumberRange", seqRange);
|
||||
shards.add(shard);
|
||||
for (DynamoDbStreamService.ShardDescription description : page.shards()) {
|
||||
ObjectNode shard = objectMapper.createObjectNode();
|
||||
shard.put("ShardId", description.shardId());
|
||||
if (description.parentShardId() != null) {
|
||||
shard.put("ParentShardId", description.parentShardId());
|
||||
}
|
||||
ObjectNode seqRange = objectMapper.createObjectNode();
|
||||
seqRange.put("StartingSequenceNumber", description.startingSequenceNumber());
|
||||
if (description.endingSequenceNumber() != null) {
|
||||
seqRange.put("EndingSequenceNumber", description.endingSequenceNumber());
|
||||
}
|
||||
shard.set("SequenceNumberRange", seqRange);
|
||||
shards.add(shard);
|
||||
}
|
||||
node.set("Shards", shards);
|
||||
|
||||
node.putNull("LastEvaluatedShardId");
|
||||
if (page.lastEvaluatedShardId() != null) {
|
||||
node.put("LastEvaluatedShardId", page.lastEvaluatedShardId());
|
||||
} else {
|
||||
node.putNull("LastEvaluatedShardId");
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
@@ -156,8 +174,11 @@ public class NativeDynamoDbStreamsJsonHandler {
|
||||
recordsArray.add(recordToNode(record));
|
||||
}
|
||||
response.set("Records", recordsArray);
|
||||
response.put("NextShardIterator", result.nextShardIterator());
|
||||
|
||||
// A closed shard read to its end has none, which tells the reader it is done.
|
||||
if (result.nextShardIterator() != null) {
|
||||
response.put("NextShardIterator", result.nextShardIterator());
|
||||
}
|
||||
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user