Skip to content

Commit

Permalink
GODRIVER-3217 Always use the manually-specified maxTimeMS on Find and…
Browse files Browse the repository at this point in the history
… Aggregate.
  • Loading branch information
matthewdale committed May 22, 2024
1 parent 345ea95 commit b798ab9
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 5 deletions.
40 changes: 40 additions & 0 deletions mongo/integration/csot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,46 @@ func TestCSOT(t *testing.T) {
bsoncore.ErrElementNotFound,
"expected maxTimeMS BSON value to be missing, but is present")
})

// TODO(GODRIVER-2944): Remove this test once the "timeoutMode" option is
// supported.
mt.RunOpts("Find always sends manually-specified maxTimeMS", csotOpts, func(mt *mtest.T) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

// Manually set a maxTimeMS on the Find and assert that it's sent with
// the command, even when CSOT is enabled.
cursor, err := mt.Coll.Find(
ctx,
bson.D{},
options.Find().SetMaxTime(5*time.Second))
require.NoError(mt, err, "Find error")
err = cursor.Close(context.Background())
require.NoError(mt, err, "Cursor.Close error")

evt := getStartedEvent(mt, "find")
assertMaxTimeMSIsSet(mt, evt.Command)
})

// TODO(GODRIVER-2944): Remove this test once the "timeoutMode" option is
// supported.
mt.RunOpts("Aggregate always sends manually-specified maxTimeMS", csotOpts, func(mt *mtest.T) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

// Manually set a maxTimeMS on the Aggregate and assert that it's sent with
// the command, even when CSOT is enabled.
cursor, err := mt.Coll.Aggregate(
ctx,
bson.D{},
options.Aggregate().SetMaxTime(5*time.Second))
require.NoError(mt, err, "Aggregate error")
err = cursor.Close(context.Background())
require.NoError(mt, err, "Cursor.Close error")

evt := getStartedEvent(mt, "aggregate")
assertMaxTimeMSIsSet(mt, evt.Command)
})
}

func TestCSOT_errors(t *testing.T) {
Expand Down
16 changes: 11 additions & 5 deletions x/mongo/driver/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -1574,11 +1574,17 @@ func (op Operation) addClusterTime(dst []byte, desc description.SelectedServer)
// operation's MaxTimeMS if set. If no MaxTimeMS is set on the operation, and context is
// not a Timeout context, calculateMaxTimeMS returns 0.
func (op Operation) calculateMaxTimeMS(ctx context.Context, mon RTTMonitor) (uint64, error) {
if csot.IsTimeoutContext(ctx) {
if op.OmitCSOTMaxTimeMS {
return 0, nil
}

// If CSOT is enabled and we're not omitting the CSOT-calculated maxTimeMS
// value, then calculate maxTimeMS.
//
// This allows commands that do not currently send CSOT-calculated maxTimeMS
// (e.g. Find and Aggregate) to still use a manually-provided maxTimeMS
// value.
//
// TODO(GODRIVER-2944): Remove or refactor this logic when we add the
// "timeoutMode" option, which will allow users to opt-in to the
// CSOT-calculated maxTimeMS values if that's the behavior they want.
if csot.IsTimeoutContext(ctx) && !op.OmitCSOTMaxTimeMS {
if deadline, ok := ctx.Deadline(); ok {
remainingTimeout := time.Until(deadline)
rtt90 := mon.P90()
Expand Down

0 comments on commit b798ab9

Please sign in to comment.