diff options
author | Randolph Tan <randolph@10gen.com> | 2012-11-12 14:07:44 -0500 |
---|---|---|
committer | Randolph Tan <randolph@10gen.com> | 2012-11-13 13:45:13 -0500 |
commit | 3d3f7e28b06d42b3f7432d834ac7056e53c41c88 (patch) | |
tree | 4c08cfcc53b8bd9b73e66b5423d7c651292e4a09 | |
parent | 75d8b0b022a1c1228484af71e1b97adecc67760b (diff) | |
download | mongo-3d3f7e28b06d42b3f7432d834ac7056e53c41c88.tar.gz |
SERVER-7612 explicit primary read pref does not work well with shard versioning
Ensure that selectNodeUsingTags will use the same connection to the primary with checkMaster
-rw-r--r-- | jstests/sharding/read_pref_multi_mongos_stale_config.js | 34 | ||||
-rw-r--r-- | src/mongo/client/dbclient_rs.cpp | 48 | ||||
-rw-r--r-- | src/mongo/client/dbclient_rs.h | 26 | ||||
-rw-r--r-- | src/mongo/dbtests/replica_set_monitor_test.cpp | 267 |
4 files changed, 294 insertions, 81 deletions
diff --git a/jstests/sharding/read_pref_multi_mongos_stale_config.js b/jstests/sharding/read_pref_multi_mongos_stale_config.js new file mode 100644 index 00000000000..1556adef9e8 --- /dev/null +++ b/jstests/sharding/read_pref_multi_mongos_stale_config.js @@ -0,0 +1,34 @@ +var st = new ShardingTest({ shards: { rs0: { quiet: '' }, rs1: { quiet: '' }}, mongos: 2 }); + +var testDB1 = st.s0.getDB('test'); +var testDB2 = st.s1.getDB('test'); + +// Trigger a query on mongos 1 so it will have a view of test.user as being unsharded. +testDB1.user.findOne(); + +testDB2.adminCommand({ enableSharding: 'test' }); +testDB2.adminCommand({ shardCollection: 'test.user', key: { x: 1 }}); + +testDB2.adminCommand({ split: 'test.user', middle: { x: 100 }}); + +var configDB2 = st.s1.getDB('config'); +var chunkToMove = configDB2.chunks.find().sort({ min: 1 }).next(); +var toShard = configDB2.shards.findOne({ _id: { $ne: chunkToMove.shard }})._id; +testDB2.adminCommand({ moveChunk: 'test.user', to: toShard, find: { x: 50 }}); + +for (var x = 0; x < 200; x++) { + testDB2.user.insert({ x: x }); +} + +testDB2.runCommand({ getLastError: 1 }); + +var cursor = testDB1.user.find({ x: 30 }).readPref('primary'); +assert(cursor.hasNext()); +assert.eq(30, cursor.next().x); + +cursor = testDB1.user.find({ x: 130 }).readPref('primary'); +assert(cursor.hasNext()); +assert.eq(130, cursor.next().x); + +st.stop(); + diff --git a/src/mongo/client/dbclient_rs.cpp b/src/mongo/client/dbclient_rs.cpp index 657731c40f0..437f75bcf6a 100644 --- a/src/mongo/client/dbclient_rs.cpp +++ b/src/mongo/client/dbclient_rs.cpp @@ -46,15 +46,19 @@ namespace mongo { * @param lastHost the last host returned (mainly used for doing round-robin). * Will be overwritten with the newly returned host if not empty. Should * never be NULL. + * @param isPrimarySelected out parameter that is set to true if the returned host + * is a primary. * * @return the host object of the node selected. If none of the nodes are - * eligible, returns an empty host. + * eligible, returns an empty host. Cannot be NULL and valid only if returned + * host is not empty. */ HostAndPort _selectNode(const vector<ReplicaSetMonitor::Node>& nodes, const BSONObj& readPreferenceTag, bool secOnly, int localThresholdMillis, - HostAndPort* lastHost /* in/out */) { + HostAndPort* lastHost /* in/out */, + bool* isPrimarySelected) { HostAndPort fallbackHost; // Implicit: start from index 0 if lastHost doesn't exist anymore @@ -85,6 +89,7 @@ namespace mongo { if (node.matchesTag(readPreferenceTag)) { // found an ok candidate; may not be local. fallbackHost = node.addr; + *isPrimarySelected = node.ismaster; if (node.isLocalSecondary(localThresholdMillis)) { // found a local node. return early. @@ -1006,14 +1011,15 @@ namespace mongo { } HostAndPort ReplicaSetMonitor::selectAndCheckNode(ReadPreference preference, - TagSet* tags) { + TagSet* tags, + bool* isPrimarySelected) { HostAndPort candidate; { scoped_lock lk(_lock); candidate = ReplicaSetMonitor::selectNode(_nodes, preference, tags, - _localThresholdMillis, &_lastReadPrefHost); + _localThresholdMillis, &_lastReadPrefHost, isPrimarySelected); } if (candidate.empty()) { @@ -1022,7 +1028,7 @@ namespace mongo { scoped_lock lk(_lock); return ReplicaSetMonitor::selectNode(_nodes, preference, tags, _localThresholdMillis, - &_lastReadPrefHost); + &_lastReadPrefHost, isPrimarySelected); } return candidate; @@ -1033,11 +1039,15 @@ namespace mongo { ReadPreference preference, TagSet* tags, int localThresholdMillis, - HostAndPort* lastHost) { + HostAndPort* lastHost, + bool* isPrimarySelected) { + *isPrimarySelected = false; + switch (preference) { case ReadPreference_PrimaryOnly: for (vector<Node>::const_iterator iter = nodes.begin(); iter != nodes.end(); ++iter) { if (iter->ismaster && iter->ok) { + *isPrimarySelected = true; return iter->addr; } } @@ -1047,14 +1057,14 @@ namespace mongo { case ReadPreference_PrimaryPreferred: { HostAndPort candidatePri = selectNode(nodes, ReadPreference_PrimaryOnly, tags, - localThresholdMillis, lastHost); + localThresholdMillis, lastHost, isPrimarySelected); if (!candidatePri.empty()) { return candidatePri; } return selectNode(nodes, ReadPreference_SecondaryOnly, tags, - localThresholdMillis, lastHost); + localThresholdMillis, lastHost, isPrimarySelected); } case ReadPreference_SecondaryOnly: @@ -1063,7 +1073,7 @@ namespace mongo { while (!tags->isExhausted()) { candidate = _selectNode(nodes, tags->getCurrentTag(), true, localThresholdMillis, - lastHost); + lastHost, isPrimarySelected); if (candidate.empty()) { tags->next(); @@ -1079,14 +1089,14 @@ namespace mongo { case ReadPreference_SecondaryPreferred: { HostAndPort candidateSec = selectNode(nodes, ReadPreference_SecondaryOnly, tags, - localThresholdMillis, lastHost); + localThresholdMillis, lastHost, isPrimarySelected); if (!candidateSec.empty()) { return candidateSec; } return selectNode(nodes, ReadPreference_PrimaryOnly, tags, - localThresholdMillis, lastHost); + localThresholdMillis, lastHost, isPrimarySelected); } case ReadPreference_Nearest: @@ -1095,7 +1105,7 @@ namespace mongo { while (!tags->isExhausted()) { candidate = _selectNode(nodes, tags->getCurrentTag(), false, localThresholdMillis, - lastHost); + lastHost, isPrimarySelected); if (candidate.empty()) { tags->next(); @@ -1559,12 +1569,24 @@ namespace mongo { } ReplicaSetMonitorPtr monitor = _getMonitor(); - _lastSlaveOkHost = monitor->selectAndCheckNode(preference, tags); + bool isPrimarySelected = false; + _lastSlaveOkHost = monitor->selectAndCheckNode(preference, tags, &isPrimarySelected); if ( _lastSlaveOkHost.empty() ){ return NULL; } + // Primary connection is special because it is the only connection that is + // versioned in mongos. Therefore, we have to make sure that this object + // maintains only one connection to the primary and use that connection + // every time we need to talk to the primary. + if (isPrimarySelected) { + checkMaster(); + _lastSlaveOkConn = _master; + _lastSlaveOkHost = _masterHost; // implied, but still assign just to be safe + return _master.get(); + } + _lastSlaveOkConn.reset(new DBClientConnection(true , this , _so_timeout)); _lastSlaveOkConn->connect(_lastSlaveOkHost); diff --git a/src/mongo/client/dbclient_rs.h b/src/mongo/client/dbclient_rs.h index 79d604384d8..1b21bae8876 100644 --- a/src/mongo/client/dbclient_rs.h +++ b/src/mongo/client/dbclient_rs.h @@ -147,6 +147,8 @@ namespace mongo { * robin, starting from the node next to this lastHost. This will be overwritten * with the newly chosen host if not empty, not primary and when preference * is not Nearest. + * @param isPrimarySelected out parameter that is set to true if the returned host + * is a primary. Cannot be NULL and valid only if returned host is not empty. * * @return the host object of the node selected. If none of the nodes are * eligible, returns an empty host. @@ -155,7 +157,8 @@ namespace mongo { ReadPreference preference, TagSet* tags, int localThresholdMillis, - HostAndPort* lastHost); + HostAndPort* lastHost, + bool* isPrimarySelected); /** * Selects the right node given the nodes to pick from and the preference. This @@ -163,14 +166,17 @@ namespace mongo { * if the primary node needs to be returned but is not currently available (except * for ReadPrefrence_Nearest). * - * @param preference the read mode to use - * @param tags the tags used for filtering nodes + * @param preference the read mode to use. + * @param tags the tags used for filtering nodes. + * @param isPrimarySelected out parameter that is set to true if the returned host + * is a primary. Cannot be NULL and valid only if returned host is not empty. * * @return the host object of the node selected. If none of the nodes are * eligible, returns an empty host. */ HostAndPort selectAndCheckNode(ReadPreference preference, - TagSet* tags); + TagSet* tags, + bool* isPrimarySelected); /** * Creates a new ReplicaSetMonitor, if it doesn't already exist. @@ -270,6 +276,8 @@ namespace mongo { */ bool isAnyNodeOk() const; + bool isHostPrimary(const std::string& hostName) const; + private: /** * This populates a list of hosts from the list of seeds (discarding the @@ -556,12 +564,18 @@ namespace mongo { string _setName; HostAndPort _masterHost; - scoped_ptr<DBClientConnection> _master; + // Note: reason why this is a shared_ptr is because we want _lastSlaveOkConn to + // keep a reference of the _master connection when it selected a primary node. + // This is because the primary connection is special in mongos - it is the only + // connection that is versioned. + // WARNING: do not assign this variable (which will increment the internal ref + // counter) to any other variable other than _lastSlaveOkConn. + boost::shared_ptr<DBClientConnection> _master; // Last used host in a slaveOk query (can be a primary) HostAndPort _lastSlaveOkHost; // Last used connection in a slaveOk query (can be a primary) - scoped_ptr<DBClientConnection> _lastSlaveOkConn; + boost::shared_ptr<DBClientConnection> _lastSlaveOkConn; double _so_timeout; diff --git a/src/mongo/dbtests/replica_set_monitor_test.cpp b/src/mongo/dbtests/replica_set_monitor_test.cpp index 717423b599e..e7965e35cda 100644 --- a/src/mongo/dbtests/replica_set_monitor_test.cpp +++ b/src/mongo/dbtests/replica_set_monitor_test.cpp @@ -369,8 +369,11 @@ namespace { TagSet tags(TagSetFixtures::getDefaultSet()); HostAndPort lastHost = nodes[0].addr; + bool isPrimarySelected = false; HostAndPort host = ReplicaSetMonitor::selectNode(nodes, - mongo::ReadPreference_PrimaryOnly, &tags, 3, &lastHost); + mongo::ReadPreference_PrimaryOnly, &tags, 3, &lastHost, + &isPrimarySelected); + ASSERT(isPrimarySelected); ASSERT_EQUALS("b", host.host()); } @@ -383,9 +386,12 @@ namespace { nodes[1].ok = false; + bool isPrimarySelected = false; HostAndPort host = ReplicaSetMonitor::selectNode(nodes, - mongo::ReadPreference_PrimaryOnly, &tags, 3, &lastHost); + mongo::ReadPreference_PrimaryOnly, &tags, 3, &lastHost, + &isPrimarySelected); + ASSERT(!isPrimarySelected); ASSERT(host.empty()); } @@ -397,8 +403,10 @@ namespace { nodes[1].ismaster = false; + bool isPrimarySelected = false; HostAndPort host = ReplicaSetMonitor::selectNode(nodes, - mongo::ReadPreference_PrimaryOnly, &tags, 3, &lastHost); + mongo::ReadPreference_PrimaryOnly, &tags, 3, &lastHost, + &isPrimarySelected); ASSERT(host.empty()); } @@ -410,9 +418,12 @@ namespace { TagSet tags(TagSetFixtures::getDefaultSet()); HostAndPort lastHost = nodes[0].addr; + bool isPrimarySelected = false; HostAndPort host = ReplicaSetMonitor::selectNode(nodes, - mongo::ReadPreference_PrimaryPreferred, &tags, 1, &lastHost); + mongo::ReadPreference_PrimaryPreferred, &tags, 1, &lastHost, + &isPrimarySelected); + ASSERT(isPrimarySelected); ASSERT_EQUALS("b", host.host()); } @@ -424,9 +435,12 @@ namespace { nodes[1].ok = false; + bool isPrimarySelected = false; HostAndPort host = ReplicaSetMonitor::selectNode(nodes, - mongo::ReadPreference_PrimaryPreferred, &tags, 1, &lastHost); + mongo::ReadPreference_PrimaryPreferred, &tags, 1, &lastHost, + &isPrimarySelected); + ASSERT(!isPrimarySelected); ASSERT_EQUALS("c", host.host()); ASSERT_EQUALS("c", lastHost.host()); } @@ -439,9 +453,12 @@ namespace { nodes[2].ok = false; + bool isPrimarySelected = false; HostAndPort host = ReplicaSetMonitor::selectNode(nodes, - mongo::ReadPreference_SecondaryOnly, &tags, 1, &lastHost); + mongo::ReadPreference_SecondaryOnly, &tags, 1, &lastHost, + &isPrimarySelected); + ASSERT(!isPrimarySelected); ASSERT_EQUALS("a", host.host()); ASSERT_EQUALS("a", lastHost.host()); } @@ -455,8 +472,10 @@ namespace { nodes[0].ok = false; nodes[2].ok = false; + bool isPrimarySelected = false; HostAndPort host = ReplicaSetMonitor::selectNode(nodes, - mongo::ReadPreference_SecondaryOnly, &tags, 1, &lastHost); + mongo::ReadPreference_SecondaryOnly, &tags, 1, &lastHost, + &isPrimarySelected); ASSERT(host.empty()); } @@ -469,9 +488,12 @@ namespace { nodes[2].ok = false; + bool isPrimarySelected = false; HostAndPort host = ReplicaSetMonitor::selectNode(nodes, - mongo::ReadPreference_SecondaryPreferred, &tags, 1, &lastHost); + mongo::ReadPreference_SecondaryPreferred, &tags, 1, &lastHost, + &isPrimarySelected); + ASSERT(!isPrimarySelected); ASSERT_EQUALS("a", host.host()); ASSERT_EQUALS("a", lastHost.host()); } @@ -485,9 +507,12 @@ namespace { nodes[0].ok = false; nodes[2].ok = false; + bool isPrimarySelected = false; HostAndPort host = ReplicaSetMonitor::selectNode(nodes, - mongo::ReadPreference_SecondaryPreferred, &tags, 1, &lastHost); + mongo::ReadPreference_SecondaryPreferred, &tags, 1, &lastHost, + &isPrimarySelected); + ASSERT(isPrimarySelected); ASSERT_EQUALS("b", host.host()); ASSERT_EQUALS("b", lastHost.host()); } @@ -502,8 +527,10 @@ namespace { nodes[1].ok = false; nodes[2].ok = false; + bool isPrimarySelected = false; HostAndPort host = ReplicaSetMonitor::selectNode(nodes, - mongo::ReadPreference_SecondaryPreferred, &tags, 1, &lastHost); + mongo::ReadPreference_SecondaryPreferred, &tags, 1, &lastHost, + &isPrimarySelected); ASSERT(host.empty()); } @@ -518,9 +545,12 @@ namespace { nodes[1].pingTimeMillis = 2; nodes[2].pingTimeMillis = 3; + bool isPrimarySelected = 0; HostAndPort host = ReplicaSetMonitor::selectNode(nodes, - mongo::ReadPreference_Nearest, &tags, 3, &lastHost); + mongo::ReadPreference_Nearest, &tags, 3, &lastHost, + &isPrimarySelected); + ASSERT(isPrimarySelected); ASSERT_EQUALS("b", host.host()); ASSERT_EQUALS("b", lastHost.host()); } @@ -535,8 +565,10 @@ namespace { nodes[1].pingTimeMillis = 20; nodes[2].pingTimeMillis = 30; + bool isPrimarySelected = false; HostAndPort host = ReplicaSetMonitor::selectNode(nodes, - mongo::ReadPreference_Nearest, &tags, 3, &lastHost); + mongo::ReadPreference_Nearest, &tags, 3, &lastHost, + &isPrimarySelected); ASSERT(!host.empty()); } @@ -547,9 +579,12 @@ namespace { TagSet tags(TagSetFixtures::getP2Tag()); HostAndPort lastHost = nodes[2].addr; + bool isPrimarySelected = false; HostAndPort host = ReplicaSetMonitor::selectNode(nodes, - mongo::ReadPreference_PrimaryOnly, &tags, 3, &lastHost); + mongo::ReadPreference_PrimaryOnly, &tags, 3, &lastHost, + &isPrimarySelected); + ASSERT(isPrimarySelected); // Note: PrimaryOnly ignores tag ASSERT_EQUALS("b", host.host()); } @@ -562,8 +597,10 @@ namespace { nodes[1].ok = false; + bool isPrimarySelected = false; HostAndPort host = ReplicaSetMonitor::selectNode(nodes, - mongo::ReadPreference_PrimaryPreferred, &tags, 3, &lastHost); + mongo::ReadPreference_PrimaryPreferred, &tags, 3, &lastHost, + &isPrimarySelected); ASSERT_EQUALS("c", host.host()); ASSERT_EQUALS("c", lastHost.host()); @@ -575,8 +612,10 @@ namespace { TagSet tags(TagSetFixtures::getSingleNoMatchTag()); HostAndPort lastHost = nodes[2].addr; + bool isPrimarySelected = false; HostAndPort host = ReplicaSetMonitor::selectNode(nodes, - mongo::ReadPreference_PrimaryPreferred, &tags, 3, &lastHost); + mongo::ReadPreference_PrimaryPreferred, &tags, 3, &lastHost, + &isPrimarySelected); ASSERT_EQUALS("b", host.host()); } @@ -589,8 +628,10 @@ namespace { nodes[1].ok = false; + bool isPrimarySelected = false; HostAndPort host = ReplicaSetMonitor::selectNode(nodes, - mongo::ReadPreference_PrimaryPreferred, &tags, 3, &lastHost); + mongo::ReadPreference_PrimaryPreferred, &tags, 3, &lastHost, + &isPrimarySelected); ASSERT(host.empty()); } @@ -601,9 +642,12 @@ namespace { TagSet tags(TagSetFixtures::getP2Tag()); HostAndPort lastHost = nodes[2].addr; + bool isPrimarySelected; HostAndPort host = ReplicaSetMonitor::selectNode(nodes, - mongo::ReadPreference_SecondaryOnly, &tags, 3, &lastHost); + mongo::ReadPreference_SecondaryOnly, &tags, 3, &lastHost, + &isPrimarySelected); + ASSERT(!isPrimarySelected); ASSERT_EQUALS("c", host.host()); ASSERT_EQUALS("c", lastHost.host()); } @@ -617,8 +661,10 @@ namespace { arrayBuilder.append(BSON("dc" << "sf")); TagSet tags(arrayBuilder.arr()); + bool isPrimarySelected = false; HostAndPort host = ReplicaSetMonitor::selectNode(nodes, - mongo::ReadPreference_SecondaryOnly, &tags, 3, &lastHost); + mongo::ReadPreference_SecondaryOnly, &tags, 3, &lastHost, + &isPrimarySelected); ASSERT(host.empty()); } @@ -629,8 +675,10 @@ namespace { TagSet tags(TagSetFixtures::getP2Tag()); HostAndPort lastHost = nodes[2].addr; + bool isPrimarySelected = false; HostAndPort host = ReplicaSetMonitor::selectNode(nodes, - mongo::ReadPreference_SecondaryPreferred, &tags, 3, &lastHost); + mongo::ReadPreference_SecondaryPreferred, &tags, 3, &lastHost, + &isPrimarySelected); ASSERT_EQUALS("c", host.host()); ASSERT_EQUALS("c", lastHost.host()); @@ -647,9 +695,12 @@ namespace { nodes[2].ok = false; + bool isPrimarySelected = false; HostAndPort host = ReplicaSetMonitor::selectNode(nodes, - mongo::ReadPreference_SecondaryPreferred, &tags, 3, &lastHost); + mongo::ReadPreference_SecondaryPreferred, &tags, 3, &lastHost, + &isPrimarySelected); + ASSERT(!isPrimarySelected); ASSERT_EQUALS("a", host.host()); ASSERT_EQUALS("a", lastHost.host()); } @@ -660,9 +711,12 @@ namespace { TagSet tags(TagSetFixtures::getSingleNoMatchTag()); HostAndPort lastHost = nodes[2].addr; + bool isPrimarySelected = false; HostAndPort host = ReplicaSetMonitor::selectNode(nodes, - mongo::ReadPreference_SecondaryPreferred, &tags, 3, &lastHost); + mongo::ReadPreference_SecondaryPreferred, &tags, 3, &lastHost, + &isPrimarySelected); + ASSERT(isPrimarySelected); ASSERT_EQUALS("b", host.host()); } @@ -674,8 +728,10 @@ namespace { nodes[1].ok = false; + bool isPrimarySelected = false; HostAndPort host = ReplicaSetMonitor::selectNode(nodes, - mongo::ReadPreference_SecondaryPreferred, &tags, 3, &lastHost); + mongo::ReadPreference_SecondaryPreferred, &tags, 3, &lastHost, + &isPrimarySelected); ASSERT(host.empty()); } @@ -686,9 +742,12 @@ namespace { TagSet tags(TagSetFixtures::getSingleNoMatchTag()); HostAndPort lastHost = nodes[2].addr; + bool isPrimarySelected = false; HostAndPort host = ReplicaSetMonitor::selectNode(nodes, - mongo::ReadPreference_SecondaryPreferred, &tags, 3, &lastHost); + mongo::ReadPreference_SecondaryPreferred, &tags, 3, &lastHost, + &isPrimarySelected); + ASSERT(isPrimarySelected); ASSERT_EQUALS("b", host.host()); } @@ -701,9 +760,12 @@ namespace { arrayBuilder.append(BSON("p" << "1")); TagSet tags(arrayBuilder.arr()); + bool isPrimarySelected = false; HostAndPort host = ReplicaSetMonitor::selectNode(nodes, - mongo::ReadPreference_Nearest, &tags, 3, &lastHost); + mongo::ReadPreference_Nearest, &tags, 3, &lastHost, + &isPrimarySelected); + ASSERT(!isPrimarySelected); ASSERT_EQUALS("a", host.host()); ASSERT_EQUALS("a", lastHost.host()); } @@ -714,8 +776,10 @@ namespace { TagSet tags(TagSetFixtures::getSingleNoMatchTag()); HostAndPort lastHost = nodes[1].addr; + bool isPrimarySelected = false; HostAndPort host = ReplicaSetMonitor::selectNode(nodes, - mongo::ReadPreference_Nearest, &tags, 3, &lastHost); + mongo::ReadPreference_Nearest, &tags, 3, &lastHost, + &isPrimarySelected); ASSERT(host.empty()); } @@ -726,9 +790,12 @@ namespace { TagSet tags(TagSetFixtures::getMultiNoMatchTag()); HostAndPort lastHost = nodes[1].addr; + bool isPrimarySelected = false; HostAndPort host = ReplicaSetMonitor::selectNode(nodes, - mongo::ReadPreference_PrimaryOnly, &tags, 3, &lastHost); + mongo::ReadPreference_PrimaryOnly, &tags, 3, &lastHost, + &isPrimarySelected); + ASSERT(isPrimarySelected); ASSERT_EQUALS("b", host.host()); ASSERT_EQUALS("b", lastHost.host()); } @@ -741,8 +808,10 @@ namespace { nodes[1].ok = false; + bool isPrimarySelected = false; HostAndPort host = ReplicaSetMonitor::selectNode(nodes, - mongo::ReadPreference_PrimaryOnly, &tags, 3, &lastHost); + mongo::ReadPreference_PrimaryOnly, &tags, 3, &lastHost, + &isPrimarySelected); ASSERT(host.empty()); } @@ -758,9 +827,12 @@ namespace { TagSet tags(arrayBuilder.arr()); HostAndPort lastHost = nodes[2].addr; + bool isPrimarySelected = false; HostAndPort host = ReplicaSetMonitor::selectNode(nodes, - mongo::ReadPreference_PrimaryPreferred, &tags, 3, &lastHost); + mongo::ReadPreference_PrimaryPreferred, &tags, 3, &lastHost, + &isPrimarySelected); + ASSERT(isPrimarySelected); ASSERT_EQUALS("b", host.host()); } @@ -839,10 +911,12 @@ namespace { nodes[1].ok = false; + bool isPrimarySelected = false; HostAndPort host = ReplicaSetMonitor::selectNode(nodes, mongo::ReadPreference_PrimaryPreferred, getMatchesFirstTagSet(), - 3, &lastHost); + 3, &lastHost, &isPrimarySelected); + ASSERT(!isPrimarySelected); ASSERT_EQUALS("a", host.host()); ASSERT_EQUALS("a", lastHost.host()); } @@ -854,10 +928,12 @@ namespace { nodes[0].ok = false; nodes[1].ok = false; + bool isPrimarySelected = false; HostAndPort host = ReplicaSetMonitor::selectNode(nodes, mongo::ReadPreference_PrimaryPreferred, getMatchesFirstTagSet(), - 3, &lastHost); + 3, &lastHost, &isPrimarySelected); + ASSERT(!isPrimarySelected); ASSERT_EQUALS("c", host.host()); ASSERT_EQUALS("c", lastHost.host()); } @@ -868,10 +944,12 @@ namespace { nodes[1].ok = false; + bool isPrimarySelected = false; HostAndPort host = ReplicaSetMonitor::selectNode(nodes, mongo::ReadPreference_PrimaryPreferred, getMatchesSecondTagSet(), - 3, &lastHost); + 3, &lastHost, &isPrimarySelected); + ASSERT(!isPrimarySelected); ASSERT_EQUALS("c", host.host()); ASSERT_EQUALS("c", lastHost.host()); } @@ -883,10 +961,12 @@ namespace { nodes[1].ok = false; nodes[2].ok = false; + bool isPrimarySelected = false; HostAndPort host = ReplicaSetMonitor::selectNode(nodes, mongo::ReadPreference_PrimaryPreferred, getMatchesSecondTagSet(), - 3, &lastHost); + 3, &lastHost, &isPrimarySelected); + ASSERT(!isPrimarySelected); ASSERT_EQUALS("a", host.host()); ASSERT_EQUALS("a", lastHost.host()); } @@ -897,10 +977,12 @@ namespace { nodes[1].ok = false; + bool isPrimarySelected = false; HostAndPort host = ReplicaSetMonitor::selectNode(nodes, mongo::ReadPreference_PrimaryPreferred, getMatchesLastTagSet(), - 3, &lastHost); + 3, &lastHost, &isPrimarySelected); + ASSERT(!isPrimarySelected); ASSERT_EQUALS("a", host.host()); ASSERT_EQUALS("a", lastHost.host()); } @@ -912,9 +994,10 @@ namespace { nodes[0].ok = false; nodes[1].ok = false; + bool isPrimarySelected = false; HostAndPort host = ReplicaSetMonitor::selectNode(nodes, mongo::ReadPreference_PrimaryPreferred, getMatchesLastTagSet(), - 3, &lastHost); + 3, &lastHost, &isPrimarySelected); ASSERT(host.empty()); } @@ -926,9 +1009,12 @@ namespace { TagSet tags(TagSetFixtures::getMultiNoMatchTag()); HostAndPort lastHost = nodes[2].addr; + bool isPrimarySelected = false; HostAndPort host = ReplicaSetMonitor::selectNode(nodes, - mongo::ReadPreference_PrimaryPreferred, &tags, 3, &lastHost); + mongo::ReadPreference_PrimaryPreferred, &tags, 3, &lastHost, + &isPrimarySelected); + ASSERT(isPrimarySelected); ASSERT_EQUALS("b", host.host()); } @@ -940,8 +1026,10 @@ namespace { nodes[1].ok = false; + bool isPrimarySelected = false; HostAndPort host = ReplicaSetMonitor::selectNode(nodes, - mongo::ReadPreference_PrimaryPreferred, &tags, 3, &lastHost); + mongo::ReadPreference_PrimaryPreferred, &tags, 3, &lastHost, + &isPrimarySelected); ASSERT(host.empty()); } @@ -950,10 +1038,12 @@ namespace { vector<ReplicaSetMonitor::Node> nodes = getNodes(); HostAndPort lastHost = nodes[2].addr; + bool isPrimarySelected = false; HostAndPort host = ReplicaSetMonitor::selectNode(nodes, mongo::ReadPreference_SecondaryOnly, getMatchesFirstTagSet(), - 3, &lastHost); + 3, &lastHost, &isPrimarySelected); + ASSERT(!isPrimarySelected); ASSERT_EQUALS("a", host.host()); ASSERT_EQUALS("a", lastHost.host()); } @@ -964,10 +1054,12 @@ namespace { nodes[0].ok = false; + bool isPrimarySelected = false; HostAndPort host = ReplicaSetMonitor::selectNode(nodes, mongo::ReadPreference_SecondaryOnly, getMatchesFirstTagSet(), - 3, &lastHost); + 3, &lastHost, &isPrimarySelected); + ASSERT(!isPrimarySelected); ASSERT_EQUALS("c", host.host()); ASSERT_EQUALS("c", lastHost.host()); } @@ -976,10 +1068,12 @@ namespace { vector<ReplicaSetMonitor::Node> nodes = getNodes(); HostAndPort lastHost = nodes[2].addr; + bool isPrimarySelected = false; HostAndPort host = ReplicaSetMonitor::selectNode(nodes, mongo::ReadPreference_SecondaryOnly, getMatchesSecondTagSet(), - 3, &lastHost); + 3, &lastHost, &isPrimarySelected); + ASSERT(!isPrimarySelected); ASSERT_EQUALS("c", host.host()); ASSERT_EQUALS("c", lastHost.host()); } @@ -990,10 +1084,12 @@ namespace { nodes[2].ok = false; + bool isPrimarySelected = false; HostAndPort host = ReplicaSetMonitor::selectNode(nodes, mongo::ReadPreference_SecondaryOnly, getMatchesSecondTagSet(), - 3, &lastHost); + 3, &lastHost, &isPrimarySelected); + ASSERT(isPrimarySelected); ASSERT_EQUALS("a", host.host()); ASSERT_EQUALS("a", lastHost.host()); } @@ -1002,10 +1098,12 @@ namespace { vector<ReplicaSetMonitor::Node> nodes = getNodes(); HostAndPort lastHost = nodes[2].addr; + bool isPrimarySelected = false; HostAndPort host = ReplicaSetMonitor::selectNode(nodes, mongo::ReadPreference_SecondaryOnly, getMatchesLastTagSet(), - 3, &lastHost); + 3, &lastHost, &isPrimarySelected); + ASSERT(!isPrimarySelected); ASSERT_EQUALS("a", host.host()); ASSERT_EQUALS("a", lastHost.host()); } @@ -1016,9 +1114,10 @@ namespace { nodes[0].ok = false; + bool isPrimarySelected = false; HostAndPort host = ReplicaSetMonitor::selectNode(nodes, mongo::ReadPreference_SecondaryOnly, getMatchesLastTagSet(), - 3, &lastHost); + 3, &lastHost, &isPrimarySelected); ASSERT(host.empty()); } @@ -1028,10 +1127,12 @@ namespace { NodeSetFixtures::getThreeMemberWithTags(); HostAndPort lastHost = nodes[2].addr; + bool isPrimarySelected = false; HostAndPort host = ReplicaSetMonitor::selectNode(nodes, mongo::ReadPreference_SecondaryOnly, getMatchesPriTagSet(), - 3, &lastHost); + 3, &lastHost, &isPrimarySelected); + ASSERT(!isPrimarySelected); ASSERT_EQUALS("a", host.host()); ASSERT_EQUALS("a", lastHost.host()); } @@ -1042,8 +1143,10 @@ namespace { TagSet tags(TagSetFixtures::getMultiNoMatchTag()); HostAndPort lastHost = nodes[2].addr; + bool isPrimarySelected = false; HostAndPort host = ReplicaSetMonitor::selectNode(nodes, - mongo::ReadPreference_SecondaryOnly, &tags, 3, &lastHost); + mongo::ReadPreference_SecondaryOnly, &tags, 3, &lastHost, + &isPrimarySelected); ASSERT(host.empty()); } @@ -1052,10 +1155,12 @@ namespace { vector<ReplicaSetMonitor::Node> nodes = getNodes(); HostAndPort lastHost = nodes[2].addr; + bool isPrimarySelected = false; HostAndPort host = ReplicaSetMonitor::selectNode(nodes, mongo::ReadPreference_SecondaryPreferred, getMatchesFirstTagSet(), - 3, &lastHost); + 3, &lastHost, &isPrimarySelected); + ASSERT(isPrimarySelected); ASSERT_EQUALS("a", host.host()); ASSERT_EQUALS("a", lastHost.host()); } @@ -1066,22 +1171,26 @@ namespace { nodes[0].ok = false; + bool isPrimarySelected = false; HostAndPort host = ReplicaSetMonitor::selectNode(nodes, mongo::ReadPreference_SecondaryPreferred, getMatchesFirstTagSet(), - 3, &lastHost); + 3, &lastHost, &isPrimarySelected); + ASSERT(isPrimarySelected); ASSERT_EQUALS("c", host.host()); ASSERT_EQUALS("c", lastHost.host()); } TEST_F(MultiTags, SecPrefMatchesSecond) { vector<ReplicaSetMonitor::Node> nodes = getNodes(); - HostAndPort lastHost = nodes[2].addr; + HostAndPort lastHost = nodes[2].addr; + bool isPrimarySelected = false; HostAndPort host = ReplicaSetMonitor::selectNode(nodes, mongo::ReadPreference_SecondaryPreferred, getMatchesSecondTagSet(), - 3, &lastHost); + 3, &lastHost, &isPrimarySelected); + ASSERT(!isPrimarySelected); ASSERT_EQUALS("c", host.host()); ASSERT_EQUALS("c", lastHost.host()); } @@ -1092,10 +1201,12 @@ namespace { nodes[2].ok = false; + bool isPrimarySelected = false; HostAndPort host = ReplicaSetMonitor::selectNode(nodes, mongo::ReadPreference_SecondaryPreferred, getMatchesSecondTagSet(), - 3, &lastHost); + 3, &lastHost, &isPrimarySelected); + ASSERT(!isPrimarySelected); ASSERT_EQUALS("a", host.host()); ASSERT_EQUALS("a", lastHost.host()); } @@ -1104,10 +1215,12 @@ namespace { vector<ReplicaSetMonitor::Node> nodes = getNodes(); HostAndPort lastHost = nodes[2].addr; + bool isPrimarySelected = false; HostAndPort host = ReplicaSetMonitor::selectNode(nodes, mongo::ReadPreference_SecondaryPreferred, getMatchesLastTagSet(), - 3, &lastHost); + 3, &lastHost, &isPrimarySelected); + ASSERT(!isPrimarySelected); ASSERT_EQUALS("a", host.host()); ASSERT_EQUALS("a", lastHost.host()); } @@ -1118,10 +1231,12 @@ namespace { nodes[0].ok = false; + bool isPrimarySelected = false; HostAndPort host = ReplicaSetMonitor::selectNode(nodes, mongo::ReadPreference_SecondaryPreferred, getMatchesLastTagSet(), - 3, &lastHost); + 3, &lastHost, &isPrimarySelected); + ASSERT(isPrimarySelected); ASSERT_EQUALS("b", host.host()); } @@ -1129,10 +1244,12 @@ namespace { vector<ReplicaSetMonitor::Node> nodes = getNodes(); HostAndPort lastHost = nodes[2].addr; + bool isPrimarySelected = false; HostAndPort host = ReplicaSetMonitor::selectNode(nodes, mongo::ReadPreference_SecondaryPreferred, getMatchesPriTagSet(), - 3, &lastHost); + 3, &lastHost, &isPrimarySelected); + ASSERT(!isPrimarySelected); ASSERT_EQUALS("a", host.host()); ASSERT_EQUALS("a", lastHost.host()); } @@ -1143,9 +1260,12 @@ namespace { TagSet tags(TagSetFixtures::getMultiNoMatchTag()); HostAndPort lastHost = nodes[2].addr; + bool isPrimarySelected = false; HostAndPort host = ReplicaSetMonitor::selectNode(nodes, - mongo::ReadPreference_SecondaryPreferred, &tags, 3, &lastHost); + mongo::ReadPreference_SecondaryPreferred, &tags, 3, &lastHost, + &isPrimarySelected); + ASSERT(isPrimarySelected); ASSERT_EQUALS("b", host.host()); } @@ -1157,8 +1277,10 @@ namespace { nodes[1].ok = false; + bool isPrimarySelected = false; HostAndPort host = ReplicaSetMonitor::selectNode(nodes, - mongo::ReadPreference_SecondaryPreferred, &tags, 3, &lastHost); + mongo::ReadPreference_SecondaryPreferred, &tags, 3, &lastHost, + &isPrimarySelected); ASSERT(host.empty()); } @@ -1167,10 +1289,12 @@ namespace { vector<ReplicaSetMonitor::Node> nodes = getNodes(); HostAndPort lastHost = nodes[2].addr; + bool isPrimarySelected = false; HostAndPort host = ReplicaSetMonitor::selectNode(nodes, mongo::ReadPreference_Nearest, getMatchesFirstTagSet(), - 3, &lastHost); + 3, &lastHost, &isPrimarySelected); + ASSERT(!isPrimarySelected); ASSERT_EQUALS("a", host.host()); ASSERT_EQUALS("a", lastHost.host()); } @@ -1187,9 +1311,12 @@ namespace { nodes[0].ok = false; + bool isPrimarySelected = false; HostAndPort host = ReplicaSetMonitor::selectNode(nodes, - mongo::ReadPreference_Nearest, &tags, 3, &lastHost); + mongo::ReadPreference_Nearest, &tags, 3, &lastHost, + &isPrimarySelected); + ASSERT(isPrimarySelected); ASSERT_EQUALS("b", host.host()); ASSERT_EQUALS("b", lastHost.host()); } @@ -1198,9 +1325,12 @@ namespace { vector<ReplicaSetMonitor::Node> nodes = getNodes(); HostAndPort lastHost = nodes[2].addr; + bool isPrimarySelected = false; HostAndPort host = ReplicaSetMonitor::selectNode(nodes, - mongo::ReadPreference_Nearest, getMatchesSecondTagSet(), 3, &lastHost); + mongo::ReadPreference_Nearest, getMatchesSecondTagSet(), 3, &lastHost, + &isPrimarySelected); + ASSERT(!isPrimarySelected); ASSERT_EQUALS("c", host.host()); ASSERT_EQUALS("c", lastHost.host()); } @@ -1218,9 +1348,12 @@ namespace { nodes[2].ok = false; + bool isPrimarySelected = false; HostAndPort host = ReplicaSetMonitor::selectNode(nodes, - mongo::ReadPreference_Nearest, &tags, 3, &lastHost); + mongo::ReadPreference_Nearest, &tags, 3, &lastHost, + &isPrimarySelected); + ASSERT(isPrimarySelected); ASSERT_EQUALS("b", host.host()); ASSERT_EQUALS("b", lastHost.host()); } @@ -1229,9 +1362,12 @@ namespace { vector<ReplicaSetMonitor::Node> nodes = getNodes(); HostAndPort lastHost = nodes[2].addr; + bool isPrimarySelected = false; HostAndPort host = ReplicaSetMonitor::selectNode(nodes, - mongo::ReadPreference_Nearest, getMatchesLastTagSet(), 3, &lastHost); + mongo::ReadPreference_Nearest, getMatchesLastTagSet(), 3, &lastHost, + &isPrimarySelected); + ASSERT(!isPrimarySelected); ASSERT_EQUALS("a", host.host()); ASSERT_EQUALS("a", lastHost.host()); } @@ -1242,8 +1378,10 @@ namespace { nodes[0].ok = false; + bool isPrimarySelected = false; HostAndPort host = ReplicaSetMonitor::selectNode(nodes, - mongo::ReadPreference_Nearest, getMatchesLastTagSet(), 3, &lastHost); + mongo::ReadPreference_Nearest, getMatchesLastTagSet(), 3, &lastHost, + &isPrimarySelected); ASSERT(host.empty()); } @@ -1253,9 +1391,12 @@ namespace { NodeSetFixtures::getThreeMemberWithTags(); HostAndPort lastHost = nodes[2].addr; + bool isPrimarySelected = false; HostAndPort host = ReplicaSetMonitor::selectNode(nodes, - mongo::ReadPreference_Nearest, getMatchesPriTagSet(), 3, &lastHost); + mongo::ReadPreference_Nearest, getMatchesPriTagSet(), 3, &lastHost, + &isPrimarySelected); + ASSERT(isPrimarySelected); ASSERT_EQUALS("b", host.host()); ASSERT_EQUALS("b", lastHost.host()); } @@ -1266,8 +1407,10 @@ namespace { TagSet tags(TagSetFixtures::getMultiNoMatchTag()); HostAndPort lastHost = nodes[2].addr; + bool isPrimarySelected = false; HostAndPort host = ReplicaSetMonitor::selectNode(nodes, - mongo::ReadPreference_Nearest, &tags, 3, &lastHost); + mongo::ReadPreference_Nearest, &tags, 3, &lastHost, + &isPrimarySelected); ASSERT(host.empty()); } |