From 9d087ed79f5c83e6f585e789300925368f0c65bb Mon Sep 17 00:00:00 2001 From: Ken Liu Date: Sat, 3 Jun 2023 13:21:20 +0800 Subject: [PATCH] Try update value if node already exists (#2314) * Try update value if node already exists * fix variable declaration shadow. --- config_center/zookeeper/impl.go | 9 +++++++++ remoting/zookeeper/listener.go | 3 +-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/config_center/zookeeper/impl.go b/config_center/zookeeper/impl.go index feafea489e..b6c3e6be51 100644 --- a/config_center/zookeeper/impl.go +++ b/config_center/zookeeper/impl.go @@ -167,6 +167,15 @@ func (c *zookeeperDynamicConfiguration) PublishConfig(key string, group string, // create every node in the path with given value which we may not expected. err := c.client.CreateWithValue(path, valueBytes) if err != nil { + // try update value if node already exists + if perrors.Is(err, zk.ErrNodeExists) { + _, stat, _ := c.client.GetContent(path) + _, setErr := c.client.SetContent(path, valueBytes, stat.Version) + if setErr != nil { + return perrors.WithStack(setErr) + } + return nil + } return perrors.WithStack(err) } return nil diff --git a/remoting/zookeeper/listener.go b/remoting/zookeeper/listener.go index b36d8830c7..673e0119af 100644 --- a/remoting/zookeeper/listener.go +++ b/remoting/zookeeper/listener.go @@ -344,8 +344,7 @@ func (l *ZkEventListener) listenDirEvent(conf *common.URL, zkRootPath string, li failTimes = MaxFailTimes } - err = perrors.Cause(err) - if !strings.Contains(err.Error(), "node does not exist") { // ignore if node not exist + if !perrors.Is(err, zk.ErrNoNode) { // ignore if node not exist logger.Errorf("[Zookeeper EventListener][listenDirEvent] Get children of path {%s} with watcher failed, the error is %+v", zkRootPath, err) } // Maybe the provider does not ready yet, sleep failTimes * ConnDelay senconds to wait