Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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...
commit f97eb442eec7332ac34fa74b0b46983743b3a095 1 parent 6ab6478
Jeff Yemin jyemin authored
81 src/main/com/mongodb/ReplicaSetStatus.java
@@ -178,8 +178,8 @@ public String toString() {
178 178 static class ReplicaSet {
179 179 final List<ReplicaSetNode> all;
180 180 final Random random;
181   - final List<ReplicaSetNode> goodSecondaries;
182   - final List<ReplicaSetNode> goodMembers;
  181 + final List<ReplicaSetNode> acceptableSecondaries;
  182 + final List<ReplicaSetNode> acceptableMembers;
183 183 final ReplicaSetNode master;
184 184 final String setName;
185 185 final ReplicaSetErrorStatus errorStatus;
@@ -195,10 +195,11 @@ public ReplicaSet(List<ReplicaSetNode> nodeList, Random random, int acceptableLa
195 195 errorStatus = validate();
196 196 setName = determineSetName();
197 197
198   - this.goodSecondaries =
199   - Collections.unmodifiableList(calculateGoodSecondaries(all, calculateBestPingTime(all), acceptableLatencyMS));
200   - this.goodMembers =
201   - Collections.unmodifiableList(calculateGoodMembers(all, calculateBestPingTime(all), acceptableLatencyMS));
  198 + this.acceptableSecondaries =
  199 + Collections.unmodifiableList(calculateGoodMembers(
  200 + all, calculateBestPingTime(all, false), acceptableLatencyMS, false));
  201 + this.acceptableMembers =
  202 + Collections.unmodifiableList(calculateGoodMembers(all, calculateBestPingTime(all, true), acceptableLatencyMS, true));
202 203 master = findMaster();
203 204 }
204 205
@@ -229,10 +230,10 @@ public int getMaxBsonObjectSize() {
229 230 public ReplicaSetNode getASecondary() {
230 231 checkStatus();
231 232
232   - if (goodSecondaries.isEmpty()) {
  233 + if (acceptableSecondaries.isEmpty()) {
233 234 return null;
234 235 }
235   - return goodSecondaries.get(random.nextInt(goodSecondaries.size()));
  236 + return acceptableSecondaries.get(random.nextInt(acceptableSecondaries.size()));
236 237 }
237 238
238 239 public ReplicaSetNode getASecondary(List<Tag> tags) {
@@ -254,10 +255,10 @@ public ReplicaSetNode getASecondary(List<Tag> tags) {
254 255 public ReplicaSetNode getAMember() {
255 256 checkStatus();
256 257
257   - if (goodMembers.isEmpty()) {
  258 + if (acceptableMembers.isEmpty()) {
258 259 return null;
259 260 }
260   - return goodMembers.get(random.nextInt(goodMembers.size()));
  261 + return acceptableMembers.get(random.nextInt(acceptableMembers.size()));
261 262 }
262 263
263 264 public ReplicaSetNode getAMember(List<Tag> tags) {
@@ -274,12 +275,23 @@ public ReplicaSetNode getAMember(List<Tag> tags) {
274 275 return acceptableTaggedMembers.get(random.nextInt(acceptableTaggedMembers.size()));
275 276 }
276 277
  278 + List<ReplicaSetNode> getGoodSecondaries(List<ReplicaSetNode> all) {
  279 + List<ReplicaSetNode> goodSecondaries = new ArrayList<ReplicaSetNode>(all.size());
  280 + for (ReplicaSetNode cur : all) {
  281 + if (!cur.isOk()) {
  282 + continue;
  283 + }
  284 + goodSecondaries.add(cur);
  285 + }
  286 + return goodSecondaries;
  287 + }
  288 +
277 289 public List<ReplicaSetNode> getGoodSecondariesByTags(final List<Tag> tags) {
278 290 checkStatus();
279 291
280 292 List<ReplicaSetNode> taggedSecondaries = getMembersByTags(all, tags);
281   - return calculateGoodSecondaries(taggedSecondaries,
282   - calculateBestPingTime(taggedSecondaries), acceptableLatencyMS);
  293 + return calculateGoodMembers(taggedSecondaries,
  294 + calculateBestPingTime(taggedSecondaries, false), acceptableLatencyMS, false);
283 295 }
284 296
285 297 public List<ReplicaSetNode> getGoodMembersByTags(final List<Tag> tags) {
@@ -287,15 +299,9 @@ public ReplicaSetNode getAMember(List<Tag> tags) {
287 299
288 300 List<ReplicaSetNode> taggedMembers = getMembersByTags(all, tags);
289 301 return calculateGoodMembers(taggedMembers,
290   - calculateBestPingTime(taggedMembers), acceptableLatencyMS);
  302 + calculateBestPingTime(taggedMembers, true), acceptableLatencyMS, true);
291 303 }
292   -
293   - public List<ReplicaSetNode> getGoodMembers() {
294   - checkStatus();
295   -
296   - return calculateGoodMembers(all, calculateBestPingTime(all), acceptableLatencyMS);
297   - }
298   -
  304 +
299 305 public String getSetName() {
300 306 checkStatus();
301 307
@@ -361,40 +367,25 @@ private ReplicaSetErrorStatus validate() {
361 367 }
362 368 }
363 369
364   - static float calculateBestPingTime(List<ReplicaSetNode> members) {
  370 + static float calculateBestPingTime(List<ReplicaSetNode> members, boolean includeMaster) {
365 371 float bestPingTime = Float.MAX_VALUE;
366 372 for (ReplicaSetNode cur : members) {
367   - if (!cur.secondary()) {
368   - continue;
369   - }
370   - if (cur._pingTime < bestPingTime) {
371   - bestPingTime = cur._pingTime;
  373 + if (cur.secondary() || (includeMaster && cur.master())) {
  374 + if (cur._pingTime < bestPingTime) {
  375 + bestPingTime = cur._pingTime;
  376 + }
372 377 }
373 378 }
374 379 return bestPingTime;
375 380 }
376 381
377   - static List<ReplicaSetNode> calculateGoodMembers(List<ReplicaSetNode> members, float bestPingTime, int acceptableLatencyMS) {
378   - List<ReplicaSetNode> goodSecondaries = new ArrayList<ReplicaSetNode>(members.size());
379   - for (ReplicaSetNode cur : members) {
380   - if (!cur.isOk()) {
381   - continue;
382   - }
383   - if (cur._pingTime - acceptableLatencyMS <= bestPingTime ) {
384   - goodSecondaries.add(cur);
385   - }
386   - }
387   - return goodSecondaries;
388   - }
389   -
390   - static List<ReplicaSetNode> calculateGoodSecondaries(List<ReplicaSetNode> members, float bestPingTime, int acceptableLatencyMS) {
  382 + static List<ReplicaSetNode> calculateGoodMembers(List<ReplicaSetNode> members, float bestPingTime, int acceptableLatencyMS, boolean includeMaster) {
391 383 List<ReplicaSetNode> goodSecondaries = new ArrayList<ReplicaSetNode>(members.size());
392 384 for (ReplicaSetNode cur : members) {
393   - if (!cur.secondary()) {
394   - continue;
395   - }
396   - if (cur._pingTime - acceptableLatencyMS <= bestPingTime ) {
397   - goodSecondaries.add(cur);
  385 + if (cur.secondary() || (includeMaster && cur.master())) {
  386 + if (cur._pingTime - acceptableLatencyMS <= bestPingTime) {
  387 + goodSecondaries.add(cur);
  388 + }
398 389 }
399 390 }
400 391 return goodSecondaries;
287 src/test/com/mongodb/ReadPreferenceGetNodeTest.java
... ... @@ -0,0 +1,287 @@
  1 +/*
  2 + * Copyright (c) 2008 - 2012 10gen, Inc. <http://10gen.com>
  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 +
  18 +package com.mongodb;
  19 +
  20 +import com.mongodb.util.TestCase;
  21 +import org.testng.annotations.BeforeMethod;
  22 +import org.testng.annotations.Test;
  23 +
  24 +import java.io.IOException;
  25 +import java.util.ArrayList;
  26 +import java.util.Arrays;
  27 +import java.util.HashSet;
  28 +import java.util.LinkedHashMap;
  29 +import java.util.List;
  30 +import java.util.Random;
  31 +import java.util.Set;
  32 +
  33 +public class ReadPreferenceGetNodeTest extends TestCase {
  34 + private static final String setName = "test";
  35 + private static final boolean _isMaster = true;
  36 + private static final boolean _isSecondary = true;
  37 + private static final boolean _isOK = true;
  38 +
  39 + private static final float acceptableLatencyMS = 15;
  40 +
  41 + private final ReplicaSetStatus.ReplicaSetNode _primary, _secondary1, _secondary2, _secondary3, _recovering1;
  42 + private final ReplicaSetStatus.ReplicaSet _set;
  43 + private final ReplicaSetStatus.ReplicaSet _setNoSecondary;
  44 + private final ReplicaSetStatus.ReplicaSet _setNoPrimary;
  45 + private final ReplicaSetStatus.ReplicaSet _emptySet;
  46 +
  47 + private Set<ReplicaSetStatus.ReplicaSetNode> expectedNodeSet;
  48 + private Set<ReplicaSetStatus.ReplicaSetNode> nodeSet;
  49 +
  50 + public ReadPreferenceGetNodeTest() throws IOException, MongoException {
  51 + Set<String> names = new HashSet<String>();
  52 +
  53 + LinkedHashMap<String, String> tagSetPrimary = new LinkedHashMap<String, String>();
  54 + tagSetPrimary.put("dc", "ny");
  55 +
  56 + LinkedHashMap<String, String> tagSet = new LinkedHashMap<String, String>();
  57 + tagSet.put("dc", "ny");
  58 + tagSet.put("rack", "1");
  59 +
  60 + names.clear();
  61 + names.add("primary");
  62 + _primary = new ReplicaSetStatus.ReplicaSetNode(new ServerAddress("127.0.0.1", 27017), names, setName, 50f, _isOK, _isMaster, !_isSecondary, tagSetPrimary, Bytes.MAX_OBJECT_SIZE);
  63 +
  64 + names.clear();
  65 + names.add("secondary1");
  66 + _secondary1 = new ReplicaSetStatus.ReplicaSetNode(new ServerAddress("127.0.0.1", 27018), names, setName, 60f, _isOK, !_isMaster, _isSecondary, tagSet, Bytes.MAX_OBJECT_SIZE);
  67 +
  68 + names.clear();
  69 + names.add("secondary2");
  70 + _secondary2 = new ReplicaSetStatus.ReplicaSetNode(new ServerAddress("127.0.0.1", 27019), names, setName, 66f, _isOK, !_isMaster, _isSecondary, tagSet, Bytes.MAX_OBJECT_SIZE);
  71 +
  72 + names.clear();
  73 + names.add("secondary3");
  74 + _secondary3 = new ReplicaSetStatus.ReplicaSetNode(new ServerAddress("127.0.0.1", 27019), names, setName, 76f, _isOK, !_isMaster, _isSecondary, tagSet, Bytes.MAX_OBJECT_SIZE);
  75 +
  76 + names.clear();
  77 + names.add("recovering1");
  78 + _recovering1 = new ReplicaSetStatus.ReplicaSetNode(new ServerAddress("127.0.0.1", 27020), names, setName, 10f, _isOK, !_isMaster, !_isSecondary, tagSet, Bytes.MAX_OBJECT_SIZE);
  79 +
  80 + List<ReplicaSetStatus.ReplicaSetNode> nodeList = new ArrayList<ReplicaSetStatus.ReplicaSetNode>();
  81 + nodeList.add(_primary);
  82 + nodeList.add(_secondary1);
  83 + nodeList.add(_secondary2);
  84 + nodeList.add(_secondary3);
  85 + nodeList.add(_recovering1);
  86 +
  87 + _set = new ReplicaSetStatus.ReplicaSet(nodeList, new Random(), (int) acceptableLatencyMS);
  88 + _setNoSecondary = new ReplicaSetStatus.ReplicaSet(Arrays.asList(_primary, _recovering1), new Random(), (int) acceptableLatencyMS);
  89 + _setNoPrimary = new ReplicaSetStatus.ReplicaSet(Arrays.asList(_secondary1, _secondary2, _secondary3, _recovering1), new Random(), (int) acceptableLatencyMS);
  90 + _emptySet = new ReplicaSetStatus.ReplicaSet(new ArrayList<ReplicaSetStatus.ReplicaSetNode>(), new Random(), (int) acceptableLatencyMS);
  91 + }
  92 +
  93 + @BeforeMethod
  94 + public void setUp() {
  95 + expectedNodeSet = new HashSet<ReplicaSetStatus.ReplicaSetNode>();
  96 + nodeSet = new HashSet<ReplicaSetStatus.ReplicaSetNode>();
  97 + }
  98 +
  99 + @Test
  100 + public void testNearest() {
  101 + for (int i = 0; i < 1000; i++) {
  102 + nodeSet.add(ReadPreference.nearest().getNode(_set));
  103 + }
  104 +
  105 + expectedNodeSet.addAll(Arrays.asList(_primary, _secondary1));
  106 + assertEquals(expectedNodeSet, nodeSet);
  107 + }
  108 +
  109 + @Test
  110 + public void testTaggedNearest() {
  111 + final TaggableReadPreference taggedNearestReadPreference = ReadPreference.nearest(new BasicDBObject("dc", "ny"));
  112 + for (int i = 0; i < 1000; i++) {
  113 + nodeSet.add(taggedNearestReadPreference.getNode(_set));
  114 + }
  115 +
  116 + expectedNodeSet.addAll(Arrays.asList(_primary, _secondary1));
  117 + assertEquals(expectedNodeSet, nodeSet);
  118 + }
  119 +
  120 + @Test
  121 + public void testSecondaryPreferredWithSecondary() {
  122 + for (int i = 0; i < 1000; i++) {
  123 + nodeSet.add(ReadPreference.secondaryPreferred().getNode(_set));
  124 + }
  125 +
  126 + expectedNodeSet.addAll(Arrays.asList(_secondary1, _secondary2));
  127 + assertEquals(expectedNodeSet, nodeSet);
  128 + }
  129 +
  130 + @Test
  131 + public void testSecondaryPreferredWithNoSecondary() {
  132 + for (int i = 0; i < 1000; i++) {
  133 + nodeSet.add(ReadPreference.secondaryPreferred().getNode(_setNoSecondary));
  134 + }
  135 +
  136 + expectedNodeSet.addAll(Arrays.asList(_primary));
  137 + assertEquals(expectedNodeSet, nodeSet);
  138 + }
  139 +
  140 + @Test
  141 + public void testSecondaryPreferredWithNoPrimaryOrSecondary() {
  142 + assertNull(ReadPreference.secondaryPreferred().getNode(_emptySet));
  143 + }
  144 +
  145 + @Test
  146 + public void testTaggedSecondaryPreferredWithSecondary() {
  147 + final TaggableReadPreference readPreference = ReadPreference.secondaryPreferred(new BasicDBObject("dc", "ny"));
  148 +
  149 + for (int i = 0; i < 1000; i++) {
  150 + nodeSet.add(readPreference.getNode(_set));
  151 + }
  152 +
  153 + expectedNodeSet.addAll(Arrays.asList(_secondary1, _secondary2));
  154 + assertEquals(expectedNodeSet, nodeSet);
  155 + }
  156 +
  157 + @Test
  158 + public void testTaggedSecondaryPreferredWithNoSecondary() {
  159 + final TaggableReadPreference readPreference = ReadPreference.secondaryPreferred(new BasicDBObject("dc", "ny"));
  160 + for (int i = 0; i < 1000; i++) {
  161 + nodeSet.add(readPreference.getNode(_setNoSecondary));
  162 + }
  163 +
  164 + expectedNodeSet.addAll(Arrays.asList(_primary));
  165 + assertEquals(expectedNodeSet, nodeSet);
  166 + }
  167 +
  168 + @Test
  169 + public void testTaggedSecondaryPreferredWithNoPrimaryOrSecondary() {
  170 + final TaggableReadPreference readPreference = ReadPreference.secondaryPreferred(new BasicDBObject("dc", "ny"));
  171 + assertNull(readPreference.getNode(_emptySet));
  172 + }
  173 +
  174 + @Test
  175 + public void testTaggedSecondaryPreferredWithNoSecondaryMatch() {
  176 + final TaggableReadPreference nonMatchingReadPreference =
  177 + ReadPreference.secondaryPreferred(new BasicDBObject("dc", "ca"));
  178 +
  179 + for (int i = 0; i < 1000; i++) {
  180 + nodeSet.add(nonMatchingReadPreference.getNode(_set));
  181 + }
  182 +
  183 + expectedNodeSet.addAll(Arrays.asList(_primary));
  184 + assertEquals(expectedNodeSet, nodeSet);
  185 + }
  186 +
  187 + @Test
  188 + public void testSecondaryWithSecondary() {
  189 + for (int i = 0; i < 1000; i++) {
  190 + nodeSet.add(ReadPreference.secondary().getNode(_set));
  191 + }
  192 +
  193 + expectedNodeSet.addAll(Arrays.asList(_secondary1, _secondary2));
  194 + assertEquals(expectedNodeSet, nodeSet);
  195 + }
  196 +
  197 + @Test
  198 + public void testSecondaryWithNoSecondary() {
  199 + assertNull(ReadPreference.secondary().getNode(_setNoSecondary));
  200 + }
  201 +
  202 + @Test
  203 + public void testTaggedSecondaryWithSecondary() {
  204 + final TaggableReadPreference taggedSecondaryReadPreference = ReadPreference.secondary(new BasicDBObject("dc", "ny"));
  205 + for (int i = 0; i < 1000; i++) {
  206 + nodeSet.add(taggedSecondaryReadPreference.getNode(_set));
  207 + }
  208 +
  209 + expectedNodeSet.addAll(Arrays.asList(_secondary1, _secondary2));
  210 + assertEquals(expectedNodeSet, nodeSet);
  211 + }
  212 +
  213 + @Test
  214 + public void testTaggedSecondaryWithNoSecondary() {
  215 + final TaggableReadPreference taggedSecondaryReadPreference = ReadPreference.secondary(new BasicDBObject("dc", "ny"));
  216 + assertNull(taggedSecondaryReadPreference.getNode(_setNoSecondary));
  217 + }
  218 +
  219 + @Test
  220 + public void testPrimaryWithPrimary() {
  221 + for (int i = 0; i < 1000; i++) {
  222 + nodeSet.add(ReadPreference.primary().getNode(_set));
  223 + }
  224 +
  225 + expectedNodeSet.addAll(Arrays.asList(_primary));
  226 + assertEquals(expectedNodeSet, nodeSet);
  227 + }
  228 +
  229 + @Test
  230 + public void testPrimaryWithNoPrimary() {
  231 + assertNull(ReadPreference.primary().getNode(_setNoPrimary));
  232 + }
  233 +
  234 + @Test
  235 + public void testPrimaryPreferredWithPrimary() {
  236 + for (int i = 0; i < 1000; i++) {
  237 + nodeSet.add(ReadPreference.primaryPreferred().getNode(_set));
  238 + }
  239 +
  240 + expectedNodeSet.addAll(Arrays.asList(_primary));
  241 + assertEquals(expectedNodeSet, nodeSet);
  242 + }
  243 +
  244 + @Test
  245 + public void testPrimaryPreferredWithNoPrimary() {
  246 + for (int i = 0; i < 1000; i++) {
  247 + nodeSet.add(ReadPreference.primaryPreferred().getNode(_setNoPrimary));
  248 + }
  249 +
  250 + expectedNodeSet.addAll(Arrays.asList(_secondary1, _secondary2));
  251 + assertEquals(expectedNodeSet, nodeSet);
  252 + }
  253 +
  254 + @Test
  255 + public void testTaggedPrimaryPreferredWithPrimary() {
  256 + final TaggableReadPreference readPreference = ReadPreference.primaryPreferred(new BasicDBObject("dc", "ny"));
  257 + for (int i = 0; i < 1000; i++) {
  258 + nodeSet.add(readPreference.getNode(_set));
  259 + }
  260 +
  261 + expectedNodeSet.addAll(Arrays.asList(_primary));
  262 + assertEquals(expectedNodeSet, nodeSet);
  263 + }
  264 +
  265 + @Test
  266 + public void testTaggedPrimaryPreferredWithNoPrimary() {
  267 + final TaggableReadPreference readPreference = ReadPreference.primaryPreferred(new BasicDBObject("dc", "ny"));
  268 + for (int i = 0; i < 1000; i++) {
  269 + nodeSet.add(readPreference.getNode(_setNoPrimary));
  270 + }
  271 +
  272 + expectedNodeSet.addAll(Arrays.asList(_secondary1, _secondary2));
  273 + assertEquals(expectedNodeSet, nodeSet);
  274 + }
  275 +
  276 + @Test
  277 + @SuppressWarnings("deprecation")
  278 + public void testTaggedPreference() {
  279 + ReadPreference readPreference = new ReadPreference.TaggedReadPreference(new BasicDBObject("dc", "ny"));
  280 + for (int i = 0; i < 1000; i++) {
  281 + nodeSet.add(readPreference.getNode(_set));
  282 + }
  283 +
  284 + expectedNodeSet.addAll(Arrays.asList(_secondary1, _secondary2));
  285 + assertEquals(expectedNodeSet, nodeSet);
  286 + }
  287 +}
169 src/test/com/mongodb/ReadPreferenceTest.java
... ... @@ -1,64 +1,11 @@
1 1 package com.mongodb;
2 2
3   -import com.mongodb.ConnectionStatus.Node;
4   -import com.mongodb.ReplicaSetStatus.ReplicaSet;
5   -import com.mongodb.ReplicaSetStatus.ReplicaSetNode;
6 3 import com.mongodb.util.TestCase;
7 4 import org.testng.annotations.Test;
8 5
9   -import java.io.IOException;
10   -import java.util.ArrayList;
11 6 import java.util.Arrays;
12   -import java.util.HashSet;
13   -import java.util.LinkedHashMap;
14   -import java.util.List;
15   -import java.util.Random;
16   -import java.util.Set;
17   -
18   -public class ReadPreferenceTest extends TestCase {
19   - public ReadPreferenceTest() throws IOException, MongoException {
20   - Set<String> names = new HashSet<String>();
21   - names.add("primary");
22   - LinkedHashMap<String, String> tagSet1 = new LinkedHashMap<String, String>();
23   - tagSet1.put("foo", "1");
24   - tagSet1.put("bar", "2");
25   - tagSet1.put("baz", "1");
26   -
27   - LinkedHashMap<String, String> tagSet2 = new LinkedHashMap<String, String>();
28   - tagSet2.put("foo", "1");
29   - tagSet2.put("bar", "2");
30   - tagSet2.put("baz", "2");
31   -
32   - LinkedHashMap<String, String> tagSet3 = new LinkedHashMap<String, String>();
33   - tagSet3.put("foo", "1");
34   - tagSet3.put("bar", "2");
35   - tagSet3.put("baz", "3");
36   -
37   - float acceptableLatencyMS = 15;
38   - float bestPingTime = 50f;
39   - float acceptablePingTime = bestPingTime + (acceptableLatencyMS/2);
40   - float unacceptablePingTime = bestPingTime + acceptableLatencyMS + 1 ;
41   -
42   - _primary = new ReplicaSetNode(new ServerAddress("127.0.0.1", 27017), names, "", acceptablePingTime, _isOK, _isMaster, !_isSecondary, tagSet1, Bytes.MAX_OBJECT_SIZE );
43   -
44   - names.clear();
45   - names.add("secondary");
46   - _secondary = new ReplicaSetNode(new ServerAddress("127.0.0.1", 27018), names, "", bestPingTime, _isOK, !_isMaster, _isSecondary, tagSet2, Bytes.MAX_OBJECT_SIZE );
47   -
48   - names.clear();
49   - names.add("tertiary");
50   - _otherSecondary = new ReplicaSetNode(new ServerAddress("127.0.0.1", 27019), names, "", unacceptablePingTime, _isOK, !_isMaster, _isSecondary, tagSet3, Bytes.MAX_OBJECT_SIZE );
51   -
52   - List<ReplicaSetNode> nodeList = new ArrayList<ReplicaSetNode>();
53   - nodeList.add(_primary);
54   - nodeList.add(_secondary);
55   - nodeList.add(_otherSecondary);
56   -
57   - _set = new ReplicaSet(nodeList, (new Random()), (int)acceptableLatencyMS);
58   - _setNoPrimary = new ReplicaSet(Arrays.asList(_secondary, _otherSecondary), (new Random()), (int)acceptableLatencyMS);
59   - _setNoSecondary = new ReplicaSet(Arrays.asList(_primary), (new Random()), (int)acceptableLatencyMS);
60   - }
61 7
  8 +public class ReadPreferenceTest extends TestCase {
62 9 @Test
63 10 @SuppressWarnings("deprecation")
64 11 public void testDeprecatedStaticMembers() {
@@ -67,92 +14,48 @@ public void testDeprecatedStaticMembers() {
67 14 }
68 15
69 16 @Test
70   - public void testStaticPreferences() {
  17 + public void testToString() {
71 18 assertEquals("{ \"mode\" : \"primary\"}", ReadPreference.primary().toDBObject().toString());
72   - assertEquals("{ \"mode\" : \"secondary\"}", ReadPreference.secondary().toDBObject().toString());
73 19 assertEquals("{ \"mode\" : \"secondaryPreferred\"}", ReadPreference.secondaryPreferred().toDBObject().toString());
74   - assertEquals("{ \"mode\" : \"primaryPreferred\"}", ReadPreference.primaryPreferred().toDBObject().toString());
75 20 assertEquals("{ \"mode\" : \"nearest\"}", ReadPreference.nearest().toDBObject().toString());
76 21 }
77   -
  22 +
78 23 @Test
79   - public void testPrimaryReadPreference() {
80   - assertEquals(_primary, ReadPreference.primary().getNode(_set));
81   - assertNull(ReadPreference.primary().getNode(_setNoPrimary));
82   - assertEquals("{ \"mode\" : \"primary\"}", ReadPreference.primary().toDBObject().toString());
  24 + public void testSecondaryReadPreference() {
  25 + final BasicDBObject asDBObject = new BasicDBObject("mode", "secondary");
  26 + assertEquals(asDBObject, ReadPreference.secondary().toDBObject());
  27 +
  28 + assertEquals(asDBObject.append("tags", Arrays.asList(new BasicDBObject("tag", "1"))),
  29 + ReadPreference.secondary(new BasicDBObject("tag", "1")).toDBObject());
83 30 }
84   -
  31 +
85 32 @Test
86   - public void testSecondaryReadPreference(){
87   - assertTrue(ReadPreference.secondary().toString().startsWith("secondary"));
88   -
89   - ReplicaSetNode candidate = ReadPreference.secondary().getNode(_set);
90   - assertTrue(!candidate.master());
91   -
92   - candidate = ReadPreference.secondary().getNode(_setNoSecondary);
93   - assertNull(candidate);
94   -
95   - // Test secondary mode, with tags
96   - ReadPreference pref = ReadPreference.secondary(new BasicDBObject("foo", "1"), new BasicDBObject("bar", "2"));
97   - assertTrue(pref.toString().startsWith("secondary"));
98   -
99   - candidate = ReadPreference.secondary().getNode(_set);
100   - assertTrue( (candidate.equals(_secondary) || candidate.equals(_otherSecondary) ) && !candidate.equals(_primary) );
101   -
102   - pref = ReadPreference.secondary(new BasicDBObject("baz", "1"));
103   - assertTrue(pref.getNode(_set) == null);
104   -
105   - pref = ReadPreference.secondary(new BasicDBObject("baz", "2"));
106   - assertTrue(pref.getNode(_set).equals(_secondary));
107   -
108   - pref = ReadPreference.secondary(new BasicDBObject("madeup", "1"));
109   - assertTrue(pref.toDBObject().toString().equals("{ \"mode\" : \"secondary\" , \"tags\" : [ { \"madeup\" : \"1\"}]}"));
110   - assertTrue(pref.getNode(_set) == null);
  33 + public void testPrimaryPreferredMode() {
  34 + final BasicDBObject asDBObject = new BasicDBObject("mode", "primaryPreferred");
  35 + assertEquals(asDBObject, ReadPreference.primaryPreferred().toDBObject());
  36 +
  37 + assertEquals(asDBObject.append("tags", Arrays.asList(new BasicDBObject("tag", "1"))),
  38 + ReadPreference.primaryPreferred(new BasicDBObject("tag", "1")).toDBObject());
111 39 }
112 40
113 41 @Test
114   - public void testPrimaryPreferredMode(){
115   - ReadPreference pref = ReadPreference.primaryPreferred();
116   - Node candidate = pref.getNode(_set);
117   - assertEquals(_primary, candidate);
  42 + public void testSecondaryPreferredMode() {
  43 + final BasicDBObject asDBObject = new BasicDBObject("mode", "secondaryPreferred");
  44 + assertEquals(asDBObject, ReadPreference.secondaryPreferred().toDBObject());
118 45
119   - assertNotNull(ReadPreference.primaryPreferred().getNode(_setNoPrimary));
  46 + assertEquals(asDBObject.append("tags", Arrays.asList(new BasicDBObject("tag", "1"))),
  47 + ReadPreference.secondaryPreferred(new BasicDBObject("tag", "1")).toDBObject());
120 48
121   - pref = ReadPreference.primaryPreferred(new BasicDBObject("baz", "2"));
122   - assertTrue(pref.getNode(_set).equals(_primary));
123   - assertTrue(pref.getNode(_setNoPrimary).equals(_secondary));
124 49 }
125 50
126 51 @Test
127   - public void testSecondaryPreferredMode(){
128   - ReadPreference pref = ReadPreference.secondary(new BasicDBObject("baz", "2"));
129   - assertTrue(pref.getNode(_set).equals(_secondary));
130   -
131   - // test that the primary is returned if no secondaries match the tag
132   - pref = ReadPreference.secondaryPreferred(new BasicDBObject("madeup", "1"));
133   - assertTrue(pref.getNode(_set).equals(_primary));
134   -
135   - pref = ReadPreference.secondaryPreferred();
136   - Node candidate = pref.getNode(_set);
137   - assertTrue((candidate.equals(_secondary) || candidate.equals(_otherSecondary)) && !candidate.equals(_primary));
138   -
139   - assertEquals(_primary, ReadPreference.secondaryPreferred().getNode(_setNoSecondary));
140   - }
141   -
142   - @Test
143   - public void testNearestMode(){
144   - ReadPreference pref = ReadPreference.nearest();
145   - assertTrue(pref.getNode(_set) != null);
146   -
147   - pref = ReadPreference.nearest(new BasicDBObject("baz", "1"));
148   - assertTrue(pref.getNode(_set).equals(_primary));
149   -
150   - pref = ReadPreference.nearest(new BasicDBObject("baz", "2"));
151   - assertTrue(pref.getNode(_set).equals(_secondary));
152   -
153   - pref = ReadPreference.nearest(new BasicDBObject("madeup", "1"));
154   - assertTrue(pref.toDBObject().toString().equals("{ \"mode\" : \"nearest\" , \"tags\" : [ { \"madeup\" : \"1\"}]}"));
155   - assertTrue(pref.getNode(_set) == null);
  52 + public void testNearestMode() {
  53 + final BasicDBObject asDBObject = new BasicDBObject("mode", "nearest");
  54 + assertEquals(asDBObject, ReadPreference.nearest().toDBObject());
  55 +
  56 + assertEquals(asDBObject.append("tags", Arrays.asList(new BasicDBObject("tag", "1"))),
  57 + ReadPreference.nearest(new BasicDBObject("tag", "1")).toDBObject());
  58 +
156 59 }
157 60
158 61 @Test
@@ -186,20 +89,4 @@ public void testGetName() {
186 89 assertEquals(ReadPreference.secondaryPreferred(first, remaining), ReadPreference.valueOf("secondaryPreferred", first, remaining));
187 90 assertEquals(ReadPreference.nearest(first, remaining), ReadPreference.valueOf("nearest", first, remaining));
188 91 }
189   -
190   - @Test
191   - @SuppressWarnings( "deprecation" )
192   - public void testTaggedPreference(){
193   - ReadPreference pref = new ReadPreference.TaggedReadPreference(new BasicDBObject("bar", "2"));
194   - assertTrue(!pref.getNode(_set).master());
195   - }
196   -
197   - static boolean _isMaster = true;
198   - static boolean _isSecondary = true;
199   - static boolean _isOK = true;
200   -
201   - ReplicaSetNode _primary, _secondary, _otherSecondary;
202   - ReplicaSet _set;
203   - ReplicaSet _setNoSecondary;
204   - ReplicaSet _setNoPrimary;
205 92 }

0 comments on commit f97eb44

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