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

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

问题描述

我使用 node-zookeeper-client 作为 java 脚本作为动物园管理员客户端.

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

我有这样的事情:

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
}

这个函数会在zookeeper中的path处设置一个watch.一旦 watch 被触发,watcher 回调将被调用,并在 watch 上调用适当的函数.

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.

对于这样设置的手表:

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

so function1 为 data1 节点触发的 watch 执行,function2 为 data2 节点触发的 watch 执行

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
}

现在我对zookeeper手表的了解是:

Now the things I know about zookeeper watches are:

  • 手表有 3 种类型;数据、子项和存在
  • 观察者是一次性触发器,即一旦被触发,必须重新注册它们以获得进一步的触发器.
  • 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.

由于上面提到的第 2 点,在手表触发和重新注册手表之间发生的变化有可能会丢失.正如zookeeper的官方文档中提到的.

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)

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

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

触发器1,watch1被激活
对于触发器 2、手表 2 等等...

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.

推荐答案

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

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

正如您所提到的(根据文档),当您读取节点的值时,您只能设置一个监视.通过这种方式,您将始终拥有节点的最新值,并在它发生变化时收到通知.这就是 ZK 提供的,而不是更多.

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 监视不是事件队列,并且不能保证您会收到每个事件.

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

我认为最好描述一下您的用例,并尝试找出实现它的最佳工具的答案,因为 ZK 可能不适合.

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天全站免登陆