Open mahi1518 opened 4 years ago
@mahi1518 when you call lockItem.isPresent()
in your code above you are calling a method of java's Optional
class.
It does not do anything with any locks.
@ironcream Than Who I will check lock is expired or not?
@mahi1518 not sure I really understand what you're trying to achieve there.
To check if the lock is expired, you call corresponding method on it:
final Optional<LockItem> maybeLockItem;
maybeLockItem = client.tryAcquireLock(AcquireLockOptions.builder("2").build());
if(maybeLockItem.isPresent()){
//lock was acquired
LockItem lockItem = maybeLockItem.get();
boolean isExpired = lockItem.isExpired(); //most likely it is not expired when this check happens
} else {
//lock was not acquired
}
Please note that if you have successfully acquired a lock most likely immediate check on isExpired()
will give you false
in most cases.
If you wait long enough, however, it will give you true
eventually which might not correctly reflect the state of what's stored in Dynamo at that point in time. That's because at that point in time the actual lock stored in Dynamo will have another version, so it will be completely different lock from the one you're holding here in this variable.
Please read the source code and the javadoc. Answers to your questions are readily available there.
@ironcream Thanks for your comment and suggestion Our main concern in DynamoDB lock client is TTL (Time To Live ) what I understand about DynamoDB TTL process if the value of ttl column in dynamoDB is set some upcoming time in milisec then that particular record will be deleted after that time.
If DynamoDB lock client uses DynamoDB TTL properties on lock expiry time (say 10 sec ) then this record will be deleted after 10 sec. please correct if I am wrong.
DynamoDB TTL is coarse grained and cannot be used for this.
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/howitworks-ttl.
DynamoDB typically deletes expired items within 48 hours of expiration. The exact duration within which an item truly gets deleted after expiration is specific to the nature of the workload and the size of the table. Items that have expired and have not been deleted still appear in reads, queries, and scans. These items can still be updated, and successful updates to change or remove the expiration attribute are honored.
I am new to this DynamoDB lock client and I flow this document to implement this https://aws.amazon.com/blogs/database/building-distributed-locks-with-the-dynamodb-lock-client/ As per this document the lock client uses client-side TTL to expire locks after the LeaseDuration time say 10sec but after 10 sec of lock acquired still lock exist.
//wait for 10 sec
TimeUnit.SECONDS.sleep(10);
now check after 10 sec lock is present or not
System.out.printn("Is lock present "+lockItem.isPresent());``
final AmazonDynamoDB dynamoDB = AmazonDynamoDBClientBuilder.standard()
.withEndpointConfiguration(DYNAMODB_LOCAL_ENDPOINT)
.withCredentials(new AWSStaticCredentialsProvider(credentials))
.withRegion(Regions.AP_SOUTHEAST_1)
.build();
final boolean createHeartbeatBackgroundThread = false;
final AmazonDynamoDBLockClient client = new AmazonDynamoDBLockClient(
AmazonDynamoDBLockClientOptions.builder(dynamoDB, "t2_lock")
.withTimeUnit(TimeUnit.SECONDS)
.withLeaseDuration(10L)
.withHeartbeatPeriod(1L)
.withCreateHeartbeatBackgroundThread(createHeartbeatBackgroundThread)
.withOwnerName("2")
.build());
final Optional<LockItem> lockItem;
lockItem =client.tryAcquireLock(AcquireLockOptions.builder("2").build());
// check lock is present or not
System.out.printn("Is lock present "+lockItem.isPresent());After 10 I get lock still present Can you please help me understand If I am wrong at any point Any comment will be appreciated. Thank you