go-zookeeper
go-zookeeper copied to clipboard
the watch channel always get the same "event(StateDisconnected)" when add a new node, can't stop!
go func() { for { event := <-watch //do something } }() When add a new node, event always get the same event(StateDisconnected)
I have the same problem. I just want to watch for any data change happen in a zookeeper node. If there is, then trigger some event. I have one process update the znode data and one process subscribe for change (as below). Unfortunately, the watch will just trigger once, then I got a bunch of events sent to znodeWatch channel: zk.EventType=Unknown, state=StateDisconnected despite that there is no update to znode. Not sure if it's being resolved or this repo has been abandoned?
_, stat , znodeWatch, err := zkConn.GetW(znodeName)
go func(){
for {
select {
case ev := <- znodeWatch:
if ev.Err != nil {
log.Error("GetW watcher error", ev.Err)
} else if(ev.Type == zk.EventNodeDataChanged) {
log.Warning("receive znode update, do something?")
} else {
log.Debug("znode type", ev.Type, ev.State.String())
}
break
}
time.Sleep(100 * time.Millisecond)
}
}()
@samuel hi, I came into the same problem. Any ideas? Thanks !
I find the solution, it should be used like this:
result, _, event, error := zk_conn.GetZkConn().GetW(path)
fmt.Println(string(result), event, error)
for {
select {
case e := <-event:
if e.Err != nil {
fmt.Printf("GetW watcher error %+v", e.Err)
} else {
fmt.Println(e)
result, _, event, error = zk_conn.GetZkConn().GetW(path)
}
}
}
@samuel
zookeeper version:3.4.10
zk version:commit 1d7be4effb13d2d908342d349d71a284a7542693
I had same problem when a node was deleted by other processes,
recvices large numbers of event,that content as the follow.
{Type:Unknown State:StateDisconnected Path: Err:<nil> Server:}
@wangxiangyu has you slove it?
It's not a problem. Because you wrote a wrong code. (Of course the comments are not complete)
You guys didn't check the channel consume result. The code should be like this:
for { select { case event, ok := <-ch: if !ok { QUIT THE LOOP OR RE-SUBSCRIBE } else { NORMAL EVENT } } }
I am facing the same problem. I have a kafka + zk setup and I am using zk to check whether any new topics have been added in the following way:
for {
select {
case event := <-e: // e is of type <-chan zk.Event
logrus.Debugf("zk event received: path: %s, server: %s, state: %s, type: %s",
event.Path,
event.Server,
event.State.String(),
event.Type.String())
if strings.HasPrefix(event.Path, "/brokers/topics") {
if event.Type.String() == "EventNodeChildrenChanged" {
// do something
}
}
case <-stop:
logrus.Infof("Stop signal received! Closing zk event watcher")
return
}
}
I am getting a huge number of StateDisconnected
events and one EventNodeChildrenChanged
event. This is hogging my CPU