Search in sources :

Example 6 with ConditionalCheckFailedException

use of software.amazon.awssdk.services.dynamodb.model.ConditionalCheckFailedException in project iep by Netflix.

the class DynamoDbLeaderDatabase method removeLeadershipFor.

@Override
public boolean removeLeadershipFor(ResourceId resourceId) {
    boolean removed;
    try {
        final Map<String, AttributeValue> resourceRecordKey = Collections.singletonMap(hashKeyName, AttributeValue.builder().s(resourceId.getId()).build());
        final Map<String, AttributeValue> expressionAttributeValues = new HashMap<>(2, 1.0f);
        expressionAttributeValues.put(LEADER_ID_PLACEHOLDER, AttributeValue.builder().s(leaderId.getId()).build());
        expressionAttributeValues.put(NO_LEADER_PLACEHOLDER, AttributeValue.builder().s(LeaderId.NO_LEADER.getId()).build());
        final UpdateItemRequest updateRequest = UpdateItemRequest.builder().tableName(tableName).key(resourceRecordKey).updateExpression(removeLeadershipExpression).conditionExpression(removeLeadershipConditionExpression).expressionAttributeValues(expressionAttributeValues).build();
        removed = db.updateItem(updateRequest).sdkHttpResponse().isSuccessful();
    } catch (ConditionalCheckFailedException e) {
        logger.debug("Didn't remove leader attribute for {}; this {} is not its leader.", resourceId, leaderId.getId());
        removed = false;
    }
    return removed;
}
Also used : AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) UpdateItemRequest(software.amazon.awssdk.services.dynamodb.model.UpdateItemRequest) HashMap(java.util.HashMap) ConditionalCheckFailedException(software.amazon.awssdk.services.dynamodb.model.ConditionalCheckFailedException)

Example 7 with ConditionalCheckFailedException

use of software.amazon.awssdk.services.dynamodb.model.ConditionalCheckFailedException in project iep by Netflix.

the class DynamoDbLeaderDatabase method updateLeadershipFor.

@Override
public boolean updateLeadershipFor(ResourceId resource) {
    final String resourceId = resource.getId();
    boolean updated;
    try {
        final Instant now = Instant.now();
        final Duration leaderMaxIdleDuration = config.getDuration(LEADER_CONFIG_PATH_NAME + ".maxIdleDuration");
        final Instant leaderTimeoutInstant = now.minus(leaderMaxIdleDuration);
        final Map<String, AttributeValue> resourceRecordKey = Collections.singletonMap(hashKeyName, AttributeValue.builder().s(resourceId).build());
        final Map<String, AttributeValue> expressionAttributeValues = new HashMap<>(3, 1.0f);
        expressionAttributeValues.put(LEADER_ID_PLACEHOLDER, AttributeValue.builder().s(leaderId.getId()).build());
        expressionAttributeValues.put(NOW_MILLIS_PLACEHOLDER, AttributeValue.builder().n(String.valueOf(now.toEpochMilli())).build());
        expressionAttributeValues.put(LEADER_TIMEOUT_PLACEHOLDER, AttributeValue.builder().n(String.valueOf(leaderTimeoutInstant.toEpochMilli())).build());
        final UpdateItemRequest updateRequest = UpdateItemRequest.builder().tableName(tableName).key(resourceRecordKey).updateExpression(leaderUpdateExpression).conditionExpression(leaderUpdateConditionExpression).expressionAttributeValues(expressionAttributeValues).build();
        updated = db.updateItem(updateRequest).sdkHttpResponse().isSuccessful();
    } catch (ConditionalCheckFailedException e) {
        logger.debug("There is already an active leader for resource: {}", resourceId);
        updated = false;
    }
    return updated;
}
Also used : AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) UpdateItemRequest(software.amazon.awssdk.services.dynamodb.model.UpdateItemRequest) HashMap(java.util.HashMap) Instant(java.time.Instant) Duration(java.time.Duration) ConditionalCheckFailedException(software.amazon.awssdk.services.dynamodb.model.ConditionalCheckFailedException)

Example 8 with ConditionalCheckFailedException

use of software.amazon.awssdk.services.dynamodb.model.ConditionalCheckFailedException in project thunder by RohanNagar.

the class DynamoDbUsersDao method delete.

