summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEliot Horowitz <eliot@10gen.com>2012-11-18 10:50:42 -0500
committerDan Pasette <dan@10gen.com>2013-01-08 02:06:21 -0500
commitb7b2db71ba49e3092119869bcccf90a6d4c45df3 (patch)
treef720b666f4bcfcaebfb241f5dd8bc6d7b0505309
parent8885784cf186b48bda5bd0ee99ff0bfa0a76ff69 (diff)
downloadmongo-b7b2db71ba49e3092119869bcccf90a6d4c45df3.tar.gz
SERVER-7704 - make Shard == handle replica set changes
Conflicts: src/mongo/SConscript note: removed mocklib from shard_test.cpp deps
-rw-r--r--src/mongo/SConscript10
-rw-r--r--src/mongo/client/dbclient.cpp28
-rw-r--r--src/mongo/client/dbclientinterface.h7
-rw-r--r--src/mongo/s/shard.h10
-rw-r--r--src/mongo/s/shard_test.cpp60
5 files changed, 107 insertions, 8 deletions
diff --git a/src/mongo/SConscript b/src/mongo/SConscript
index ccb0367d9f3..0cfbedcc8a4 100644
--- a/src/mongo/SConscript
+++ b/src/mongo/SConscript
@@ -344,6 +344,16 @@ env.CppUnitTest( "balancer_policy_test" , [ "s/balancer_policy_tests.cpp" ] ,
LIBDEPS=["mongoscore", "coreshard","mongocommon","coreserver","coredb","dbcmdline","mongodandmongos"] ,
NO_CRUTCH=True)
+env.CppUnitTest("shard_test", [ "s/shard_test.cpp" ],
+ LIBDEPS=[ "mongoscore",
+ "coreshard",
+ "mongocommon",
+ "coreserver",
+ "coredb",
+ "dbcmdline",
+ "mongodandmongos"],
+ NO_CRUTCH=True)
+
serverOnlyFiles += [ "s/d_logic.cpp",
"s/d_writeback.cpp",
"s/d_migrate.cpp",
diff --git a/src/mongo/client/dbclient.cpp b/src/mongo/client/dbclient.cpp
index 1177cfd1172..25457410344 100644
--- a/src/mongo/client/dbclient.cpp
+++ b/src/mongo/client/dbclient.cpp
@@ -154,6 +154,34 @@ namespace mongo {
return 0;
}
+ bool ConnectionString::sameLogicalEndpoint( const ConnectionString& other ) const {
+ if ( _type != other._type )
+ return false;
+
+ switch ( _type ) {
+ case INVALID:
+ // both are invalid
+ // I think that should be ==
+ return true;
+ case MASTER:
+ return _servers[0] == other._servers[0];
+ case PAIR:
+ if ( _servers[0] == other._servers[0] )
+ return _servers[1] == other._servers[1];
+ return
+ ( _servers[0] == other._servers[1] ) &&
+ ( _servers[1] == other._servers[0] );
+ case SET:
+ // should this also make sure at least one host overlaps?
+ return _setName == other._setName;
+ case SYNC:
+ // should this check the _servers array instead?
+ return _string == other._string;
+ case CUSTOM:
+ return _string == other._string;
+ }
+ }
+
ConnectionString ConnectionString::parse( const string& host , string& errmsg ) {
string::size_type i = host.find( '/' );
diff --git a/src/mongo/client/dbclientinterface.h b/src/mongo/client/dbclientinterface.h
index e615e6abbaf..a47625c6cff 100644
--- a/src/mongo/client/dbclientinterface.h
+++ b/src/mongo/client/dbclientinterface.h
@@ -248,6 +248,13 @@ namespace mongo {
ConnectionType type() const { return _type; }
+ /**
+ * this returns true if this and other point to the same logical entity
+ * for single nodes, thats the same address
+ * for replica sets, thats the same name
+ */
+ bool sameLogicalEndpoint( const ConnectionString& other ) const;
+
static ConnectionString parse( const string& url , string& errmsg );
static string typeToString( ConnectionType type );
diff --git a/src/mongo/s/shard.h b/src/mongo/s/shard.h
index 3d4c7d05d5a..c02869e85d3 100644
--- a/src/mongo/s/shard.h
+++ b/src/mongo/s/shard.h
@@ -100,19 +100,13 @@ namespace mongo {
bool operator==( const Shard& s ) const {
bool n = _name == s._name;
- bool a = _addr == s._addr;
-
- verify( n == a ); // names and address are 1 to 1
- return n;
+ return n && _cs.sameLogicalEndpoint( s._cs );
}
bool operator!=( const Shard& s ) const {
- bool n = _name == s._name;
- bool a = _addr == s._addr;
- return ! ( n && a );
+ return ! ( *this == s );
}
-
bool operator==( const string& s ) const {
return _name == s || _addr == s;
}
diff --git a/src/mongo/s/shard_test.cpp b/src/mongo/s/shard_test.cpp
new file mode 100644
index 00000000000..413f8ab6f07
--- /dev/null
+++ b/src/mongo/s/shard_test.cpp
@@ -0,0 +1,60 @@
+// shard_test.cpp
+
+/**
+* Copyright (C) 2008 10gen Inc.
+*
+* This program is free software: you can redistribute it and/or modify
+* it under the terms of the GNU Affero General Public License, version 3,
+* as published by the Free Software Foundation.
+*
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU Affero General Public License for more details.
+*
+* You should have received a copy of the GNU Affero General Public License
+* along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "mongo/s/shard.h"
+#include "mongo/unittest/unittest.h"
+
+namespace mongo {
+
+ // Note: these are all crutch and hopefully will eventually go away
+ CmdLine cmdLine;
+
+ bool inShutdown() {
+ return false;
+ }
+
+ void setupSignals(bool inFork) {}
+
+ DBClientBase *createDirectClient() { return NULL; }
+
+ void dbexit(ExitCode rc, const char *why){
+ ::_exit(-1);
+ }
+
+ bool haveLocalShardingInfo(const string& ns) {
+ return false;
+ }
+
+ // -----------------------------------
+
+ TEST( Shard, Simple1 ) {
+ Shard a( "foo", "bar/a,b" );
+ Shard b( "foo", "bar/a,b" );
+ ASSERT_EQUALS( a, b );
+
+ b = Shard( "foo", "bar/b,a" );
+ ASSERT_EQUALS( a, b );
+ }
+
+ TEST( Shard, Simple2 ) {
+ ASSERT_EQUALS( Shard( "foo", "b.foo.com:123"), Shard( "foo", "b.foo.com:123") );
+ ASSERT_NOT_EQUALS( Shard( "foo", "b.foo.com:123"), Shard( "foo", "a.foo.com:123") );
+ ASSERT_NOT_EQUALS( Shard( "foo", "b.foo.com:123"), Shard( "foo", "b.foo.com:124") );
+ ASSERT_NOT_EQUALS( Shard( "foo", "b.foo.com:123"), Shard( "foa", "b.foo.com:123") );
+ }
+}