-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PBM-1223] capture oldest active transaction timestamp
- Loading branch information
Showing
2 changed files
with
73 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package oplog | ||
|
||
import ( | ||
"context" | ||
|
||
"go.mongodb.org/mongo-driver/bson" | ||
"go.mongodb.org/mongo-driver/bson/primitive" | ||
"go.mongodb.org/mongo-driver/mongo" | ||
"go.mongodb.org/mongo-driver/mongo/options" | ||
"go.mongodb.org/mongo-driver/mongo/readconcern" | ||
|
||
"github.com/percona/percona-backup-mongodb/pbm/errors" | ||
) | ||
|
||
var errNoTransaction = errors.New("no transaction found") | ||
|
||
func GetOplogStartTime(ctx context.Context, m *mongo.Client) (primitive.Timestamp, error) { | ||
ts, err := findTransactionStartTime(ctx, m) | ||
if errors.Is(err, errNoTransaction) { | ||
return findLastOplogTS(ctx, m) | ||
} | ||
|
||
return ts, nil | ||
} | ||
|
||
func findTransactionStartTime(ctx context.Context, m *mongo.Client) (primitive.Timestamp, error) { | ||
coll := m.Database("config").Collection("transactions", options.Collection().SetReadConcern(readconcern.Local())) | ||
f := bson.D{{"state", bson.D{{"$in", bson.A{"prepared", "inProgress"}}}}} | ||
o := options.FindOne().SetSort(bson.D{{"startOpTime", 1}}) | ||
doc, err := coll.FindOne(ctx, f, o).Raw() | ||
if err != nil { | ||
if errors.Is(err, mongo.ErrNoDocuments) { | ||
return primitive.Timestamp{}, errNoTransaction | ||
} | ||
return primitive.Timestamp{}, errors.Wrap(err, "query transactions") | ||
} | ||
|
||
rawTS, err := doc.LookupErr("startOpTime", "ts") | ||
if err != nil { | ||
return primitive.Timestamp{}, errors.Wrap(err, "lookup timestamp") | ||
} | ||
|
||
t, i, ok := rawTS.TimestampOK() | ||
if !ok { | ||
return primitive.Timestamp{}, errors.Wrap(err, "parse timestamp") | ||
} | ||
|
||
return primitive.Timestamp{T: t, I: i}, nil | ||
} | ||
|
||
func findLastOplogTS(ctx context.Context, m *mongo.Client) (primitive.Timestamp, error) { | ||
coll := m.Database("local").Collection("oplog.rs") | ||
o := options.FindOne().SetSort(bson.M{"$natural": -1}) | ||
doc, err := coll.FindOne(ctx, bson.D{}, o).Raw() | ||
if err != nil { | ||
return primitive.Timestamp{}, errors.Wrap(err, "query oplog") | ||
} | ||
|
||
rawTS, err := doc.LookupErr("ts") | ||
if err != nil { | ||
return primitive.Timestamp{}, errors.Wrap(err, "lookup oplog ts") | ||
} | ||
|
||
t, i, ok := rawTS.TimestampOK() | ||
if !ok { | ||
return primitive.Timestamp{}, errors.Wrap(err, "parse oplog ts") | ||
} | ||
|
||
return primitive.Timestamp{T: t, I: i}, nil | ||
} |