Reputation: 1273
What will happen if one of the instances of Hazelcast cluster will acquire lock and die in locked state? e.g.
if (hz.getCPSubsystem().getLock("lockName").tryLock(500, TimeUnit.MILLISECONDS)){
try{
System.exit(0);
}finally {
hz.getCPSubsystem().getLock("lockName").unlock();
}
}
Upvotes: 2
Views: 1347
Reputation: 886
Short version: The CP subsystem has a session mechanism that keeps track of the liveness of all members. When it recognizes the member holding the lock has died, the lock will be released. The session heartbeat mechanism can be tuned if needed to meet the performance needs of the specific application.
Long version: I'd suggest reading this blog post: https://hazelcast.com/blog/long-live-distributed-locks/ and in particular, the section 'what happens if a lock holder dies'.
One of the trickier aspects of making this type of code reliable is what happens when there is a network problem ... for example, the lock holder may go offline, and heartbeats won't be seen. So the cluster declares the lock holder dead, releases the lock, and assigns it to someone else. Then the lock holder comes back online and tries to continue processing, thinking it still has the lock. The FencedLock implementation within Hazelcast will recognize the lock as stale and not allow the holder of the stale lock to proceed - he must re-acquire the lock before taking any action covered by the lock.
Upvotes: 3