Implementation:Apache Shardingsphere ZookeeperDistributedLock Lock
| Knowledge Sources | |
|---|---|
| Domains | Distributed_Locking, ZooKeeper |
| Last Updated | 2026-02-10 00:00 GMT |
Overview
ZookeeperDistributedLock implements distributed locking by wrapping Apache Curator's InterProcessMutex for ZooKeeper-based cluster coordination.
Description
ZookeeperDistributedLock implements the DistributedLock interface using Curator's InterProcessMutex, which provides reentrant mutex semantics backed by ZooKeeper ephemeral sequential nodes. The constructor creates an InterProcessMutex with the given lock key path and CuratorFramework client. The tryLock method delegates to lock.acquire with a millisecond timeout, returning false and handling exceptions via ZookeeperExceptionHandler on failure. The unlock method calls lock.release, also delegating exception handling to ZookeeperExceptionHandler.
Usage
This lock implementation is automatically used when ZooKeeper is the configured cluster persist repository. It leverages Curator's battle-tested distributed locking recipe for reliable mutual exclusion in ZooKeeper-based clusters.
Code Reference
Source Location
- Repository: Apache_Shardingsphere
- File: ZookeeperDistributedLock.java
- Lines: 1-61
Signature
public final class ZookeeperDistributedLock implements DistributedLock {
public ZookeeperDistributedLock(final String lockKey, final CuratorFramework client)
public boolean tryLock(final long timeoutMillis)
public void unlock()
}
Import
import org.apache.shardingsphere.mode.repository.cluster.zookeeper.lock.ZookeeperDistributedLock;
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| lockKey | String | Yes | ZooKeeper path used as the lock node |
| client | CuratorFramework | Yes | Curator ZooKeeper client |
| timeoutMillis | long | Yes | Maximum time to wait for lock acquisition |
Outputs
| Name | Type | Description |
|---|---|---|
| isLocked | boolean | Whether the lock was acquired within the timeout |
Usage Examples
ZookeeperDistributedLock lock = new ZookeeperDistributedLock("/locks/exclusive_op", curatorClient);
if (lock.tryLock(5000L)) {
try {
// Reentrant lock backed by ZooKeeper ephemeral sequential nodes
performExclusiveOperation();
} finally {
lock.unlock();
}
}