From dfc25dd01d2d280e252e5523afd100f74093981c Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Tue, 17 May 2022 13:52:45 -0400 Subject: [PATCH] GH-205: Skip block in DynamoDbLockReg.tryLock() Fixes https://github.com/spring-projects/spring-integration-aws/issues/205 The `AmazonDynamoDBLockClient.acquireLock()` steps into a busy-wait loop with a sleep timeout. * Use `AcquireLockOptions.shouldSkipBlockingWait = true` for `tryLock()` without timeout to have an immediate answer according `tryLock()` contract * Reset flag to `false` for all other use-cases with `AcquireLockOptions` **Cherry-pick to `2.5.x`** --- .../aws/lock/DynamoDbLockRegistry.java | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/springframework/integration/aws/lock/DynamoDbLockRegistry.java b/src/main/java/org/springframework/integration/aws/lock/DynamoDbLockRegistry.java index 48d54a1..b862f5c 100644 --- a/src/main/java/org/springframework/integration/aws/lock/DynamoDbLockRegistry.java +++ b/src/main/java/org/springframework/integration/aws/lock/DynamoDbLockRegistry.java @@ -1,5 +1,5 @@ /* - * Copyright 2018-2021 the original author or authors. + * Copyright 2018-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -56,6 +56,7 @@ import com.amazonaws.services.dynamodbv2.model.BillingMode; import com.amazonaws.services.dynamodbv2.model.CreateTableRequest; import com.amazonaws.services.dynamodbv2.model.KeySchemaElement; import com.amazonaws.services.dynamodbv2.model.KeyType; +import com.amazonaws.services.dynamodbv2.model.LockCurrentlyUnavailableException; import com.amazonaws.services.dynamodbv2.model.LockNotGrantedException; import com.amazonaws.services.dynamodbv2.model.LockTableDoesNotExistException; import com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput; @@ -450,7 +451,8 @@ public class DynamoDbLockRegistry implements ExpirableLockRegistry, Initializing private void setupDefaultAcquireLockOptionsBuilder() { this.acquireLockOptionsBuilder .withAdditionalTimeToWaitForLock(Long.MAX_VALUE - DynamoDbLockRegistry.this.leaseDuration) - .withRefreshPeriod(DynamoDbLockRegistry.this.refreshPeriod); + .withRefreshPeriod(DynamoDbLockRegistry.this.refreshPeriod) + .withShouldSkipBlockingWait(false); } @Override @@ -505,7 +507,12 @@ public class DynamoDbLockRegistry implements ExpirableLockRegistry, Initializing .max(TimeUnit.MILLISECONDS.convert(time, unit) - System.currentTimeMillis() + start, 0L); this.acquireLockOptionsBuilder.withAdditionalTimeToWaitForLock(additionalTimeToWait) - .withRefreshPeriod(DynamoDbLockRegistry.this.refreshPeriod); + .withRefreshPeriod(DynamoDbLockRegistry.this.refreshPeriod) + .withShouldSkipBlockingWait(false); + + if (additionalTimeToWait == 0) { + this.acquireLockOptionsBuilder.withShouldSkipBlockingWait(true); + } boolean acquired = false; try { @@ -518,6 +525,10 @@ public class DynamoDbLockRegistry implements ExpirableLockRegistry, Initializing this.lastUsed = System.currentTimeMillis(); } } + catch (LockCurrentlyUnavailableException ex) { + this.delegate.unlock(); + logger.trace("The lock '" + this + "' cannot be acquired at the moment", ex); + } catch (Exception e) { this.delegate.unlock(); rethrowAsLockException(e);