Skip to content

Commit

Permalink
Refactor resync Init so the reason for a reset is always logged
Browse files Browse the repository at this point in the history
  • Loading branch information
bbrks committed Mar 7, 2025
1 parent 31dab98 commit 5f374eb
Showing 1 changed file with 24 additions and 30 deletions.
54 changes: 24 additions & 30 deletions db/background_mgr_resync_dcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,6 @@ func (r *ResyncManagerDCP) Init(ctx context.Context, options map[string]interfac
db := options["database"].(*Database)
resyncCollections := options["collections"].(ResyncCollections)

newRunInit := func() error {
uniqueUUID, err := uuid.NewRandom()
if err != nil {
return err
}

r.ResyncID = uniqueUUID.String()
base.InfofCtx(ctx, base.KeyAll, "Resync: Starting new resync run with resync ID: %q", r.ResyncID)
return nil
}

// Get collectionIds and store in manager for use in DCP client later
collectionIDs, hasAllCollections, collectionNames, err := getCollectionIdsAndNames(db, resyncCollections)
if err != nil {
Expand All @@ -85,29 +74,34 @@ func (r *ResyncManagerDCP) Init(ctx context.Context, options map[string]interfac
// add collection list to manager for use in status call
r.SetCollectionStatus(collectionNames)

if clusterStatus != nil {
var statusDoc ResyncManagerStatusDocDCP
err := base.JSONUnmarshal(clusterStatus, &statusDoc)

reset, ok := options["reset"].(bool)
if reset && ok {
base.InfofCtx(ctx, base.KeyAll, "Resync: Resetting resync process. Will not resume any partially completed process")
}

// If the previous run completed, or there was an error during unmarshalling the status we will start the
// process from scratch with a new resync ID. Otherwise, we should resume with the resync ID, stats specified in the doc.
if statusDoc.State == BackgroundProcessStateCompleted || err != nil || (reset && ok) {
return newRunInit()
// If the previous run completed, or there was an error during unmarshalling the status we will start the
// process from scratch with a new resync ID. Otherwise, we should resume with the resync ID, stats specified in the doc.
var resetReason string
var statusDoc ResyncManagerStatusDocDCP
if clusterStatus == nil {
resetReason = "no previous run found"
} else if resetOpt, _ := options["reset"].(bool); resetOpt {
resetReason = "reset option requested"
} else if err := base.JSONUnmarshal(clusterStatus, &statusDoc); err != nil {
resetReason = "failed to unmarshal cluster status"
} else if statusDoc.State == BackgroundProcessStateCompleted {
resetReason = "previous run completed"
}
if resetReason != "" {
newID, err := uuid.NewRandom()
if err != nil {
return err
}
r.ResyncID = statusDoc.ResyncID
r.SetStatus(statusDoc.DocsChanged, statusDoc.DocsProcessed)

base.InfofCtx(ctx, base.KeyAll, "Resync: Attempting to resume resync with resync ID: %s", r.ResyncID)

r.ResyncID = newID.String()
base.InfofCtx(ctx, base.KeyAll, "Resync: Resetting resync process with ID: %q - %s", r.ResyncID, resetReason)
return nil
}

return newRunInit()
// use the resync ID from the status doc to resume
r.ResyncID = statusDoc.ResyncID
r.SetStatus(statusDoc.DocsChanged, statusDoc.DocsProcessed)
base.InfofCtx(ctx, base.KeyAll, "Resync: Resuming resync with ID: %q", r.ResyncID)
return nil
}

func (r *ResyncManagerDCP) Run(ctx context.Context, options map[string]interface{}, persistClusterStatusCallback updateStatusCallbackFunc, terminator *base.SafeTerminator) error {
Expand Down

0 comments on commit 5f374eb

Please sign in to comment.