Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust maxAwaitTimeMS by remaining timeout. #1650

Merged
merged 8 commits into from
Mar 25, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion driver-core/src/main/com/mongodb/internal/TimeoutContext.java
Original file line number Diff line number Diff line change
@@ -192,7 +192,10 @@ public long getMaxAwaitTimeMS() {

public void runMaxTimeMS(final LongConsumer onRemaining) {
if (maxTimeSupplier != null) {
runWithFixedTimeout(maxTimeSupplier.get(), onRemaining);
long maxTimeMS = maxTimeSupplier.get();
if (maxTimeMS > 0) {
runMinTimeout(onRemaining, maxTimeMS);
}
return;
}
if (timeout == null) {
@@ -209,6 +212,22 @@ public void runMaxTimeMS(final LongConsumer onRemaining) {

}

private void runMinTimeout(final LongConsumer onRemaining, final long fixedMs) {
Timeout timeout = timeoutIncludingRoundTrip();
if (timeout != null) {
timeout.run(MILLISECONDS, () -> {
onRemaining.accept(fixedMs);
},
(renamingMs) -> {
onRemaining.accept(Math.min(renamingMs, fixedMs));
}, () -> {
throwMongoTimeoutException("The operation exceeded the timeout limit.");
});
} else {
onRemaining.accept(fixedMs);
}
}

private static void runWithFixedTimeout(final long ms, final LongConsumer onRemaining) {
if (ms != 0) {
onRemaining.accept(ms);
@@ -227,10 +246,14 @@ public void resetToDefaultMaxTime() {
* <p>
* NOTE: Suitable for static user-defined values only (i.e MaxAwaitTimeMS),
* not for running timeouts that adjust dynamically (CSOT).
*
* If remaining CSOT timeout is less than this static timeout, then CSOT timeout will be used.
*
*/
public void setMaxTimeOverride(final long maxTimeMS) {
this.maxTimeSupplier = () -> maxTimeMS;
}

/**
* Disable the maxTimeMS override. This way the maxTimeMS will not
* be appended to the command in the {@link CommandMessage}.
Original file line number Diff line number Diff line change
@@ -4,7 +4,8 @@
"schemaVersion": "1.9",
"runOnRequirements": [
{
"minServerVersion": "4.4"
"minServerVersion": "4.4",
"serverless": "forbid"
}
],
"createEntities": [
@@ -419,6 +420,141 @@
]
}
]
},
{
"description": "apply remaining timeoutMS if less than maxAwaitTimeMS",
"operations": [
{
"name": "failPoint",
"object": "testRunner",
"arguments": {
"client": "failPointClient",
"failPoint": {
"configureFailPoint": "failCommand",
"mode": {
"times": 1
},
"data": {
"failCommands": [
"getMore"
],
"blockConnection": true,
"blockTimeMS": 30
}
}
}
},
{
"name": "createFindCursor",
"object": "collection",
"arguments": {
"filter": {
"_id": 1
},
"cursorType": "tailableAwait",
"batchSize": 1,
"maxAwaitTimeMS": 100,
"timeoutMS": 200
},
"saveResultAsEntity": "tailableCursor"
},
{
"name": "iterateOnce",
"object": "tailableCursor"
},
{
"name": "iterateUntilDocumentOrError",
"object": "tailableCursor",
"expectError": {
"isTimeoutError": true
}
}
],
"expectEvents": [
{
"client": "client",
"ignoreExtraEvents": true,
"events": [
{
"commandStartedEvent": {
"commandName": "find",
"databaseName": "test"
}
},
{
"commandStartedEvent": {
"commandName": "getMore",
"databaseName": "test",
"command": {
"maxTimeMS": {
"$$lte": 100
}
}
}
},
{
"commandStartedEvent": {
"commandName": "getMore",
"databaseName": "test",
"command": {
"maxTimeMS": {
"$$lte": 70
}
}
}
}
]
}
]
},
Comment on lines +424 to +509
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The failpoint is used here to delay the first getMore by 30ms, reducing raciness and allowing verification that the second getMore occurs with maxTimeMs being less than 70ms.

{
"description": "apply maxAwaitTimeMS if less than remaining timeout",
"operations": [
{
"name": "createFindCursor",
"object": "collection",
"arguments": {
"filter": {},
"cursorType": "tailableAwait",
"batchSize": 1,
"maxAwaitTimeMS": 100,
"timeoutMS": 200
},
"saveResultAsEntity": "tailableCursor"
},
{
"name": "iterateOnce",
"object": "tailableCursor"
},
{
"name": "iterateOnce",
"object": "tailableCursor"
}
],
"expectEvents": [
{
"client": "client",
"events": [
{
"commandStartedEvent": {
"commandName": "find",
"databaseName": "test"
}
},
{
"commandStartedEvent": {
"commandName": "getMore",
"databaseName": "test",
"command": {
"maxTimeMS": {
"$$lte": 100
}
}
}
}
]
}
]
}
]
}
}
Original file line number Diff line number Diff line change
@@ -65,6 +65,10 @@ public static void applyCustomizations(final TestDef def) {

// client-side-operation-timeout (CSOT)

def.skipNoncompliantReactive("No good way to fulfill tryNext() requirement with a Publisher<T>")
.directory("client-side-operation-timeout")
.test("timeoutMS behaves correctly for tailable awaitData cursors", "apply remaining timeoutMS if less than maxAwaitTimeMS");

// TODO-JAVA-5712

// collection-management