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;
}
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;
}
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);
});
}
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);
}
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);
}
Aggregations