Sure, here’s the translation: AWS DynamoDB分区键锁在Java中的实现

huangapple go评论109阅读模式
英文:

AWS DynamoDB Partition Key Lock Implementation in Java

问题

以下是您提供的内容的翻译部分:

  1. @Component
  2. public class AwsDynamoDBConfiguration {
  3. Logger logger = LoggerFactory.getLogger(AwsDynamoDBConfiguration.class);
  4. /**
  5. * AWS 配置。
  6. */
  7. @Autowired
  8. private AwsConfiguration awsConfiguration;
  9. private static final AwsClientBuilder.EndpointConfiguration DYNAMODB_ENDPOINT = new AwsClientBuilder.EndpointConfiguration(
  10. "https://dynamodb.us-east-1.amazonaws.com", "us-east-1");
  11. private Optional<LockItem> lockItem;
  12. private AmazonDynamoDBLockClient client;
  13. public boolean lockDynamoDB() throws InterruptedException, IOException {
  14. logger.info("开始:AWS DynamoDB lockDynamoDB");
  15. boolean isDynamoDBLockAccessed = false;
  16. final AmazonDynamoDB dynamoDB = AmazonDynamoDBClientBuilder.standard()
  17. .withEndpointConfiguration(DYNAMODB_ENDPOINT)
  18. .withCredentials(new AWSStaticCredentialsProvider(
  19. new BasicAWSCredentials("access_key", "secret_key")))
  20. .build();
  21. logger.info("已创建 DynamoDB。");
  22. // 是否创建心跳后台线程
  23. final boolean createHeartbeatBackgroundThread = true;
  24. // 构建锁客户端
  25. client = new AmazonDynamoDBLockClient(AmazonDynamoDBLockClientOptions.builder(dynamoDB, "tbl_test_lock")
  26. .withTimeUnit(TimeUnit.SECONDS).withLeaseDuration(100L).withHeartbeatPeriod(3L)
  27. .withCreateHeartbeatBackgroundThread(createHeartbeatBackgroundThread).build());
  28. logger.info("已创建 DynamoDB 客户端。");
  29. // 尝试在分区键 "key" 上获取锁
  30. lockItem = client.tryAcquireLock(AcquireLockOptions.builder("key").build());
  31. logger.info("尝试在 DynamoDB 上获取锁。");
  32. if (lockItem.isPresent()) {
  33. logger.info("已获取锁!如果我死了,我的锁将在 10 秒后过期。");
  34. logger.info("否则,我将持有锁,直到停止心跳。");
  35. isDynamoDBLockAccessed = true;
  36. } else {
  37. logger.info("无法获取锁!");
  38. isDynamoDBLockAccessed = false;
  39. }
  40. client.close();
  41. logger.info("结束:AWS DynamoDB lockDynamoDB");
  42. return isDynamoDBLockAccessed;
  43. }
  44. @PreDestroy
  45. public void destroy() {
  46. logger.info("已触发 AWS DynamoDB 回调 - @PreDestroy。");
  47. if (client != null && lockItem != null && lockItem.isPresent()) {
  48. client.releaseLock(lockItem.get());
  49. }
  50. logger.info("已释放 AWS DynamoDB 锁。");
  51. }
  52. }
英文:

I am running same application with two different ports in my local. The code I have provided is only for testing. Here access_key and secret_key will be provided from environment variables in later stage.

When I run same application in different ports, I see both are able to acquire the lock in the dynamodb partition key.

This is totally new to me. Please need your support whether I need to make any locking mechanism in AWS DynamoDB from AWS Console.

Below are the AWS Console DynamoDB table snapshot and code snippet where I can see the log: "Acquired lock! If I die, my lock will expire in 10 seconds." is coming from both the running instance. As per my requirement, if I lock from one instance, the other instance should not acquire the lock where it should provide the log "Failed to acquire lock!".

> Also I am getting an error while executing the code in Spring Boot.
> AmazonDynamoDBLockClient : Heartbeat thread recieved interrupt, exiting run() (possibly exiting thread)

Sure, here’s the translation:
AWS DynamoDB分区键锁在Java中的实现

  1. @Component
  2. public class AwsDynamoDBConfiguration {
  3. Logger logger = LoggerFactory.getLogger(AwsDynamoDBConfiguration.class);
  4. /**
  5. * AWS configuration.
  6. */
  7. @Autowired
  8. private AwsConfiguration awsConfiguration;
  9. private static final AwsClientBuilder.EndpointConfiguration DYNAMODB_ENDPOINT = new AwsClientBuilder.EndpointConfiguration(
  10. &quot;https://dynamodb.us-east-1.amazonaws.com&quot;, &quot;us-east-1&quot;);
  11. private Optional&lt;LockItem&gt; lockItem;
  12. private AmazonDynamoDBLockClient client;
  13. public boolean lockDynamoDB() throws InterruptedException, IOException {
  14. logger.info(&quot;Start : AWS DynamoDB lockDynamoDB&quot;);
  15. boolean isDynamoDBLockAccessed = false;
  16. final AmazonDynamoDB dynamoDB = AmazonDynamoDBClientBuilder.standard()
  17. .withEndpointConfiguration(DYNAMODB_ENDPOINT)
  18. .withCredentials(new AWSStaticCredentialsProvider(
  19. new BasicAWSCredentials(&quot;access_key&quot;, &quot;secret_key&quot;)))
  20. .build();
  21. logger.info(&quot;DynamoDB created.&quot;);
  22. // Whether or not to create a heartbeating background thread
  23. final boolean createHeartbeatBackgroundThread = true;
  24. // build the lock client
  25. client = new AmazonDynamoDBLockClient(AmazonDynamoDBLockClientOptions.builder(dynamoDB, &quot;tbl_test_lock&quot;)
  26. .withTimeUnit(TimeUnit.SECONDS).withLeaseDuration(100L).withHeartbeatPeriod(3L)
  27. .withCreateHeartbeatBackgroundThread(createHeartbeatBackgroundThread).build());
  28. logger.info(&quot;DynamoDB Client created.&quot;);
  29. // try to acquire a lock on the partition key &quot;key&quot;
  30. lockItem = client.tryAcquireLock(AcquireLockOptions.builder(&quot;key&quot;).build());
  31. logger.info(&quot;DynamoDB try acquire lock.&quot;);
  32. if (lockItem.isPresent()) {
  33. logger.info(&quot;Acquired lock! If I die, my lock will expire in 10 seconds.&quot;);
  34. logger.info(&quot;Otherwise, I will hold it until I stop heartbeating.&quot;);
  35. isDynamoDBLockAccessed = true;
  36. } else {
  37. logger.info(&quot;Failed to acquire lock!&quot;);
  38. isDynamoDBLockAccessed = false;
  39. }
  40. client.close();
  41. logger.info(&quot;End : AWS DynamoDB lockDynamoDB&quot;);
  42. return isDynamoDBLockAccessed;
  43. }
  44. @PreDestroy
  45. public void destroy() {
  46. logger.info(&quot;AWS DynamoDB Callback triggered - @PreDestroy.&quot;);
  47. if (client != null &amp;&amp; lockItem != null &amp;&amp; lockItem.isPresent()) {
  48. client.releaseLock(lockItem.get());
  49. }
  50. logger.info(&quot;AWS DynamoDB Lock has been released.&quot;);
  51. }
  52. }

答案1

得分: 1

需要在条件下获取锁(client.getLock("key", Optional.empty())),并且它对我有效。

if (lockItem.isPresent()) {
client.getLock("key", Optional.empty());
logger.info("已获取锁!如果我死亡,我的锁将在 " + leaseDuration + " 秒后过期。");
logger.info("否则,我将一直持有它,直到我停止心跳。");
isDynamoDBLockAccessed = true;
} else {
logger.info("无法获取锁!");
isDynamoDBLockAccessed = false;
}

英文:

Need to get lock (client.getLock("key", Optional.empty())) under the condition and it worked for me.

  1. if (lockItem.isPresent()) {
  2. client.getLock(&quot;key&quot;, Optional.empty());
  3. logger.info(&quot;Acquired lock! If I die, my lock will expire in &quot; + leaseDuration + &quot; seconds.&quot;);
  4. logger.info(&quot;Otherwise, I will hold it until I stop heartbeating.&quot;);
  5. isDynamoDBLockAccessed = true;
  6. } else {
  7. logger.info(&quot;Failed to acquire lock!&quot;);
  8. isDynamoDBLockAccessed = false;
  9. }

huangapple
  • 本文由 发表于 2020年5月3日 19:37:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/61573794.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定