有没有办法在Zookeeper中添加观察者队列? [英] Is there a way to add watcher queue in zookeeper?

查看:56
本文介绍了有没有办法在Zookeeper中添加观察者队列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 node-zookeeper-client 作为Zookeeper客户端的Java脚本.

我有这样的东西:

  const setWatch =(path,functionToExecuteOnTrigger)=>{client.getData(path,(event)=> {//观看者在这里设置functionToExecuteOnTrigger(event);}, 无效的)//null,因为我们仅在此处设置watch,不需要数据} 

此功能将在Zookeeper中的 path 处设置手表.触发监视后,将调用监视者回调,并调用要在监视上调用的适当函数.

对于这样设置的手表:

  setWatch('/data1',function1);setWatch('/data2',function2); 

因此对在 data1 节点中触发的监视执行function1,而对在 data2 节点中触发的监视执行function2

现在,如果我必须继续在节点上监视手表,以便每次进行更改时,我将不得不在触发手表后立即重新注册手表:

  const setWatch =(path,functionToExecuteOnTrigger)=>{client.getData(path,(event)=> {//观看者在这里设置//立即为同一功能设置另一只手表setWatch(path,functionToExecuteOnTrigger);functionToExecuteOnTrigger(event);}, 无效的)//null,因为我们仅在此处设置watch,不需要数据} 

现在我所了解的有关Zookeeper手表的事情是:

  • 有3种类型的手表:数据,孩子和存在
  • 观察者是一次触发,即一旦触发,就必须重新注册以获得进一步的触发.

由于上述第二点,手表触发和重新注册之间可能会丢失更改.如Zookeeper的官方文件所述.

所以我想知道是否有一种方法可以为同一个节点实际设置多个监视(以队列方式),并且每次触发监视时,都只调用一个触发器回调.像这样:

  setWatch(节点,functionToTrigger,noOfWatchesInQueue) 

因此,我们将为同一节点和触发器设置多个监视,仅触发这些监视中的一个.因此,如果我在同一节点的队列中设置3个手表,即

对于触发器1,watch1已激活
对于触发器2,请观看2,依此类推...

这样,在重新注册手表的过程中不会错过任何事件.

有没有办法得到这个?我不知道这是否已经在某处完成,有关该问题的任何类型的研究材料或实施方式也将有所帮助.

解决方案

在Zookeeper中,无法通过单个操作在同一节点上设置多个手表.这是设计使然.

如前所述,(并且根据文档)您只能在读取节点的值时设置单个监视.这样,您将始终拥有节点的最新值,并在节点发生更改时得到通知.这是ZK所提供的,而不是更多.

Zookeeper手表不是事件队列,不能保证您会收到每个事件.

我认为最好描述您的用例并尝试获得最佳解决方案的答案,因为ZK可能不合适.

I am using node-zookeeper-client for java script as a zookeeper client.

I have something like this:

const setWatch = (path, functionToExecuteOnTrigger) => {
  client.getData(path, (event) => {
      // watcher set here
      functionToExecuteOnTrigger(event);
  }, null)
  // null because we are only setting watch here and do not need data
}

This function will set a watch at a path in the zookeeper. Once a watch is triggered, the watcher callback will be called and an appropriate function to be called on the watch is called.

for watches set like this:

setWatch('/data1', function1);
setWatch('/data2', function2);

so function1 is executed for watch triggered in data1 node and function2 executed for watch triggered in data2 node

Now if I have to continuously keep watch at the nodes so that each time something is changed, I would have to re-register the watches immediately after they are triggered:

const setWatch = (path, functionToExecuteOnTrigger) => {
  client.getData(path, (event) => {
      // watcher set here
      // immediately set another watch for the same function
      setWatch(path, functionToExecuteOnTrigger);
      functionToExecuteOnTrigger(event);
  }, null)
  // null because we are only setting watch here and do not need data
}

Now the things I know about zookeeper watches are:

  • There are 3 types of watches; data, children and exist
  • Watchers are one time trigger, i.e. once they are triggered, one has to re-register them to obtain further triggers.

Because of the 2nd point mentioned above, there is a chance of missing changes that occur between the period of watch triggered and re-registering the watch. As mentioned in the official documentation of zookeeper.

So I was wondering if there is a way in which we could actually set multiple watches for the same node (in a queue sort of way) and for each time a watch is triggered, only a single trigger callback is called. Something like this:

setWatch(node, functionToTrigger, noOfWatchesInQueue)

So we will be setting multiple watch for the same node and for a trigger, only one of those set watches are triggered. Hence, if I set 3 watches in queue for the same node i.e

for trigger 1, watch1 is activated
for trigger 2, watch 2 and so on...

That way no event is missed during the time taken to re-register the watch.

Is there a way to obtain this ?? I do not know if this is already been done somewhere, any kind of research material or implementations regarding the issue would be helpful as well.

解决方案

There's no way in Zookeeper to set multiple watches on the same node with a single operation. This is by design.

As you mentioned, (and according to the documentation) you can only set a single watch when you read the value of a node. This way you will always have a the most up-to-date value of a node and notified when it changes. This is what ZK offers, not more.

Zookeeper watches aren't event queues and it's not guaranteed that you'll receive every single event.

I think it'd be better to describe your use case and try to get an answer for what would be the best tool to accomplish it, because ZK might not be suitable.

这篇关于有没有办法在Zookeeper中添加观察者队列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