Search in sources :

Example 1 with ContinuousBackupsDescription

use of software.amazon.awssdk.services.dynamodb.model.ContinuousBackupsDescription in project scalardb by scalar-labs.

the class DynamoAdminTest method createTable_WhenMetadataTableExists_ShouldCreateOnlyTable.

private void createTable_WhenMetadataTableExists_ShouldCreateOnlyTable(Optional<String> tableMetadataNamespace) throws ExecutionException {
    // Arrange
    String metadataNamespaceName = tableMetadataNamespace.orElse(DynamoAdmin.METADATA_NAMESPACE);
    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).addSecondaryIndex("c4").build();
    DescribeTableResponse describeTableResponse = mock(DescribeTableResponse.class);
    when(client.describeTable(any(DescribeTableRequest.class))).thenReturn(describeTableResponse);
    TableDescription tableDescription = mock(TableDescription.class);
    when(describeTableResponse.table()).thenReturn(tableDescription);
    when(tableDescription.tableStatus()).thenReturn(TableStatus.ACTIVE);
    DescribeContinuousBackupsResponse describeContinuousBackupsResponse = mock(DescribeContinuousBackupsResponse.class);
    when(client.describeContinuousBackups(any(DescribeContinuousBackupsRequest.class))).thenReturn(describeContinuousBackupsResponse);
    ContinuousBackupsDescription continuousBackupsDescription = mock(ContinuousBackupsDescription.class);
    when(describeContinuousBackupsResponse.continuousBackupsDescription()).thenReturn(continuousBackupsDescription);
    when(continuousBackupsDescription.continuousBackupsStatus()).thenReturn(ContinuousBackupsStatus.ENABLED);
    if (tableMetadataNamespace.isPresent()) {
        when(config.getTableMetadataNamespace()).thenReturn(tableMetadataNamespace);
        admin = new DynamoAdmin(client, applicationAutoScalingClient, config);
    }
    Map<String, String> options = new HashMap<>();
    options.put(DynamoAdmin.REQUEST_UNIT, "100");
    options.put(DynamoAdmin.NO_SCALING, "true");
    options.put(DynamoAdmin.NO_BACKUP, "true");
    // Act
    admin.createTable(NAMESPACE, TABLE, metadata, options);
    // Assert
    ArgumentCaptor<CreateTableRequest> createTableRequestCaptor = ArgumentCaptor.forClass(CreateTableRequest.class);
    verify(client).createTable(createTableRequestCaptor.capture());
    CreateTableRequest actualCreateTableRequest = createTableRequestCaptor.getValue();
    List<AttributeDefinition> attributeDefinitions = actualCreateTableRequest.attributeDefinitions();
    assertThat(attributeDefinitions.size()).isEqualTo(3);
    assertThat(attributeDefinitions.get(0).attributeName()).isEqualTo(DynamoAdmin.PARTITION_KEY);
    assertThat(attributeDefinitions.get(0).attributeType()).isEqualTo(ScalarAttributeType.B);
    assertThat(attributeDefinitions.get(1).attributeName()).isEqualTo(DynamoAdmin.CLUSTERING_KEY);
    assertThat(attributeDefinitions.get(1).attributeType()).isEqualTo(ScalarAttributeType.B);
    assertThat(attributeDefinitions.get(2).attributeName()).isEqualTo("c4");
    assertThat(attributeDefinitions.get(2).attributeType()).isEqualTo(ScalarAttributeType.N);
    assertThat(actualCreateTableRequest.keySchema().size()).isEqualTo(2);
    assertThat(actualCreateTableRequest.keySchema().get(0).attributeName()).isEqualTo(DynamoAdmin.PARTITION_KEY);
    assertThat(actualCreateTableRequest.keySchema().get(0).keyType()).isEqualTo(KeyType.HASH);
    assertThat(actualCreateTableRequest.keySchema().get(1).attributeName()).isEqualTo(DynamoAdmin.CLUSTERING_KEY);
    assertThat(actualCreateTableRequest.keySchema().get(1).keyType()).isEqualTo(KeyType.RANGE);
    assertThat(actualCreateTableRequest.globalSecondaryIndexes().size()).isEqualTo(1);
    assertThat(actualCreateTableRequest.globalSecondaryIndexes().get(0).indexName()).isEqualTo(FULL_TABLE_NAME + ".global_index.c4");
    assertThat(actualCreateTableRequest.globalSecondaryIndexes().get(0).keySchema().size()).isEqualTo(1);
    assertThat(actualCreateTableRequest.globalSecondaryIndexes().get(0).keySchema().get(0).attributeName()).isEqualTo("c4");
    assertThat(actualCreateTableRequest.globalSecondaryIndexes().get(0).keySchema().get(0).keyType()).isEqualTo(KeyType.HASH);
    assertThat(actualCreateTableRequest.globalSecondaryIndexes().get(0).projection().projectionType()).isEqualTo(ProjectionType.ALL);
    assertThat(actualCreateTableRequest.globalSecondaryIndexes().get(0).provisionedThroughput().readCapacityUnits()).isEqualTo(100);
    assertThat(actualCreateTableRequest.globalSecondaryIndexes().get(0).provisionedThroughput().writeCapacityUnits()).isEqualTo(100);
    assertThat(actualCreateTableRequest.provisionedThroughput().writeCapacityUnits()).isEqualTo(100);
    assertThat(actualCreateTableRequest.provisionedThroughput().readCapacityUnits()).isEqualTo(100);
    assertThat(actualCreateTableRequest.tableName()).isEqualTo(FULL_TABLE_NAME);
    // for the table metadata table
    ArgumentCaptor<PutItemRequest> putItemRequestCaptor = ArgumentCaptor.forClass(PutItemRequest.class);
    verify(client).putItem(putItemRequestCaptor.capture());
    PutItemRequest actualPutItemRequest = putItemRequestCaptor.getValue();
    assertThat(actualPutItemRequest.tableName()).isEqualTo(metadataNamespaceName + "." + DynamoAdmin.METADATA_TABLE);
    Map<String, AttributeValue> itemValues = new HashMap<>();
    itemValues.put(DynamoAdmin.METADATA_ATTR_TABLE, AttributeValue.builder().s(FULL_TABLE_NAME).build());
    Map<String, AttributeValue> columns = new HashMap<>();
    columns.put("c1", AttributeValue.builder().s("text").build());
    columns.put("c2", AttributeValue.builder().s("bigint").build());
    columns.put("c3", AttributeValue.builder().s("boolean").build());
    columns.put("c4", AttributeValue.builder().s("int").build());
    columns.put("c5", AttributeValue.builder().s("blob").build());
    columns.put("c6", AttributeValue.builder().s("double").build());
    columns.put("c7", AttributeValue.builder().s("float").build());
    itemValues.put(DynamoAdmin.METADATA_ATTR_COLUMNS, AttributeValue.builder().m(columns).build());
    itemValues.put(DynamoAdmin.METADATA_ATTR_PARTITION_KEY, AttributeValue.builder().l(AttributeValue.builder().s("c1").build()).build());
    itemValues.put(DynamoAdmin.METADATA_ATTR_CLUSTERING_KEY, AttributeValue.builder().l(AttributeValue.builder().s("c2").build(), AttributeValue.builder().s("c3").build()).build());
    Map<String, AttributeValue> clusteringOrders = new HashMap<>();
    clusteringOrders.put("c2", AttributeValue.builder().s("DESC").build());
    clusteringOrders.put("c3", AttributeValue.builder().s("ASC").build());
    itemValues.put(DynamoAdmin.METADATA_ATTR_CLUSTERING_ORDERS, AttributeValue.builder().m(clusteringOrders).build());
    itemValues.put(DynamoAdmin.METADATA_ATTR_SECONDARY_INDEX, AttributeValue.builder().ss("c4").build());
    assertThat(actualPutItemRequest.item()).isEqualTo(itemValues);
    verify(applicationAutoScalingClient, never()).registerScalableTarget(any(RegisterScalableTargetRequest.class));
    verify(applicationAutoScalingClient, never()).putScalingPolicy(any(PutScalingPolicyRequest.class));
    verify(client, never()).updateContinuousBackups(any(UpdateContinuousBackupsRequest.class));
}
Also used : TableMetadata(com.scalar.db.api.TableMetadata) ContinuousBackupsDescription(software.amazon.awssdk.services.dynamodb.model.ContinuousBackupsDescription) AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) RegisterScalableTargetRequest(software.amazon.awssdk.services.applicationautoscaling.model.RegisterScalableTargetRequest) HashMap(java.util.HashMap) UpdateContinuousBackupsRequest(software.amazon.awssdk.services.dynamodb.model.UpdateContinuousBackupsRequest) AttributeDefinition(software.amazon.awssdk.services.dynamodb.model.AttributeDefinition) DescribeTableResponse(software.amazon.awssdk.services.dynamodb.model.DescribeTableResponse) DescribeTableRequest(software.amazon.awssdk.services.dynamodb.model.DescribeTableRequest) TableDescription(software.amazon.awssdk.services.dynamodb.model.TableDescription) DescribeContinuousBackupsRequest(software.amazon.awssdk.services.dynamodb.model.DescribeContinuousBackupsRequest) DescribeContinuousBackupsResponse(software.amazon.awssdk.services.dynamodb.model.DescribeContinuousBackupsResponse) PutScalingPolicyRequest(software.amazon.awssdk.services.applicationautoscaling.model.PutScalingPolicyRequest) PutItemRequest(software.amazon.awssdk.services.dynamodb.model.PutItemRequest) CreateTableRequest(software.amazon.awssdk.services.dynamodb.model.CreateTableRequest)

