From 730f22dd6e572eff66da7ca867719d6acec5f9b8 Mon Sep 17 00:00:00 2001 From: termag agent runtime Date: Tue, 17 Nov 2026 16:09:56 +0000 Subject: [PATCH] fix: Write a JSON tombstone for deleted items in the DynamoDB store The DynamoDB store wrote the string "null" as the item of a deleted flag or segment. SDKs in other languages and the Relay Proxy fail a whole bulk read on that value. Store the SDK's serialized tombstone ({"version":N,"deleted":true}) instead, as the Redis and Consul stores do. The store still reads "null" as a deleted item, so tables written by older versions keep working. Remove the contract test suppression for DynamoDB data source deletions. Generated-by: termag --- .../integrations/DynamoDbDataStoreImpl.java | 10 ++- .../DynamoDbDataStoreImplTest.java | 88 +++++++++++++++++++ 2 files changed, 96 insertions(+), 2 deletions(-) diff --git a/lib/java-server-sdk-dynamodb-store/src/main/java/com/launchdarkly/sdk/server/integrations/DynamoDbDataStoreImpl.java b/lib/java-server-sdk-dynamodb-store/src/main/java/com/launchdarkly/sdk/server/integrations/DynamoDbDataStoreImpl.java index 61ea3975..5203175d 100644 --- a/lib/java-server-sdk-dynamodb-store/src/main/java/com/launchdarkly/sdk/server/integrations/DynamoDbDataStoreImpl.java +++ b/lib/java-server-sdk-dynamodb-store/src/main/java/com/launchdarkly/sdk/server/integrations/DynamoDbDataStoreImpl.java @@ -3,6 +3,7 @@ import com.launchdarkly.logging.LDLogger; import com.launchdarkly.sdk.server.subsystems.DataStoreTypes.DataKind; import com.launchdarkly.sdk.server.subsystems.DataStoreTypes.FullDataSet; +import com.launchdarkly.sdk.server.subsystems.DataStoreTypes.ItemDescriptor; import com.launchdarkly.sdk.server.subsystems.DataStoreTypes.KeyedItems; import com.launchdarkly.sdk.server.subsystems.DataStoreTypes.SerializedItemDescriptor; import com.launchdarkly.sdk.server.subsystems.PersistentDataStore; @@ -59,7 +60,8 @@ final class DynamoDbDataStoreImpl extends DynamoDbStoreImplBase implements PersistentDataStore { private static final String versionAttribute = "version"; private static final String itemJsonAttribute = "item"; - private static final String deletedItemPlaceholder = "null"; // DynamoDB doesn't allow empty strings + // Older versions of this store wrote this string for deleted items; we still read it as deleted. + private static final String deletedItemPlaceholder = "null"; // We won't try to store items whose total size exceeds this. The DynamoDB documentation says // only "400KB", which probably means 400*1024, but to avoid any chance of trying to store a @@ -244,7 +246,11 @@ private Set> readExistingKeys(FullDataSet kindsFrom } private Map marshalItem(DataKind kind, String key, SerializedItemDescriptor item) { - String json = item.isDeleted() ? deletedItemPlaceholder : item.getSerializedItem(); + String json = item.getSerializedItem(); + if (json == null) { + // Store the same tombstone JSON that the SDK serializes for deleted items, as the other stores do. + json = kind.serialize(ItemDescriptor.deletedItem(item.getVersion())); + } return mapOf( PARTITION_KEY, AttributeValue.builder().s(namespaceForKind(kind)).build(), SORT_KEY, AttributeValue.builder().s(key).build(), diff --git a/lib/java-server-sdk-dynamodb-store/src/test/java/com/launchdarkly/sdk/server/integrations/DynamoDbDataStoreImplTest.java b/lib/java-server-sdk-dynamodb-store/src/test/java/com/launchdarkly/sdk/server/integrations/DynamoDbDataStoreImplTest.java index 365de38a..f4fcfb05 100644 --- a/lib/java-server-sdk-dynamodb-store/src/test/java/com/launchdarkly/sdk/server/integrations/DynamoDbDataStoreImplTest.java +++ b/lib/java-server-sdk-dynamodb-store/src/test/java/com/launchdarkly/sdk/server/integrations/DynamoDbDataStoreImplTest.java @@ -9,6 +9,7 @@ import com.launchdarkly.sdk.server.subsystems.ComponentConfigurer; import com.launchdarkly.sdk.server.subsystems.DataStoreTypes.DataKind; import com.launchdarkly.sdk.server.subsystems.DataStoreTypes.FullDataSet; +import com.launchdarkly.sdk.server.subsystems.DataStoreTypes.ItemDescriptor; import com.launchdarkly.sdk.server.subsystems.DataStoreTypes.KeyedItems; import com.launchdarkly.sdk.server.subsystems.DataStoreTypes.SerializedItemDescriptor; import com.launchdarkly.sdk.server.subsystems.PersistentDataStore; @@ -24,9 +25,19 @@ import static com.launchdarkly.sdk.server.TestComponents.clientContext; import static com.launchdarkly.sdk.server.integrations.TestUtils.baseDataStoreBuilder; import static com.launchdarkly.sdk.server.integrations.TestUtils.clearEverything; +import static com.launchdarkly.sdk.server.integrations.CollectionHelpers.mapOf; +import static com.launchdarkly.sdk.server.integrations.DynamoDbStoreImplBase.PARTITION_KEY; +import static com.launchdarkly.sdk.server.integrations.DynamoDbStoreImplBase.SORT_KEY; +import static com.launchdarkly.sdk.server.integrations.TestUtils.TABLE_NAME; +import static com.launchdarkly.sdk.server.integrations.TestUtils.createTestClient; import static com.launchdarkly.sdk.server.integrations.TestUtils.createTableIfNecessary; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.greaterThan; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import software.amazon.awssdk.services.dynamodb.model.AttributeValue; /** * Runs the standard database data store test suite that's defined in the Java SDK. @@ -40,6 +51,8 @@ @SuppressWarnings("javadoc") public class DynamoDbDataStoreImplTest extends PersistentDataStoreTestBase { private static final String BAD_ITEM_KEY = "baditem"; + private static final String TOMBSTONE_PREFIX = "tombstonetest"; + private static final String TOMBSTONE_KEY = "deleted-flag"; @BeforeClass public static void setUpAll() { @@ -82,6 +95,81 @@ public void dataStoreSkipsAndLogsTooLargeItemOnUpsertForSegment() throws Excepti dataStoreSkipsAndLogsTooLargeItemOnUpsert(DataModel.SEGMENTS); } + @Test + public void upsertOfDeletedItemStoresTombstoneJson() throws Exception { + clearAllData(); + try (PersistentDataStore store = buildStore(TOMBSTONE_PREFIX).build(makeClientContext())) { + store.init(new FullDataSet<>(ImmutableList.of())); + ItemDescriptor deleted = ItemDescriptor.deletedItem(7); + store.upsert(DataModel.FEATURES, TOMBSTONE_KEY, + new SerializedItemDescriptor(7, true, DataModel.FEATURES.serialize(deleted))); + + assertEquals("{\"version\":7,\"deleted\":true}", getRawItemJson(DataModel.FEATURES, TOMBSTONE_KEY)); + assertReadsAsDeleted(DataModel.FEATURES, store.get(DataModel.FEATURES, TOMBSTONE_KEY), 7); + assertReadsAsDeleted(DataModel.FEATURES, store.getAll(DataModel.FEATURES).getItems().iterator().next().getValue(), 7); + } + } + + @Test + public void initWithDeletedItemStoresTombstoneJson() throws Exception { + clearAllData(); + try (PersistentDataStore store = buildStore(TOMBSTONE_PREFIX).build(makeClientContext())) { + ItemDescriptor deleted = ItemDescriptor.deletedItem(3); + store.init(new FullDataSet<>(ImmutableList.of( + new SimpleEntry<>(DataModel.SEGMENTS, new KeyedItems<>(ImmutableList.of( + new SimpleEntry<>(TOMBSTONE_KEY, + new SerializedItemDescriptor(3, true, DataModel.SEGMENTS.serialize(deleted))))))))); + + assertEquals("{\"version\":3,\"deleted\":true}", getRawItemJson(DataModel.SEGMENTS, TOMBSTONE_KEY)); + assertReadsAsDeleted(DataModel.SEGMENTS, store.get(DataModel.SEGMENTS, TOMBSTONE_KEY), 3); + } + } + + @Test + public void deletedItemWithoutSerializedFormStoresTombstoneJson() throws Exception { + clearAllData(); + try (PersistentDataStore store = buildStore(TOMBSTONE_PREFIX).build(makeClientContext())) { + store.init(new FullDataSet<>(ImmutableList.of())); + store.upsert(DataModel.FEATURES, TOMBSTONE_KEY, new SerializedItemDescriptor(4, true, null)); + + assertEquals("{\"version\":4,\"deleted\":true}", getRawItemJson(DataModel.FEATURES, TOMBSTONE_KEY)); + } + } + + @Test + public void legacyNullPlaceholderReadsAsDeleted() throws Exception { + clearAllData(); + try (PersistentDataStore store = buildStore(TOMBSTONE_PREFIX).build(makeClientContext())) { + store.init(new FullDataSet<>(ImmutableList.of())); + createTestClient().putItem(builder -> builder.tableName(TABLE_NAME).item(mapOf( + PARTITION_KEY, AttributeValue.builder().s(TOMBSTONE_PREFIX + ":" + DataModel.FEATURES.getName()).build(), + SORT_KEY, AttributeValue.builder().s(TOMBSTONE_KEY).build(), + "version", AttributeValue.builder().n("5").build(), + "item", AttributeValue.builder().s("null").build()))); + + SerializedItemDescriptor item = store.get(DataModel.FEATURES, TOMBSTONE_KEY); + assertTrue(item.isDeleted()); + assertEquals(5, item.getVersion()); + assertNull(item.getSerializedItem()); + + SerializedItemDescriptor fromAll = store.getAll(DataModel.FEATURES).getItems().iterator().next().getValue(); + assertTrue(fromAll.isDeleted()); + assertEquals(5, fromAll.getVersion()); + } + } + + private static String getRawItemJson(DataKind kind, String key) { + Map item = createTestClient().getItem(builder -> builder.tableName(TABLE_NAME).key(mapOf( + PARTITION_KEY, AttributeValue.builder().s(TOMBSTONE_PREFIX + ":" + kind.getName()).build(), + SORT_KEY, AttributeValue.builder().s(key).build()))).item(); + return item.get("item").s(); + } + + private static void assertReadsAsDeleted(DataKind kind, SerializedItemDescriptor item, int version) { + assertEquals(version, item.getVersion()); + assertEquals(ItemDescriptor.deletedItem(version), kind.deserialize(item.getSerializedItem())); + } + private void dataStoreSkipsAndLogsTooLargeItemOnInit( DataKind dataKind, int collIndex