Copilot commented on code in PR #3612:
URL: https://github.com/apache/dubbo-go/pull/3612#discussion_r3788385437
##########
config_center/zookeeper/listener.go:
##########
@@ -58,31 +88,157 @@ func (l *CacheListener) AddListener(key string, listener
config_center.Configura
listeners.(map[config_center.ConfigurationListener]struct{})[listener] =
struct{}{}
l.keyListeners.Store(key, listeners)
}
+ if l.cache != nil {
+ l.cache.promoteWatch(key)
+ }
+}
+
+func (l *CacheListener) restoreBusinessWatches() {
+ if l.cache == nil || !l.cache.enabled() || l.zkEventListener == nil ||
+ l.zkEventListener.Client == nil ||
l.zkEventListener.Client.Conn == nil {
+ return
+ }
Review Comment:
`restoreBusinessWatches` returns early when `cache-ttl` disables the cache
(`!l.cache.enabled()`). ZooKeeper reconnects still invalidate *all* watches
(including those created by `AddListener`), so with cache disabled listeners
may stop receiving updates after a reconnect because no re-registration happens
(see `config_center/zookeeper/impl.go:345-351`).
##########
config_center/zookeeper/config_cache.go:
##########
@@ -0,0 +1,433 @@
+/*
+ * 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"
+)
+
+import (
+ "github.com/dubbogo/go-zookeeper/zk"
+
+ "github.com/hashicorp/golang-lru"
+)
+
+const (
+ pathLockShardCount = 128
+ maxCacheEntries = 1024
+ maxAutoWatches = 1024
+)
+
+type configCacheEntry struct {
+ content string
+ exists bool
+ expiresAt time.Time
+}
+
+type configWatchState struct {
+ watcher *zk.Watcher
+ auto bool
+ pending bool
+}
+
+func (s configWatchState) tracked() bool {
+ return s.watcher != nil || s.pending
+}
+
+func (s configWatchState) holdsAutoSlot() bool {
+ return s.auto && s.tracked()
+}
+
+func (s configWatchState) holdsAutoWatch() bool {
+ return s.auto && s.watcher != nil
+}
+
+func (s configWatchState) holdsAutoReservation() bool {
+ return s.auto && s.pending
+}
+
+type configCache struct {
+ ttl time.Duration
+
+ stateLock sync.RWMutex
+ entries *lru.Cache
+ watches map[string]configWatchState
+ autoWatchCount int
+ autoWatchReservations int
+ generation uint64
+
+ pathLocks [pathLockShardCount]sync.Mutex
+}
+
+func newConfigCache(ttl time.Duration) configCache {
+ entries, err := lru.New(maxCacheEntries)
+ if err != nil {
+ panic(err)
+ }
+ return configCache{
+ ttl: ttl,
+ entries: entries,
+ watches: make(map[string]configWatchState),
+ }
+}
+
+func (c *configCache) enabled() bool {
+ return c.ttl > 0
+}
+
+func (c *configCache) load(
+ path string,
+ loader func(*zk.Watcher, bool) (configCacheEntry, *zk.Watcher, error),
+ removeWatcher func(*zk.Watcher),
+) (configCacheEntry, error) {
+ if !c.enabled() {
+ entry, _, err := loader(nil, false)
+ return entry, err
+ }
+ if entry, ok := c.getFresh(path); ok {
+ return entry, nil
+ }
+
+ pathLock := c.pathLock(path)
+ pathLock.Lock()
+ defer pathLock.Unlock()
+
+ for {
+ if entry, ok := c.getFresh(path); ok {
+ return entry, nil
+ }
+
+ generation, watchState, registerWatch := c.prepareLoad(path)
+ entry, watcher, err := loader(watchState.watcher, registerWatch)
+ nextWatchState := watchState
+ if registerWatch {
+ nextWatchState = configWatchState{}
+ if watcher != nil {
+ nextWatchState = configWatchState{watcher:
watcher, auto: true}
+ }
+ }
+ if err != nil {
+ if !c.storeWatchState(path, generation, nextWatchState)
{
+ if registerWatch {
+ removeRegisteredWatcher(removeWatcher,
watcher)
+ }
+ continue
+ }
+ return configCacheEntry{}, err
+ }
+ if !c.storeLoad(path, generation, entry, nextWatchState) {
+ if registerWatch {
+ removeRegisteredWatcher(removeWatcher, watcher)
+ }
+ continue
+ }
+ return entry, nil
+ }
+}
+
+func (c *configCache) prepareLoad(path string) (uint64, configWatchState,
bool) {
+ c.stateLock.Lock()
+ defer c.stateLock.Unlock()
+
+ generation := c.generation
+ watchState := c.watches[path]
+ if watchState.tracked() {
+ return generation, watchState, false
+ }
+
+ pendingState := configWatchState{auto: true, pending: true}
+ if !c.setWatchStateLocked(path, pendingState) {
+ return generation, configWatchState{}, false
+ }
+ return generation, pendingState, true
+}
+
+func (c *configCache) store(path string, entry configCacheEntry) {
+ if !c.enabled() {
+ return
+ }
+ pathLock := c.pathLock(path)
+ pathLock.Lock()
+ defer pathLock.Unlock()
+
+ c.stateLock.Lock()
+ defer c.stateLock.Unlock()
+ c.storeEntryLocked(path, entry)
+}
+
+func (c *configCache) storeAtGeneration(path string, generation uint64, entry
configCacheEntry) {
+ if !c.enabled() {
+ return
+ }
+ pathLock := c.pathLock(path)
+ pathLock.Lock()
+ defer pathLock.Unlock()
+
+ c.stateLock.Lock()
+ defer c.stateLock.Unlock()
+ if c.generation == generation {
+ c.storeEntryLocked(path, entry)
+ }
+}
+
+func (c *configCache) getFresh(path string) (configCacheEntry, bool) {
+ c.stateLock.RLock()
+ defer c.stateLock.RUnlock()
+
+ value, ok := c.entries.Get(path)
+ if !ok {
+ return configCacheEntry{}, false
+ }
+ entry := value.(configCacheEntry)
+ if !entry.expiresAt.After(time.Now()) {
+ c.entries.Remove(path)
+ return configCacheEntry{}, false
+ }
+ return entry, true
+}
Review Comment:
`getFresh` holds `stateLock.RLock()` but removes from `c.entries` when the
entry is expired. That mutates shared state under a read lock and can race with
concurrent readers/writers (HashiCorp `lru.Cache` is not concurrency-safe
without external locking), potentially causing data races or panics.
--
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]