Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions core/src/main/java/com/scalar/db/storage/dynamo/DynamoAdmin.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public class DynamoAdmin implements DistributedStorageAdmin {
public static final String DEFAULT_NO_BACKUP = "false";
public static final String DEFAULT_REQUEST_UNIT = "10";
private static final int DEFAULT_WAITING_DURATION_SECS = 3;
@VisibleForTesting static final int MAX_RETRY_COUNT = 10;

@VisibleForTesting static final String PARTITION_KEY = "concatenatedPartitionKey";
@VisibleForTesting static final String CLUSTERING_KEY = "concatenatedClusteringKey";
Expand Down Expand Up @@ -422,15 +423,28 @@ private void putTableMetadata(Namespace namespace, String table, TableMetadata m
METADATA_ATTR_SECONDARY_INDEX,
AttributeValue.builder().ss(metadata.getSecondaryIndexNames()).build());
}
try {
client.putItem(
PutItemRequest.builder()
.tableName(ScalarDbUtils.getFullTableName(metadataNamespace, METADATA_TABLE))
.item(itemValues)
.build());
} catch (Exception e) {
throw new ExecutionException(
"Adding the meta data for table " + getFullTableName(namespace, table) + " failed", e);
int retryCount = 0;
while (true) {
try {
client.putItem(
PutItemRequest.builder()
.tableName(ScalarDbUtils.getFullTableName(metadataNamespace, METADATA_TABLE))
.item(itemValues)
.build());
return;
} catch (ResourceNotFoundException e) {
if (retryCount >= MAX_RETRY_COUNT) {
throw new ExecutionException(
"Adding the metadata for the " + getFullTableName(namespace, table) + " table failed",
e);
}
Uninterruptibles.sleepUninterruptibly(waitingDurationSecs, TimeUnit.SECONDS);
retryCount++;
} catch (Exception e) {
throw new ExecutionException(
"Adding the metadata for the " + getFullTableName(namespace, table) + " table failed",
e);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,51 @@ public void createTable_WhenMetadataTableExists_ShouldCreateOnlyTable()
.isInstanceOf(IllegalArgumentException.class);
}

@Test
public void createTable_WhenActualMetadataTableCreationIsDelayed_ShouldFailAfterRetries() {
// Arrange
// prepare tableIsActiveResponse
TableDescription tableDescription = mock(TableDescription.class);
when(tableIsActiveResponse.table()).thenReturn(tableDescription);
when(tableDescription.tableStatus()).thenReturn(TableStatus.ACTIVE);
when(client.describeTable(any(DescribeTableRequest.class))).thenReturn(tableIsActiveResponse);
// prepare backupIsEnabledResponse
DescribeContinuousBackupsResponse backupIsEnabledResponse =
mock(DescribeContinuousBackupsResponse.class);
when(client.describeContinuousBackups(any(DescribeContinuousBackupsRequest.class)))
.thenReturn(backupIsEnabledResponse);
ContinuousBackupsDescription continuousBackupsDescription =
mock(ContinuousBackupsDescription.class);
when(backupIsEnabledResponse.continuousBackupsDescription())
.thenReturn(continuousBackupsDescription);
when(continuousBackupsDescription.continuousBackupsStatus())
.thenReturn(ContinuousBackupsStatus.ENABLED);
when(client.putItem(any(PutItemRequest.class))).thenThrow(ResourceNotFoundException.class);
TableMetadata metadata =
TableMetadata.newBuilder()
.addPartitionKey("c1")
.addClusteringKey("c2", Order.DESC)
.addClusteringKey("c3", Order.ASC)
.addColumn("c1", DataType.TEXT)
.addColumn("c2", DataType.BIGINT)
.addColumn("c3", DataType.BOOLEAN)
.addColumn("c4", DataType.INT)
.addColumn("c5", DataType.BLOB)
.addColumn("c6", DataType.DOUBLE)
.addColumn("c7", DataType.FLOAT)
.addColumn("c8", DataType.DATE)
.addColumn("c9", DataType.TIME)
.addColumn("c10", DataType.TIMESTAMP)
.addColumn("c11", DataType.TIMESTAMPTZ)
.addSecondaryIndex("c4")
.build();

// Act Assert
assertThatThrownBy(() -> admin.createTable(NAMESPACE, TABLE, metadata))
.isInstanceOf(ExecutionException.class);
verify(client, times(DynamoAdmin.MAX_RETRY_COUNT + 1)).putItem(any(PutItemRequest.class));
}

@Test
public void dropTable_WithNoMetadataLeft_ShouldDropTableAndDeleteMetadata()
throws ExecutionException {
Expand Down