Skip to content

Commit

Permalink
Avoid leaking Zookeeper znodes in tryLock
Browse files Browse the repository at this point in the history
Creating a EPHEMERAL_SEQUENTIAL node was being used to ensure the
Zookeeper connection had been established. These were never explicitly
removed, which led to tryLock effectively leaking zNodes until the
client was terminated. This in turn lead to an extremely large number of
ephemeral nodes being deleted whenever a long running service
terminated!

This fix replaces the creation of a node with a stat of "/" instead.
This has the desired effect of re-establishing the Zookeeper connection,
but is a read-only operation.

* Remove unused import
  • Loading branch information
JonathanO authored and artembilan committed Aug 31, 2018
1 parent 9e33395 commit 47a74ab
Showing 1 changed file with 6 additions and 11 deletions.
Expand Up @@ -29,7 +29,6 @@

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.locks.InterProcessMutex;
import org.apache.zookeeper.CreateMode;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.core.task.AsyncTaskExecutor;
Expand Down Expand Up @@ -275,28 +274,24 @@ public boolean tryLock() {

@Override
public boolean tryLock(long time, TimeUnit unit) throws InterruptedException {
Future<String> future = null;
Future<Boolean> future = null;
try {
long startTime = System.currentTimeMillis();

future = this.mutexTaskExecutor.submit(new Callable<String>() {
future = this.mutexTaskExecutor.submit(new Callable<Boolean>() {

@Override
public String call() throws Exception {
return ZkLock.this.client.create()
.creatingParentContainersIfNeeded()
.withProtection()
.withMode(CreateMode.EPHEMERAL_SEQUENTIAL)
.forPath(ZkLock.this.path);
public Boolean call() throws Exception {
return ZkLock.this.client.checkExists().forPath("/") != null;
}

});

long waitTime = unit.toMillis(time);

String ourPath = future.get(waitTime, TimeUnit.MILLISECONDS);
boolean connected = future.get(waitTime, TimeUnit.MILLISECONDS);

if (ourPath == null) {
if (!connected) {
future.cancel(true);
return false;
}
Expand Down

0 comments on commit 47a74ab

Please sign in to comment.