-
Notifications
You must be signed in to change notification settings - Fork 15
Track change stream stats independently of generation #145
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
Merged
FGasper
merged 8 commits into
mongodb-labs:main
from
FGasper:felipe_changestream_stats_history
Oct 30, 2025
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
378a3ab
save
FGasper 89b4138
mitigation
FGasper e54bf6c
Merge branch 'main' into felipe_changestream_stats_history
FGasper 120648d
note tweak
FGasper 6896207
fix total
FGasper 62f576b
comments
FGasper 3e83200
add test
FGasper e43e7b2
add TTL test
FGasper 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,75 @@ | ||
| package history | ||
|
|
||
| import ( | ||
| "slices" | ||
| "sync" | ||
| "time" | ||
| ) | ||
|
|
||
| // History stores an ordered list of entries, each with a TTL (time-to-live). | ||
| // Once an entry expires, it goes away. | ||
| // | ||
| // This facilitates computation of data flow rates across batches. | ||
| type History[T any] struct { | ||
| mu sync.RWMutex | ||
| ttl time.Duration | ||
| logs []Log[T] | ||
| } | ||
|
|
||
| // Log represents a single entry in a History. | ||
| type Log[T any] struct { | ||
| At time.Time | ||
| Datum T | ||
| } | ||
|
|
||
| // New creates & returns a new History. | ||
| func New[T any](ttl time.Duration) *History[T] { | ||
| return &History[T]{ | ||
| ttl: ttl, | ||
| } | ||
| } | ||
|
|
||
| // Get returns a copy of the History’s (non-expired) elements. | ||
| func (h *History[T]) Get() []Log[T] { | ||
| h.mu.RLock() | ||
| defer h.mu.RUnlock() | ||
|
|
||
| now := time.Now() | ||
|
|
||
| return slices.Clone(h.logs[h.getFirstValidIdxWhileLocked(now):]) | ||
| } | ||
|
|
||
| // Add augments the History’s Log list. It returns the list’s count of | ||
| // (non-expired) elements. | ||
| func (h *History[T]) Add(datum T) int { | ||
| h.mu.Lock() | ||
| defer h.mu.Unlock() | ||
|
|
||
| now := time.Now() | ||
|
|
||
| h.reapWhileLocked(now) | ||
|
|
||
| h.logs = append(h.logs, Log[T]{now, datum}) | ||
|
|
||
| return len(h.logs) | ||
| } | ||
|
|
||
| // NB: If all entries are invalid this returns len(logs). | ||
| func (h *History[T]) getFirstValidIdxWhileLocked(now time.Time) int { | ||
| cutoff := now.Add(-h.ttl) | ||
|
|
||
| for i, logItem := range h.logs { | ||
| if logItem.At.Before(cutoff) { | ||
| continue | ||
| } | ||
|
|
||
| return i | ||
| } | ||
|
|
||
| // We only get here if all logs are stale. | ||
| return len(h.logs) | ||
| } | ||
|
|
||
| func (h *History[T]) reapWhileLocked(now time.Time) { | ||
| h.logs = h.logs[h.getFirstValidIdxWhileLocked(now):] | ||
| } | ||
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,64 @@ | ||
| package history | ||
|
|
||
| import ( | ||
| "slices" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/10gen/migration-verifier/mslices" | ||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestHistory(t *testing.T) { | ||
| h := New[int](time.Hour) | ||
|
|
||
| assert.Equal(t, 1, h.Add(234)) | ||
| assert.Equal(t, 2, h.Add(234)) | ||
| assert.Equal(t, 3, h.Add(345)) | ||
|
|
||
| got := h.Get() | ||
| times, data := splitLogs(got) | ||
| assert.True( | ||
| t, | ||
| slices.IsSortedFunc(times, time.Time.Compare), | ||
| "times should be increasing", | ||
| ) | ||
| assert.Equal(t, mslices.Of(234, 234, 345), data, "data as expected") | ||
|
|
||
| got[0].Datum = 999 | ||
| got = h.Get() | ||
| _, data = splitLogs(got) | ||
| assert.Equal(t, mslices.Of(234, 234, 345), data, "slice is copied") | ||
| } | ||
|
|
||
| func TestHistoryTTL(t *testing.T) { | ||
| h := New[int](time.Millisecond) | ||
|
|
||
| assert.Equal(t, 1, h.Add(234)) | ||
| assert.Equal(t, 2, h.Add(234)) | ||
| assert.Equal(t, 3, h.Add(345)) | ||
|
|
||
| assert.Eventually( | ||
| t, | ||
| func() bool { | ||
| return len(h.Get()) == 0 | ||
| }, | ||
| time.Minute, | ||
| time.Millisecond, | ||
| "history should expire its entries", | ||
| ) | ||
|
|
||
| assert.Equal(t, 1, h.Add(234), "new record should be the first") | ||
| } | ||
|
|
||
| func splitLogs[T any](in []Log[T]) ([]time.Time, []T) { | ||
| var times []time.Time | ||
| var data []T | ||
|
|
||
| for _, cur := range in { | ||
| times = append(times, cur.At) | ||
| data = append(data, cur.Datum) | ||
| } | ||
|
|
||
| return times, data | ||
| } |
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
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.