Search in sources :

Example 1 with TransactionCanceledException

use of software.amazon.awssdk.services.dynamodb.model.TransactionCanceledException in project aws-sdk-java-v2 by aws.

the class TransactWriteItemsTest method mixedCommands_conditionCheckFailsTransaction.

@Test
public void mixedCommands_conditionCheckFailsTransaction() {
    mappedTable1.putItem(r -> r.item(RECORDS_1.get(0)));
    mappedTable2.putItem(r -> r.item(RECORDS_2.get(0)));
    Expression conditionExpression = Expression.builder().expression("#attribute = :attribute").expressionValues(singletonMap(":attribute", stringValue("1"))).expressionNames(singletonMap("#attribute", "attribute")).build();
    Key key = Key.builder().partitionValue(0).build();
    TransactWriteItemsEnhancedRequest transactWriteItemsEnhancedRequest = TransactWriteItemsEnhancedRequest.builder().addPutItem(mappedTable2, RECORDS_2.get(1)).addUpdateItem(mappedTable1, RECORDS_1.get(1)).addConditionCheck(mappedTable1, ConditionCheck.builder().key(key).conditionExpression(conditionExpression).build()).addDeleteItem(mappedTable2, RECORDS_2.get(0)).build();
    try {
        enhancedClient.transactWriteItems(transactWriteItemsEnhancedRequest);
        fail("Expected TransactionCanceledException to be thrown");
    } catch (TransactionCanceledException ignored) {
    }
    assertThat(mappedTable1.getItem(r -> r.key(k -> k.partitionValue(1))), is(nullValue()));
    assertThat(mappedTable2.getItem(r -> r.key(k -> k.partitionValue(0))), is(RECORDS_2.get(0)));
    assertThat(mappedTable2.getItem(r -> r.key(k -> k.partitionValue(1))), is(nullValue()));
}
Also used : IntStream(java.util.stream.IntStream) DynamoDbEnhancedClient(software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient) TransactDeleteItemEnhancedRequest(software.amazon.awssdk.enhanced.dynamodb.model.TransactDeleteItemEnhancedRequest) TransactUpdateItemEnhancedRequest(software.amazon.awssdk.enhanced.dynamodb.model.TransactUpdateItemEnhancedRequest) Expression(software.amazon.awssdk.enhanced.dynamodb.Expression) TransactWriteItemsEnhancedRequest(software.amazon.awssdk.enhanced.dynamodb.model.TransactWriteItemsEnhancedRequest) Matchers.nullValue(org.hamcrest.Matchers.nullValue) After(org.junit.After) Collections.singletonMap(java.util.Collections.singletonMap) Assert.fail(org.junit.Assert.fail) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) DeleteTableRequest(software.amazon.awssdk.services.dynamodb.model.DeleteTableRequest) Before(org.junit.Before) ReturnValuesOnConditionCheckFailure(software.amazon.awssdk.services.dynamodb.model.ReturnValuesOnConditionCheckFailure) TransactionCanceledException(software.amazon.awssdk.services.dynamodb.model.TransactionCanceledException) DynamoDbTable(software.amazon.awssdk.enhanced.dynamodb.DynamoDbTable) Key(software.amazon.awssdk.enhanced.dynamodb.Key) CancellationReason(software.amazon.awssdk.services.dynamodb.model.CancellationReason) TableSchema(software.amazon.awssdk.enhanced.dynamodb.TableSchema) Test(org.junit.Test) TransactPutItemEnhancedRequest(software.amazon.awssdk.enhanced.dynamodb.model.TransactPutItemEnhancedRequest) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) StaticAttributeTags.primaryPartitionKey(software.amazon.awssdk.enhanced.dynamodb.mapper.StaticAttributeTags.primaryPartitionKey) List(java.util.List) ConditionCheck(software.amazon.awssdk.enhanced.dynamodb.model.ConditionCheck) Matchers.is(org.hamcrest.Matchers.is) StaticTableSchema(software.amazon.awssdk.enhanced.dynamodb.mapper.StaticTableSchema) AttributeValues.stringValue(software.amazon.awssdk.enhanced.dynamodb.internal.AttributeValues.stringValue) Expression(software.amazon.awssdk.enhanced.dynamodb.Expression) TransactionCanceledException(software.amazon.awssdk.services.dynamodb.model.TransactionCanceledException) Key(software.amazon.awssdk.enhanced.dynamodb.Key) StaticAttributeTags.primaryPartitionKey(software.amazon.awssdk.enhanced.dynamodb.mapper.StaticAttributeTags.primaryPartitionKey) TransactWriteItemsEnhancedRequest(software.amazon.awssdk.enhanced.dynamodb.model.TransactWriteItemsEnhancedRequest) Test(org.junit.Test)

Example 2 with TransactionCanceledException

use of software.amazon.awssdk.services.dynamodb.model.TransactionCanceledException in project aws-sdk-java-v2 by aws.

the class TransactWriteItemsTest method mixedCommands_returnValuesOnConditionCheckFailureSet_allConditionsFail.

@Test
public void mixedCommands_returnValuesOnConditionCheckFailureSet_allConditionsFail() {
    mappedTable1.putItem(r -> r.item(RECORDS_1.get(0)));
    mappedTable1.putItem(r -> r.item(RECORDS_1.get(1)));
    mappedTable2.putItem(r -> r.item(RECORDS_2.get(0)));
    Expression conditionExpression = Expression.builder().expression("#attribute = :attribute").expressionValues(singletonMap(":attribute", stringValue("99"))).expressionNames(singletonMap("#attribute", "attribute")).build();
    Key key0 = Key.builder().partitionValue(0).build();
    Key key1 = Key.builder().partitionValue(1).build();
    ReturnValuesOnConditionCheckFailure returnValues = ReturnValuesOnConditionCheckFailure.ALL_OLD;
    TransactPutItemEnhancedRequest<Record2> putItemRequest = TransactPutItemEnhancedRequest.builder(Record2.class).conditionExpression(conditionExpression).item(RECORDS_2.get(0)).returnValuesOnConditionCheckFailure(returnValues).build();
    TransactUpdateItemEnhancedRequest<Record1> updateItemRequest = TransactUpdateItemEnhancedRequest.builder(Record1.class).conditionExpression(conditionExpression).item(RECORDS_1.get(0)).returnValuesOnConditionCheckFailure(returnValues).build();
    TransactDeleteItemEnhancedRequest deleteItemRequest = TransactDeleteItemEnhancedRequest.builder().key(key1).conditionExpression(conditionExpression).returnValuesOnConditionCheckFailure(returnValues).build();
    TransactWriteItemsEnhancedRequest transactWriteItemsEnhancedRequest = TransactWriteItemsEnhancedRequest.builder().addPutItem(mappedTable2, putItemRequest).addUpdateItem(mappedTable1, updateItemRequest).addConditionCheck(mappedTable1, ConditionCheck.builder().key(key0).conditionExpression(conditionExpression).returnValuesOnConditionCheckFailure(returnValues).build()).addDeleteItem(mappedTable1, deleteItemRequest).build();
    try {
        enhancedClient.transactWriteItems(transactWriteItemsEnhancedRequest);
        fail("Expected TransactionCanceledException to be thrown");
    } catch (TransactionCanceledException e) {
        List<CancellationReason> cancellationReasons = e.cancellationReasons();
        assertThat(cancellationReasons.size(), is(4));
        cancellationReasons.forEach(r -> assertThat(r.item().isEmpty(), is(false)));
    }
}
Also used : IntStream(java.util.stream.IntStream) DynamoDbEnhancedClient(software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient) TransactDeleteItemEnhancedRequest(software.amazon.awssdk.enhanced.dynamodb.model.TransactDeleteItemEnhancedRequest) TransactUpdateItemEnhancedRequest(software.amazon.awssdk.enhanced.dynamodb.model.TransactUpdateItemEnhancedRequest) Expression(software.amazon.awssdk.enhanced.dynamodb.Expression) TransactWriteItemsEnhancedRequest(software.amazon.awssdk.enhanced.dynamodb.model.TransactWriteItemsEnhancedRequest) Matchers.nullValue(org.hamcrest.Matchers.nullValue) After(org.junit.After) Collections.singletonMap(java.util.Collections.singletonMap) Assert.fail(org.junit.Assert.fail) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) DeleteTableRequest(software.amazon.awssdk.services.dynamodb.model.DeleteTableRequest) Before(org.junit.Before) ReturnValuesOnConditionCheckFailure(software.amazon.awssdk.services.dynamodb.model.ReturnValuesOnConditionCheckFailure) TransactionCanceledException(software.amazon.awssdk.services.dynamodb.model.TransactionCanceledException) DynamoDbTable(software.amazon.awssdk.enhanced.dynamodb.DynamoDbTable) Key(software.amazon.awssdk.enhanced.dynamodb.Key) CancellationReason(software.amazon.awssdk.services.dynamodb.model.CancellationReason) TableSchema(software.amazon.awssdk.enhanced.dynamodb.TableSchema) Test(org.junit.Test) TransactPutItemEnhancedRequest(software.amazon.awssdk.enhanced.dynamodb.model.TransactPutItemEnhancedRequest) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) StaticAttributeTags.primaryPartitionKey(software.amazon.awssdk.enhanced.dynamodb.mapper.StaticAttributeTags.primaryPartitionKey) List(java.util.List) ConditionCheck(software.amazon.awssdk.enhanced.dynamodb.model.ConditionCheck) Matchers.is(org.hamcrest.Matchers.is) StaticTableSchema(software.amazon.awssdk.enhanced.dynamodb.mapper.StaticTableSchema) AttributeValues.stringValue(software.amazon.awssdk.enhanced.dynamodb.internal.AttributeValues.stringValue) TransactDeleteItemEnhancedRequest(software.amazon.awssdk.enhanced.dynamodb.model.TransactDeleteItemEnhancedRequest) Expression(software.amazon.awssdk.enhanced.dynamodb.Expression) List(java.util.List) ReturnValuesOnConditionCheckFailure(software.amazon.awssdk.services.dynamodb.model.ReturnValuesOnConditionCheckFailure) TransactionCanceledException(software.amazon.awssdk.services.dynamodb.model.TransactionCanceledException) Key(software.amazon.awssdk.enhanced.dynamodb.Key) StaticAttributeTags.primaryPartitionKey(software.amazon.awssdk.enhanced.dynamodb.mapper.StaticAttributeTags.primaryPartitionKey) TransactWriteItemsEnhancedRequest(software.amazon.awssdk.enhanced.dynamodb.model.TransactWriteItemsEnhancedRequest) Test(org.junit.Test)

Example 3 with TransactionCanceledException

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

the class BatchHandler method handle.

/**
 * Execute the specified list of {@link Mutation}s in batch. All the {@link Mutation}s in the list
 * must be for the same partition.
 *
 * @param mutations a list of {@code Mutation}s to execute
 * @throws NoMutationException if at least one of conditional {@code Mutation}s failed because it
 *     didn't meet the condition
 */
public void handle(List<? extends Mutation> mutations) throws ExecutionException {
    if (mutations.size() > 25) {
        throw new IllegalArgumentException("DynamoDB cannot batch more than 25 mutations at once.");
    }
    TableMetadata tableMetadata = metadataManager.getTableMetadata(mutations.get(0));
    TransactWriteItemsRequest.Builder builder = TransactWriteItemsRequest.builder();
    List<TransactWriteItem> transactItems = new ArrayList<>();
    mutations.forEach(m -> transactItems.add(makeWriteItem(m, tableMetadata)));
    builder.transactItems(transactItems);
    try {
        client.transactWriteItems(builder.build());
    } catch (TransactionCanceledException e) {
        boolean allReasonsAreTransactionConflicts = true;
        for (CancellationReason reason : e.cancellationReasons()) {
            if (reason.code().equals("ConditionalCheckFailed")) {
                throw new NoMutationException("no mutation was applied.", e);
            }
            if (!reason.code().equals("TransactionConflict") && !reason.code().equals("None")) {
                allReasonsAreTransactionConflicts = false;
            }
        }
        if (allReasonsAreTransactionConflicts) {
            // RetriableExecutionException
            throw new RetriableExecutionException(e.getMessage(), e);
        }
        throw new ExecutionException(e.getMessage(), e);
    } catch (DynamoDbException e) {
        throw new ExecutionException(e.getMessage(), e);
    }
}
Also used : TableMetadata(com.scalar.db.api.TableMetadata) CancellationReason(software.amazon.awssdk.services.dynamodb.model.CancellationReason) TransactWriteItemsRequest(software.amazon.awssdk.services.dynamodb.model.TransactWriteItemsRequest) ArrayList(java.util.ArrayList) RetriableExecutionException(com.scalar.db.exception.storage.RetriableExecutionException) TransactionCanceledException(software.amazon.awssdk.services.dynamodb.model.TransactionCanceledException) DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException) TransactWriteItem(software.amazon.awssdk.services.dynamodb.model.TransactWriteItem) ExecutionException(com.scalar.db.exception.storage.ExecutionException) RetriableExecutionException(com.scalar.db.exception.storage.RetriableExecutionException) NoMutationException(com.scalar.db.exception.storage.NoMutationException)

