Skip to content

Commit 7bf3214

Browse files
torcolvinCopilotadamcfraser
authored
Allow resync background manager Resume to round trip options (#8331)
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> Co-authored-by: Adam Fraser <adam.fraser@couchbase.com>
1 parent 98db576 commit 7bf3214

13 files changed

Lines changed: 290 additions & 220 deletions

db/background_mgr.go

Lines changed: 47 additions & 37 deletions
Large diffs are not rendered by default.

db/background_mgr_async_index_init.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,10 @@ func (a *AsyncIndexInitManager) ResetStatus() {
8989
return
9090
}
9191

92-
var _ BackgroundManagerProcessI = &AsyncIndexInitManager{}
92+
var _ BackgroundManagerProcessI[map[string]any] = &AsyncIndexInitManager{}
9393

94-
func NewAsyncIndexInitManager(metadataStore base.DataStore, metaKeys *base.MetadataKeys) *BackgroundManager {
95-
return &BackgroundManager{
94+
func NewAsyncIndexInitManager(metadataStore base.DataStore, metaKeys *base.MetadataKeys) *BackgroundManager[map[string]any] {
95+
return &BackgroundManager[map[string]any]{
9696
name: "index_init",
9797
Process: &AsyncIndexInitManager{},
9898
clusterAwareOptions: &ClusterAwareBackgroundManagerOptions{

db/background_mgr_attachment_compaction.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ type AttachmentCompactionManager struct {
3838
runFunctionStartedCallback atomic.Pointer[runFunctionStartedCallbackFunc]
3939
}
4040

41-
var _ BackgroundManagerProcessI = &AttachmentCompactionManager{}
41+
var _ BackgroundManagerProcessI[map[string]any] = &AttachmentCompactionManager{}
4242

43-
func NewAttachmentCompactionManager(metadataStore base.DataStore, metaKeys *base.MetadataKeys) *BackgroundManager {
44-
return &BackgroundManager{
43+
func NewAttachmentCompactionManager(metadataStore base.DataStore, metaKeys *base.MetadataKeys) *BackgroundManager[map[string]any] {
44+
return &BackgroundManager[map[string]any]{
4545
name: "attachment_compaction",
4646
Process: &AttachmentCompactionManager{},
4747
clusterAwareOptions: &ClusterAwareBackgroundManagerOptions{

db/background_mgr_attachment_migration.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ type AttachmentMigrationManager struct {
3131
lock sync.RWMutex
3232
}
3333

34-
var _ BackgroundManagerProcessI = &AttachmentMigrationManager{}
34+
var _ BackgroundManagerProcessI[map[string]any] = &AttachmentMigrationManager{}
3535

3636
const MetaVersionValue = "4.0.0" // Meta version to set in syncInfo document upon completion of attachment migration for collection
3737

38-
func NewAttachmentMigrationManager(database *DatabaseContext) *BackgroundManager {
38+
func NewAttachmentMigrationManager(database *DatabaseContext) *BackgroundManager[map[string]any] {
3939
metadataStore := database.MetadataStore
4040
metaKeys := database.MetadataKeys
41-
return &BackgroundManager{
41+
return &BackgroundManager[map[string]any]{
4242
name: "attachment_migration",
4343
Process: &AttachmentMigrationManager{
4444
databaseCtx: database,

db/background_mgr_metadata_migration.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ type MetadataMigrationManager struct {
3434

3535
const MetadataMigrationManagerName = "metadata_migration"
3636

37-
var _ BackgroundManagerProcessI = &MetadataMigrationManager{}
37+
var _ BackgroundManagerProcessI[map[string]any] = &MetadataMigrationManager{}
3838

3939
// errMetadataMigrationTerminated is the cancellation cause propagated to the run context when
4040
// the BackgroundManager terminator fires, so ops blocked on ctx (seq-counter retry loop, range
4141
// scan iteration) can distinguish an operator stop from a parent-context cancellation.
4242
var errMetadataMigrationTerminated = errors.New("metadata migration terminated by stop request")
4343

44-
func NewMetadataMigrationManager(dbContext *DatabaseContext) *BackgroundManager {
45-
return &BackgroundManager{
44+
func NewMetadataMigrationManager(dbContext *DatabaseContext) *BackgroundManager[map[string]any] {
45+
return &BackgroundManager[map[string]any]{
4646
name: MetadataMigrationManagerName,
4747
Process: &MetadataMigrationManager{dbContext: dbContext},
4848
clusterAwareOptions: &ClusterAwareBackgroundManagerOptions{

db/background_mgr_resync_dcp.go

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ type ResyncManagerDCP struct {
4545
ResyncID string
4646
VBUUIDs []uint64
4747
ResyncedCollections base.CollectionNames
48-
startOptions map[string]any // options from the most recent Start call, persisted to meta for Resume
48+
startOptions ResyncOptions // options from the most recent Start call, persisted to meta for Resume
4949
resyncCollectionInfo
5050
lock sync.RWMutex
5151
Distributed bool
@@ -94,13 +94,20 @@ type resyncCollectionInfo struct {
9494
hasAllCollections bool
9595
}
9696

97-
var _ BackgroundManagerProcessI = &ResyncManagerDCP{}
97+
// ResyncOptions are used to initialize a resync process.
98+
type ResyncOptions struct {
99+
Collections base.CollectionNames `json:"collections,omitempty"`
100+
Reset bool `json:"reset,omitempty"`
101+
RegenerateSequences bool `json:"regenerateSequences,omitempty"`
102+
}
103+
104+
var _ BackgroundManagerProcessI[ResyncOptions] = &ResyncManagerDCP{}
98105

99106
// NewResyncManagerDCP returns a new instance of ResyncManagerDCP wrapped in a BackgroundManager. If distributed is
100107
// true, the manager will be set up to run in a distributed manner across multiple nodes, otherwise it will run on a
101108
// single node.
102-
func NewResyncManagerDCP(db *DatabaseContext, distributed bool) *BackgroundManager {
103-
b := &BackgroundManager{
109+
func NewResyncManagerDCP(db *DatabaseContext, distributed bool) *BackgroundManager[ResyncOptions] {
110+
b := &BackgroundManager[ResyncOptions]{
104111
name: "resync",
105112
Process: &ResyncManagerDCP{
106113
db: db,
@@ -126,18 +133,14 @@ func NewResyncManagerDCP(db *DatabaseContext, distributed bool) *BackgroundManag
126133
return b
127134
}
128135

129-
// Init processes the options to start a resync process and sets them as struct memebers.
130-
func (r *ResyncManagerDCP) Init(ctx context.Context, options map[string]any, clusterStatus []byte) error {
131-
resyncCollections, ok := options["collections"].(base.CollectionNames)
132-
if !ok {
133-
return errors.New("collections option is required and must be of type base.CollectionNames")
134-
}
136+
// Init processes the options to start a resync process and sets them as struct members.
137+
func (r *ResyncManagerDCP) Init(ctx context.Context, options ResyncOptions, clusterStatus []byte) error {
135138
r.setStartOptions(options)
136139

137140
var collections DatabaseCollections
138-
if len(resyncCollections) > 0 {
141+
if len(options.Collections) > 0 {
139142
var err error
140-
collections, err = r.db.collections(resyncCollections)
143+
collections, err = r.db.collections(options.Collections)
141144
if err != nil {
142145
return err
143146
}
@@ -154,7 +157,7 @@ func (r *ResyncManagerDCP) Init(ctx context.Context, options map[string]any, clu
154157
var statusDoc ResyncManagerStatusDocDCP
155158
if clusterStatus == nil {
156159
resetMsg = "no previous run found"
157-
} else if resetOpt, _ := options["reset"].(bool); resetOpt {
160+
} else if options.Reset {
158161
resetMsg = "reset option requested"
159162
} else if err := base.JSONUnmarshal(clusterStatus, &statusDoc); err != nil {
160163
resetMsg = "failed to unmarshal cluster status"
@@ -216,7 +219,7 @@ func (r *ResyncManagerDCP) purgeCheckpoints(ctx context.Context, resyncID string
216219
}
217220

218221
// setStartOptions stores the options used to start the current run so that Resume can reconstruct them.
219-
func (r *ResyncManagerDCP) setStartOptions(options map[string]any) {
222+
func (r *ResyncManagerDCP) setStartOptions(options ResyncOptions) {
220223
r.lock.Lock()
221224
defer r.lock.Unlock()
222225
r.startOptions = options
@@ -230,12 +233,9 @@ func (r *ResyncManagerDCP) SetVBUUIDs(vbuuids []uint64) {
230233
}
231234

232235
// Run starts a DCP feed to process documents for resync.
233-
func (r *ResyncManagerDCP) Run(ctx context.Context, options map[string]any, persistClusterStatusCallback updateStatusCallbackFunc, terminator *base.SafeTerminator) (err error) {
236+
func (r *ResyncManagerDCP) Run(ctx context.Context, options ResyncOptions, persistClusterStatusCallback updateStatusCallbackFunc, terminator *base.SafeTerminator) (err error) {
234237
db := r.db
235-
regenerateSequences, ok := options["regenerateSequences"].(bool)
236-
if !ok {
237-
return errors.New("regenerateSequences option is required and must be of type bool")
238-
}
238+
regenerateSequences := options.RegenerateSequences
239239
ctx = context.WithoutCancel(ctx) // drop cancellation from parent context
240240
ctx = base.CorrelationIDLogCtx(ctx, r.ResyncID)
241241
ctx, cancelResync := context.WithCancelCause(ctx)
@@ -789,9 +789,9 @@ type resyncManagerCompletedVBuckets struct {
789789
}
790790

791791
type ResyncManagerMeta struct {
792-
VBUUIDs []uint64 `json:"vbuuids"`
793-
CollectionIDs []uint32 `json:"collection_ids,omitempty"`
794-
Options map[string]any `json:"options,omitempty"` // start options, persisted for Resume
792+
VBUUIDs []uint64 `json:"vbuuids"`
793+
CollectionIDs []uint32 `json:"collection_ids,omitempty"`
794+
Options ResyncOptions `json:"options,omitempty"` // start options, persisted for Resume
795795
resyncManagerCompletedVBuckets
796796
}
797797

0 commit comments

Comments
 (0)