CBG-5326: Function to handle document channel history compaction#8218
Open
RIT3shSapata wants to merge 3 commits intomainfrom
Open
CBG-5326: Function to handle document channel history compaction#8218RIT3shSapata wants to merge 3 commits intomainfrom
RIT3shSapata wants to merge 3 commits intomainfrom
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Adds a new DatabaseCollection helper to compact per-document channel assignment history up to a given sequence, with a REST-level test validating the basic behavior.
Changes:
- Introduces
CompactDocChannelHistory(ctx, docid, seq)to pruneChannelSetHistoryand persist the updated_sync(and_mou) xattrs. - Adds an API test that creates/removes/re-adds channels to generate history, then compacts and validates the result.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 8 comments.
| File | Description |
|---|---|
db/crud.go |
Adds the CompactDocChannelHistory implementation and persists updated sync metadata via xattr update. |
rest/api_test.go |
Adds a high-level test covering history generation and compaction behavior. |
adamcfraser
requested changes
May 1, 2026
Collaborator
adamcfraser
left a comment
There was a problem hiding this comment.
Left one comment - have a look at that as well as the copilot comments and assign back once you've updated the PR.
Comment on lines
+4060
to
+4074
| // Compact at seq 3 | ||
| err = collection.CompactDocChannelHistory(ctx, "doc4", 2) | ||
| require.NoError(t, err) | ||
|
|
||
| syncDataAfter, err := collection.GetDocSyncData(ctx, "doc4") | ||
| require.NoError(t, err) | ||
|
|
||
| // Verify: only entries with End > 3 or End == 0 (still active) should remain | ||
| for _, entry := range syncDataAfter.ChannelSet { | ||
| assert.True(t, entry.End == 0 || entry.End > uint64(3)) | ||
| } | ||
|
|
||
| // Verify only channels with seq > 3 should remain | ||
| for _, entry := range syncDataAfter.Channels { | ||
| assert.True(t, entry.Seq > uint64(3)) |
Comment on lines
+4113
to
+4122
| syncDataBefore, err := collection.GetDocSyncData(ctx, "doc7") | ||
| require.NoError(t, err) | ||
| err = collection.CompactDocChannelHistory(ctx, "doc7", 2) | ||
| require.NoError(t, err) | ||
|
|
||
| syncData, err := collection.GetDocSyncData(ctx, "doc7") | ||
| require.NoError(t, err) | ||
| // Should still have active channels | ||
| assert.Less(t, len(syncData.ChannelSet), len(syncDataBefore.ChannelSet)) | ||
| assert.Less(t, len(syncData.Channels), len(syncDataBefore.Channels)) |
Comment on lines
+219
to
+230
| // CompactDocChannelHistory removes channel history entries that ended at or before the given sequence number. | ||
| // This is used to truncate stale channel assignment history to reduce storage overhead. | ||
| func (c *DatabaseCollection) CompactDocChannelHistory(ctx context.Context, docid string, seq uint64) (err error) { | ||
| key := realDocID(docid) | ||
| if key == "" { | ||
| return base.HTTPErrorf(400, "Invalid doc ID") | ||
| } | ||
|
|
||
| _, xattrs, cas, err := c.dataStore.GetWithXattrs(ctx, key, c.syncGlobalSyncMouRevSeqNoAndUserXattrKeys()) | ||
| if err != nil { | ||
| return | ||
| } |
Comment on lines
+227
to
+233
| _, xattrs, cas, err := c.dataStore.GetWithXattrs(ctx, key, c.syncGlobalSyncMouRevSeqNoAndUserXattrKeys()) | ||
| if err != nil { | ||
| return | ||
| } | ||
|
|
||
| doc, err := c.unmarshalDocumentWithXattrs(ctx, docid, nil, xattrs, cas, DocUnmarshalSync) | ||
| if err != nil { |
Comment on lines
+288
to
+292
| updatedXattr := map[string][]byte{ | ||
| base.SyncXattrName: rawSyncXattr, | ||
| base.MouXattrName: rawMouXattr, | ||
| } | ||
| _, err = c.dataStore.UpdateXattrs(ctx, key, 0, cas, updatedXattr, opts) |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
CBG-5326
Describe your PR here...
Pre-review checklist
fmt.Print,log.Print, ...)base.UD(docID),base.MD(dbName))docs/apiDependencies (if applicable)
Integration Tests