-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
24287b3
Added ClientSideOperationTimeout class
rozza fb84a57
Validate timeout value
rozza 6cbbcce
Fix kdoc/dokka & scalaDoc
rozza 4f2b07f
Fix and test MongoOperationPublisher
rozza 4c994c9
Add CSOT identifiers to do
rozza d792d89
Revert checkstyle parameter count change and suppress warning locally
rozza File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
158 changes: 158 additions & 0 deletions
158
driver-core/src/main/com/mongodb/internal/ClientSideOperationTimeout.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
katcharov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
@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; | ||
katcharov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
driver-core/src/main/com/mongodb/internal/ClientSideOperationTimeouts.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.