Skip to content
Browse files

JAVA-708, JAVA-753: Fixed several read preference bugs which caused q…

…ueries to be sent to secondaries with unacceptable latency and members that are not available for reads (in recovering state)
  • Loading branch information...
1 parent 6ab6478 commit f97eb442eec7332ac34fa74b0b46983743b3a095 @jyemin jyemin committed Jan 7, 2013
View
81 src/main/com/mongodb/ReplicaSetStatus.java
@@ -178,8 +178,8 @@ public String toString() {
static class ReplicaSet {
final List<ReplicaSetNode> all;
final Random random;
- final List<ReplicaSetNode> goodSecondaries;
- final List<ReplicaSetNode> goodMembers;
+ final List<ReplicaSetNode> acceptableSecondaries;
+ final List<ReplicaSetNode> acceptableMembers;
final ReplicaSetNode master;
final String setName;
final ReplicaSetErrorStatus errorStatus;
@@ -195,10 +195,11 @@ public ReplicaSet(List<ReplicaSetNode> nodeList, Random random, int acceptableLa
errorStatus = validate();
setName = determineSetName();
- this.goodSecondaries =
- Collections.unmodifiableList(calculateGoodSecondaries(all, calculateBestPingTime(all), acceptableLatencyMS));
- this.goodMembers =
- Collections.unmodifiableList(calculateGoodMembers(all, calculateBestPingTime(all), acceptableLatencyMS));
+ this.acceptableSecondaries =
+ Collections.unmodifiableList(calculateGoodMembers(
+ all, calculateBestPingTime(all, false), acceptableLatencyMS, false));
+ this.acceptableMembers =
+ Collections.unmodifiableList(calculateGoodMembers(all, calculateBestPingTime(all, true), acceptableLatencyMS, true));
master = findMaster();
}
@@ -229,10 +230,10 @@ public int getMaxBsonObjectSize() {
public ReplicaSetNode getASecondary() {
checkStatus();
- if (goodSecondaries.isEmpty()) {
+ if (acceptableSecondaries.isEmpty()) {
return null;
}
- return goodSecondaries.get(random.nextInt(goodSecondaries.size()));
+ return acceptableSecondaries.get(random.nextInt(acceptableSecondaries.size()));
}
public ReplicaSetNode getASecondary(List<Tag> tags) {
@@ -254,10 +255,10 @@ public ReplicaSetNode getASecondary(List<Tag> tags) {
public ReplicaSetNode getAMember() {
checkStatus();
- if (goodMembers.isEmpty()) {
+ if (acceptableMembers.isEmpty()) {
return null;
}
- return goodMembers.get(random.nextInt(goodMembers.size()));
+ return acceptableMembers.get(random.nextInt(acceptableMembers.size()));
}
public ReplicaSetNode getAMember(List<Tag> tags) {
@@ -274,28 +275,33 @@ public ReplicaSetNode getAMember(List<Tag> tags) {
return acceptableTaggedMembers.get(random.nextInt(acceptableTaggedMembers.size()));
}
+ List<ReplicaSetNode> getGoodSecondaries(List<ReplicaSetNode> all) {
+ List<ReplicaSetNode> goodSecondaries = new ArrayList<ReplicaSetNode>(all.size());
+ for (ReplicaSetNode cur : all) {
+ if (!cur.isOk()) {
+ continue;
+ }
+ goodSecondaries.add(cur);
+ }
+ return goodSecondaries;
+ }
+
public List<ReplicaSetNode> getGoodSecondariesByTags(final List<Tag> tags) {
checkStatus();
List<ReplicaSetNode> taggedSecondaries = getMembersByTags(all, tags);
- return calculateGoodSecondaries(taggedSecondaries,
- calculateBestPingTime(taggedSecondaries), acceptableLatencyMS);
+ return calculateGoodMembers(taggedSecondaries,
+ calculateBestPingTime(taggedSecondaries, false), acceptableLatencyMS, false);
}
public List<ReplicaSetNode> getGoodMembersByTags(final List<Tag> tags) {
checkStatus();
List<ReplicaSetNode> taggedMembers = getMembersByTags(all, tags);
return calculateGoodMembers(taggedMembers,
- calculateBestPingTime(taggedMembers), acceptableLatencyMS);
+ calculateBestPingTime(taggedMembers, true), acceptableLatencyMS, true);
}
-
- public List<ReplicaSetNode> getGoodMembers() {
- checkStatus();
-
- return calculateGoodMembers(all, calculateBestPingTime(all), acceptableLatencyMS);
- }
-
+
public String getSetName() {
checkStatus();
@@ -361,40 +367,25 @@ private ReplicaSetErrorStatus validate() {
}
}
- static float calculateBestPingTime(List<ReplicaSetNode> members) {
+ static float calculateBestPingTime(List<ReplicaSetNode> members, boolean includeMaster) {
float bestPingTime = Float.MAX_VALUE;
for (ReplicaSetNode cur : members) {
- if (!cur.secondary()) {
- continue;
- }
- if (cur._pingTime < bestPingTime) {
- bestPingTime = cur._pingTime;
+ if (cur.secondary() || (includeMaster && cur.master())) {
+ if (cur._pingTime < bestPingTime) {
+ bestPingTime = cur._pingTime;
+ }
}
}
return bestPingTime;
}
- static List<ReplicaSetNode> calculateGoodMembers(List<ReplicaSetNode> members, float bestPingTime, int acceptableLatencyMS) {
- List<ReplicaSetNode> goodSecondaries = new ArrayList<ReplicaSetNode>(members.size());
- for (ReplicaSetNode cur : members) {
- if (!cur.isOk()) {
- continue;
- }
- if (cur._pingTime - acceptableLatencyMS <= bestPingTime ) {
- goodSecondaries.add(cur);
- }
- }
- return goodSecondaries;
- }
-
- static List<ReplicaSetNode> calculateGoodSecondaries(List<ReplicaSetNode> members, float bestPingTime, int acceptableLatencyMS) {
+ static List<ReplicaSetNode> calculateGoodMembers(List<ReplicaSetNode> members, float bestPingTime, int acceptableLatencyMS, boolean includeMaster) {
List<ReplicaSetNode> goodSecondaries = new ArrayList<ReplicaSetNode>(members.size());
for (ReplicaSetNode cur : members) {
- if (!cur.secondary()) {
- continue;
- }
- if (cur._pingTime - acceptableLatencyMS <= bestPingTime ) {
- goodSecondaries.add(cur);
+ if (cur.secondary() || (includeMaster && cur.master())) {
+ if (cur._pingTime - acceptableLatencyMS <= bestPingTime) {
+ goodSecondaries.add(cur);
+ }
}
}
return goodSecondaries;
View
287 src/test/com/mongodb/ReadPreferenceGetNodeTest.java
@@ -0,0 +1,287 @@
+/*
+ * Copyright (c) 2008 - 2012 10gen, Inc. <http://10gen.com>
+ *
+ * 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;
+
+import com.mongodb.util.TestCase;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Random;
+import java.util.Set;
+
+public class ReadPreferenceGetNodeTest extends TestCase {
+ private static final String setName = "test";
+ private static final boolean _isMaster = true;
+ private static final boolean _isSecondary = true;
+ private static final boolean _isOK = true;
+
+ private static final float acceptableLatencyMS = 15;
+
+ private final ReplicaSetStatus.ReplicaSetNode _primary, _secondary1, _secondary2, _secondary3, _recovering1;
+ private final ReplicaSetStatus.ReplicaSet _set;
+ private final ReplicaSetStatus.ReplicaSet _setNoSecondary;
+ private final ReplicaSetStatus.ReplicaSet _setNoPrimary;
+ private final ReplicaSetStatus.ReplicaSet _emptySet;
+
+ private Set<ReplicaSetStatus.ReplicaSetNode> expectedNodeSet;
+ private Set<ReplicaSetStatus.ReplicaSetNode> nodeSet;
+
+ public ReadPreferenceGetNodeTest() throws IOException, MongoException {
+ Set<String> names = new HashSet<String>();
+
+ LinkedHashMap<String, String> tagSetPrimary = new LinkedHashMap<String, String>();
+ tagSetPrimary.put("dc", "ny");
+
+ LinkedHashMap<String, String> tagSet = new LinkedHashMap<String, String>();
+ tagSet.put("dc", "ny");
+ tagSet.put("rack", "1");
+
+ names.clear();
+ names.add("primary");
+ _primary = new ReplicaSetStatus.ReplicaSetNode(new ServerAddress("127.0.0.1", 27017), names, setName, 50f, _isOK, _isMaster, !_isSecondary, tagSetPrimary, Bytes.MAX_OBJECT_SIZE);
+
+ names.clear();
+ names.add("secondary1");
+ _secondary1 = new ReplicaSetStatus.ReplicaSetNode(new ServerAddress("127.0.0.1", 27018), names, setName, 60f, _isOK, !_isMaster, _isSecondary, tagSet, Bytes.MAX_OBJECT_SIZE);
+
+ names.clear();
+ names.add("secondary2");
+ _secondary2 = new ReplicaSetStatus.ReplicaSetNode(new ServerAddress("127.0.0.1", 27019), names, setName, 66f, _isOK, !_isMaster, _isSecondary, tagSet, Bytes.MAX_OBJECT_SIZE);
+
+ names.clear();
+ names.add("secondary3");
+ _secondary3 = new ReplicaSetStatus.ReplicaSetNode(new ServerAddress("127.0.0.1", 27019), names, setName, 76f, _isOK, !_isMaster, _isSecondary, tagSet, Bytes.MAX_OBJECT_SIZE);
+
+ names.clear();
+ names.add("recovering1");
+ _recovering1 = new ReplicaSetStatus.ReplicaSetNode(new ServerAddress("127.0.0.1", 27020), names, setName, 10f, _isOK, !_isMaster, !_isSecondary, tagSet, Bytes.MAX_OBJECT_SIZE);
+
+ List<ReplicaSetStatus.ReplicaSetNode> nodeList = new ArrayList<ReplicaSetStatus.ReplicaSetNode>();
+ nodeList.add(_primary);
+ nodeList.add(_secondary1);
+ nodeList.add(_secondary2);
+ nodeList.add(_secondary3);
+ nodeList.add(_recovering1);
+
+ _set = new ReplicaSetStatus.ReplicaSet(nodeList, new Random(), (int) acceptableLatencyMS);
+ _setNoSecondary = new ReplicaSetStatus.ReplicaSet(Arrays.asList(_primary, _recovering1), new Random(), (int) acceptableLatencyMS);
+ _setNoPrimary = new ReplicaSetStatus.ReplicaSet(Arrays.asList(_secondary1, _secondary2, _secondary3, _recovering1), new Random(), (int) acceptableLatencyMS);
+ _emptySet = new ReplicaSetStatus.ReplicaSet(new ArrayList<ReplicaSetStatus.ReplicaSetNode>(), new Random(), (int) acceptableLatencyMS);
+ }
+
+ @BeforeMethod
+ public void setUp() {
+ expectedNodeSet = new HashSet<ReplicaSetStatus.ReplicaSetNode>();
+ nodeSet = new HashSet<ReplicaSetStatus.ReplicaSetNode>();
+ }
+
+ @Test
+ public void testNearest() {
+ for (int i = 0; i < 1000; i++) {
+ nodeSet.add(ReadPreference.nearest().getNode(_set));
+ }
+
+ expectedNodeSet.addAll(Arrays.asList(_primary, _secondary1));
+ assertEquals(expectedNodeSet, nodeSet);
+ }
+
+ @Test
+ public void testTaggedNearest() {
+ final TaggableReadPreference taggedNearestReadPreference = ReadPreference.nearest(new BasicDBObject("dc", "ny"));
+ for (int i = 0; i < 1000; i++) {
+ nodeSet.add(taggedNearestReadPreference.getNode(_set));
+ }
+
+ expectedNodeSet.addAll(Arrays.asList(_primary, _secondary1));
+ assertEquals(expectedNodeSet, nodeSet);
+ }
+
+ @Test
+ public void testSecondaryPreferredWithSecondary() {
+ for (int i = 0; i < 1000; i++) {
+ nodeSet.add(ReadPreference.secondaryPreferred().getNode(_set));
+ }
+
+ expectedNodeSet.addAll(Arrays.asList(_secondary1, _secondary2));
+ assertEquals(expectedNodeSet, nodeSet);
+ }
+
+ @Test
+ public void testSecondaryPreferredWithNoSecondary() {
+ for (int i = 0; i < 1000; i++) {
+ nodeSet.add(ReadPreference.secondaryPreferred().getNode(_setNoSecondary));
+ }
+
+ expectedNodeSet.addAll(Arrays.asList(_primary));
+ assertEquals(expectedNodeSet, nodeSet);
+ }
+
+ @Test
+ public void testSecondaryPreferredWithNoPrimaryOrSecondary() {
+ assertNull(ReadPreference.secondaryPreferred().getNode(_emptySet));
+ }
+
+ @Test
+ public void testTaggedSecondaryPreferredWithSecondary() {
+ final TaggableReadPreference readPreference = ReadPreference.secondaryPreferred(new BasicDBObject("dc", "ny"));
+
+ for (int i = 0; i < 1000; i++) {
+ nodeSet.add(readPreference.getNode(_set));
+ }
+
+ expectedNodeSet.addAll(Arrays.asList(_secondary1, _secondary2));
+ assertEquals(expectedNodeSet, nodeSet);
+ }
+
+ @Test
+ public void testTaggedSecondaryPreferredWithNoSecondary() {
+ final TaggableReadPreference readPreference = ReadPreference.secondaryPreferred(new BasicDBObject("dc", "ny"));
+ for (int i = 0; i < 1000; i++) {
+ nodeSet.add(readPreference.getNode(_setNoSecondary));
+ }
+
+ expectedNodeSet.addAll(Arrays.asList(_primary));
+ assertEquals(expectedNodeSet, nodeSet);
+ }
+
+ @Test
+ public void testTaggedSecondaryPreferredWithNoPrimaryOrSecondary() {
+ final TaggableReadPreference readPreference = ReadPreference.secondaryPreferred(new BasicDBObject("dc", "ny"));
+ assertNull(readPreference.getNode(_emptySet));
+ }
+
+ @Test
+ public void testTaggedSecondaryPreferredWithNoSecondaryMatch() {
+ final TaggableReadPreference nonMatchingReadPreference =
+ ReadPreference.secondaryPreferred(new BasicDBObject("dc", "ca"));
+
+ for (int i = 0; i < 1000; i++) {
+ nodeSet.add(nonMatchingReadPreference.getNode(_set));
+ }
+
+ expectedNodeSet.addAll(Arrays.asList(_primary));
+ assertEquals(expectedNodeSet, nodeSet);
+ }
+
+ @Test
+ public void testSecondaryWithSecondary() {
+ for (int i = 0; i < 1000; i++) {
+ nodeSet.add(ReadPreference.secondary().getNode(_set));
+ }
+
+ expectedNodeSet.addAll(Arrays.asList(_secondary1, _secondary2));
+ assertEquals(expectedNodeSet, nodeSet);
+ }
+
+ @Test
+ public void testSecondaryWithNoSecondary() {
+ assertNull(ReadPreference.secondary().getNode(_setNoSecondary));
+ }
+
+ @Test
+ public void testTaggedSecondaryWithSecondary() {
+ final TaggableReadPreference taggedSecondaryReadPreference = ReadPreference.secondary(new BasicDBObject("dc", "ny"));
+ for (int i = 0; i < 1000; i++) {
+ nodeSet.add(taggedSecondaryReadPreference.getNode(_set));
+ }
+
+ expectedNodeSet.addAll(Arrays.asList(_secondary1, _secondary2));
+ assertEquals(expectedNodeSet, nodeSet);
+ }
+
+ @Test
+ public void testTaggedSecondaryWithNoSecondary() {
+ final TaggableReadPreference taggedSecondaryReadPreference = ReadPreference.secondary(new BasicDBObject("dc", "ny"));
+ assertNull(taggedSecondaryReadPreference.getNode(_setNoSecondary));
+ }
+
+ @Test
+ public void testPrimaryWithPrimary() {
+ for (int i = 0; i < 1000; i++) {
+ nodeSet.add(ReadPreference.primary().getNode(_set));
+ }
+
+ expectedNodeSet.addAll(Arrays.asList(_primary));
+ assertEquals(expectedNodeSet, nodeSet);
+ }
+
+ @Test
+ public void testPrimaryWithNoPrimary() {
+ assertNull(ReadPreference.primary().getNode(_setNoPrimary));
+ }
+
+ @Test
+ public void testPrimaryPreferredWithPrimary() {
+ for (int i = 0; i < 1000; i++) {
+ nodeSet.add(ReadPreference.primaryPreferred().getNode(_set));
+ }
+
+ expectedNodeSet.addAll(Arrays.asList(_primary));
+ assertEquals(expectedNodeSet, nodeSet);
+ }
+
+ @Test
+ public void testPrimaryPreferredWithNoPrimary() {
+ for (int i = 0; i < 1000; i++) {
+ nodeSet.add(ReadPreference.primaryPreferred().getNode(_setNoPrimary));
+ }
+
+ expectedNodeSet.addAll(Arrays.asList(_secondary1, _secondary2));
+ assertEquals(expectedNodeSet, nodeSet);
+ }
+
+ @Test
+ public void testTaggedPrimaryPreferredWithPrimary() {
+ final TaggableReadPreference readPreference = ReadPreference.primaryPreferred(new BasicDBObject("dc", "ny"));
+ for (int i = 0; i < 1000; i++) {
+ nodeSet.add(readPreference.getNode(_set));
+ }
+
+ expectedNodeSet.addAll(Arrays.asList(_primary));
+ assertEquals(expectedNodeSet, nodeSet);
+ }
+
+ @Test
+ public void testTaggedPrimaryPreferredWithNoPrimary() {
+ final TaggableReadPreference readPreference = ReadPreference.primaryPreferred(new BasicDBObject("dc", "ny"));
+ for (int i = 0; i < 1000; i++) {
+ nodeSet.add(readPreference.getNode(_setNoPrimary));
+ }
+
+ expectedNodeSet.addAll(Arrays.asList(_secondary1, _secondary2));
+ assertEquals(expectedNodeSet, nodeSet);
+ }
+
+ @Test
+ @SuppressWarnings("deprecation")
+ public void testTaggedPreference() {
+ ReadPreference readPreference = new ReadPreference.TaggedReadPreference(new BasicDBObject("dc", "ny"));
+ for (int i = 0; i < 1000; i++) {
+ nodeSet.add(readPreference.getNode(_set));
+ }
+
+ expectedNodeSet.addAll(Arrays.asList(_secondary1, _secondary2));
+ assertEquals(expectedNodeSet, nodeSet);
+ }
+}
View
169 src/test/com/mongodb/ReadPreferenceTest.java
@@ -1,64 +1,11 @@
package com.mongodb;
-import com.mongodb.ConnectionStatus.Node;
-import com.mongodb.ReplicaSetStatus.ReplicaSet;
-import com.mongodb.ReplicaSetStatus.ReplicaSetNode;
import com.mongodb.util.TestCase;
import org.testng.annotations.Test;
-import java.io.IOException;
-import java.util.ArrayList;
import java.util.Arrays;
-import java.util.HashSet;
-import java.util.LinkedHashMap;
-import java.util.List;
-import java.util.Random;
-import java.util.Set;
-
-public class ReadPreferenceTest extends TestCase {
- public ReadPreferenceTest() throws IOException, MongoException {
- Set<String> names = new HashSet<String>();
- names.add("primary");
- LinkedHashMap<String, String> tagSet1 = new LinkedHashMap<String, String>();
- tagSet1.put("foo", "1");
- tagSet1.put("bar", "2");
- tagSet1.put("baz", "1");
-
- LinkedHashMap<String, String> tagSet2 = new LinkedHashMap<String, String>();
- tagSet2.put("foo", "1");
- tagSet2.put("bar", "2");
- tagSet2.put("baz", "2");
-
- LinkedHashMap<String, String> tagSet3 = new LinkedHashMap<String, String>();
- tagSet3.put("foo", "1");
- tagSet3.put("bar", "2");
- tagSet3.put("baz", "3");
-
- float acceptableLatencyMS = 15;
- float bestPingTime = 50f;
- float acceptablePingTime = bestPingTime + (acceptableLatencyMS/2);
- float unacceptablePingTime = bestPingTime + acceptableLatencyMS + 1 ;
-
- _primary = new ReplicaSetNode(new ServerAddress("127.0.0.1", 27017), names, "", acceptablePingTime, _isOK, _isMaster, !_isSecondary, tagSet1, Bytes.MAX_OBJECT_SIZE );
-
- names.clear();
- names.add("secondary");
- _secondary = new ReplicaSetNode(new ServerAddress("127.0.0.1", 27018), names, "", bestPingTime, _isOK, !_isMaster, _isSecondary, tagSet2, Bytes.MAX_OBJECT_SIZE );
-
- names.clear();
- names.add("tertiary");
- _otherSecondary = new ReplicaSetNode(new ServerAddress("127.0.0.1", 27019), names, "", unacceptablePingTime, _isOK, !_isMaster, _isSecondary, tagSet3, Bytes.MAX_OBJECT_SIZE );
-
- List<ReplicaSetNode> nodeList = new ArrayList<ReplicaSetNode>();
- nodeList.add(_primary);
- nodeList.add(_secondary);
- nodeList.add(_otherSecondary);
-
- _set = new ReplicaSet(nodeList, (new Random()), (int)acceptableLatencyMS);
- _setNoPrimary = new ReplicaSet(Arrays.asList(_secondary, _otherSecondary), (new Random()), (int)acceptableLatencyMS);
- _setNoSecondary = new ReplicaSet(Arrays.asList(_primary), (new Random()), (int)acceptableLatencyMS);
- }
+public class ReadPreferenceTest extends TestCase {
@Test
@SuppressWarnings("deprecation")
public void testDeprecatedStaticMembers() {
@@ -67,92 +14,48 @@ public void testDeprecatedStaticMembers() {
}
@Test
- public void testStaticPreferences() {
+ public void testToString() {
assertEquals("{ \"mode\" : \"primary\"}", ReadPreference.primary().toDBObject().toString());
- assertEquals("{ \"mode\" : \"secondary\"}", ReadPreference.secondary().toDBObject().toString());
assertEquals("{ \"mode\" : \"secondaryPreferred\"}", ReadPreference.secondaryPreferred().toDBObject().toString());
- assertEquals("{ \"mode\" : \"primaryPreferred\"}", ReadPreference.primaryPreferred().toDBObject().toString());
assertEquals("{ \"mode\" : \"nearest\"}", ReadPreference.nearest().toDBObject().toString());
}
-
+
@Test
- public void testPrimaryReadPreference() {
- assertEquals(_primary, ReadPreference.primary().getNode(_set));
- assertNull(ReadPreference.primary().getNode(_setNoPrimary));
- assertEquals("{ \"mode\" : \"primary\"}", ReadPreference.primary().toDBObject().toString());
+ public void testSecondaryReadPreference() {
+ final BasicDBObject asDBObject = new BasicDBObject("mode", "secondary");
+ assertEquals(asDBObject, ReadPreference.secondary().toDBObject());
+
+ assertEquals(asDBObject.append("tags", Arrays.asList(new BasicDBObject("tag", "1"))),
+ ReadPreference.secondary(new BasicDBObject("tag", "1")).toDBObject());
}
-
+
@Test
- public void testSecondaryReadPreference(){
- assertTrue(ReadPreference.secondary().toString().startsWith("secondary"));
-
- ReplicaSetNode candidate = ReadPreference.secondary().getNode(_set);
- assertTrue(!candidate.master());
-
- candidate = ReadPreference.secondary().getNode(_setNoSecondary);
- assertNull(candidate);
-
- // Test secondary mode, with tags
- ReadPreference pref = ReadPreference.secondary(new BasicDBObject("foo", "1"), new BasicDBObject("bar", "2"));
- assertTrue(pref.toString().startsWith("secondary"));
-
- candidate = ReadPreference.secondary().getNode(_set);
- assertTrue( (candidate.equals(_secondary) || candidate.equals(_otherSecondary) ) && !candidate.equals(_primary) );
-
- pref = ReadPreference.secondary(new BasicDBObject("baz", "1"));
- assertTrue(pref.getNode(_set) == null);
-
- pref = ReadPreference.secondary(new BasicDBObject("baz", "2"));
- assertTrue(pref.getNode(_set).equals(_secondary));
-
- pref = ReadPreference.secondary(new BasicDBObject("madeup", "1"));
- assertTrue(pref.toDBObject().toString().equals("{ \"mode\" : \"secondary\" , \"tags\" : [ { \"madeup\" : \"1\"}]}"));
- assertTrue(pref.getNode(_set) == null);
+ public void testPrimaryPreferredMode() {
+ final BasicDBObject asDBObject = new BasicDBObject("mode", "primaryPreferred");
+ assertEquals(asDBObject, ReadPreference.primaryPreferred().toDBObject());
+
+ assertEquals(asDBObject.append("tags", Arrays.asList(new BasicDBObject("tag", "1"))),
+ ReadPreference.primaryPreferred(new BasicDBObject("tag", "1")).toDBObject());
}
@Test
- public void testPrimaryPreferredMode(){
- ReadPreference pref = ReadPreference.primaryPreferred();
- Node candidate = pref.getNode(_set);
- assertEquals(_primary, candidate);
+ public void testSecondaryPreferredMode() {
+ final BasicDBObject asDBObject = new BasicDBObject("mode", "secondaryPreferred");
+ assertEquals(asDBObject, ReadPreference.secondaryPreferred().toDBObject());
- assertNotNull(ReadPreference.primaryPreferred().getNode(_setNoPrimary));
+ assertEquals(asDBObject.append("tags", Arrays.asList(new BasicDBObject("tag", "1"))),
+ ReadPreference.secondaryPreferred(new BasicDBObject("tag", "1")).toDBObject());
- pref = ReadPreference.primaryPreferred(new BasicDBObject("baz", "2"));
- assertTrue(pref.getNode(_set).equals(_primary));
- assertTrue(pref.getNode(_setNoPrimary).equals(_secondary));
}
@Test
- public void testSecondaryPreferredMode(){
- ReadPreference pref = ReadPreference.secondary(new BasicDBObject("baz", "2"));
- assertTrue(pref.getNode(_set).equals(_secondary));
-
- // test that the primary is returned if no secondaries match the tag
- pref = ReadPreference.secondaryPreferred(new BasicDBObject("madeup", "1"));
- assertTrue(pref.getNode(_set).equals(_primary));
-
- pref = ReadPreference.secondaryPreferred();
- Node candidate = pref.getNode(_set);
- assertTrue((candidate.equals(_secondary) || candidate.equals(_otherSecondary)) && !candidate.equals(_primary));
-
- assertEquals(_primary, ReadPreference.secondaryPreferred().getNode(_setNoSecondary));
- }
-
- @Test
- public void testNearestMode(){
- ReadPreference pref = ReadPreference.nearest();
- assertTrue(pref.getNode(_set) != null);
-
- pref = ReadPreference.nearest(new BasicDBObject("baz", "1"));
- assertTrue(pref.getNode(_set).equals(_primary));
-
- pref = ReadPreference.nearest(new BasicDBObject("baz", "2"));
- assertTrue(pref.getNode(_set).equals(_secondary));
-
- pref = ReadPreference.nearest(new BasicDBObject("madeup", "1"));
- assertTrue(pref.toDBObject().toString().equals("{ \"mode\" : \"nearest\" , \"tags\" : [ { \"madeup\" : \"1\"}]}"));
- assertTrue(pref.getNode(_set) == null);
+ public void testNearestMode() {
+ final BasicDBObject asDBObject = new BasicDBObject("mode", "nearest");
+ assertEquals(asDBObject, ReadPreference.nearest().toDBObject());
+
+ assertEquals(asDBObject.append("tags", Arrays.asList(new BasicDBObject("tag", "1"))),
+ ReadPreference.nearest(new BasicDBObject("tag", "1")).toDBObject());
+
}
@Test
@@ -186,20 +89,4 @@ public void testGetName() {
assertEquals(ReadPreference.secondaryPreferred(first, remaining), ReadPreference.valueOf("secondaryPreferred", first, remaining));
assertEquals(ReadPreference.nearest(first, remaining), ReadPreference.valueOf("nearest", first, remaining));
}
-
- @Test
- @SuppressWarnings( "deprecation" )
- public void testTaggedPreference(){
- ReadPreference pref = new ReadPreference.TaggedReadPreference(new BasicDBObject("bar", "2"));
- assertTrue(!pref.getNode(_set).master());
- }
-
- static boolean _isMaster = true;
- static boolean _isSecondary = true;
- static boolean _isOK = true;
-
- ReplicaSetNode _primary, _secondary, _otherSecondary;
- ReplicaSet _set;
- ReplicaSet _setNoSecondary;
- ReplicaSet _setNoPrimary;
}

0 comments on commit f97eb44

Please sign in to comment.
Something went wrong with that request. Please try again.