Skip to content

Commit 648cdce

Browse files
committed
Ensured that ServerAddressSelector.choose returns either a List with a single ok ServerDescription, or else an empty list.
Ensured that an attempt to invalidate a server after an error doesn't fail if that server has already been removed from the cluster description. JAVA-1634
1 parent 9a0e591 commit 648cdce

File tree

5 files changed

+64
-3
lines changed

5 files changed

+64
-3
lines changed

src/main/com/mongodb/ClusterDescription.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public Set<ServerDescription> getAll() {
8686

8787
public ServerDescription getByServerAddress(final ServerAddress serverAddress) {
8888
for (ServerDescription cur : getAll()) {
89-
if (cur.getAddress().equals(serverAddress)) {
89+
if (cur.isOk() && cur.getAddress().equals(serverAddress)) {
9090
return cur;
9191
}
9292
}

src/main/com/mongodb/DBTCPConnector.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import static com.mongodb.ClusterType.Unknown;
3131
import static com.mongodb.MongoAuthority.Type.Set;
3232
import static java.util.concurrent.TimeUnit.MILLISECONDS;
33+
import static java.util.concurrent.TimeUnit.NANOSECONDS;
3334
import static org.bson.util.Assertions.isTrue;
3435

3536
/**
@@ -479,7 +480,12 @@ void done( DBPort port ) {
479480
*/
480481
void error( DBPort port , Exception e ){
481482
if (!(e instanceof InterruptedIOException)) {
482-
getServer(new ServerAddressSelector(port.getAddress())).invalidate();
483+
try {
484+
// no need to wait if the server is no longer available
485+
cluster.getServer(new ServerAddressSelector(port.getAddress()), 1, NANOSECONDS).invalidate();
486+
} catch (MongoTimeoutException timeoutException) {
487+
// ignore this if the server is already no longer available
488+
}
483489
}
484490
port.close();
485491
pinnedRequestStatusThreadLocal.remove();

src/main/com/mongodb/ServerAddressSelector.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.mongodb;
1818

1919
import java.util.Arrays;
20+
import java.util.Collections;
2021
import java.util.List;
2122

2223
class ServerAddressSelector implements ServerSelector {
@@ -28,6 +29,10 @@ public ServerAddressSelector(final ServerAddress address) {
2829

2930
@Override
3031
public List<ServerDescription> choose(final ClusterDescription clusterDescription) {
31-
return Arrays.asList(clusterDescription.getByServerAddress(address));
32+
ServerDescription serverDescription = clusterDescription.getByServerAddress(address);
33+
if (serverDescription != null) {
34+
return Arrays.asList(serverDescription);
35+
}
36+
return Collections.emptyList();
3237
}
3338
}

src/test/com/mongodb/ClusterDescriptionTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import static java.util.Arrays.asList;
4040
import static org.junit.Assert.assertEquals;
4141
import static org.junit.Assert.assertFalse;
42+
import static org.junit.Assert.assertNull;
4243
import static org.junit.Assert.assertTrue;
4344

4445
public class ClusterDescriptionTest {
@@ -111,6 +112,12 @@ public void testPrimaryOrSecondary() throws UnknownHostException {
111112
new Tag("bar", "2")))));
112113
}
113114

115+
@Test
116+
public void testServerAddress(){
117+
assertEquals(primary, cluster.getByServerAddress(primary.getAddress()));
118+
assertNull(cluster.getByServerAddress(notOkMember.getAddress()));
119+
}
120+
114121
@Test
115122
public void testSortingOfAll() throws UnknownHostException {
116123
ClusterDescription description =
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.mongodb;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
6+
import java.net.UnknownHostException;
7+
import java.util.Collections;
8+
9+
import static com.mongodb.ServerConnectionState.Connected;
10+
import static com.mongodb.ServerDescription.builder;
11+
import static java.util.Arrays.asList;
12+
import static org.junit.Assert.assertEquals;
13+
14+
public class ServerAddressSelectorTest {
15+
16+
private ClusterDescription clusterDescription;
17+
private ServerDescription goodServer;
18+
private ServerDescription badServer;
19+
20+
21+
@Before
22+
public void setUp() throws UnknownHostException {
23+
goodServer = builder().ok(true).state(Connected).address(new ServerAddress("localhost", 27018)).build();
24+
badServer = builder().ok(false).state(Connected).address(new ServerAddress("localhost", 27019)).build();
25+
clusterDescription = new ClusterDescription(ClusterConnectionMode.Multiple, ClusterType.ReplicaSet,
26+
asList(goodServer, badServer));
27+
}
28+
29+
@Test
30+
public void shouldFindServerDescriptionForServerAddress() throws UnknownHostException {
31+
assertEquals(new ServerAddressSelector(new ServerAddress("localhost", 27018)).choose(clusterDescription), asList(goodServer));
32+
}
33+
34+
@Test
35+
public void shouldNotFindServerDescriptionForServerAddressThatIsNotOk() throws UnknownHostException {
36+
assertEquals(new ServerAddressSelector(new ServerAddress("localhost", 27019)).choose(clusterDescription), Collections.emptyList());
37+
}
38+
39+
@Test
40+
public void shouldNotFindServerDescriptionForServerAddressThatIsNotInClusterDescription() throws UnknownHostException {
41+
assertEquals(new ServerAddressSelector(new ServerAddress("localhost", 27017)).choose(clusterDescription), Collections.emptyList());
42+
}
43+
}

0 commit comments

Comments
 (0)