Skip to content

Commit 8c04965

Browse files
committed
Fix the DynamoDB metadata table upsert operation to handle retries when table is not yet available.
1 parent 30057d8 commit 8c04965

File tree

1 file changed

+43
-18
lines changed

1 file changed

+43
-18
lines changed

core/src/main/java/com/scalar/db/storage/dynamo/DynamoAdmin.java

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ public class DynamoAdmin implements DistributedStorageAdmin {
9797
public static final String DEFAULT_NO_BACKUP = "false";
9898
public static final String DEFAULT_REQUEST_UNIT = "10";
9999
private static final int DEFAULT_WAITING_DURATION_SECS = 3;
100+
private static final int DEFAULT_MAX_RETRY_COUNT = 10;
100101

101102
@VisibleForTesting static final String PARTITION_KEY = "concatenatedPartitionKey";
102103
@VisibleForTesting static final String CLUSTERING_KEY = "concatenatedClusteringKey";
@@ -238,15 +239,26 @@ public void createNamespace(String nonPrefixedNamespace, Map<String, String> opt
238239
private void upsertIntoNamespacesTable(Namespace namespace) throws ExecutionException {
239240
Map<String, AttributeValue> itemValues = new HashMap<>();
240241
itemValues.put(NAMESPACES_ATTR_NAME, AttributeValue.builder().s(namespace.prefixed()).build());
241-
try {
242-
client.putItem(
243-
PutItemRequest.builder()
244-
.tableName(ScalarDbUtils.getFullTableName(metadataNamespace, NAMESPACES_TABLE))
245-
.item(itemValues)
246-
.build());
247-
} catch (Exception e) {
248-
throw new ExecutionException(
249-
"Inserting the " + namespace + " namespace into the namespaces table failed", e);
242+
int retryCount = 0;
243+
while (true) {
244+
try {
245+
client.putItem(
246+
PutItemRequest.builder()
247+
.tableName(ScalarDbUtils.getFullTableName(metadataNamespace, NAMESPACES_TABLE))
248+
.item(itemValues)
249+
.build());
250+
return;
251+
} catch (ResourceNotFoundException e) {
252+
if (retryCount >= DEFAULT_MAX_RETRY_COUNT) {
253+
throw new ExecutionException(
254+
"Inserting the " + namespace + " namespace into the namespaces table failed", e);
255+
}
256+
Uninterruptibles.sleepUninterruptibly(waitingDurationSecs, TimeUnit.SECONDS);
257+
retryCount++;
258+
} catch (Exception e) {
259+
throw new ExecutionException(
260+
"Inserting the " + namespace + " namespace into the namespaces table failed", e);
261+
}
250262
}
251263
}
252264

@@ -470,15 +482,28 @@ private void upsertTableMetadata(Namespace namespace, String table, TableMetadat
470482
METADATA_ATTR_SECONDARY_INDEX,
471483
AttributeValue.builder().ss(metadata.getSecondaryIndexNames()).build());
472484
}
473-
try {
474-
client.putItem(
475-
PutItemRequest.builder()
476-
.tableName(ScalarDbUtils.getFullTableName(metadataNamespace, METADATA_TABLE))
477-
.item(itemValues)
478-
.build());
479-
} catch (Exception e) {
480-
throw new ExecutionException(
481-
"Adding the metadata for the " + getFullTableName(namespace, table) + " table failed", e);
485+
int retryCount = 0;
486+
while (true) {
487+
try {
488+
client.putItem(
489+
PutItemRequest.builder()
490+
.tableName(ScalarDbUtils.getFullTableName(metadataNamespace, METADATA_TABLE))
491+
.item(itemValues)
492+
.build());
493+
return;
494+
} catch (ResourceNotFoundException e) {
495+
if (retryCount >= DEFAULT_MAX_RETRY_COUNT) {
496+
throw new ExecutionException(
497+
"Adding the metadata for the " + getFullTableName(namespace, table) + " table failed",
498+
e);
499+
}
500+
Uninterruptibles.sleepUninterruptibly(waitingDurationSecs, TimeUnit.SECONDS);
501+
retryCount++;
502+
} catch (Exception e) {
503+
throw new ExecutionException(
504+
"Adding the metadata for the " + getFullTableName(namespace, table) + " table failed",
505+
e);
506+
}
482507
}
483508
}
484509

0 commit comments

Comments
 (0)