Merge pull request #86 from ashwing/ltr_1_lease_update_cond_check

Add conditional check while updating the lease table meta info
This commit is contained in:
ashwing 2020-07-27 16:40:50 -07:00 committed by GitHub
commit 11034dd5b8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 0 deletions

View file

@ -81,6 +81,13 @@ public interface LeaseSerializer {
*/
Map<String, ExpectedAttributeValue> getDynamoNonexistantExpectation();
/**
* @return the attribute value map asserting that a lease does exist.
*/
default Map<String, ExpectedAttributeValue> getDynamoExistantExpectation() {
throw new UnsupportedOperationException("DynamoExistantExpectation is not implemented");
}
/**
* @param lease
* @return the attribute value map that increments a lease counter

View file

@ -681,8 +681,10 @@ public class DynamoDBLeaseRefresher implements LeaseRefresher {
throws DependencyException, InvalidStateException, ProvisionedThroughputException {
log.debug("Updating lease without expectation {}", lease);
final AWSExceptionManager exceptionManager = createExceptionManager();
exceptionManager.add(ConditionalCheckFailedException.class, t -> t);
Map<String, AttributeValueUpdate> updates = serializer.getDynamoUpdateLeaseUpdate(lease, updateField);
UpdateItemRequest request = UpdateItemRequest.builder().tableName(table).key(serializer.getDynamoHashKey(lease))
.expected(serializer.getDynamoExistantExpectation())
.attributeUpdates(updates).build();
try {
try {
@ -692,6 +694,9 @@ public class DynamoDBLeaseRefresher implements LeaseRefresher {
} catch (InterruptedException e) {
throw new DependencyException(e);
}
} catch (ConditionalCheckFailedException e) {
log.warn("Lease update failed for lease with key {} because the lease did not exist at the time of the update",
lease.leaseKey(), e);
} catch (DynamoDbException | TimeoutException e) {
throw convertAndRethrowExceptions("update", lease.leaseKey(), e);
}

View file

@ -192,6 +192,16 @@ public class DynamoDBLeaseSerializer implements LeaseSerializer {
return result;
}
@Override
public Map<String, ExpectedAttributeValue> getDynamoExistantExpectation() {
Map<String, ExpectedAttributeValue> result = new HashMap<>();
ExpectedAttributeValue expectedAV = ExpectedAttributeValue.builder().exists(true).build();
result.put(LEASE_KEY_KEY, expectedAV);
return result;
}
@Override
public Map<String, AttributeValueUpdate> getDynamoLeaseCounterUpdate(final Lease lease) {
return getDynamoLeaseCounterUpdate(lease.leaseCounter());