Skip to content

Commit 67b48f5

Browse files
committed
Support for server hedged reads
JAVA-3666
1 parent f1b76d7 commit 67b48f5

File tree

6 files changed

+503
-28
lines changed

6 files changed

+503
-28
lines changed

driver-core/src/main/com/mongodb/ReadPreference.java

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,59 @@ public abstract class ReadPreference {
4848
ReadPreference() {
4949
}
5050

51+
/**
52+
* Create a new ReadPreference instance with a new tag set.
53+
* <p>
54+
* Note: this method is not supported for a primary read preference.
55+
* </p>
56+
*
57+
* @param tagSet the new tag set
58+
* @return a new ReadPreference instance with a new tag set
59+
* @since 4.1
60+
*/
61+
public abstract ReadPreference withTagSet(TagSet tagSet);
62+
63+
/**
64+
* Create a new ReadPreference instance with a new tag set list.
65+
* <p>
66+
* Note: this method is not supported for a primary read preference.
67+
* </p>
68+
*
69+
* @param tagSet the new tag set list
70+
* @return a new ReadPreference instance with a new tag set list
71+
* @since 4.1
72+
*/
73+
public abstract ReadPreference withTagSetList(List<TagSet> tagSet);
74+
75+
/**
76+
* Create a new ReadPreference instance with the maximum acceptable staleness of a secondary in order to be considered for
77+
* read operations.
78+
* <p>
79+
* Note: this method is not supported for a primary read preference.
80+
* </p>
81+
*
82+
* @param maxStalenessMS the max allowable staleness of secondaries. The minimum value is either 90 seconds, or the heartbeat frequency
83+
* plus 10 seconds, whichever is greatest.
84+
* @param timeUnit the time unit of maxStaleness
85+
* @return a new ReadPreference instance with a new maximum allowable staleness
86+
* @since 4.1
87+
* @mongodb.server.release 3.4
88+
*/
89+
public abstract ReadPreference withMaxStalenessMS(Long maxStalenessMS, TimeUnit timeUnit);
90+
91+
/**
92+
* Create a new ReadPreference instance with hedge options.
93+
* <p>
94+
* Note: this method is not supported for a primary read preference.
95+
* </p>
96+
*
97+
* @param hedgeOptions the hedge options
98+
* @return a new ReadPreference instance with hedge options
99+
* @since 4.1
100+
* @mongodb.server.release 4.4
101+
*/
102+
public abstract ReadPreference withHedgeOptions(ReadPreferenceHedgeOptions hedgeOptions);
103+
51104
/**
52105
* True if this read preference allows reading from a secondary member of a replica set.
53106
*
@@ -573,7 +626,7 @@ private static TaggableReadPreference valueOf(final String name, final List<TagS
573626
String nameToCheck = name.toLowerCase();
574627

575628
if (nameToCheck.equals(PRIMARY.getName().toLowerCase())) {
576-
throw new IllegalArgumentException("Primary read preference can not also specify tag sets or max staleness");
629+
throw new IllegalArgumentException("Primary read preference can not also specify tag sets, max staleness or hedge");
577630
}
578631

579632
if (nameToCheck.equals(SECONDARY.getName().toLowerCase())) {
@@ -599,6 +652,26 @@ private static final class PrimaryReadPreference extends ReadPreference {
599652
private PrimaryReadPreference() {
600653
}
601654

655+
@Override
656+
public ReadPreference withTagSet(final TagSet tagSet) {
657+
throw new UnsupportedOperationException("Primary read preference can not also specify tag sets");
658+
}
659+
660+
@Override
661+
public TaggableReadPreference withTagSetList(final List<TagSet> tagSet) {
662+
throw new UnsupportedOperationException("Primary read preference can not also specify tag sets");
663+
}
664+
665+
@Override
666+
public TaggableReadPreference withMaxStalenessMS(final Long maxStalenessMS, final TimeUnit timeUnit) {
667+
throw new UnsupportedOperationException("Primary read preference can not also specify max staleness");
668+
}
669+
670+
@Override
671+
public TaggableReadPreference withHedgeOptions(final ReadPreferenceHedgeOptions hedgeOptions) {
672+
throw new UnsupportedOperationException("Primary read preference can not also specify hedge");
673+
}
674+
602675
@Override
603676
public boolean isSlaveOk() {
604677
return false;
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
* Copyright 2008-present MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.mongodb;
18+
19+
import com.mongodb.annotations.Immutable;
20+
import org.bson.BsonBoolean;
21+
import org.bson.BsonDocument;
22+
23+
/**
24+
* Options to apply to hedged reads in the server.
25+
*
26+
* @since 4.1
27+
* @mongodb.server.release 4.4
28+
*/
29+
@Immutable
30+
public final class ReadPreferenceHedgeOptions {
31+
private final boolean enabled;
32+
33+
/**
34+
* Gets whether hedged reads are enabled in the server.
35+
*
36+
* @return true if hedged reads are enabled in the server
37+
*/
38+
public boolean isEnabled() {
39+
return enabled;
40+
}
41+
42+
/**
43+
* Gets an instance of a builder
44+
*
45+
* @return a builder instance
46+
*/
47+
public static Builder builder() {
48+
return new Builder();
49+
}
50+
51+
/**
52+
* Convert the hedge options to a BsonDocument.
53+
*
54+
* @return a BsonDocument containing the hedge options
55+
*/
56+
public BsonDocument toBsonDocument() {
57+
return new BsonDocument("enabled", new BsonBoolean(enabled));
58+
}
59+
60+
@Override
61+
public boolean equals(final Object o) {
62+
if (this == o) {
63+
return true;
64+
}
65+
if (o == null || getClass() != o.getClass()) {
66+
return false;
67+
}
68+
69+
ReadPreferenceHedgeOptions that = (ReadPreferenceHedgeOptions) o;
70+
71+
return enabled == that.enabled;
72+
}
73+
74+
@Override
75+
public int hashCode() {
76+
return enabled ? 1 : 0;
77+
}
78+
79+
@Override
80+
public String toString() {
81+
return "ReadPreferenceHedgeOptions{"
82+
+ "enabled=" + enabled
83+
+ '}';
84+
}
85+
86+
/**
87+
* The builder for read preference hedge options
88+
*/
89+
public static final class Builder {
90+
private boolean enabled;
91+
92+
/**
93+
* Sets whether hedged reads are enabled in the server.
94+
*
95+
* @param enabled true if hedged reads are enabled
96+
* @return this
97+
*/
98+
public Builder enabled(final boolean enabled) {
99+
this.enabled = enabled;
100+
return this;
101+
}
102+
103+
/**
104+
* Build the transaction options instance.
105+
*
106+
* @return The {@code TransactionOptions}
107+
*/
108+
public ReadPreferenceHedgeOptions build() {
109+
return new ReadPreferenceHedgeOptions(this);
110+
}
111+
112+
private Builder() {
113+
}
114+
}
115+
116+
117+
private ReadPreferenceHedgeOptions(final Builder builder) {
118+
enabled = builder.enabled;
119+
}
120+
}

0 commit comments

Comments
 (0)