Skip to content

Added ClientSideOperationTimeout class #1171

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 6 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ configure(scalaProjects) {
"-unchecked",
"-language:reflectiveCalls",
"-Wconf:cat=deprecation:ws,any:e",
"-Wconf:msg=While parsing annotations in:silent",
"-Xlint:strict-unsealed-patmat"
]
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/*
* Copyright 2008-present MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mongodb.internal;

import com.mongodb.lang.Nullable;

import java.util.Objects;

import static com.mongodb.assertions.Assertions.assertNotNull;
import static com.mongodb.assertions.Assertions.isTrueArgument;
import static java.util.concurrent.TimeUnit.MILLISECONDS;

/**
* Client Side Operation Timeout.
*
* <p>Includes support for the deprecated {@code maxTimeMS} and {@code maxCommitTimeMS} operation configurations</p>
*/
public class ClientSideOperationTimeout {

private final Long timeoutMS;
private final long maxAwaitTimeMS;

// Deprecated operation based operation timeouts
private final long maxTimeMS;
private final long maxCommitTimeMS;

@Nullable
private Timeout timeout;

public ClientSideOperationTimeout(@Nullable final Long timeoutMS,
final long maxAwaitTimeMS,
final long maxTimeMS,
final long maxCommitTimeMS) {
isTrueArgument("timeoutMS must be >= 0", timeoutMS == null || timeoutMS >= 0);
this.timeoutMS = timeoutMS;
this.maxAwaitTimeMS = maxAwaitTimeMS;
this.maxTimeMS = timeoutMS == null ? maxTimeMS : 0;
this.maxCommitTimeMS = timeoutMS == null ? maxCommitTimeMS : 0;

if (timeoutMS != null) {
if (timeoutMS == 0) {
this.timeout = Timeout.infinite();
} else {
this.timeout = Timeout.startNow(timeoutMS, MILLISECONDS);
}
}
}

/**
* Allows for the differentiation between users explicitly setting a global operation timeout via {@code timeoutMS}.
*
* @return true if a timeout has been set.
*/
public boolean hasTimeoutMS() {
return timeoutMS != null;
}

/**
* Checks the expiry of the timeout.
*
* @return true if the timeout has been set and it has expired
*/
public boolean expired() {
return timeout != null && timeout.expired();
}

/**
* Returns the remaining {@code timeoutMS} if set or the {@code alternativeTimeoutMS}.
*
* @param alternativeTimeoutMS the alternative timeout.
* @return timeout to use.
*/
public long timeoutOrAlternative(final long alternativeTimeoutMS) {
if (timeoutMS == null) {
return alternativeTimeoutMS;
} else if (timeoutMS == 0) {
return timeoutMS;
} else {
return timeoutRemainingMS();
}
}

/**
* Calculates the minimum timeout value between two possible timeouts.
*
* @param alternativeTimeoutMS the alternative timeout
* @return the minimum value to use.
*/
public long calculateMin(final long alternativeTimeoutMS) {
if (timeoutMS == null) {
return alternativeTimeoutMS;
} else if (timeoutMS == 0) {
return alternativeTimeoutMS;
} else if (alternativeTimeoutMS == 0) {
return timeoutRemainingMS();
} else {
return Math.min(timeoutRemainingMS(), alternativeTimeoutMS);
}
}

public long getMaxAwaitTimeMS() {
return maxAwaitTimeMS;
}

public long getMaxTimeMS() {
return timeoutOrAlternative(maxTimeMS);
}

public long getMaxCommitTimeMS() {
return timeoutOrAlternative(maxCommitTimeMS);
}

private long timeoutRemainingMS() {
assertNotNull(timeout);
return timeout.isInfinite() ? 0 : timeout.remaining(MILLISECONDS);
}

@Override
public String toString() {
return "ClientSideOperationTimeout{"
+ "timeoutMS=" + timeoutMS
+ ", maxAwaitTimeMS=" + maxAwaitTimeMS
+ ", maxTimeMS=" + maxTimeMS
+ ", maxCommitTimeMS=" + maxCommitTimeMS
+ '}';
}

@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final ClientSideOperationTimeout that = (ClientSideOperationTimeout) o;
return maxAwaitTimeMS == that.maxAwaitTimeMS && maxTimeMS == that.maxTimeMS && maxCommitTimeMS == that.maxCommitTimeMS
&& Objects.equals(timeoutMS, that.timeoutMS);
}

