Skip to content

Commit 585d382

Browse files
committed
simplify
1 parent b55cd15 commit 585d382

File tree

2 files changed

+29
-66
lines changed

2 files changed

+29
-66
lines changed

internal/verifier/clustertime.go

Lines changed: 25 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,12 @@ func GetNewClusterTime(
3636
logger,
3737
func(_ *retry.Info) error {
3838
var err error
39-
clusterTime, err = fetchNewClusterTime(ctx, client)
39+
clusterTime, err = runAppendOplogNote(
40+
ctx,
41+
client,
42+
"new ts",
43+
option.None[primitive.Timestamp](),
44+
)
4045
return err
4146
},
4247
)
@@ -53,7 +58,12 @@ func GetNewClusterTime(
5358
logger,
5459
func(_ *retry.Info) error {
5560
var err error
56-
_, err = syncClusterTimeAcrossShards(ctx, client, clusterTime)
61+
_, err = runAppendOplogNote(
62+
ctx,
63+
client,
64+
"sync ts",
65+
option.Some(clusterTime),
66+
)
5767
return err
5868
},
5969
)
@@ -66,66 +76,12 @@ func GetNewClusterTime(
6676
return clusterTime, nil
6777
}
6878

69-
// Use this when we just need the correct cluster time without
70-
// actually changing any shards’ oplogs.
71-
func fetchNewClusterTime(
72-
ctx context.Context,
73-
client *mongo.Client,
74-
) (primitive.Timestamp, error) {
75-
cmd, rawResponse, err := runAppendOplogNote(
76-
ctx,
77-
client,
78-
"expect StaleClusterTime error",
79-
option.None[primitive.Timestamp](),
80-
)
81-
82-
// We expect an error here; if we didn't get one then something is
83-
// amiss on the server.
84-
if err == nil {
85-
return primitive.Timestamp{}, errors.Errorf("server request unexpectedly succeeded: %v", cmd)
86-
}
87-
88-
if !util.IsStaleClusterTimeError(err) {
89-
return primitive.Timestamp{}, errors.Wrap(
90-
err,
91-
"unexpected error (expected StaleClusterTime) from request",
92-
)
93-
}
94-
95-
return getOpTimeFromRawResponse(rawResponse)
96-
}
97-
98-
func syncClusterTimeAcrossShards(
99-
ctx context.Context,
100-
client *mongo.Client,
101-
maxTime primitive.Timestamp,
102-
) (primitive.Timestamp, error) {
103-
_, rawResponse, err := runAppendOplogNote(
104-
ctx,
105-
client,
106-
"syncing cluster time",
107-
option.Some(maxTime),
108-
)
109-
110-
// If any shard’s cluster time >= maxTime, the mongos will return a
111-
// StaleClusterTime error. This particular error doesn’t indicate a
112-
// failure, so we ignore it.
113-
if err != nil && !util.IsStaleClusterTimeError(err) {
114-
return primitive.Timestamp{}, errors.Wrap(
115-
err,
116-
"failed to append note to oplog",
117-
)
118-
}
119-
120-
return getOpTimeFromRawResponse(rawResponse)
121-
}
122-
12379
func runAppendOplogNote(
12480
ctx context.Context,
12581
client *mongo.Client,
12682
note string,
12783
maxClusterTimeOpt option.Option[primitive.Timestamp],
128-
) (bson.D, bson.Raw, error) {
84+
) (primitive.Timestamp, error) {
12985
cmd := bson.D{
13086
{"appendOplogNote", 1},
13187
{"data", bson.D{
@@ -144,13 +100,19 @@ func runAppendOplogNote(
144100
).
145101
RunCommand(ctx, cmd)
146102

147-
raw, err := resp.Raw()
103+
rawResponse, err := resp.Raw()
148104

149-
return cmd, raw, errors.Wrapf(
150-
err,
151-
"command (%v) failed unexpectedly",
152-
cmd,
153-
)
105+
// If any shard’s cluster time >= maxTime, the mongos will return a
106+
// StaleClusterTime error. This particular error doesn’t indicate a
107+
// failure, so we ignore it.
108+
if err != nil && !util.IsStaleClusterTimeError(err) {
109+
return primitive.Timestamp{}, errors.Wrap(
110+
err,
111+
"failed to append note to oplog",
112+
)
113+
}
114+
115+
return getOpTimeFromRawResponse(rawResponse)
154116
}
155117

156118
func getOpTimeFromRawResponse(rawResponse bson.Raw) (primitive.Timestamp, error) {

internal/verifier/clustertime_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ import (
55

66
"go.mongodb.org/mongo-driver/bson"
77
"go.mongodb.org/mongo-driver/bson/primitive"
8+
"go.mongodb.org/mongo-driver/mongo"
89
)
910

10-
func (suite *IntegrationTestSuite) TestGetClusterTime() {
11+
func (suite *IntegrationTestSuite) TestGetNewClusterTime() {
1112
ctx := context.Background()
1213
logger, _ := getLoggerAndWriter("stdout")
1314

@@ -17,9 +18,9 @@ func (suite *IntegrationTestSuite) TestGetClusterTime() {
1718
_, err = suite.srcMongoClient.
1819
Database(suite.DBNameForTest()).
1920
Collection("mycoll").
20-
InsertOne(ctx, bson.D{})
21+
InsertOne(mongo.NewSessionContext(ctx, sess), bson.D{})
2122
clusterTimeVal, err := sess.ClusterTime().LookupErr("$clusterTime", "clusterTime")
22-
suite.Require().NoError(err)
23+
suite.Require().NoError(err, "should extract cluster time from %+v", sess.ClusterTime())
2324

2425
clusterT, clusterI, ok := clusterTimeVal.TimestampOK()
2526
suite.Require().True(ok, "session cluster time (%s: %v) must be a timestamp", clusterTimeVal.Type, clusterTimeVal)

0 commit comments

Comments
 (0)