Skip to content

Commit b1ec094

Browse files
committed
When selecting a server that satisifies a read preference, select any ok server when cluster connection mode is SINGLE.
JAVA-1767
1 parent 93be1cf commit b1ec094

File tree

3 files changed

+80
-59
lines changed

3 files changed

+80
-59
lines changed

driver-core/src/main/com/mongodb/selector/ReadPreferenceServerSelector.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2008-2014 MongoDB, Inc.
2+
* Copyright 2008-2015 MongoDB, Inc.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
1717
package com.mongodb.selector;
1818

1919
import com.mongodb.ReadPreference;
20+
import com.mongodb.connection.ClusterConnectionMode;
2021
import com.mongodb.connection.ClusterDescription;
2122
import com.mongodb.connection.ServerDescription;
2223

@@ -52,6 +53,9 @@ public ReadPreference getReadPreference() {
5253

5354
@Override
5455
public List<ServerDescription> select(final ClusterDescription clusterDescription) {
56+
if (clusterDescription.getConnectionMode() == ClusterConnectionMode.SINGLE) {
57+
return clusterDescription.getAny();
58+
}
5559
return readPreference.choose(clusterDescription);
5660
}
5761

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright 2015 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+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
15+
package com.mongodb.selector
16+
17+
import com.mongodb.ServerAddress
18+
import com.mongodb.connection.ClusterDescription
19+
import com.mongodb.connection.ServerDescription
20+
import com.mongodb.connection.ServerType
21+
import spock.lang.Specification
22+
23+
import static com.mongodb.ReadPreference.primary
24+
import static com.mongodb.ReadPreference.secondary
25+
import static com.mongodb.connection.ClusterConnectionMode.MULTIPLE
26+
import static com.mongodb.connection.ClusterConnectionMode.SINGLE
27+
import static com.mongodb.connection.ClusterType.REPLICA_SET
28+
import static com.mongodb.connection.ServerConnectionState.CONNECTED
29+
30+
class ReadPreferenceServerSelectorSpecification extends Specification {
31+
32+
def primary = ServerDescription.builder()
33+
.state(CONNECTED)
34+
.address(new ServerAddress())
35+
.ok(true)
36+
.type(ServerType.REPLICA_SET_PRIMARY)
37+
.build();
38+
def secondary = ServerDescription.builder()
39+
.state(CONNECTED)
40+
.address(new ServerAddress('localhost', 27018))
41+
.ok(true)
42+
.type(ServerType.REPLICA_SET_SECONDARY)
43+
.build();
44+
45+
def 'constructor should throws if read preference is null'() {
46+
when:
47+
new ReadPreferenceServerSelector(null)
48+
49+
then:
50+
thrown(IllegalArgumentException)
51+
}
52+
53+
def 'should get read preference'() {
54+
expect:
55+
new ReadPreferenceServerSelector(primary()).readPreference == primary()
56+
}
57+
58+
def 'should override toString'() {
59+
expect:
60+
new ReadPreferenceServerSelector(primary()).toString() == 'ReadPreferenceServerSelector{readPreference=primary}'
61+
}
62+
63+
def 'should select server that matches read preference when connection mode is multiple'() {
64+
expect:
65+
new ReadPreferenceServerSelector(primary()).select(new ClusterDescription(MULTIPLE, REPLICA_SET, [primary, secondary])) ==
66+
[primary]
67+
new ReadPreferenceServerSelector(secondary()).select(new ClusterDescription(MULTIPLE, REPLICA_SET, [primary, secondary])) ==
68+
[secondary]
69+
}
70+
71+
def 'should select any ok server when connection mode is single'() {
72+
expect:
73+
new ReadPreferenceServerSelector(primary()).select(new ClusterDescription(SINGLE, REPLICA_SET, [secondary])) == [secondary]
74+
}
75+
}

driver-core/src/test/unit/com/mongodb/selector/ReadPreferenceServerSelectorTest.java

Lines changed: 0 additions & 58 deletions
This file was deleted.

0 commit comments

Comments
 (0)