-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
REP-5550 Add resumability for recheck generations (#85)
Previously migration-verifier did not persist its generation, so if it crashed in a generation after generation 0 it would still restart at generation 0. This could cause any enqueued rechecks not to happen until the restarted verifier reached the prior verifier run’s generation. If the user called `writesOff` before then, though, the rechecks would never happen. Thus, migration-verifier could neglect important rechecks. This fixes that by persisting migration-verifier’s generation at the start of each generation. Thus, any tasks that need to be rerun, or enqueued rechecks that need to be processed into checks for the next generation, will be replayed in the event of a crash.
- Loading branch information
Showing
4 changed files
with
210 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package verifier | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/10gen/migration-verifier/option" | ||
"github.com/pkg/errors" | ||
"go.mongodb.org/mongo-driver/bson" | ||
"go.mongodb.org/mongo-driver/mongo" | ||
"go.mongodb.org/mongo-driver/mongo/options" | ||
) | ||
|
||
const ( | ||
generationCollName = "generation" | ||
generationFieldName = "generation" | ||
) | ||
|
||
func (v *Verifier) persistGenerationWhileLocked(ctx context.Context) error { | ||
generation, _ := v.getGenerationWhileLocked() | ||
|
||
db := v.verificationDatabase() | ||
|
||
result, err := db.Collection(generationCollName).ReplaceOne( | ||
ctx, | ||
bson.D{}, | ||
bson.D{{generationFieldName, generation}}, | ||
options.Replace().SetUpsert(true), | ||
) | ||
|
||
if err == nil && (result.ModifiedCount+result.UpsertedCount != 1) { | ||
panic(fmt.Sprintf("persist of generation (%d) should affect exactly 1 doc! (%+v)", generation, result)) | ||
} | ||
|
||
return err | ||
} | ||
|
||
func (v *Verifier) readGeneration(ctx context.Context) (option.Option[int], error) { | ||
db := v.verificationDatabase() | ||
|
||
result := db.Collection(generationCollName).FindOne( | ||
ctx, | ||
bson.D{}, | ||
) | ||
|
||
parsed := struct { | ||
Generation int `bson:"generation"` | ||
}{} | ||
|
||
err := result.Decode(&parsed) | ||
|
||
if err != nil { | ||
if errors.Is(err, mongo.ErrNoDocuments) { | ||
err = nil | ||
} else { | ||
err = errors.Wrap(err, "failed to read persisted generation") | ||
} | ||
|
||
return option.None[int](), err | ||
} | ||
|
||
return option.Some(parsed.Generation), nil | ||
} |
This file contains 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