Example 2 with ContinuousBackupsDescription

use of software.amazon.awssdk.services.dynamodb.model.ContinuousBackupsDescription in project scalardb by scalar-labs.

the class DynamoAdminTest method createTable_WhenMetadataTableNotExist_ShouldCreateTableAndMetadataTable.

private void createTable_WhenMetadataTableNotExist_ShouldCreateTableAndMetadataTable(Optional<String> tableMetadataNamespace) throws ExecutionException {
    // Arrange
    String metadataNamespaceName = tableMetadataNamespace.orElse(DynamoAdmin.METADATA_NAMESPACE);
    TableMetadata metadata = TableMetadata.newBuilder().addPartitionKey("c1").addClusteringKey("c2", Order.ASC).addClusteringKey("c3", Order.ASC).addColumn("c1", DataType.TEXT).addColumn("c2", DataType.BIGINT).addColumn("c3", DataType.BOOLEAN).addColumn("c4", DataType.BLOB).addColumn("c5", DataType.INT).addColumn("c6", DataType.DOUBLE).addColumn("c7", DataType.FLOAT).addSecondaryIndex("c4").build();
    DescribeTableResponse describeTableResponse = mock(DescribeTableResponse.class);
    when(client.describeTable(DescribeTableRequest.builder().tableName(FULL_TABLE_NAME).build())).thenReturn(describeTableResponse);
    TableDescription tableDescription = mock(TableDescription.class);
    when(describeTableResponse.table()).thenReturn(tableDescription);
    when(tableDescription.tableStatus()).thenReturn(TableStatus.ACTIVE);
    DescribeContinuousBackupsResponse describeContinuousBackupsResponse = mock(DescribeContinuousBackupsResponse.class);
    when(client.describeContinuousBackups(any(DescribeContinuousBackupsRequest.class))).thenReturn(describeContinuousBackupsResponse);
    ContinuousBackupsDescription continuousBackupsDescription = mock(ContinuousBackupsDescription.class);
    when(describeContinuousBackupsResponse.continuousBackupsDescription()).thenReturn(continuousBackupsDescription);
    when(continuousBackupsDescription.continuousBackupsStatus()).thenReturn(ContinuousBackupsStatus.ENABLED);
    // for the table metadata table
    describeTableResponse = mock(DescribeTableResponse.class);
    tableDescription = mock(TableDescription.class);
    when(describeTableResponse.table()).thenReturn(tableDescription);
    when(tableDescription.tableStatus()).thenReturn(TableStatus.ACTIVE);
    when(client.describeTable(DescribeTableRequest.builder().tableName(metadataNamespaceName + "." + DynamoAdmin.METADATA_TABLE).build())).thenThrow(ResourceNotFoundException.class).thenReturn(describeTableResponse);
    if (tableMetadataNamespace.isPresent()) {
        when(config.getTableMetadataNamespace()).thenReturn(tableMetadataNamespace);
        admin = new DynamoAdmin(client, applicationAutoScalingClient, config);
    }
    // Act
    admin.createTable(NAMESPACE, TABLE, metadata);
    // Assert
    ArgumentCaptor<CreateTableRequest> createTableRequestCaptor = ArgumentCaptor.forClass(CreateTableRequest.class);
    verify(client, times(2)).createTable(createTableRequestCaptor.capture());
    List<CreateTableRequest> actualCreateTableRequests = createTableRequestCaptor.getAllValues();
    List<AttributeDefinition> attributeDefinitions = actualCreateTableRequests.get(0).attributeDefinitions();
    assertThat(attributeDefinitions.size()).isEqualTo(3);
    assertThat(attributeDefinitions.get(0).attributeName()).isEqualTo(DynamoAdmin.PARTITION_KEY);
    assertThat(attributeDefinitions.get(0).attributeType()).isEqualTo(ScalarAttributeType.B);
    assertThat(attributeDefinitions.get(1).attributeName()).isEqualTo(DynamoAdmin.CLUSTERING_KEY);
    assertThat(attributeDefinitions.get(1).attributeType()).isEqualTo(ScalarAttributeType.B);
    assertThat(attributeDefinitions.get(2).attributeName()).isEqualTo("c4");
    assertThat(attributeDefinitions.get(2).attributeType()).isEqualTo(ScalarAttributeType.B);
    assertThat(actualCreateTableRequests.get(0).keySchema().size()).isEqualTo(2);
    assertThat(actualCreateTableRequests.get(0).keySchema().get(0).attributeName()).isEqualTo(DynamoAdmin.PARTITION_KEY);
    assertThat(actualCreateTableRequests.get(0).keySchema().get(0).keyType()).isEqualTo(KeyType.HASH);
    assertThat(actualCreateTableRequests.get(0).keySchema().get(1).attributeName()).isEqualTo(DynamoAdmin.CLUSTERING_KEY);
    assertThat(actualCreateTableRequests.get(0).keySchema().get(1).keyType()).isEqualTo(KeyType.RANGE);
    assertThat(actualCreateTableRequests.get(0).globalSecondaryIndexes().size()).isEqualTo(1);
    assertThat(actualCreateTableRequests.get(0).globalSecondaryIndexes().get(0).indexName()).isEqualTo(FULL_TABLE_NAME + ".global_index.c4");
    assertThat(actualCreateTableRequests.get(0).globalSecondaryIndexes().get(0).keySchema().size()).isEqualTo(1);
    assertThat(actualCreateTableRequests.get(0).globalSecondaryIndexes().get(0).keySchema().get(0).attributeName()).isEqualTo("c4");
    assertThat(actualCreateTableRequests.get(0).globalSecondaryIndexes().get(0).keySchema().get(0).keyType()).isEqualTo(KeyType.HASH);
    assertThat(actualCreateTableRequests.get(0).globalSecondaryIndexes().get(0).projection().projectionType()).isEqualTo(ProjectionType.ALL);
    assertThat(actualCreateTableRequests.get(0).globalSecondaryIndexes().get(0).provisionedThroughput().readCapacityUnits()).isEqualTo(10);
    assertThat(actualCreateTableRequests.get(0).globalSecondaryIndexes().get(0).provisionedThroughput().writeCapacityUnits()).isEqualTo(10);
    assertThat(actualCreateTableRequests.get(0).provisionedThroughput().writeCapacityUnits()).isEqualTo(10);
    assertThat(actualCreateTableRequests.get(0).provisionedThroughput().readCapacityUnits()).isEqualTo(10);
    assertThat(actualCreateTableRequests.get(0).tableName()).isEqualTo(FULL_TABLE_NAME);
    // for the table metadata table
    attributeDefinitions = actualCreateTableRequests.get(1).attributeDefinitions();
    assertThat(attributeDefinitions.size()).isEqualTo(1);
    assertThat(attributeDefinitions.get(0).attributeName()).isEqualTo(DynamoAdmin.METADATA_ATTR_TABLE);
    assertThat(attributeDefinitions.get(0).attributeType()).isEqualTo(ScalarAttributeType.S);
    assertThat(actualCreateTableRequests.get(1).keySchema().size()).isEqualTo(1);
    assertThat(actualCreateTableRequests.get(1).keySchema().get(0).attributeName()).isEqualTo(DynamoAdmin.METADATA_ATTR_TABLE);
    assertThat(actualCreateTableRequests.get(1).keySchema().get(0).keyType()).isEqualTo(KeyType.HASH);
    assertThat(actualCreateTableRequests.get(1).provisionedThroughput().writeCapacityUnits()).isEqualTo(1);
    assertThat(actualCreateTableRequests.get(1).provisionedThroughput().readCapacityUnits()).isEqualTo(1);
    assertThat(actualCreateTableRequests.get(1).tableName()).isEqualTo(metadataNamespaceName + "." + DynamoAdmin.METADATA_TABLE);
    ArgumentCaptor<PutItemRequest> putItemRequestCaptor = ArgumentCaptor.forClass(PutItemRequest.class);
    verify(client).putItem(putItemRequestCaptor.capture());
    PutItemRequest actualPutItemRequest = putItemRequestCaptor.getValue();
    assertThat(actualPutItemRequest.tableName()).isEqualTo(metadataNamespaceName + "." + DynamoAdmin.METADATA_TABLE);
    Map<String, AttributeValue> itemValues = new HashMap<>();
    itemValues.put(DynamoAdmin.METADATA_ATTR_TABLE, AttributeValue.builder().s(FULL_TABLE_NAME).build());
    Map<String, AttributeValue> columns = new HashMap<>();
    columns.put("c1", AttributeValue.builder().s("text").build());
    columns.put("c2", AttributeValue.builder().s("bigint").build());
    columns.put("c3", AttributeValue.builder().s("boolean").build());
    columns.put("c4", AttributeValue.builder().s("blob").build());
    columns.put("c5", AttributeValue.builder().s("int").build());
    columns.put("c6", AttributeValue.builder().s("double").build());
    columns.put("c7", AttributeValue.builder().s("float").build());
    itemValues.put(DynamoAdmin.METADATA_ATTR_COLUMNS, AttributeValue.builder().m(columns).build());
    itemValues.put(DynamoAdmin.METADATA_ATTR_PARTITION_KEY, AttributeValue.builder().l(AttributeValue.builder().s("c1").build()).build());
    itemValues.put(DynamoAdmin.METADATA_ATTR_CLUSTERING_KEY, AttributeValue.builder().l(AttributeValue.builder().s("c2").build(), AttributeValue.builder().s("c3").build()).build());
    Map<String, AttributeValue> clusteringOrders = new HashMap<>();
    clusteringOrders.put("c2", AttributeValue.builder().s("ASC").build());
    clusteringOrders.put("c3", AttributeValue.builder().s("ASC").build());
    itemValues.put(DynamoAdmin.METADATA_ATTR_CLUSTERING_ORDERS, AttributeValue.builder().m(clusteringOrders).build());
    itemValues.put(DynamoAdmin.METADATA_ATTR_SECONDARY_INDEX, AttributeValue.builder().ss("c4").build());
    assertThat(actualPutItemRequest.item()).isEqualTo(itemValues);
    verify(applicationAutoScalingClient, times(4)).registerScalableTarget(any(RegisterScalableTargetRequest.class));
    verify(applicationAutoScalingClient, times(4)).putScalingPolicy(any(PutScalingPolicyRequest.class));
    ArgumentCaptor<UpdateContinuousBackupsRequest> updateContinuousBackupsRequestCaptor = ArgumentCaptor.forClass(UpdateContinuousBackupsRequest.class);
    verify(client, times(2)).updateContinuousBackups(updateContinuousBackupsRequestCaptor.capture());
    List<UpdateContinuousBackupsRequest> updateContinuousBackupsRequests = updateContinuousBackupsRequestCaptor.getAllValues();
    assertThat(updateContinuousBackupsRequests.size()).isEqualTo(2);
    assertThat(updateContinuousBackupsRequests.get(0).tableName()).isEqualTo(FULL_TABLE_NAME);
    assertThat(updateContinuousBackupsRequests.get(1).tableName()).isEqualTo(metadataNamespaceName + "." + DynamoAdmin.METADATA_TABLE);
}
Also used : TableMetadata(com.scalar.db.api.TableMetadata) ContinuousBackupsDescription(software.amazon.awssdk.services.dynamodb.model.ContinuousBackupsDescription) AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) RegisterScalableTargetRequest(software.amazon.awssdk.services.applicationautoscaling.model.RegisterScalableTargetRequest) HashMap(java.util.HashMap) UpdateContinuousBackupsRequest(software.amazon.awssdk.services.dynamodb.model.UpdateContinuousBackupsRequest) AttributeDefinition(software.amazon.awssdk.services.dynamodb.model.AttributeDefinition) DescribeTableResponse(software.amazon.awssdk.services.dynamodb.model.DescribeTableResponse) TableDescription(software.amazon.awssdk.services.dynamodb.model.TableDescription) DescribeContinuousBackupsRequest(software.amazon.awssdk.services.dynamodb.model.DescribeContinuousBackupsRequest) DescribeContinuousBackupsResponse(software.amazon.awssdk.services.dynamodb.model.DescribeContinuousBackupsResponse) PutScalingPolicyRequest(software.amazon.awssdk.services.applicationautoscaling.model.PutScalingPolicyRequest) PutItemRequest(software.amazon.awssdk.services.dynamodb.model.PutItemRequest) ResourceNotFoundException(software.amazon.awssdk.services.dynamodb.model.ResourceNotFoundException) CreateTableRequest(software.amazon.awssdk.services.dynamodb.model.CreateTableRequest)

