我能够成功锁定/ads/lock/0-test1
,然后无法锁定/ads/lock
我该如何解决这个问题?
InterProcessMutex lock1 = new InterProcessMutex(client, "/ads/lock/0-test1");
if(lock1.acquire(30000, TimeUnit.MILLISECONDS)){
InterProcessMutex lock2 = new InterProcessMutex(client, "/ads/lock");
if(lock2.acquire(30000, TimeUnit.MILLISECONDS)) { //Failing
...
}
}
更新:这是https://github.com/Microsoft/Cluster-Partition-Rebalancer-For-Kafka/ZookeeperBackedAdoptionLogicImpl.java第 250 行(详细路径)和 299(根路径)中发生的事情的本质是连续的. 因此,当另一个实例尝试锁定详细路径 (250) 时,锁定失败,因为根路径 (299) 被锁定。逻辑有效但从未获得根锁
更新 2:我写了一个小程序来检查重叠锁是否有效,它确实有效。
public class LockTesting {
public static final String ROOT_LOCK = "/locks";
public static final String CHILD_LOCK = ROOT_LOCK+"/child";
private static CuratorFramework client;
public static void main(String[] args) throws Exception {
client = CuratorFrameworkFactory.newClient("127.0.0.1:2181", new ExponentialBackoffRetry(1000, 30));
client.start();
InterProcessMutex lock1 = new InterProcessMutex(client, CHILD_LOCK);
if (lock1.acquire(30000, TimeUnit.MILLISECONDS)) {
System.out.println("Child Locked");
InterProcessMutex lock2 = new InterProcessMutex(client, ROOT_LOCK);
if (lock2.acquire(30000, TimeUnit.MILLISECONDS)) {
System.out.println("Root Locked");
}
}
}
}