Skip to content

Commit 9bb0f73

Browse files
committed
JAVA-284: Support replica set name in MongoClientURI and MongoClientOptions
1 parent 851478c commit 9bb0f73

File tree

7 files changed

+80
-3
lines changed

7 files changed

+80
-3
lines changed

src/main/com/mongodb/DBTCPConnector.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import static com.mongodb.ClusterConnectionMode.Single;
3232
import static com.mongodb.ClusterType.ReplicaSet;
3333
import static com.mongodb.ClusterType.Sharded;
34-
import static com.mongodb.MongoAuthority.Type.Direct;
34+
import static com.mongodb.MongoAuthority.Type.Set;
3535
import static java.util.concurrent.TimeUnit.MILLISECONDS;
3636
import static org.bson.util.Assertions.isTrue;
3737

@@ -76,7 +76,9 @@ public void start() {
7676
Clusters.create(clusterId,
7777
ClusterSettings.builder()
7878
.hosts(_mongo.getAuthority().getServerAddresses())
79-
.mode(_mongo.getAuthority().getType() == Direct ? Single : Multiple)
79+
.mode(_mongo.getAuthority().getType() == Set || options.getRequiredReplicaSetName() != null ?
80+
Multiple : Single)
81+
.requiredReplicaSetName(_mongo.getMongoOptions().getRequiredReplicaSetName())
8082
.build(),
8183
ServerSettings.builder()
8284
.heartbeatFrequency(options.heartbeatFrequencyMS, MILLISECONDS)

src/main/com/mongodb/MongoClientOptions.java

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public static class Builder {
6464
private int heartbeatSocketTimeout = Integer.parseInt(System.getProperty("com.mongodb.updaterSocketTimeoutMS", "20000"));
6565
private int heartbeatThreadCount;
6666
private int acceptableLatencyDifference = Integer.parseInt(System.getProperty("com.mongodb.slaveAcceptableLatencyMS", "15"));
67+
private String requiredReplicaSetName;
6768

6869
/**
6970
* Sets the heartbeat frequency.
@@ -453,6 +454,20 @@ public Builder alwaysUseMBeans(final boolean alwaysUseMBeans) {
453454
return this;
454455
}
455456

457+
458+
/**
459+
* Sets the required replica set name for the cluster.
460+
*
461+
* @param requiredReplicaSetName the required replica set name for the replica set.
462+
* @return this
463+
* @see MongoClientOptions#getRequiredReplicaSetName()
464+
* @since 2.12
465+
*/
466+
public Builder requiredReplicaSetName(final String requiredReplicaSetName) {
467+
this.requiredReplicaSetName = requiredReplicaSetName;
468+
return this;
469+
}
470+
456471
/**
457472
* Sets defaults to be what they are in {@code MongoOptions}.
458473
*
@@ -473,7 +488,7 @@ public Builder legacyDefaults() {
473488
public MongoClientOptions build() {
474489
return new MongoClientOptions(this);
475490
}
476-
}
491+
}
477492

478493
/**
479494
* Create a new Builder instance. This is a convenience method, equivalent to {@code new MongoClientOptions.Builder()}.
@@ -801,6 +816,22 @@ public int getAcceptableLatencyDifference() {
801816
return acceptableLatencyDifference;
802817
}
803818

819+
/**
820+
* Gets the required replica set name. With this option set, the MongoClient instance will
821+
* <p> 1. Connect in replica set mode, and discover all members of the set based on the given servers
822+
* </p>
823+
* <p> 2. Make sure that the set name reported by all members matches the required set name.
824+
* </p>
825+
* <p> 3. Refuse to service any requests if any member of the seed list is not part of a replica set with the required name.
826+
* </p>
827+
*
828+
* @return the required replica set name
829+
* @since 2.12
830+
*/
831+
public String getRequiredReplicaSetName() {
832+
return requiredReplicaSetName;
833+
}
834+
804835
@Override
805836
public boolean equals(final Object o) {
806837
if (this == o) {
@@ -887,6 +918,10 @@ public boolean equals(final Object o) {
887918
if (!writeConcern.equals(that.writeConcern)) {
888919
return false;
889920
}
921+
if (requiredReplicaSetName != null ? !requiredReplicaSetName.equals(that.requiredReplicaSetName)
922+
: that.requiredReplicaSetName != null) {
923+
return false;
924+
}
890925

891926
return true;
892927
}
@@ -918,6 +953,7 @@ public int hashCode() {
918953
result = 31 * result + heartbeatSocketTimeout;
919954
result = 31 * result + heartbeatThreadCount;
920955
result = 31 * result + acceptableLatencyDifference;
956+
result = 31 * result + (requiredReplicaSetName != null ? requiredReplicaSetName.hashCode() : 0);
921957
return result;
922958
}
923959

@@ -946,6 +982,7 @@ public String toString() {
946982
+ ", heartbeatSocketTimeout=" + heartbeatSocketTimeout
947983
+ ", heartbeatThreadCount=" + heartbeatThreadCount
948984
+ ", acceptableLatencyDifference=" + acceptableLatencyDifference
985+
+ ", requiredReplicaSetName=" + requiredReplicaSetName
949986
+ '}';
950987
}
951988

@@ -975,6 +1012,7 @@ private MongoClientOptions(final Builder builder) {
9751012
heartbeatSocketTimeout = builder.heartbeatSocketTimeout;
9761013
heartbeatThreadCount = builder.heartbeatThreadCount;
9771014
acceptableLatencyDifference = builder.acceptableLatencyDifference;
1015+
requiredReplicaSetName = builder.requiredReplicaSetName;
9781016
}
9791017

9801018

@@ -1003,4 +1041,5 @@ private MongoClientOptions(final Builder builder) {
10031041
private final int heartbeatSocketTimeout;
10041042
private final int heartbeatThreadCount;
10051043
private final int acceptableLatencyDifference;
1044+
private final String requiredReplicaSetName;
10061045
}

src/main/com/mongodb/MongoClientURI.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ public MongoClientURI(String uri, MongoClientOptions.Builder builder) {
270270
generalOptionsKeys.add("sockettimeoutms");
271271
generalOptionsKeys.add("autoconnectretry");
272272
generalOptionsKeys.add("ssl");
273+
generalOptionsKeys.add("replicaset");
273274

274275
readPreferenceKeys.add("slaveok");
275276
readPreferenceKeys.add("readpreference");
@@ -318,6 +319,8 @@ private MongoClientOptions createOptions(Map<String, List<String>> optionsMap, M
318319
builder.socketTimeout(Integer.parseInt(value));
319320
} else if (key.equals("autoconnectretry")) {
320321
builder.autoConnectRetry(_parseBoolean(value));
322+
} else if (key.equals("replicaset")) {
323+
builder.requiredReplicaSetName(value);
321324
} else if (key.equals("ssl")) {
322325
if (_parseBoolean(value)) {
323326
builder.socketFactory(SSLSocketFactory.getDefault());

src/main/com/mongodb/MongoOptions.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public MongoOptions(final MongoClientOptions options) {
6868
heartbeatReadTimeoutMS = options.getHeartbeatSocketTimeout();
6969
heartbeatThreadCount = options.getHeartbeatThreadCount();
7070
acceptableLatencyDifferenceMS = options.getAcceptableLatencyDifference();
71+
requiredReplicaSetName = options.getRequiredReplicaSetName();
7172
}
7273

7374
public void reset(){
@@ -102,6 +103,7 @@ public void reset(){
102103
heartbeatReadTimeoutMS = Integer.parseInt(System.getProperty("com.mongodb.updaterSocketTimeoutMS", "20000"));
103104
heartbeatThreadCount = 0;
104105
acceptableLatencyDifferenceMS = Integer.parseInt(System.getProperty("com.mongodb.slaveAcceptableLatencyMS", "15"));
106+
requiredReplicaSetName = null;
105107
}
106108

107109
public MongoOptions copy() {
@@ -137,6 +139,7 @@ public MongoOptions copy() {
137139
m.heartbeatReadTimeoutMS = heartbeatReadTimeoutMS;
138140
m.heartbeatThreadCount = heartbeatThreadCount;
139141
m.acceptableLatencyDifferenceMS = acceptableLatencyDifferenceMS;
142+
m.requiredReplicaSetName = requiredReplicaSetName;
140143
return m;
141144
}
142145

@@ -250,6 +253,10 @@ public boolean equals(final Object o) {
250253
if (writeConcern != null ? !writeConcern.equals(options.writeConcern) : options.writeConcern != null) {
251254
return false;
252255
}
256+
if (requiredReplicaSetName != null ? !requiredReplicaSetName.equals(options.requiredReplicaSetName)
257+
: options.requiredReplicaSetName != null) {
258+
return false;
259+
}
253260

254261
return true;
255262
}
@@ -284,6 +291,7 @@ public int hashCode() {
284291
result = 31 * result + heartbeatReadTimeoutMS;
285292
result = 31 * result + acceptableLatencyDifferenceMS;
286293
result = 31 * result + heartbeatThreadCount;
294+
result = 31 * result + (requiredReplicaSetName != null ? requiredReplicaSetName.hashCode() : 0);
287295
return result;
288296
}
289297

@@ -469,6 +477,8 @@ public int hashCode() {
469477
int acceptableLatencyDifferenceMS;
470478
int heartbeatThreadCount;
471479

480+
String requiredReplicaSetName;
481+
472482
/**
473483
* @return The description for <code>MongoClient</code> instances created with these options
474484
*/
@@ -808,6 +818,16 @@ public void setAlwaysUseMBeans(final boolean alwaysUseMBeans) {
808818
this.alwaysUseMBeans = alwaysUseMBeans;
809819
}
810820

821+
/**
822+
* Gets the required replica set name that this client should be connecting to.
823+
*
824+
* @return the required replica set name, or null if none is required
825+
* @since 2.12
826+
*/
827+
public String getRequiredReplicaSetName() {
828+
return requiredReplicaSetName;
829+
}
830+
811831
@Override
812832
public String toString() {
813833
return "MongoOptions{" +
@@ -833,6 +853,7 @@ public String toString() {
833853
", cursorFinalizerEnabled=" + cursorFinalizerEnabled +
834854
", writeConcern=" + writeConcern +
835855
", alwaysUseMBeans=" + alwaysUseMBeans +
856+
", requiredReplicaSetName=" + requiredReplicaSetName +
836857
'}';
837858
}
838859
}

src/test/com/mongodb/MongoClientOptionsTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import static java.lang.System.getProperty;
2727
import static org.junit.Assert.assertEquals;
2828
import static org.junit.Assert.assertNotNull;
29+
import static org.junit.Assert.assertNull;
2930
import static org.junit.Assert.fail;
3031

3132
public class MongoClientOptionsTest {
@@ -60,6 +61,7 @@ public void testBuilderDefaults() {
6061
assertEquals(getProperty("com.mongodb.slaveAcceptableLatencyMS") != null
6162
? parseInt(getProperty("com.mongodb.slaveAcceptableLatencyMS")) : 15,
6263
options.getAcceptableLatencyDifference());
64+
assertNull(options.getRequiredReplicaSetName());
6365
}
6466

6567
@Test
@@ -225,6 +227,7 @@ public void testBuilderBuild() {
225227
builder.heartbeatConnectTimeout(53);
226228
builder.heartbeatSocketTimeout(54);
227229
builder.heartbeatThreadCount(4);
230+
builder.requiredReplicaSetName("test");
228231

229232
SocketFactory socketFactory = SSLSocketFactory.getDefault();
230233
builder.socketFactory(socketFactory);
@@ -269,6 +272,7 @@ public DBDecoder create() {
269272
assertEquals(socketFactory, options.getSocketFactory());
270273
assertEquals(encoderFactory, options.getDbEncoderFactory());
271274
assertEquals(decoderFactory, options.getDbDecoderFactory());
275+
assertEquals("test", options.getRequiredReplicaSetName());
272276
}
273277

274278
@Test

src/test/com/mongodb/MongoClientURITest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,16 +201,19 @@ public void testSSLOption() {
201201
public void testOptions() {
202202
MongoClientURI uAmp = new MongoClientURI("mongodb://localhost/?" +
203203
"maxPoolSize=10&waitQueueMultiple=5&waitQueueTimeoutMS=150&" +
204+
"replicaSet=test&" +
204205
"connectTimeoutMS=2500&socketTimeoutMS=5500&autoConnectRetry=true&" +
205206
"slaveOk=true&safe=false&w=1&wtimeout=2500&fsync=true");
206207
assertOnOptions(uAmp.getOptions());
207208
MongoClientURI uSemi = new MongoClientURI("mongodb://localhost/?" +
208209
"maxPoolSize=10;waitQueueMultiple=5;waitQueueTimeoutMS=150;" +
210+
"replicaSet=test;" +
209211
"connectTimeoutMS=2500;socketTimeoutMS=5500;autoConnectRetry=true;" +
210212
"slaveOk=true;safe=false;w=1;wtimeout=2500;fsync=true");
211213
assertOnOptions(uSemi.getOptions());
212214
MongoClientURI uMixed = new MongoClientURI("mongodb://localhost/test?" +
213215
"maxPoolSize=10&waitQueueMultiple=5;waitQueueTimeoutMS=150;" +
216+
"replicaSet=test;" +
214217
"connectTimeoutMS=2500;socketTimeoutMS=5500&autoConnectRetry=true;" +
215218
"slaveOk=true;safe=false&w=1;wtimeout=2500;fsync=true");
216219
assertOnOptions(uMixed.getOptions());
@@ -310,5 +313,6 @@ private void assertOnOptions(MongoClientOptions options) {
310313
assertTrue(options.isAutoConnectRetry());
311314
assertEquals(new WriteConcern(1, 2500, true), options.getWriteConcern());
312315
assertEquals(ReadPreference.secondaryPreferred(), options.getReadPreference());
316+
assertEquals("test", options.getRequiredReplicaSetName());
313317
}
314318
}

src/test/com/mongodb/MongoOptionsTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public void testCopy() throws Exception {
6161
options.minConnectionsPerHost = 5;
6262
options.maxConnectionIdleTime = 50000;
6363
options.maxConnectionLifeTime = 500000;
64+
options.requiredReplicaSetName = "set1";
6465

6566
final MongoOptions copy = options.copy();
6667
assertEquals(options.connectionsPerHost, copy.connectionsPerHost);
@@ -86,6 +87,7 @@ public void testCopy() throws Exception {
8687
assertEquals(options.minConnectionsPerHost, copy.minConnectionsPerHost);
8788
assertEquals(options.maxConnectionIdleTime, copy.maxConnectionIdleTime);
8889
assertEquals(options.maxConnectionLifeTime, copy.maxConnectionLifeTime);
90+
assertEquals(options.requiredReplicaSetName, copy.requiredReplicaSetName);
8991
}
9092

9193
@Test
@@ -114,6 +116,7 @@ public void testGetterSetters() throws Exception {
114116
options.setReadPreference(ReadPreference.secondary());
115117
options.setCursorFinalizerEnabled(true);
116118
options.setAlwaysUseMBeans(true);
119+
options.requiredReplicaSetName = "set1";
117120

118121
assertEquals(options.getConnectionsPerHost(), 100);
119122
assertEquals(options.getThreadsAllowedToBlockForConnectionMultiplier(), 101);
@@ -135,6 +138,7 @@ public void testGetterSetters() throws Exception {
135138
assertEquals(options.getReadPreference(), ReadPreference.secondary());
136139
assertEquals(options.isCursorFinalizerEnabled(), true);
137140
assertEquals(options.isAlwaysUseMBeans(), true);
141+
assertEquals(options.getRequiredReplicaSetName(), "set1");
138142
}
139143

140144
@Test

0 commit comments

Comments
 (0)