go-zookeeper icon indicating copy to clipboard operation
go-zookeeper copied to clipboard

the watch channel always get the same "event(StateDisconnected)" when add a new node, can't stop!

Open nosix-me opened this issue 9 years ago • 7 comments

go func() { for { event := <-watch //do something } }() When add a new node, event always get the same event(StateDisconnected)

nosix-me avatar Sep 06 '15 16:09 nosix-me

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)
        }
    }()

nguyenxuantuong avatar Sep 27 '15 04:09 nguyenxuantuong

@samuel hi, I came into the same problem. Any ideas? Thanks !

wangxiangyu avatar Oct 28 '15 05:10 wangxiangyu

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)
            }
        }
    }

wangxiangyu avatar Oct 28 '15 06:10 wangxiangyu

@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:}

tfzxyinhao avatar Apr 19 '17 06:04 tfzxyinhao

@wangxiangyu has you slove it?

tfzxyinhao avatar Apr 19 '17 09:04 tfzxyinhao

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 } } }

jifengzhilong avatar Dec 18 '17 17:12 jifengzhilong

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

bb-rajdeep avatar Aug 20 '20 11:08 bb-rajdeep