Skip to content

CBG-5900: Add support to compact channel history with Channel Names - #8857

Open
RIT3shSapata wants to merge 2 commits into
mainfrom
CBG-5900
Open

RIT3shSapata wants to merge 2 commits into
mainfrom
CBG-5900

Conversation

@RIT3shSapata

Copy link
Copy Markdown
Contributor

CBG-5900

Add a channels option to the document channel history compact endpoint, POST /{keyspace}/_channel_history/{docid}/compact. You can now name the channels to compact. Before, you could only give a sequence number.

  • Add an optional channels array to CompactDocChannelHistoryRequest. A request must set exactly one of seq or channels.
  • Return 400 if a request sets both, or sets neither. An empty channels array counts as not set.
  • Add a channels parameter to CompactDocChannelHistory. With seq set to zero, the function removes the history of the named channels.
  • In channels mode, SG keeps the entries for channels that the document is still in.
  • Update the OpenAPI spec for the endpoint: the request body, the oneOf rule, and the 400 description.
  • Parameterize the existing test to run for both seq and channel mode

Pre-review checklist

  • Logging sensitive data? Make sure it's tagged (e.g. base.UD(docID), base.MD(dbName))
  • Updated relevant information in the API specifications (such as endpoint descriptions, schemas, ...) in docs/api

Dependencies (if applicable)

  • Link upstream PRs
  • Update Go module dependencies when merged

Integration Tests

- compact channel history with channel names
- parameterize the existing tests to test for channel name based compaction
- update the REST API Endpoint
- update the API spec
- update inline comments and documentation
@RIT3shSapata
RIT3shSapata requested review from a team and a balanced review from Copilot October 6, 2026 12:40
@github-actions

github-actions Bot commented Oct 6, 2026 •

Copy link
Copy Markdown

Redocly previews

@RIT3shSapata RIT3shSapata changed the title Add support to compact channel history with Channel Names CBG-5900: Add support to compact channel history with Channel Names Oct 6, 2026
@factory-droid

factory-droid Bot commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

Droid finished @RIT3shSapata's task —— View job


Review summary

This PR's overall approach (mutual exclusivity between seq and channels, plus tests and OpenAPI updates) looks solid. The main correctness edge case is that compaction can currently delete Channels-map entries with Seq==0, even though those entries are treated as current memberships by getCurrentChannels.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

Channel matching introduces quadratic processing for large documents and channel lists.

Review effort: Balanced
Findings: 1 Medium severity

Open (1)
What changed in this PR

Adds channel-name-based document channel-history compaction alongside sequence-based compaction.

Changes:

  • Adds mutually exclusive seq and channels request modes.
  • Implements channel-name filtering while preserving active memberships.
  • Updates API documentation and parameterizes compaction tests.
File Description
db/​crud.go Implements channel-based compaction.
rest/​doc_api.go Validates and handles the new request mode.
rest/​api_test.go Tests both compaction modes.
rest/​doc_api_test.go Tests request validation.
docs/​api/​paths/​admin/​keyspace-_channel_history-compact.yaml Documents the new API option.
db/​import_test.go Updates the compaction call signature.
db/​hybrid_logical_vector_test.go Updates the compaction call signature.
db/​attachment_test.go Updates the compaction call signature.

💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread db/crud.go
Comment on lines +330 to +334
var del bool
if seq != 0 {
del = channel.End <= seq
} else if len(channels) > 0 {
del = slices.Contains(channels, channel.Name)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this comment is valid, particularly given some of the known use cases related to large numbers of channels per doc - the iteration over channels to create the map wouldn't be significantly more expensive than a single slices.Contains, and you'd only need to do it once.

Comment thread db/crud.go
return del
})

for chanName, chanEntry := range doc.SyncData.Channels {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P1] Avoid compacting Channels entries with Seq==0

SyncData.getCurrentChannels treats both nil removals and non-nil removals with Seq==0 as current channel memberships. CompactDocChannelHistory currently deletes non-nil entries whenever chanEntry.Seq <= seq (seq mode) or when the channel name is listed (channels mode), which can drop a channel that is still considered current and change the effective channel set.

Consider skipping deletions for chanEntry.Seq==0 (consistent with GetDocChannelHistory, which only reports removals where Seq != 0), for example:

Suggested change
for chanName, chanEntry := range doc.SyncData.Channels {
var del bool
if seq != 0 {
del = chanEntry.Seq != 0 && chanEntry.Seq <= seq
} else if len(channels) > 0 {
del = chanEntry.Seq != 0 && slices.Contains(channels, chanName)
}

@RIT3shSapata
RIT3shSapata requested a review from bbrks October 6, 2026 14:17
@RIT3shSapata RIT3shSapata assigned bbrks and adamcfraser and unassigned bbrks Oct 6, 2026
Comment thread db/crud.go
Comment on lines +330 to +334
var del bool
if seq != 0 {
del = channel.End <= seq
} else if len(channels) > 0 {
del = slices.Contains(channels, channel.Name)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this comment is valid, particularly given some of the known use cases related to large numbers of channels per doc - the iteration over channels to create the map wouldn't be significantly more expensive than a single slices.Contains, and you'd only need to do it once.

Comment thread db/crud.go
// If seq is zero, it removes the history entries of the named channels instead.
// This is used to prune stale channel assignment history to reduce storage overhead.
func (c *DatabaseCollection) CompactDocChannelHistory(ctx context.Context, docid string, seq uint64) ([]string, error) {
func (c *DatabaseCollection) CompactDocChannelHistory(ctx context.Context, docid string, seq uint64, channels []string) ([]string, error) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Naming the new parameter 'channelsToDelete' might make the multiple references to channel and channels below more readable.

Comment thread rest/doc_api.go

type CompactDocChannelHistoryRequest struct {
Seq uint64 `json:"seq"`
Seq uint64 `json:"seq"`

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I realize it was the previous handling, but why didn't we allow setting seq=0 for users that wanted to delete all channel history? They needed to use seq=1 for that?

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants