Skip to content

Commit e2de2e3

Browse files
JonathanOartembilan
authored andcommitted
Avoid leaking Zookeeper znodes in tryLock
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 (cherry picked from commit 47a74ab)
1 parent 424bf91 commit e2de2e3

File tree

1 file changed

+6
-11
lines changed

1 file changed

+6
-11
lines changed

spring-integration-zookeeper/src/main/java/org/springframework/integration/zookeeper/lock/ZookeeperLockRegistry.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929

3030
import org.apache.curator.framework.CuratorFramework;
3131
import org.apache.curator.framework.recipes.locks.InterProcessMutex;
32-
import org.apache.zookeeper.CreateMode;
3332

3433
import org.springframework.beans.factory.DisposableBean;
3534
import org.springframework.core.task.AsyncTaskExecutor;
@@ -281,28 +280,24 @@ public boolean tryLock() {
281280

282281
@Override
283282
public boolean tryLock(long time, TimeUnit unit) throws InterruptedException {
284-
Future<String> future = null;
283+
Future<Boolean> future = null;
285284
try {
286285
long startTime = System.currentTimeMillis();
287286

288-
future = this.mutexTaskExecutor.submit(new Callable<String>() {
287+
future = this.mutexTaskExecutor.submit(new Callable<Boolean>() {
289288

290289
@Override
291-
public String call() throws Exception {
292-
return ZkLock.this.client.create()
293-
.creatingParentContainersIfNeeded()
294-
.withProtection()
295-
.withMode(CreateMode.EPHEMERAL_SEQUENTIAL)
296-
.forPath(ZkLock.this.path);
290+
public Boolean call() throws Exception {
291+
return ZkLock.this.client.checkExists().forPath("/") != null;
297292
}
298293

299294
});
300295

301296
long waitTime = unit.toMillis(time);
302297

303-
String ourPath = future.get(waitTime, TimeUnit.MILLISECONDS);
298+
boolean connected = future.get(waitTime, TimeUnit.MILLISECONDS);
304299

305-
if (ourPath == null) {
300+
if (!connected) {
306301
future.cancel(true);
307302
return false;
308303
}

0 commit comments

Comments
 (0)