Skip to content

Remove TimeoutSettings from Operations #1309

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

Merged
merged 3 commits into from
Feb 27, 2024
Merged

Remove TimeoutSettings from Operations #1309

merged 3 commits into from
Feb 27, 2024

Conversation

rozza
Copy link
Member

@rozza rozza commented Feb 13, 2024

Decoupled Operations from TimeoutSettings.

Added OperationExecutor#withTimeoutSettings to pass in timeout settings.

JAVA-5176

The OperationExecutor is now in charge of TimeoutContext.
Paves the way for multiple operations using the same TimeoutContext.

JAVA-5176
Copy link

There is an existing patch(es) for this commit SHA:

Please note that the status that is posted is not in the context of this PR but rather the (latest) existing patch and that may affect some tests that may depend on the particular PR. If your tests do not rely on any PR-specific values (like base or head branch name) then your tests will report the same status. If you would like a patch to run in the context of this PR and abort the other(s), comment 'evergreen retry'.

@rozza rozza requested a review from vbabanin February 13, 2024 15:48
Copy link
Member

@vbabanin vbabanin left a comment

Choose a reason for hiding this comment

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

Looks good overall! I have just a couple of suggestions.

@@ -225,6 +216,7 @@ private CommandReadTransformerAsync<BsonDocument, AsyncBatchCursor<T>> asyncTran

private CommandCreator getCommandCreator() {
return (operationContext, serverDescription, connectionDescription) -> {
validateTimeoutMode(operationContext, timeoutMode);
Copy link
Member

Choose a reason for hiding this comment

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

The current implementation defers validation until a CommandCreator is executed. This approach means we've already selected the server and checked out a connection from the pool before realizing that the operation should not proceed due to invalid inputs. By validating inputs earlier, we can avoid unnecessary overhead.

I propose moving the validation to:

  • The execute/executeAsync method to preemptively reject the execution of operations with invalid inputs.
  • Preferably, in the timeoutMode() method of an Iterable. This placement allows us to inform the user about invalid inputs immediately. Since TimeoutSetting is accessible within an Iterable and timeoutMS cannot be overridden once an Iterable created, we can safely perform the validation here.
    This aligns with our existing validation logic in AggregateIterableImpl.java#L213.

For example:

@Override
public ListIndexesIterable<TResult> timeoutMode(final TimeoutMode timeoutMode) {
    if (getTimeoutSettings().getTimeoutMS() == null) {
        throw new IllegalArgumentException("TimeoutMode requires timeoutMS to be set.");
    }
    super.timeoutMode(timeoutMode);
    return this;
}

This comment also applies to the following commands, as they currently include validateTimeoutMode in their CommandCreators:

  • ListIndexOperations
  • ListCollectionsOperations
  • FindOperation
  • AggregateOperation

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

@@ -81,11 +79,9 @@ public class ListCollectionsOperation<T> implements AsyncReadOperation<AsyncBatc
private boolean nameOnly;
private boolean authorizedCollections;
private BsonValue comment;
private TimeoutMode timeoutMode = TimeoutMode.CURSOR_LIFETIME;
Copy link
Member

@vbabanin vbabanin Feb 18, 2024

Choose a reason for hiding this comment

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

According to the specification, the default value for a non-tailable cursor should be CURSOR_LIFETIME.

Currently, if a user does not specify any timeout mode, a null value will be passed down to CommandBatchCursor.

While using null does not change functionality, to maintain code clarity, I suggest continuing to use TimeoutMode.CURSOR_LIFETIME as the explicit default value in our codebase, as it was before this change.

  • Moving the validateTimeoutMode to a higher level within the Iterable interface, as suggested in this discussion_r1493611453, would allow us to maintain this explicit default value in Operations or even Iterables itself.

  • Alternatively, if validateTimeoutMode remains within Operation, I propose explicitly passing CURSOR_LIFETIME as the default to CommandBatchCursor:

  private CommandReadTransformer<BsonDocument, BatchCursor<T>> transformer() {
        return (result, source, connection) ->
                cursorDocumentToBatchCursor(defaultIfNull(timeoutMode, TimeoutMode.CURSOR_LIFETIME), result, batchSize, decoder, comment, source, connection);
    }

This comment also applies to the following commands:

  • ListIndexOperations
  • ListCollectionsOperations
  • AggregateOperation

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

Copy link
Member Author

@rozza rozza left a comment

Choose a reason for hiding this comment

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

Updated & ready for review - thanks @vbabanin

iterable.batchSize(cur.getValue().asNumber().intValue());
break;
case "timeoutMode":
setTimeoutMode(iterable, cur);
Copy link
Member Author

Choose a reason for hiding this comment

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

Setting the timeout mode can now throw an exception - so running in the resultOf block.

@rozza rozza requested a review from vbabanin February 20, 2024 13:36
@rozza rozza merged commit 3ba95b0 into CSOT Feb 27, 2024
@rozza rozza deleted the JAVA-5176 branch February 27, 2024 10:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants