我正在尝试在 Zookeeper 中实现障碍。当有少量节点需要加入以通过障碍时,我的实现始终有效。但是,当我使用 100 多个需要加入屏障的节点来测试我的实现时,大约有 1% 的时间似乎其中一个节点缺少最后一个观察者事件,并且不检查是否有子节点的数量屏障节点已更改。
我什至同步了 watcher 上的 process 方法,但这并没有改变任何东西。下面是我的 process 方法的代码,以及检查是否需要前进的逻辑。
观察者进程:
public BarrierWatcher(FastBarrier FastBarrier) {
this.ofb = FastBarrier;
}
@Override
public synchronized void process(WatchedEvent event) {
synchronized (ofb) {
ofb.notify();
}
}
控制屏障机制的逻辑:
BarrierWatcher bw = new BarrierWatcher(this);
List<String> memberList = zk.getChildren(barrierPath, bw);
synchronized(this) {
while (memberList.size() < numOfMembers) {
this.wait(1000);
memberList = zk.getChildren(barrierPath, bw);
}
}
我没有只调用 this.wait(),而是为罕见的失败添加了 this.wait(1000)。 1000 到位后,一旦所有节点都加入,它总是会通过障碍。我确信同步过程方法会解决这个问题,但事实并非如此。任何人都有这方面的经验,或者我可能做错了什么?
最佳答案
您可以将您的实现与 netflix-curator 进行比较哪里distributed barrier已经实现。
关于apache-zookeeper - Zookeeper屏障实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6480266/