-
Notifications
You must be signed in to change notification settings - Fork 1k
Feat: Add ZK config cache #3612
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ywxzm03
wants to merge
19
commits into
apache:develop
Choose a base branch
from
ywxzm03:feat/zk-config-cache
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
7f308b5
feat: add Zookeeper config cache
ywxzm03 9ce4e2e
test: cover Zookeeper cache and watch flows
ywxzm03 8fe492a
refactor(zookeeper): bound config cache path locks
ywxzm03 dc11049
fix(zookeeper): bound config cache entries
ywxzm03 3a346f7
refactor(zookeeper): track watcher ownership
ywxzm03 ff4389f
fix(zookeeper): bound automatic watches
ywxzm03 9a59b60
fix(zookeeper): complete config watch lifecycle
ywxzm03 b4d9662
fix(zookeeper): align listener paths and empty config events
ywxzm03 2f7821f
fix(zookeeper): restore and clean up watches on reconnect
ywxzm03 ac59ef1
Merge branch 'apache:main' into feat/zk-config-cache
ywxzm03 e329517
Merge branch 'develop' into feat/zk-config-cache
ywxzm03 9828ff6
Merge remote-tracking branch 'origin/feat/zk-config-cache' into feat/…
ywxzm03 f5beed9
fix(zookeeper): safely remove expired cache entries
ywxzm03 5e78d22
fix(zookeeper): restore business watches with cache disabled
ywxzm03 2fb702f
Merge branch 'develop' into feat/zk-config-cache
ywxzm03 6a309a2
Merge branch 'develop' into feat/zk-config-cache
ywxzm03 266a149
fix(zookeeper): release configuration listener wait group
ywxzm03 4094b35
ci: configure ZooKeeper dependency for tests
ywxzm03 c046c1a
Merge branch 'develop' into feat/zk-config-cache
ywxzm03 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
| } | ||
|
|
||
| func newConfigCache(ttl time.Duration) configCache { | ||
| return configCache{ | ||
| ttl: ttl, | ||
| entries: make(map[string]configCacheEntry), | ||
| watches: make(map[string]bool), | ||
| } | ||
| } | ||
|
|
||
| func (c *configCache) enabled() bool { | ||
| return c.ttl > 0 | ||
| } | ||
|
|
||
| func (c *configCache) load(path string, loader func(bool) (configCacheEntry, bool, error)) (configCacheEntry, error) { | ||
| if !c.enabled() { | ||
| entry, _, err := loader(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, watchActive := c.snapshot(path) | ||
| entry, nextWatchActive, err := loader(watchActive) | ||
| if err != nil { | ||
| if !c.storeWatchState(path, generation, nextWatchActive) { | ||
| continue | ||
| } | ||
| return configCacheEntry{}, err | ||
| } | ||
| if !c.storeLoad(path, generation, entry, nextWatchActive) { | ||
| continue | ||
| } | ||
| return entry, nil | ||
| } | ||
| } | ||
|
|
||
| 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() | ||
|
|
||
| entry, ok := c.entries[path] | ||
| if !ok { | ||
| return configCacheEntry{}, false | ||
| } | ||
| return entry, entry.expiresAt.After(time.Now()) | ||
| } | ||
|
ywxzm03 marked this conversation as resolved.
|
||
|
|
||
| func (c *configCache) storeEntryLocked(path string, entry configCacheEntry) { | ||
| entry.expiresAt = time.Now().Add(c.ttl) | ||
| c.entries[path] = entry | ||
| } | ||
|
|
||
| func (c *configCache) snapshot(path string) (uint64, bool) { | ||
| c.stateLock.RLock() | ||
| defer c.stateLock.RUnlock() | ||
| return c.generation, c.watches[path] | ||
| } | ||
|
|
||
| func (c *configCache) isCurrentGeneration(generation uint64) bool { | ||
| c.stateLock.RLock() | ||
| defer c.stateLock.RUnlock() | ||
| return c.generation == generation | ||
| } | ||
|
|
||
| func (c *configCache) storeLoad(path string, generation uint64, entry configCacheEntry, watchActive bool) bool { | ||
| c.stateLock.Lock() | ||
| defer c.stateLock.Unlock() | ||
| if c.generation != generation { | ||
| return false | ||
| } | ||
|
|
||
| c.storeEntryLocked(path, entry) | ||
| c.setWatchActiveLocked(path, watchActive) | ||
| return true | ||
| } | ||
|
|
||
| func (c *configCache) storeWatchState(path string, generation uint64, watchActive bool) bool { | ||
| c.stateLock.Lock() | ||
| defer c.stateLock.Unlock() | ||
| if c.generation != generation { | ||
| return false | ||
| } | ||
| c.setWatchActiveLocked(path, watchActive) | ||
| return true | ||
| } | ||
|
|
||
| func (c *configCache) setWatchActive(path string, active bool) { | ||
| if !c.enabled() { | ||
| return | ||
| } | ||
| pathLock := c.pathLock(path) | ||
| pathLock.Lock() | ||
| defer pathLock.Unlock() | ||
|
|
||
| c.stateLock.Lock() | ||
| defer c.stateLock.Unlock() | ||
| c.setWatchActiveLocked(path, active) | ||
| } | ||
|
|
||
| func (c *configCache) setWatchActiveAtGeneration(path string, generation uint64, active bool) { | ||
| if !c.enabled() { | ||
| return | ||
| } | ||
| pathLock := c.pathLock(path) | ||
| pathLock.Lock() | ||
| defer pathLock.Unlock() | ||
| c.storeWatchState(path, generation, active) | ||
| } | ||
|
|
||
| func (c *configCache) setWatchActiveLocked(path string, active bool) { | ||
| if active { | ||
| c.watches[path] = true | ||
| return | ||
| } | ||
| delete(c.watches, path) | ||
| } | ||
|
|
||
| func (c *configCache) ensureWatch(path string, register func() error) error { | ||
| if !c.enabled() { | ||
| return register() | ||
| } | ||
| pathLock := c.pathLock(path) | ||
| pathLock.Lock() | ||
| defer pathLock.Unlock() | ||
|
|
||
| for { | ||
| generation, active := c.snapshot(path) | ||
| if active { | ||
| return nil | ||
| } | ||
| if err := register(); err != nil { | ||
| if !c.isCurrentGeneration(generation) { | ||
| continue | ||
| } | ||
| return err | ||
| } | ||
|
|
||
| c.stateLock.Lock() | ||
| if c.generation == generation { | ||
| c.watches[path] = true | ||
| c.stateLock.Unlock() | ||
| return nil | ||
| } | ||
| c.stateLock.Unlock() | ||
| } | ||
| } | ||
|
|
||
| func (c *configCache) reset() { | ||
| c.stateLock.Lock() | ||
| defer c.stateLock.Unlock() | ||
| c.generation++ | ||
| c.entries = make(map[string]configCacheEntry) | ||
| c.watches = make(map[string]bool) | ||
| } | ||
|
|
||
| func (c *configCache) pathLock(path string) *sync.Mutex { | ||
| lock, _ := c.pathLocks.LoadOrStore(path, &sync.Mutex{}) | ||
|
ywxzm03 marked this conversation as resolved.
Outdated
|
||
| return lock.(*sync.Mutex) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| /* | ||
| * 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 ( | ||
| "errors" | ||
| "sync/atomic" | ||
| "testing" | ||
| "time" | ||
| ) | ||
|
|
||
| import ( | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestConfigCacheLoadAndExpiry(t *testing.T) { | ||
| cache := newConfigCache(20 * time.Millisecond) | ||
| var loads atomic.Int32 | ||
| var watchStates []bool | ||
| loader := func(watchActive bool) (configCacheEntry, bool, error) { | ||
| watchStates = append(watchStates, watchActive) | ||
| count := loads.Add(1) | ||
| return configCacheEntry{content: string(rune('0' + count)), exists: true}, true, nil | ||
| } | ||
|
|
||
| first, err := cache.load("/path", loader) | ||
| require.NoError(t, err) | ||
| second, err := cache.load("/path", loader) | ||
| require.NoError(t, err) | ||
| require.Equal(t, first.content, second.content) | ||
| require.Equal(t, int32(1), loads.Load()) | ||
|
|
||
| require.Eventually(t, func() bool { | ||
| entry, loadErr := cache.load("/path", loader) | ||
| return loadErr == nil && entry.content == "2" | ||
| }, time.Second, 5*time.Millisecond) | ||
| require.Equal(t, int32(2), loads.Load()) | ||
| require.Equal(t, []bool{false, true}, watchStates) | ||
|
|
||
| readErr := errors.New("read failed after watch registration") | ||
| _, err = cache.load("/error", func(bool) (configCacheEntry, bool, error) { | ||
| return configCacheEntry{}, true, readErr | ||
| }) | ||
| require.ErrorIs(t, err, readErr) | ||
| _, watchActive := cache.snapshot("/error") | ||
| require.True(t, watchActive) | ||
| } | ||
|
|
||
| func TestConfigCacheWatchUpdateWinsOverLoad(t *testing.T) { | ||
| cache := newConfigCache(time.Minute) | ||
| loadStarted := make(chan struct{}) | ||
| releaseLoad := make(chan struct{}) | ||
| loadDone := make(chan struct{}) | ||
| go func() { | ||
| defer close(loadDone) | ||
| _, _ = cache.load("/path", func(bool) (configCacheEntry, bool, error) { | ||
| close(loadStarted) | ||
| <-releaseLoad | ||
| return configCacheEntry{content: "old", exists: true}, true, nil | ||
| }) | ||
| }() | ||
|
|
||
| <-loadStarted | ||
| updateDone := make(chan struct{}) | ||
| go func() { | ||
| defer close(updateDone) | ||
| cache.store("/path", configCacheEntry{content: "new", exists: true}) | ||
| }() | ||
| close(releaseLoad) | ||
| <-loadDone | ||
| <-updateDone | ||
|
|
||
| entry, ok := cache.getFresh("/path") | ||
| require.True(t, ok) | ||
| require.Equal(t, "new", entry.content) | ||
| } | ||
|
|
||
| func TestConfigCacheResetDiscardsInFlightLoad(t *testing.T) { | ||
| cache := newConfigCache(time.Minute) | ||
| cache.setWatchActive("/path", true) | ||
| loadStarted := make(chan struct{}) | ||
| releaseLoad := make(chan struct{}) | ||
| result := make(chan configCacheEntry, 1) | ||
| var loads atomic.Int32 | ||
|
|
||
| go func() { | ||
| entry, _ := cache.load("/path", func(bool) (configCacheEntry, bool, error) { | ||
| if loads.Add(1) == 1 { | ||
| close(loadStarted) | ||
| <-releaseLoad | ||
| return configCacheEntry{content: "old", exists: true}, true, nil | ||
| } | ||
| return configCacheEntry{content: "new", exists: true}, true, nil | ||
| }) | ||
| result <- entry | ||
| }() | ||
|
|
||
| <-loadStarted | ||
| cache.reset() | ||
| _, ok := cache.getFresh("/path") | ||
| require.False(t, ok) | ||
| _, watchActive := cache.snapshot("/path") | ||
| require.False(t, watchActive) | ||
| close(releaseLoad) | ||
|
|
||
| require.Equal(t, "new", (<-result).content) | ||
| require.Equal(t, int32(2), loads.Load()) | ||
| entry, ok := cache.getFresh("/path") | ||
| require.True(t, ok) | ||
| require.Equal(t, "new", entry.content) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.