summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRandolph Tan <randolph@10gen.com>2012-07-23 14:57:45 -0400
committerRandolph Tan <randolph@10gen.com>2012-07-27 10:51:17 -0400
commitb83edef89f7f7194fd247c813d9fb0aaf59a3873 (patch)
treeb2274740d796f925a1545e3343f90324d5a1629e
parentc41ed5c474f4da4a4bbb3890f2e1d4171de02d42 (diff)
downloadmongo-b83edef89f7f7194fd247c813d9fb0aaf59a3873.tar.gz
SERVER-6565 Do not use primary if secondaries are available for slaveOk
-rw-r--r--src/mongo/client/dbclient_rs.cpp7
-rw-r--r--src/mongo/client/dbclient_rs.h5
-rw-r--r--src/mongo/dbtests/replica_set_monitor_test.cpp4
3 files changed, 13 insertions, 3 deletions
diff --git a/src/mongo/client/dbclient_rs.cpp b/src/mongo/client/dbclient_rs.cpp
index 8df431a89eb..4f3853e4161 100644
--- a/src/mongo/client/dbclient_rs.cpp
+++ b/src/mongo/client/dbclient_rs.cpp
@@ -1192,7 +1192,12 @@ namespace mongo {
return false;
}
- if (readPreference == ReadPreference_SecondaryOnly &&
+ if ((readPreference == ReadPreference_SecondaryOnly ||
+ /* This is the original behavior for slaveOk. This can result to reading
+ * data back in time, but the main idea here is to avoid overloading the
+ * primary when secondary is available.
+ */
+ readPreference == ReadPreference_SecondaryPreferred) &&
!okForSecondaryQueries()) {
return false;
}
diff --git a/src/mongo/client/dbclient_rs.h b/src/mongo/client/dbclient_rs.h
index 9cda552a1e2..98d27fc9944 100644
--- a/src/mongo/client/dbclient_rs.h
+++ b/src/mongo/client/dbclient_rs.h
@@ -99,6 +99,11 @@ namespace mongo {
}
/**
+ * Checks whether this nodes is compatible with the given readPreference and
+ * tag. Compatibility check is strict in the sense that secondary preferred
+ * is treated like secondary only and primary preferred is treated like
+ * primary only.
+ *
* @return true if this node is compatible with the read preference and tags.
*/
bool isCompatible(ReadPreference readPreference, const TagSet* tag) const;
diff --git a/src/mongo/dbtests/replica_set_monitor_test.cpp b/src/mongo/dbtests/replica_set_monitor_test.cpp
index 771ee60c0f7..b2a7377c068 100644
--- a/src/mongo/dbtests/replica_set_monitor_test.cpp
+++ b/src/mongo/dbtests/replica_set_monitor_test.cpp
@@ -199,7 +199,7 @@ namespace {
ASSERT( node.isCompatible( ReadPreference_PrimaryOnly, &tags ));
ASSERT( node.isCompatible( ReadPreference_PrimaryPreferred, &tags ));
- ASSERT( node.isCompatible( ReadPreference_SecondaryPreferred, &tags ));
+ ASSERT( !node.isCompatible( ReadPreference_SecondaryPreferred, &tags ));
ASSERT( !node.isCompatible( ReadPreference_SecondaryOnly, &tags ));
ASSERT( node.isCompatible( ReadPreference_Nearest, &tags ));
}
@@ -292,7 +292,7 @@ namespace {
ASSERT( node.isCompatible( ReadPreference_PrimaryOnly, &tags ));
ASSERT( node.isCompatible( ReadPreference_PrimaryPreferred, &tags ));
- ASSERT( node.isCompatible( ReadPreference_SecondaryPreferred, &tags ));
+ ASSERT( !node.isCompatible( ReadPreference_SecondaryPreferred, &tags ));
ASSERT( !node.isCompatible( ReadPreference_SecondaryOnly, &tags ));
ASSERT( node.isCompatible( ReadPreference_Nearest, &tags ));
}