@Override
public int hashCode() {
return Objects.hash(timeoutMS, maxAwaitTimeMS, maxTimeMS, maxCommitTimeMS);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2008-present MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mongodb.internal;

import com.mongodb.lang.Nullable;

/**
* A factory for creating {@link ClientSideOperationTimeout} instances
*/
public final class ClientSideOperationTimeouts {

public static final ClientSideOperationTimeout NO_TIMEOUT = create(null, 0, 0, 0);

public static ClientSideOperationTimeout create(@Nullable final Long timeoutMS) {
return create(timeoutMS, 0);
}

public static ClientSideOperationTimeout create(@Nullable final Long timeoutMS, final long maxTimeMS) {
return create(timeoutMS, maxTimeMS, 0);
}

public static ClientSideOperationTimeout create(@Nullable final Long timeoutMS,
final long maxTimeMS,
final long maxAwaitTimeMS) {
return new ClientSideOperationTimeout(timeoutMS, maxAwaitTimeMS, maxTimeMS, 0);
}

public static ClientSideOperationTimeout create(@Nullable final Long timeoutMS,
final long maxTimeMS,
final long maxAwaitTimeMS,
final long maxCommitMS) {
return new ClientSideOperationTimeout(timeoutMS, maxAwaitTimeMS, maxTimeMS, maxCommitMS);
}

public static ClientSideOperationTimeout withMaxCommitMS(@Nullable final Long timeoutMS,
@Nullable final Long maxCommitMS) {
return create(timeoutMS, 0, 0, maxCommitMS != null ? maxCommitMS : 0);
}

private ClientSideOperationTimeouts() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.mongodb.Function;
import com.mongodb.WriteConcern;
import com.mongodb.internal.ClientSideOperationTimeout;
import com.mongodb.lang.Nullable;
import org.bson.BsonDocument;

Expand All @@ -31,8 +32,8 @@
public class AbortTransactionOperation extends TransactionOperation {
private BsonDocument recoveryToken;

public AbortTransactionOperation(final WriteConcern writeConcern) {
super(writeConcern);
public AbortTransactionOperation(final ClientSideOperationTimeout clientSideOperationTimeout, final WriteConcern writeConcern) {
super(clientSideOperationTimeout, writeConcern);
}

public AbortTransactionOperation recoveryToken(@Nullable final BsonDocument recoveryToken) {
Expand All @@ -49,7 +50,9 @@ protected String getCommandName() {
CommandCreator getCommandCreator() {
CommandCreator creator = super.getCommandCreator();
if (recoveryToken != null) {
return (serverDescription, connectionDescription) -> creator.create(serverDescription, connectionDescription).append("recoveryToken", recoveryToken);
return (clientSideOperationTimeout, serverDescription, connectionDescription) ->
creator.create(clientSideOperationTimeout, serverDescription, connectionDescription)
.append("recoveryToken", recoveryToken);
}
return creator;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.mongodb.ExplainVerbosity;
import com.mongodb.MongoNamespace;
import com.mongodb.client.model.Collation;
import com.mongodb.internal.ClientSideOperationTimeout;
import com.mongodb.internal.async.AsyncBatchCursor;
import com.mongodb.internal.async.SingleResultCallback;
import com.mongodb.internal.binding.AsyncReadBinding;
Expand All @@ -31,7 +32,6 @@
import org.bson.codecs.Decoder;

import java.util.List;
import java.util.concurrent.TimeUnit;

import static com.mongodb.internal.operation.ExplainHelper.asExplainCommand;
import static com.mongodb.internal.operation.ServerVersionHelper.MIN_WIRE_VERSION;
Expand All @@ -44,13 +44,14 @@
public class AggregateOperation<T> implements AsyncExplainableReadOperation<AsyncBatchCursor<T>>, ExplainableReadOperation<BatchCursor<T>> {
private final AggregateOperationImpl<T> wrapped;

public AggregateOperation(final MongoNamespace namespace, final List<BsonDocument> pipeline, final Decoder<T> decoder) {
this(namespace, pipeline, decoder, AggregationLevel.COLLECTION);
public AggregateOperation(final ClientSideOperationTimeout clientSideOperationTimeout, final MongoNamespace namespace,
final List<BsonDocument> pipeline, final Decoder<T> decoder) {
this(clientSideOperationTimeout, namespace, pipeline, decoder, AggregationLevel.COLLECTION);
}

public AggregateOperation(final MongoNamespace namespace, final List<BsonDocument> pipeline, final Decoder<T> decoder,
final AggregationLevel aggregationLevel) {
this.wrapped = new AggregateOperationImpl<>(namespace, pipeline, decoder, aggregationLevel);
public AggregateOperation(final ClientSideOperationTimeout clientSideOperationTimeout, final MongoNamespace namespace,
final List<BsonDocument> pipeline, final Decoder<T> decoder, final AggregationLevel aggregationLevel) {
this.wrapped = new AggregateOperationImpl<>(clientSideOperationTimeout, namespace, pipeline, decoder, aggregationLevel);
}

public List<BsonDocument> getPipeline() {
Expand All @@ -75,24 +76,6 @@ public AggregateOperation<T> batchSize(@Nullable final Integer batchSize) {
return this;
}

public long getMaxAwaitTime(final TimeUnit timeUnit) {
return wrapped.getMaxAwaitTime(timeUnit);
}

public AggregateOperation<T> maxAwaitTime(final long maxAwaitTime, final TimeUnit timeUnit) {
wrapped.maxAwaitTime(maxAwaitTime, timeUnit);
return this;
}

public long getMaxTime(final TimeUnit timeUnit) {
return wrapped.getMaxTime(timeUnit);
}

public AggregateOperation<T> maxTime(final long maxTime, final TimeUnit timeUnit) {
wrapped.maxTime(maxTime, timeUnit);
return this;
}

public Collation getCollation() {
return wrapped.getCollation();
}
Expand Down Expand Up @@ -159,24 +142,20 @@ public void executeAsync(final AsyncReadBinding binding, final SingleResultCallb
}

public <R> ReadOperation<R> asExplainableOperation(@Nullable final ExplainVerbosity verbosity, final Decoder<R> resultDecoder) {
return new CommandReadOperation<>(getNamespace().getDatabaseName(),
asExplainCommand(wrapped.getCommand(NoOpSessionContext.INSTANCE, MIN_WIRE_VERSION), verbosity),
resultDecoder);
return new CommandReadOperation<>(wrapped.getClientSideOperationTimeout(), getNamespace().getDatabaseName(),
asExplainCommand(wrapped.getCommand(wrapped.getClientSideOperationTimeout(), NoOpSessionContext.INSTANCE, MIN_WIRE_VERSION),
verbosity), resultDecoder);
}

public <R> AsyncReadOperation<R> asAsyncExplainableOperation(@Nullable final ExplainVerbosity verbosity,
final Decoder<R> resultDecoder) {
return new CommandReadOperation<>(getNamespace().getDatabaseName(),
asExplainCommand(wrapped.getCommand(NoOpSessionContext.INSTANCE, MIN_WIRE_VERSION), verbosity),
resultDecoder);
return new CommandReadOperation<>(wrapped.getClientSideOperationTimeout(), getNamespace().getDatabaseName(),
asExplainCommand(wrapped.getCommand(wrapped.getClientSideOperationTimeout(), NoOpSessionContext.INSTANCE, MIN_WIRE_VERSION),
verbosity), resultDecoder);
}


MongoNamespace getNamespace() {
return wrapped.getNamespace();
}

Decoder<T> getDecoder() {
return wrapped.getDecoder();
}
}
Loading