Alanxtl commented on code in PR #3612: URL: https://github.com/apache/dubbo-go/pull/3612#discussion_r3757261481
########## config_center/zookeeper/impl.go: ########## Review Comment: Public `RemoveListener` 删除不到 `AddListener` 注册的 listener `AddListener` 存的是 `buildPath(rootPath, namespace/key)`,但 `RemoveListener` 直接拿原始 `key` 去删,路径不一致,实际移除失败。并且 `AddListener` 也仍然忽略传入的 `options` group。PR 描述说修了 group/option path handling,但代码没有完整做到。 ########## config_center/zookeeper/config_cache.go: ########## @@ -0,0 +1,237 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package zookeeper + +import ( + "sync" + "time" +) + +type configCacheEntry struct { + content string + exists bool + expiresAt time.Time +} + +type configCache struct { + ttl time.Duration + + stateLock sync.RWMutex + entries map[string]configCacheEntry + watches map[string]bool + generation uint64 + + pathLocks sync.Map +} Review Comment: `entries` / `watches` 只判断过期,不删除;`pathLocks sync.Map` 永不回收,`reset()` 也不清理。`GetProperties` 的 key/group 是外部输入,动态 dataId、错误 key、探测 key 多了以后,内存会线性增长。这个问题 PR 里已经有人评论了,我同意是 P1,需要 bounded cache 或 TTL/LRU 回收。 ########## config_center/zookeeper/listener_test.go: ########## @@ -62,6 +68,48 @@ func TestCacheListenerDataChangeEmptyContent(t *testing.T) { if len(rec.events) != 1 || rec.events[0].ConfigType != remoting.EventTypeDel { t.Fatalf("unexpected events %+v", rec.events) } + entry, ok := cache.getFresh(path) + if !ok || !entry.exists || entry.content != "" { + t.Fatalf("empty configuration should be cached as existing: %+v", entry) + } + + l.DataChange(remoting.Event{Path: path, Action: remoting.EventTypeDel}) + entry, ok = cache.getFresh(path) + if !ok || entry.exists { + t.Fatalf("deleted configuration should be cached as missing: %+v", entry) + } +} + +func TestCacheListenerIgnoresEventAcrossReset(t *testing.T) { + cache := newConfigCache(time.Minute) + l := &CacheListener{rootPath: "/dubbo/config", cache: &cache} + path := "/dubbo/config/group/app" + pathLock := cache.pathLock(path) + pathLock.Lock() + watchStateDone := make(chan struct{}) + go func() { + l.WatchStateChanged(path, false) + close(watchStateDone) + }() + require.Eventually(t, func() bool { + _, ok := l.eventGeneration.Load(path) + return ok + }, time.Second, time.Millisecond) + + cache.reset() + pathLock.Unlock() + <-watchStateDone + l.WatchStateChanged(path, true) + l.DataChange(remoting.Event{Path: path, Action: remoting.EventTypeUpdate, Content: "old"}) + + _, ok := cache.getFresh(path) + if ok { + t.Fatal("event started before reset should not repopulate cache") + } + _, watchActive := cache.snapshot(path) + if watchActive { + t.Fatal("event started before reset should not reactivate watch state") + } Review Comment: “空内容节点”和“不存在节点”的区分没有贯穿 listener 语义 缓存写入时会把 empty content 作为 exists=true 存下来,但随后 `event.Content == ""` 仍然把 change type 改成 `EventTypeDel`。所以监听者和 metrics 看到的还是删除事件。测试 `listener_test.go:64-73` 甚至把这个行为固化了。 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
