-
Notifications
You must be signed in to change notification settings - Fork 211
Reduce catalog contention for RecordMetricsCritical #4776
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
ilidemi
wants to merge
5
commits into
main
Choose a base branch
from
customer-cached-dynconf
base: main
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.
+269
−51
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c77f2fd
Remove extra lock and ping from catalog pool after init
ilidemi 1fbdd36
Cache dynconf settings polled per snapshot partition
ilidemi 35427fa
Global cache per setting
ilidemi 1d5d391
Merge two bookkeeping calls
ilidemi 60e681d
lint
ilidemi 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
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
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,60 @@ | ||
| package internal | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "sync" | ||
| "time" | ||
| ) | ||
|
|
||
| // CachedDynconfSetting caches the value returned by a typed dynamic setting getter. | ||
| type CachedDynconfSetting[T any] struct { | ||
| loadedAt time.Time | ||
| value T | ||
| getter func(context.Context, map[string]string, string) (T, error) | ||
| name string | ||
| ttl time.Duration | ||
| once sync.Once | ||
| mu sync.Mutex | ||
| } | ||
|
|
||
| // InitOnce configures the setting cache on its first call. | ||
| func (s *CachedDynconfSetting[T]) InitOnce( | ||
| name string, | ||
| ttl time.Duration, | ||
| getter func(context.Context, map[string]string, string) (T, error), | ||
| ) { | ||
| s.once.Do(func() { | ||
| s.name = name | ||
| s.ttl = ttl | ||
| s.getter = getter | ||
| }) | ||
| } | ||
|
|
||
| // Get returns the cached value or refreshes it using the configured getter. | ||
| func (s *CachedDynconfSetting[T]) Get(ctx context.Context, env map[string]string) (T, error) { | ||
| if s.getter == nil { | ||
| var zero T | ||
| return zero, errors.New("cached dynamic setting is not initialized") | ||
| } | ||
|
|
||
| if _, overridden := env[s.name]; overridden { | ||
| return s.getter(ctx, env, s.name) | ||
| } | ||
|
|
||
| s.mu.Lock() | ||
| defer s.mu.Unlock() | ||
|
|
||
| if !s.loadedAt.IsZero() && time.Since(s.loadedAt) < s.ttl { | ||
| return s.value, nil | ||
| } | ||
|
|
||
| value, err := s.getter(ctx, nil, s.name) | ||
| if err != nil { | ||
| var zero T | ||
| return zero, err | ||
| } | ||
| s.value = value | ||
| s.loadedAt = time.Now() | ||
| return value, nil | ||
| } |
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,165 @@ | ||
| package internal | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "math/rand/v2" | ||
| "strconv" | ||
| "sync" | ||
| "sync/atomic" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestCachedDynconfSettingCachesWithinTTL(t *testing.T) { | ||
| var calls int | ||
| getter := func(context.Context, map[string]string, string) (int64, error) { | ||
| calls++ | ||
| return 10, nil | ||
| } | ||
| var setting CachedDynconfSetting[int64] | ||
| setting.InitOnce("TEST_SETTING", time.Minute, getter) | ||
|
|
||
| for range 2 { | ||
| value, err := setting.Get(t.Context(), nil) | ||
| require.NoError(t, err) | ||
| require.Equal(t, int64(10), value) | ||
| } | ||
| require.Equal(t, 1, calls) | ||
| } | ||
|
|
||
| func TestCachedDynconfSettingRequiresInitialization(t *testing.T) { | ||
| var setting CachedDynconfSetting[int64] | ||
|
|
||
| _, err := setting.Get(t.Context(), nil) | ||
| require.EqualError(t, err, "cached dynamic setting is not initialized") | ||
| } | ||
|
|
||
| func TestCachedDynconfSettingRefreshesAfterTTL(t *testing.T) { | ||
| var calls int64 | ||
| getter := func(context.Context, map[string]string, string) (int64, error) { | ||
| calls++ | ||
| return calls * 10, nil | ||
| } | ||
| var setting CachedDynconfSetting[int64] | ||
| setting.InitOnce("TEST_SETTING", time.Millisecond, getter) | ||
|
|
||
| value, err := setting.Get(t.Context(), nil) | ||
| require.NoError(t, err) | ||
| require.Equal(t, int64(10), value) | ||
|
|
||
| time.Sleep(10 * time.Millisecond) | ||
|
|
||
| value, err = setting.Get(t.Context(), nil) | ||
| require.NoError(t, err) | ||
| require.Equal(t, int64(20), value) | ||
| require.Equal(t, int64(2), calls) | ||
| } | ||
|
|
||
| func TestCachedDynconfSettingDoesNotCacheErrors(t *testing.T) { | ||
| wantErr := errors.New("lookup failed") | ||
| var calls int | ||
| getter := func(context.Context, map[string]string, string) (int64, error) { | ||
| calls++ | ||
| if calls == 1 { | ||
| return 0, wantErr | ||
| } | ||
| return 10, nil | ||
| } | ||
| var setting CachedDynconfSetting[int64] | ||
| setting.InitOnce("TEST_SETTING", time.Minute, getter) | ||
|
|
||
| _, err := setting.Get(t.Context(), nil) | ||
| require.ErrorIs(t, err, wantErr) | ||
| value, err := setting.Get(t.Context(), nil) | ||
| require.NoError(t, err) | ||
| require.Equal(t, int64(10), value) | ||
| require.Equal(t, 2, calls) | ||
| } | ||
|
|
||
| func TestCachedDynconfSettingCoalescesConcurrentRefreshes(t *testing.T) { | ||
| const goroutines = 8 | ||
|
|
||
| var calls atomic.Int32 | ||
| started := make(chan struct{}) | ||
| release := make(chan struct{}) | ||
| //nolint:unparam // Signature must match the getter type | ||
| getter := func(context.Context, map[string]string, string) (int64, error) { | ||
| if calls.Add(1) == 1 { | ||
| close(started) | ||
| } | ||
| <-release | ||
| return 10, nil | ||
| } | ||
| var setting CachedDynconfSetting[int64] | ||
|
|
||
| results := make(chan int64, goroutines) | ||
| errs := make(chan error, goroutines) | ||
| var wg sync.WaitGroup | ||
| wg.Add(goroutines) | ||
| for range goroutines { | ||
| go func() { | ||
| defer wg.Done() | ||
| setting.InitOnce("TEST_SETTING", time.Minute, getter) | ||
| value, err := setting.Get(t.Context(), nil) | ||
| results <- value | ||
| errs <- err | ||
| }() | ||
| } | ||
|
|
||
| <-started | ||
| close(release) | ||
| wg.Wait() | ||
| close(results) | ||
| close(errs) | ||
|
|
||
| for err := range errs { | ||
| require.NoError(t, err) | ||
| } | ||
| for value := range results { | ||
| require.Equal(t, int64(10), value) | ||
| } | ||
| require.Equal(t, int32(1), calls.Load()) | ||
| } | ||
|
|
||
| func TestCachedDynconfSettingWithTypedDynconfGetter(t *testing.T) { | ||
| //nolint:gosec // Test data does not need cryptographically secure randomness. | ||
| expectedPartSize := rand.Int64() | ||
| var setting CachedDynconfSetting[int64] | ||
| setting.InitOnce("PEERDB_S3_PART_SIZE", time.Minute, dynamicConfSigned[int64]) | ||
|
|
||
| value, err := setting.Get(t.Context(), map[string]string{ | ||
| "PEERDB_S3_PART_SIZE": strconv.FormatInt(expectedPartSize, 10), | ||
| }) | ||
| require.NoError(t, err) | ||
| require.Equal(t, expectedPartSize, value) | ||
| } | ||
|
|
||
| func TestCachedDynconfSettingDoesNotCacheEnvOverrides(t *testing.T) { | ||
| const name = "TEST_SETTING" | ||
| var calls int | ||
| getter := func(_ context.Context, env map[string]string, name string) (int64, error) { | ||
| calls++ | ||
| if value, overridden := env[name]; overridden { | ||
| return strconv.ParseInt(value, 10, 64) | ||
| } | ||
| return 10, nil | ||
| } | ||
| var setting CachedDynconfSetting[int64] | ||
| setting.InitOnce(name, time.Minute, getter) | ||
|
|
||
| value, err := setting.Get(t.Context(), nil) | ||
| require.NoError(t, err) | ||
| require.Equal(t, int64(10), value) | ||
|
|
||
| value, err = setting.Get(t.Context(), map[string]string{name: "20"}) | ||
| require.NoError(t, err) | ||
| require.Equal(t, int64(20), value) | ||
|
|
||
| value, err = setting.Get(t.Context(), nil) | ||
| require.NoError(t, err) | ||
| require.Equal(t, int64(10), value) | ||
| require.Equal(t, 2, calls) | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: maybe do early return instead of branching two times: