Skip to content

Commit 0859ca6

Browse files
authored
Merge branch 'master' into data-loader/add-ci-for-build
2 parents 1a64df6 + 0e13365 commit 0859ca6

File tree

2 files changed

+86
-18
lines changed

2 files changed

+86
-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+
@VisibleForTesting static final int 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 >= 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 >= 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

core/src/test/java/com/scalar/db/storage/dynamo/DynamoAdminTestBase.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,38 @@ public void createTable_WhenMetadataTableExists_ShouldCreateOnlyTable()
703703
.isInstanceOf(IllegalArgumentException.class);
704704
}
705705

706+
@Test
707+
public void createTable_WhenActualMetadataTableCreationIsDelayed_ShouldFailAfterRetries() {
708+
// Arrange
709+
when(client.describeTable(any(DescribeTableRequest.class))).thenReturn(tableIsActiveResponse);
710+
when(client.describeContinuousBackups(any(DescribeContinuousBackupsRequest.class)))
711+
.thenReturn(backupIsEnabledResponse);
712+
when(client.putItem(any(PutItemRequest.class))).thenThrow(ResourceNotFoundException.class);
713+
TableMetadata metadata =
714+
TableMetadata.newBuilder()
715+
.addPartitionKey("c1")
716+
.addClusteringKey("c2", Order.DESC)
717+
.addClusteringKey("c3", Order.ASC)
718+
.addColumn("c1", DataType.TEXT)
719+
.addColumn("c2", DataType.BIGINT)
720+
.addColumn("c3", DataType.BOOLEAN)
721+
.addColumn("c4", DataType.INT)
722+
.addColumn("c5", DataType.BLOB)
723+
.addColumn("c6", DataType.DOUBLE)
724+
.addColumn("c7", DataType.FLOAT)
725+
.addColumn("c8", DataType.DATE)
726+
.addColumn("c9", DataType.TIME)
727+
.addColumn("c10", DataType.TIMESTAMP)
728+
.addColumn("c11", DataType.TIMESTAMPTZ)
729+
.addSecondaryIndex("c4")
730+
.build();
731+
732+
// Act Assert
733+
assertThatThrownBy(() -> admin.createTable(NAMESPACE, TABLE, metadata))
734+
.isInstanceOf(ExecutionException.class);
735+
verify(client, times(DynamoAdmin.MAX_RETRY_COUNT + 1)).putItem(any(PutItemRequest.class));
736+
}
737+
706738
@Test
707739
public void dropTable_WithNoMetadataLeft_ShouldDropTableAndDeleteMetadata()
708740
throws ExecutionException {
@@ -1436,6 +1468,17 @@ public void createNamespace_WithExistingNamespacesTable_ShouldAddNamespace()
14361468
.build());
14371469
}
14381470

1471+
@Test
1472+
public void createNamespace_WhenActualNamespaceTableCreationIsDelayed_ShouldFailAfterRetries() {
1473+
// Arrange
1474+
when(client.putItem(any(PutItemRequest.class))).thenThrow(ResourceNotFoundException.class);
1475+
1476+
// Act Assert
1477+
assertThatThrownBy(() -> admin.createNamespace(NAMESPACE, Collections.emptyMap()))
1478+
.isInstanceOf(ExecutionException.class);
1479+
verify(client, times(DynamoAdmin.MAX_RETRY_COUNT + 1)).putItem(any(PutItemRequest.class));
1480+
}
1481+
14391482
@Test
14401483
public void namespaceExists_WithExistingNamespace_ShouldReturnTrue() throws ExecutionException {
14411484
// Arrange

0 commit comments

Comments
 (0)