Example 4 with TransactionCanceledException

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

the class BatchHandlerTest method handle_TransactionCanceledExceptionWithConditionCheckFailed_ShouldThrowNoMutationException.

@Test
public void handle_TransactionCanceledExceptionWithConditionCheckFailed_ShouldThrowNoMutationException() {
    TransactionCanceledException toThrow = TransactionCanceledException.builder().cancellationReasons(CancellationReason.builder().code("ConditionalCheckFailed").build()).build();
    doThrow(toThrow).when(client).transactWriteItems(any(TransactWriteItemsRequest.class));
    Put put = preparePut().withCondition(new PutIfNotExists());
    Delete delete = prepareDelete().withCondition(new DeleteIfExists());
    // Act Assert
    assertThatThrownBy(() -> handler.handle(Arrays.asList(put, delete))).isInstanceOf(NoMutationException.class);
}
Also used : PutIfNotExists(com.scalar.db.api.PutIfNotExists) Delete(com.scalar.db.api.Delete) TransactWriteItemsRequest(software.amazon.awssdk.services.dynamodb.model.TransactWriteItemsRequest) TransactionCanceledException(software.amazon.awssdk.services.dynamodb.model.TransactionCanceledException) Put(com.scalar.db.api.Put) DeleteIfExists(com.scalar.db.api.DeleteIfExists) Test(org.junit.Test)

Example 5 with TransactionCanceledException

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

the class BatchHandlerTest method handle_TransactionCanceledExceptionWithTransactionConflict_ShouldThrowRetriableExecutionException.

@Test
public void handle_TransactionCanceledExceptionWithTransactionConflict_ShouldThrowRetriableExecutionException() {
    TransactionCanceledException toThrow = TransactionCanceledException.builder().cancellationReasons(CancellationReason.builder().code("TransactionConflict").build(), CancellationReason.builder().code("None").build()).build();
    doThrow(toThrow).when(client).transactWriteItems(any(TransactWriteItemsRequest.class));
    Put put = preparePut().withCondition(new PutIfNotExists());
    Delete delete = prepareDelete().withCondition(new DeleteIfExists());
    // Act Assert
    assertThatThrownBy(() -> handler.handle(Arrays.asList(put, delete))).isInstanceOf(RetriableExecutionException.class);
}
Also used : PutIfNotExists(com.scalar.db.api.PutIfNotExists) Delete(com.scalar.db.api.Delete) TransactWriteItemsRequest(software.amazon.awssdk.services.dynamodb.model.TransactWriteItemsRequest) TransactionCanceledException(software.amazon.awssdk.services.dynamodb.model.TransactionCanceledException) Put(com.scalar.db.api.Put) DeleteIfExists(com.scalar.db.api.DeleteIfExists) Test(org.junit.Test)

Aggregations

TransactionCanceledException (software.amazon.awssdk.services.dynamodb.model.TransactionCanceledException)5 Test (org.junit.Test)4 CancellationReason (software.amazon.awssdk.services.dynamodb.model.CancellationReason)3 TransactWriteItemsRequest (software.amazon.awssdk.services.dynamodb.model.TransactWriteItemsRequest)3 Delete (com.scalar.db.api.Delete)2 DeleteIfExists (com.scalar.db.api.DeleteIfExists)2 Put (com.scalar.db.api.Put)2 PutIfNotExists (com.scalar.db.api.PutIfNotExists)2 Collections.singletonMap (java.util.Collections.singletonMap)2 List (java.util.List)2 Objects (java.util.Objects)2 Collectors (java.util.stream.Collectors)2 IntStream (java.util.stream.IntStream)2 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)2 Matchers.is (org.hamcrest.Matchers.is)2 Matchers.nullValue (org.hamcrest.Matchers.nullValue)2 After (org.junit.After)2 Assert.fail (org.junit.Assert.fail)2 Before (org.junit.Before)2 DynamoDbEnhancedClient (software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient)2