mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(cloudformation): reconcile DynamoDB streams declared by StreamSpecification (#2411)
Provisioning AWS::DynamoDB::Table ignored StreamSpecification, so a stack that declared a stream produced a streamless table: DescribeTable returned neither StreamSpecification nor LatestStreamArn, and event source mappings registered against the expected stream ARN polled "Stream not found" forever. The plain UpdateTable API enabled streams correctly on the same runtime, which is what made this look CloudFormation-specific. The CloudFormation property carries no StreamEnabled flag — unlike the DynamoDB API, declaring the block IS the request — so the block's presence now turns the stream on, honouring StreamViewType. A block that omits StreamViewType still yields a working stream: the schema marks it required and AWS documents no default, so this is deliberately the same leniency DynamoDbJsonHandler already applies on CreateTable/UpdateTable, not an AWS behaviour. Removing the block on an update is the inverse request and switches the stream back off, so a table updated out of streaming stops emitting records to whatever still holds its ARN. Streams are reconciled through new DynamoDbService enableStream/disableStream, mirroring what the JSON handler already does inline for CreateTable/UpdateTable and persisting the table so the change survives. Callers that reach DynamoDB through the service rather than the handler had no entry point for this. Re-enabling a live stream now retargets its view type. The records a stream emits are built from the stream description, which was left untouched once the stream was ENABLED, so a changed StreamViewType was reported by DescribeTable while records kept the old shape. This also fixes the same inconsistency on the UpdateTable path. Make that stream state durable. Both handler paths mutate the table after the service has stored it, and a persistent backend serializes on write rather than holding the live object, so the on-disk copy kept the previous settings. Since startup rebuilds every stream from the persisted table, a restart resurrected the old view type — and a stream declared on CreateTable was lost outright, because the persisted copy still had streamEnabled false. Both paths now flush once after their mutations, which also makes the billing mode, table class, tags and SSE settings they set survive a restart. Also reconcile the StreamArn attribute: only a live stream has an ARN worth handing to Fn::GetAtt. The previous synthetic value resolved to nothing on a streamless table, and because an update starts from the resource's previous attributes, a stale ARN is now removed rather than merely left unwritten. Adds DynamoDbStreamSpecificationCfnProvisionerTest covering the declared stream, an explicit StreamViewType, a block with no view type, removal and retention on update, a view-type change on update, and the unchanged no-stream path; DynamoDbStreamServiceTest coverage that a retarget reaches the emitted records; and DynamoDbStreamViewTypeDurabilityTest, which drives the handler against a file-backed store and reloads it to prove both paths survive a restart.
This commit is contained in:
1 parent
2314c85b62
commit
c4597f2e53
7 files changed
+526
-1
No files matched your search
+260
@@ -0,0 +1,260 @@
|
||||
package io.github.hectorvent.floci.services.cloudformation;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.services.cloudformation.model.StackResource;
|
||||
import io.github.hectorvent.floci.services.cloudformation.provisioners.CloudFormationResourceRegistry;
|
||||
import io.github.hectorvent.floci.services.dynamodb.DynamoDbService;
|
||||
import io.github.hectorvent.floci.services.dynamodb.model.TableDefinition;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyList;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* A template that declares {@code StreamSpecification} on {@code AWS::DynamoDB::Table} must get a
|
||||
* streamed table.
|
||||
*
|
||||
* <p>The CloudFormation property carries no {@code StreamEnabled} flag — unlike the DynamoDB API,
|
||||
* declaring the block IS the request — so provisioning read it as absent and created the table
|
||||
* streamless. DescribeTable then returned no {@code LatestStreamArn} and an event source mapping
|
||||
* registered against the expected stream ARN polled "Stream not found" forever, while the plain
|
||||
* UpdateTable API on the same runtime enabled streams correctly.
|
||||
*/
|
||||
class DynamoDbStreamSpecificationCfnProvisionerTest {
|
||||
|
||||
private static final String TABLE = "stream-repro";
|
||||
private static final String TABLE_ARN =
|
||||
"arn:aws:dynamodb:us-east-1:000000000000:table/" + TABLE;
|
||||
private static final String STREAM_ARN = TABLE_ARN + "/stream/2026-01-01T00:00:00.000";
|
||||
|
||||
private final ObjectMapper mapper = new ObjectMapper();
|
||||
private DynamoDbService dynamoDbService;
|
||||
private CloudFormationResourceProvisioner provisioner;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
dynamoDbService = mock(DynamoDbService.class);
|
||||
provisioner = new CloudFormationResourceProvisioner(
|
||||
null, null, null, dynamoDbService, null, null, null, null, null, null,
|
||||
null, null, null, null, null, null, mapper, null, null, null, null, null,
|
||||
null, null, null, null, null, null, null, null, null, null, null,
|
||||
new CloudFormationResourceRegistry(List.of()));
|
||||
|
||||
TableDefinition created = new TableDefinition();
|
||||
created.setTableName(TABLE);
|
||||
created.setTableArn(TABLE_ARN);
|
||||
when(dynamoDbService.createTable(anyString(), anyList(), anyList(), any(), any(),
|
||||
anyList(), anyList(), anyString())).thenReturn(created);
|
||||
}
|
||||
|
||||
/** Make provisioning take the update path: the table already exists, with a live stream. */
|
||||
private void existingStreamedTable() {
|
||||
when(dynamoDbService.createTable(anyString(), anyList(), anyList(), any(), any(),
|
||||
anyList(), anyList(), anyString()))
|
||||
.thenThrow(new AwsException("ResourceInUseException", "Table already exists", 400));
|
||||
when(dynamoDbService.describeTable(TABLE, "us-east-1"))
|
||||
.thenReturn(streamed("NEW_AND_OLD_IMAGES"));
|
||||
}
|
||||
|
||||
/** The table the service reports once its stream is switched off (ARN retained, as AWS does). */
|
||||
private TableDefinition streamDisabled() {
|
||||
TableDefinition table = streamed("NEW_AND_OLD_IMAGES");
|
||||
table.setStreamEnabled(false);
|
||||
return table;
|
||||
}
|
||||
|
||||
/** The table the service reports once its stream is on. */
|
||||
private TableDefinition streamed(String viewType) {
|
||||
TableDefinition table = new TableDefinition();
|
||||
table.setTableName(TABLE);
|
||||
table.setTableArn(TABLE_ARN);
|
||||
table.setStreamEnabled(true);
|
||||
table.setStreamArn(STREAM_ARN);
|
||||
table.setStreamViewType(viewType);
|
||||
return table;
|
||||
}
|
||||
|
||||
private StackResource provisionTable(String properties) throws Exception {
|
||||
return provisioner.provision("Table", "AWS::DynamoDB::Table",
|
||||
mapper.readTree(properties), engine(), "us-east-1", "000000000000", "my-stack");
|
||||
}
|
||||
|
||||
/**
|
||||
* An UpdateStack re-provisions with the resource's PREVIOUS attributes already in place, so a
|
||||
* stale StreamArn has to be actively removed rather than merely left unwritten.
|
||||
*/
|
||||
private StackResource reprovisionTable(String properties, Map<String, String> priorAttributes)
|
||||
throws Exception {
|
||||
return provisioner.provision("Table", "AWS::DynamoDB::Table",
|
||||
mapper.readTree(properties), engine(), "us-east-1", "000000000000", "my-stack",
|
||||
TABLE, priorAttributes);
|
||||
}
|
||||
|
||||
// ── the reported bug ──────────────────────────────────────────────────────
|
||||
|
||||
@Test
|
||||
void declaredStreamSpecificationEnablesTheStream() throws Exception {
|
||||
when(dynamoDbService.enableStream(eq(TABLE), eq("NEW_AND_OLD_IMAGES"), eq("us-east-1")))
|
||||
.thenReturn(streamed("NEW_AND_OLD_IMAGES"));
|
||||
|
||||
StackResource resource = provisionTable("""
|
||||
{"TableName":"stream-repro",
|
||||
"AttributeDefinitions":[{"AttributeName":"id","AttributeType":"S"}],
|
||||
"KeySchema":[{"AttributeName":"id","KeyType":"HASH"}],
|
||||
"BillingMode":"PAY_PER_REQUEST",
|
||||
"StreamSpecification":{"StreamViewType":"NEW_AND_OLD_IMAGES"}}
|
||||
""");
|
||||
|
||||
// Previously never called: the table came out streamless.
|
||||
verify(dynamoDbService).enableStream(TABLE, "NEW_AND_OLD_IMAGES", "us-east-1");
|
||||
assertEquals(TABLE, resource.getPhysicalId());
|
||||
assertEquals(TABLE_ARN, resource.getAttributes().get("Arn"));
|
||||
// Fn::GetAtt StreamArn must resolve to the stream that actually exists.
|
||||
assertEquals(STREAM_ARN, resource.getAttributes().get("StreamArn"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void streamViewTypeIsHonoured() throws Exception {
|
||||
when(dynamoDbService.enableStream(eq(TABLE), eq("KEYS_ONLY"), eq("us-east-1")))
|
||||
.thenReturn(streamed("KEYS_ONLY"));
|
||||
|
||||
provisionTable("""
|
||||
{"TableName":"stream-repro",
|
||||
"KeySchema":[{"AttributeName":"id","KeyType":"HASH"}],
|
||||
"AttributeDefinitions":[{"AttributeName":"id","AttributeType":"S"}],
|
||||
"StreamSpecification":{"StreamViewType":"KEYS_ONLY"}}
|
||||
""");
|
||||
|
||||
verify(dynamoDbService).enableStream(TABLE, "KEYS_ONLY", "us-east-1");
|
||||
}
|
||||
|
||||
/**
|
||||
* The CloudFormation schema marks StreamViewType required, but floci accepts a template that
|
||||
* omits it and applies a view type rather than rejecting the stack — the same leniency
|
||||
* DynamoDbJsonHandler already applies on CreateTable/UpdateTable.
|
||||
*/
|
||||
@Test
|
||||
void streamSpecificationWithoutAViewTypeStillEnablesTheStream() throws Exception {
|
||||
when(dynamoDbService.enableStream(eq(TABLE), any(), eq("us-east-1")))
|
||||
.thenReturn(streamed("NEW_AND_OLD_IMAGES"));
|
||||
|
||||
provisionTable("""
|
||||
{"TableName":"stream-repro",
|
||||
"KeySchema":[{"AttributeName":"id","KeyType":"HASH"}],
|
||||
"AttributeDefinitions":[{"AttributeName":"id","AttributeType":"S"}],
|
||||
"StreamSpecification":{}}
|
||||
""");
|
||||
|
||||
verify(dynamoDbService).enableStream(TABLE, null, "us-east-1");
|
||||
}
|
||||
|
||||
// ── unchanged behaviour without the block ─────────────────────────────────
|
||||
|
||||
@Test
|
||||
void tableWithoutStreamSpecificationGetsNoStreamAndNoStreamArnAttribute() throws Exception {
|
||||
StackResource resource = provisionTable("""
|
||||
{"TableName":"stream-repro",
|
||||
"KeySchema":[{"AttributeName":"id","KeyType":"HASH"}],
|
||||
"AttributeDefinitions":[{"AttributeName":"id","AttributeType":"S"}]}
|
||||
""");
|
||||
|
||||
verify(dynamoDbService, never()).enableStream(anyString(), any(), anyString());
|
||||
assertEquals(TABLE_ARN, resource.getAttributes().get("Arn"));
|
||||
// A streamless table previously advertised a synthetic StreamArn that resolved to nothing.
|
||||
assertNull(resource.getAttributes().get("StreamArn"));
|
||||
assertFalse(resource.getAttributes().containsKey("StreamArn"));
|
||||
}
|
||||
|
||||
// ── an update that drops the block switches the stream back off ──────────
|
||||
|
||||
@Test
|
||||
void removingStreamSpecificationOnUpdateDisablesTheStream() throws Exception {
|
||||
existingStreamedTable();
|
||||
when(dynamoDbService.disableStream(TABLE, "us-east-1")).thenReturn(streamDisabled());
|
||||
|
||||
StackResource resource = reprovisionTable("""
|
||||
{"TableName":"stream-repro",
|
||||
"KeySchema":[{"AttributeName":"id","KeyType":"HASH"}],
|
||||
"AttributeDefinitions":[{"AttributeName":"id","AttributeType":"S"}]}
|
||||
""", Map.of("Arn", TABLE_ARN, "StreamArn", STREAM_ARN));
|
||||
|
||||
// Otherwise the table keeps emitting records to whatever still holds its ARN.
|
||||
verify(dynamoDbService).disableStream(TABLE, "us-east-1");
|
||||
verify(dynamoDbService, never()).enableStream(anyString(), any(), anyString());
|
||||
// and the stale ARN must stop resolving through Fn::GetAtt
|
||||
assertNull(resource.getAttributes().get("StreamArn"));
|
||||
assertFalse(resource.getAttributes().containsKey("StreamArn"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void keepingStreamSpecificationOnUpdateDoesNotDisableTheStream() throws Exception {
|
||||
existingStreamedTable();
|
||||
when(dynamoDbService.enableStream(eq(TABLE), eq("NEW_AND_OLD_IMAGES"), eq("us-east-1")))
|
||||
.thenReturn(streamed("NEW_AND_OLD_IMAGES"));
|
||||
|
||||
StackResource resource = provisionTable("""
|
||||
{"TableName":"stream-repro",
|
||||
"KeySchema":[{"AttributeName":"id","KeyType":"HASH"}],
|
||||
"AttributeDefinitions":[{"AttributeName":"id","AttributeType":"S"}],
|
||||
"StreamSpecification":{"StreamViewType":"NEW_AND_OLD_IMAGES"}}
|
||||
""");
|
||||
|
||||
verify(dynamoDbService, never()).disableStream(anyString(), anyString());
|
||||
assertEquals(STREAM_ARN, resource.getAttributes().get("StreamArn"));
|
||||
}
|
||||
|
||||
/** A table that never had a stream must not trigger a pointless disable call. */
|
||||
@Test
|
||||
void streamlessTableIsNotDisabledAgain() throws Exception {
|
||||
provisionTable("""
|
||||
{"TableName":"stream-repro",
|
||||
"KeySchema":[{"AttributeName":"id","KeyType":"HASH"}],
|
||||
"AttributeDefinitions":[{"AttributeName":"id","AttributeType":"S"}]}
|
||||
""");
|
||||
|
||||
verify(dynamoDbService, never()).disableStream(anyString(), anyString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Changing StreamViewType on an update must reach the stream, not just the table metadata:
|
||||
* the records a stream emits are built from its view type.
|
||||
*/
|
||||
@Test
|
||||
void changingStreamViewTypeOnUpdateRetargetsTheStream() throws Exception {
|
||||
existingStreamedTable();
|
||||
when(dynamoDbService.enableStream(eq(TABLE), eq("KEYS_ONLY"), eq("us-east-1")))
|
||||
.thenReturn(streamed("KEYS_ONLY"));
|
||||
|
||||
StackResource resource = reprovisionTable("""
|
||||
{"TableName":"stream-repro",
|
||||
"KeySchema":[{"AttributeName":"id","KeyType":"HASH"}],
|
||||
"AttributeDefinitions":[{"AttributeName":"id","AttributeType":"S"}],
|
||||
"StreamSpecification":{"StreamViewType":"KEYS_ONLY"}}
|
||||
""", Map.of("Arn", TABLE_ARN, "StreamArn", STREAM_ARN));
|
||||
|
||||
verify(dynamoDbService).enableStream(TABLE, "KEYS_ONLY", "us-east-1");
|
||||
verify(dynamoDbService, never()).disableStream(anyString(), anyString());
|
||||
assertEquals(STREAM_ARN, resource.getAttributes().get("StreamArn"));
|
||||
}
|
||||
|
||||
private CloudFormationTemplateEngine engine() {
|
||||
return new CloudFormationTemplateEngine("000000000000", "us-east-1", "my-stack",
|
||||
"stack/id", Map.of(), Map.of(), Map.of(), Map.of(), Map.of(), mapper,
|
||||
(Function<String, String>) name -> null);
|
||||
}
|
||||
}
|
||||
+46
@@ -6,6 +6,7 @@ import io.github.hectorvent.floci.services.dynamodb.model.AttributeDefinition;
|
||||
import io.github.hectorvent.floci.services.dynamodb.model.KeySchemaElement;
|
||||
import io.github.hectorvent.floci.services.dynamodb.model.StreamDescription;
|
||||
import io.github.hectorvent.floci.services.dynamodb.model.TableDefinition;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -16,6 +17,9 @@ import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class DynamoDbStreamServiceTest {
|
||||
|
||||
private static final String TABLE_ARN =
|
||||
"arn:aws:dynamodb:us-east-1:000000000000:table/ViewTypeTable";
|
||||
|
||||
private DynamoDbStreamService service;
|
||||
private ObjectMapper mapper;
|
||||
private StorageBackend<String, TableDefinition> storage;
|
||||
@@ -39,6 +43,48 @@ class DynamoDbStreamServiceTest {
|
||||
return tableDef;
|
||||
}
|
||||
|
||||
/**
|
||||
* Re-enabling a live stream with a different view type must retarget it. Records are built
|
||||
* from the description's view type, so a stale one keeps emitting the old shape while the
|
||||
* table reports the requested one — CloudFormation can request this on an UpdateStack.
|
||||
*/
|
||||
@Test
|
||||
void reEnablingALiveStreamRetargetsItsViewType() {
|
||||
service.enableStream("ViewTypeTable", TABLE_ARN, "NEW_AND_OLD_IMAGES", "us-east-1");
|
||||
|
||||
StreamDescription sd =
|
||||
service.enableStream("ViewTypeTable", TABLE_ARN, "KEYS_ONLY", "us-east-1");
|
||||
|
||||
assertEquals("KEYS_ONLY", sd.getStreamViewType());
|
||||
assertEquals("ENABLED", sd.getStreamStatus());
|
||||
}
|
||||
|
||||
/** The retarget must reach the records themselves, not just the reported configuration. */
|
||||
@Test
|
||||
void recordsEmittedAfterARetargetUseTheNewViewType() throws Exception {
|
||||
var table = new TableDefinition("ViewTypeTable",
|
||||
List.of(new KeySchemaElement("userId", "HASH")),
|
||||
List.of(new AttributeDefinition("userId", "S")),
|
||||
"us-east-1", "000000000000");
|
||||
table.setStreamEnabled(true);
|
||||
|
||||
service.enableStream("ViewTypeTable", TABLE_ARN, "NEW_AND_OLD_IMAGES", "us-east-1");
|
||||
StreamDescription sd =
|
||||
service.enableStream("ViewTypeTable", TABLE_ARN, "KEYS_ONLY", "us-east-1");
|
||||
|
||||
JsonNode item = mapper.readTree("{\"userId\":{\"S\":\"u1\"},\"name\":{\"S\":\"a\"}}");
|
||||
service.captureEvent("ViewTypeTable", "INSERT", null, item, table, "us-east-1");
|
||||
|
||||
String iterator = service.getShardIterator(
|
||||
sd.getStreamArn(), "shardId-000000000000", "TRIM_HORIZON", null);
|
||||
var records = service.getRecords(iterator, 10).records();
|
||||
|
||||
assertEquals(1, records.size());
|
||||
assertEquals("KEYS_ONLY", records.get(0).getStreamViewType());
|
||||
// KEYS_ONLY carries keys only — a stale NEW_AND_OLD_IMAGES view would attach the image.
|
||||
assertNull(records.get(0).getNewImage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void loadsStreamOnStartup() {
|
||||
var streams = service.listStreams(null, null);
|
||||
|
||||
+110
@@ -0,0 +1,110 @@
|
||||
package io.github.hectorvent.floci.services.dynamodb;
|
||||
|
||||
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.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.storage.PersistentStorage;
|
||||
import io.github.hectorvent.floci.core.storage.StorageBackend;
|
||||
import io.github.hectorvent.floci.services.dynamodb.model.StreamDescription;
|
||||
import io.github.hectorvent.floci.services.dynamodb.model.TableDefinition;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* Stream configuration written through the JSON handler must survive a restart.
|
||||
*
|
||||
* <p>A persistent backend serializes on write rather than holding the live table object, and both
|
||||
* CreateTable and UpdateTable mutate the table AFTER the service has stored it. Without a flush the
|
||||
* on-disk copy keeps the previous stream settings, and because startup rebuilds every stream from
|
||||
* the persisted table, a restart resurrects the old view type and records resume the old image
|
||||
* shape — with nothing in the running system reporting the stale value.
|
||||
*/
|
||||
class DynamoDbStreamViewTypeDurabilityTest {
|
||||
|
||||
private static final String TABLE = "durable-stream";
|
||||
private static final String REGION = "us-east-1";
|
||||
|
||||
private final ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
/** A store backed by a real file, so a "restart" is just reading it again. */
|
||||
private StorageBackend<String, TableDefinition> diskStore(Path file) {
|
||||
PersistentStorage<String, TableDefinition> store =
|
||||
new PersistentStorage<>(file, new TypeReference<Map<String, TableDefinition>>() { });
|
||||
store.load();
|
||||
return store;
|
||||
}
|
||||
|
||||
private DynamoDbJsonHandler handlerFor(StorageBackend<String, TableDefinition> store,
|
||||
DynamoDbStreamService streams) {
|
||||
DynamoDbService service = new DynamoDbService(
|
||||
store, null, new RegionResolver(REGION, "000000000000"), streams, null);
|
||||
return new DynamoDbJsonHandler(service, streams, null, mapper);
|
||||
}
|
||||
|
||||
private ObjectNode createTableRequest(String viewType) {
|
||||
ObjectNode req = mapper.createObjectNode();
|
||||
req.put("TableName", TABLE);
|
||||
req.putArray("KeySchema").addObject().put("AttributeName", "id").put("KeyType", "HASH");
|
||||
req.putArray("AttributeDefinitions").addObject()
|
||||
.put("AttributeName", "id").put("AttributeType", "S");
|
||||
req.putObject("StreamSpecification")
|
||||
.put("StreamEnabled", true).put("StreamViewType", viewType);
|
||||
return req;
|
||||
}
|
||||
|
||||
private ObjectNode updateViewTypeRequest(String viewType) {
|
||||
ObjectNode req = mapper.createObjectNode();
|
||||
req.put("TableName", TABLE);
|
||||
req.putObject("StreamSpecification")
|
||||
.put("StreamEnabled", true).put("StreamViewType", viewType);
|
||||
return req;
|
||||
}
|
||||
|
||||
/** The stream settings a restart would read back off disk. */
|
||||
private TableDefinition reloadFromDisk(Path file) {
|
||||
return diskStore(file).get(REGION + "::" + TABLE).orElseThrow();
|
||||
}
|
||||
|
||||
@Test
|
||||
void createTableStreamSettingsSurviveARestart(@TempDir Path tmp) throws Exception {
|
||||
Path file = tmp.resolve("tables.json");
|
||||
StorageBackend<String, TableDefinition> store = diskStore(file);
|
||||
DynamoDbStreamService streams = new DynamoDbStreamService(mapper, store);
|
||||
|
||||
handlerFor(store, streams).handle("CreateTable",
|
||||
createTableRequest("NEW_IMAGE"), REGION);
|
||||
|
||||
TableDefinition persisted = reloadFromDisk(file);
|
||||
assertTrue(persisted.isStreamEnabled(), "a restart must still see the stream as enabled");
|
||||
assertEquals("NEW_IMAGE", persisted.getStreamViewType());
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateTableViewTypeRetargetSurvivesARestart(@TempDir Path tmp) throws Exception {
|
||||
Path file = tmp.resolve("tables.json");
|
||||
StorageBackend<String, TableDefinition> store = diskStore(file);
|
||||
DynamoDbStreamService streams = new DynamoDbStreamService(mapper, store);
|
||||
DynamoDbJsonHandler handler = handlerFor(store, streams);
|
||||
|
||||
handler.handle("CreateTable", createTableRequest("NEW_AND_OLD_IMAGES"), REGION);
|
||||
handler.handle("UpdateTable", updateViewTypeRequest("KEYS_ONLY"), REGION);
|
||||
|
||||
assertEquals("KEYS_ONLY", reloadFromDisk(file).getStreamViewType(),
|
||||
"the retargeted view type must be on disk, not only in the live stream");
|
||||
|
||||
// The restart itself: a fresh stream service rebuilds streams from the persisted table.
|
||||
StorageBackend<String, TableDefinition> reopened = diskStore(file);
|
||||
DynamoDbStreamService afterRestart = new DynamoDbStreamService(mapper, reopened);
|
||||
StreamDescription sd = afterRestart.listStreams(TABLE, REGION).get(0);
|
||||
assertEquals("KEYS_ONLY", sd.getStreamViewType(),
|
||||
"a restarted stream must not resume the old image shape");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user