Aggregations

TableMetadata (com.scalar.db.api.TableMetadata)2 HashMap (java.util.HashMap)2 PutScalingPolicyRequest (software.amazon.awssdk.services.applicationautoscaling.model.PutScalingPolicyRequest)2 RegisterScalableTargetRequest (software.amazon.awssdk.services.applicationautoscaling.model.RegisterScalableTargetRequest)2 AttributeDefinition (software.amazon.awssdk.services.dynamodb.model.AttributeDefinition)2 AttributeValue (software.amazon.awssdk.services.dynamodb.model.AttributeValue)2 ContinuousBackupsDescription (software.amazon.awssdk.services.dynamodb.model.ContinuousBackupsDescription)2 CreateTableRequest (software.amazon.awssdk.services.dynamodb.model.CreateTableRequest)2 DescribeContinuousBackupsRequest (software.amazon.awssdk.services.dynamodb.model.DescribeContinuousBackupsRequest)2 DescribeContinuousBackupsResponse (software.amazon.awssdk.services.dynamodb.model.DescribeContinuousBackupsResponse)2 DescribeTableResponse (software.amazon.awssdk.services.dynamodb.model.DescribeTableResponse)2 PutItemRequest (software.amazon.awssdk.services.dynamodb.model.PutItemRequest)2 TableDescription (software.amazon.awssdk.services.dynamodb.model.TableDescription)2 UpdateContinuousBackupsRequest (software.amazon.awssdk.services.dynamodb.model.UpdateContinuousBackupsRequest)2 DescribeTableRequest (software.amazon.awssdk.services.dynamodb.model.DescribeTableRequest)1 ResourceNotFoundException (software.amazon.awssdk.services.dynamodb.model.ResourceNotFoundException)1