-
Notifications
You must be signed in to change notification settings - Fork 140
CBG-2234 rationalize close behavior #7919
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
base: main
Are you sure you want to change the base?
Conversation
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.
Pull request overview
This PR refactors the DCP client's close behavior to make it more intuitive and consistent across implementations. Previously, GoCBDCPClient.Close returned an error if already closed but no error otherwise, which was confusing. The changes standardize the close pattern: Close() is now a void, asynchronous function that can be called multiple times, while completion is signaled through the doneChan returned by Start().
Key changes:
DCPClient.Close()no longer returns an error and can be safely called multiple timesStart()returnsnilfordoneChanwhen it fails, eliminating the need to callClose()on startup failures- All test code now properly waits for
doneChanto ensure clean shutdown
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| base/gocb_dcp_client.go | Changed Close() signature to be void and updated Start() to handle cleanup internally on failure |
| base/gocb_dcp_feed.go | Simplified DCP feed shutdown logic to use new Close() behavior and removed redundant error handling |
| base/dcp_client_test.go | Updated all tests to wait for doneChan and removed Close() error checks |
| db/attachment_compaction.go | Refactored mark, sweep, and cleanup phases to use simplified close pattern |
| db/attachment_compaction_test.go | Replaced custom wait logic with RequireBackgroundManagerState helper |
| db/background_mgr_attachment_migration.go | Updated to use new close pattern with proper doneChan handling |
| db/background_mgr_resync_dcp.go | Simplified termination logic to ignore close errors when background manager terminates |
| db/util_testing.go | Updated purge DCP feed to properly close and wait for doneChan on timeout |
Comments suppressed due to low confidence (1)
db/attachment_compaction.go:1
- Extra space before the period in the log message. Should be "unexpectedly." instead of "unexpectedly .".
// Copyright 2022-Present Couchbase, Inc.
base/gocb_dcp_feed.go
Outdated
| if dcpCloseError != nil { | ||
| WarnfCtx(ctx, "Error on closing DCP Feed %q for %q: %v", feedName, MD(bucketName), dcpCloseError) | ||
| } | ||
| InfofCtx(ctx, KeyDCP, "DCP Feed %q closed unexpectedly with %s", dcpCloseError) |
Copilot
AI
Dec 4, 2025
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.
The log format uses %s but dcpCloseError is an error type. This should use %v instead to properly format the error, or include the missing feedName and MD(bucketName) parameters to match the original logging pattern.
| InfofCtx(ctx, KeyDCP, "DCP Feed %q closed unexpectedly with %s", dcpCloseError) | |
| InfofCtx(ctx, KeyDCP, "DCP Feed %q for bucket %q closed unexpectedly with %v", feedName, MD(bucketName), dcpCloseError) |
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.
I dropped bucket from this because it is included with ctx. I don't think it matters whether error is logged with %s or %v?
| return markedAttachmentCount.Value(), nil, metadataKeyPrefix, markProcessFailureErr | ||
| case err := <-doneChan: | ||
| if err != nil { | ||
| base.InfofCtx(ctx, base.KeyAll, "[%s] Mark phase of attachment compaction terminated unexpectedly: %v. Marked %d attachments", compactionLoggingID, err, markedAttachmentCount.Value()) |
Copilot
AI
Dec 4, 2025
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.
The error returned from doneChan is being logged but the function still returns markProcessFailureErr at line 176. If both err and markProcessFailureErr are non-nil, the logged error differs from the returned error, which could be confusing during debugging. Consider whether err should take precedence or be combined with markProcessFailureErr.
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.
This is intentional here.
CBG-2234 rationalize close behavior in preparation for abstracted DCP client for DCP
Having
GoCBDCPClient.Closereturn an error if it was already closed, but no error if it wasn't already closed was very confusing. This meant the initial error fromClosewere logged, but then you'd have to wait again for another error. This pattern will not work well with another implementation, so remove this separately.DCPClient.Closeis an asynchronous function, since it can be called from the callback function, and would block. e.g. ifProcessFeedEventcallsClosethen it could deadlock, so it wasStartto not return adoneChanif there is an error. This also means thatDCPClient.Closeis not required to be called whenStartfails.doneChanin testsThis is prep for CBG-5013 to make communication with a dcp client easier to understand.
Closeany number of times.doneChannelfromStart. It will return an err (or nil) a single time, and then the channel is closed. This means one caller can find the error.For this code, consider if this cleanup will negatively affect unrelated backports. Not all of this code is well tested since it is primarily around error handling, but it also could have been buggy before!.
Pre-review checklist
fmt.Print,log.Print, ...)base.UD(docID),base.MD(dbName))docs/apiIntegration Tests
GSI=true,xattrs=truehttps://jenkins.sgwdev.com/job/SyncGatewayIntegration/197/