【发布时间】:2017-07-05 17:36:56
【问题描述】:
我能够成功锁定/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");
}
}
}
}
【问题讨论】:
-
请查看策展人技术说明 7:cwiki.apache.org/confluence/display/CURATOR/TN7 - 您不能像示例中那样使用重叠路径。此外,没有理由这样做。为第二个锁使用不同的路径。
-
谢谢。但正如您在更新 2 中看到的那样,重叠锁起作用了。所以 Microsoft/Cluster-Partition-Rebalancer-For-Kafka 没有任何问题。但正如你所说,没有理由使用它,因为它在库中使用。
-
更新 2 仍可能失败。我现在已经多次警告过你有重叠的路径。请不要这样做 - 策展人不支持,也没有理由这样做。
-
Ansel - 你能解释一下为什么你一直尝试使用这样的重叠路径,因为它很容易使用 "/locks/set1" "/locks/set2" 等。绝对没有理由放父锁的子路径中的子锁。
-
没有理由@Randgait 我只是在尝试代码库中的代码。感谢您的建议,我会遵循的。
标签: java apache-zookeeper apache-curator distributed-lock