@Override
public CompletableFuture<User> delete(String email) {
    Objects.requireNonNull(email);
    DeleteItemRequest deleteItemRequest = DeleteItemRequest.builder().tableName(tableName).key(Collections.singletonMap("email", AttributeValue.builder().s(email).build())).expected(Collections.singletonMap("email", ExpectedAttributeValue.builder().value(AttributeValue.builder().s(email).build()).exists(true).build())).returnValues(ReturnValue.ALL_OLD).build();
    return dynamoDbClient.deleteItem(deleteItemRequest).thenApply(response -> UsersDao.fromJson(mapper, response.attributes().get("document").s()).withTime(Long.parseLong(response.attributes().get("creation_time").n()), Long.parseLong(response.attributes().get("update_time").n()))).exceptionally(throwable -> {
        // result than convertToDatabaseException() supplies
        if (throwable.getCause() instanceof ConditionalCheckFailedException) {
            LOG.warn("The email {} was not found in the database.", email, throwable);
            throw new DatabaseException("User not found in the database.", DatabaseException.Error.USER_NOT_FOUND);
        }
        throw convertToDatabaseException(throwable.getCause(), email);
    });
}
Also used : GetItemRequest(software.amazon.awssdk.services.dynamodb.model.GetItemRequest) LoggerFactory(org.slf4j.LoggerFactory) DynamoDbAsyncClient(software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient) SdkException(software.amazon.awssdk.core.exception.SdkException) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) ComparisonOperator(software.amazon.awssdk.services.dynamodb.model.ComparisonOperator) Map(java.util.Map) ExpectedAttributeValue(software.amazon.awssdk.services.dynamodb.model.ExpectedAttributeValue) ConditionalCheckFailedException(software.amazon.awssdk.services.dynamodb.model.ConditionalCheckFailedException) Nullable(javax.annotation.Nullable) AwsServiceException(software.amazon.awssdk.awscore.exception.AwsServiceException) Logger(org.slf4j.Logger) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) UUID(java.util.UUID) Instant(java.time.Instant) DatabaseException(com.sanctionco.thunder.dao.DatabaseException) Objects(java.util.Objects) PutItemRequest(software.amazon.awssdk.services.dynamodb.model.PutItemRequest) User(com.sanctionco.thunder.models.User) AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) DeleteItemRequest(software.amazon.awssdk.services.dynamodb.model.DeleteItemRequest) Collections(java.util.Collections) UsersDao(com.sanctionco.thunder.dao.UsersDao) ReturnValue(software.amazon.awssdk.services.dynamodb.model.ReturnValue) DeleteItemRequest(software.amazon.awssdk.services.dynamodb.model.DeleteItemRequest) ConditionalCheckFailedException(software.amazon.awssdk.services.dynamodb.model.ConditionalCheckFailedException) DatabaseException(com.sanctionco.thunder.dao.DatabaseException)

Example 9 with ConditionalCheckFailedException

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

the class DeleteStatementHandlerTest method handle_DynamoDbExceptionWithConditionalCheckFailed_ShouldThrowNoMutationException.

@Test
public void handle_DynamoDbExceptionWithConditionalCheckFailed_ShouldThrowNoMutationException() {
    // Arrange
    when(metadata.getClusteringKeyNames()).thenReturn(new LinkedHashSet<>(Collections.singletonList(ANY_NAME_2)));
    when(client.deleteItem(any(DeleteItemRequest.class))).thenReturn(response);
    ConditionalCheckFailedException toThrow = mock(ConditionalCheckFailedException.class);
    doThrow(toThrow).when(client).deleteItem(any(DeleteItemRequest.class));
    Delete delete = prepareDelete().withCondition(new DeleteIfExists());
    // Act Assert
    assertThatThrownBy(() -> handler.handle(delete)).isInstanceOf(NoMutationException.class);
}
Also used : Delete(com.scalar.db.api.Delete) DeleteItemRequest(software.amazon.awssdk.services.dynamodb.model.DeleteItemRequest) ConditionalCheckFailedException(software.amazon.awssdk.services.dynamodb.model.ConditionalCheckFailedException) DeleteIfExists(com.scalar.db.api.DeleteIfExists) Test(org.junit.jupiter.api.Test)

Example 10 with ConditionalCheckFailedException

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

the class PutStatementHandlerTest method handle_DynamoDbExceptionWithConditionalCheckFailed_ShouldThrowNoMutationException.

@Test
public void handle_DynamoDbExceptionWithConditionalCheckFailed_ShouldThrowNoMutationException() {
    // Arrange
    ConditionalCheckFailedException toThrow = mock(ConditionalCheckFailedException.class);
    doThrow(toThrow).when(client).updateItem(any(UpdateItemRequest.class));
    Put put = preparePut().withCondition(new PutIfExists());
    // Act Assert
    assertThatThrownBy(() -> handler.handle(put)).isInstanceOf(NoMutationException.class);
}
Also used : UpdateItemRequest(software.amazon.awssdk.services.dynamodb.model.UpdateItemRequest) ConditionalCheckFailedException(software.amazon.awssdk.services.dynamodb.model.ConditionalCheckFailedException) Put(com.scalar.db.api.Put) PutIfExists(com.scalar.db.api.PutIfExists) Test(org.junit.jupiter.api.Test)

Aggregations

ConditionalCheckFailedException (software.amazon.awssdk.services.dynamodb.model.ConditionalCheckFailedException)20 AttributeValue (software.amazon.awssdk.services.dynamodb.model.AttributeValue)14 UpdateItemRequest (software.amazon.awssdk.services.dynamodb.model.UpdateItemRequest)8 HashMap (java.util.HashMap)7 GetItemResponse (software.amazon.awssdk.services.dynamodb.model.GetItemResponse)3 PutItemRequest (software.amazon.awssdk.services.dynamodb.model.PutItemRequest)3 Delete (com.scalar.db.api.Delete)2 Put (com.scalar.db.api.Put)2 TableMetadata (com.scalar.db.api.TableMetadata)2 ExecutionException (com.scalar.db.exception.storage.ExecutionException)2 NoMutationException (com.scalar.db.exception.storage.NoMutationException)2 RetriableExecutionException (com.scalar.db.exception.storage.RetriableExecutionException)2 AuditEntry (io.automatiko.engine.api.audit.AuditEntry)2 AbstractProcessInstance (io.automatiko.engine.workflow.AbstractProcessInstance)2 BaseAuditEntry (io.automatiko.engine.workflow.audit.BaseAuditEntry)2 Instant (java.time.Instant)2 ArrayList (java.util.ArrayList)2 CompletionException (java.util.concurrent.CompletionException)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 Nonnull (javax.annotation.Nonnull)2