diff options
-rw-r--r-- | jstests/sharding/movePrimary1.js | 4 | ||||
-rw-r--r-- | jstests/sharding/shard1.js | 2 | ||||
-rw-r--r-- | s/config.cpp | 137 | ||||
-rw-r--r-- | s/config.h | 9 | ||||
-rw-r--r-- | s/server.cpp | 12 | ||||
-rw-r--r-- | shell/servers.js | 6 |
6 files changed, 142 insertions, 28 deletions
diff --git a/jstests/sharding/movePrimary1.js b/jstests/sharding/movePrimary1.js index 20dc6c16fd0..81a9eb07dfa 100644 --- a/jstests/sharding/movePrimary1.js +++ b/jstests/sharding/movePrimary1.js @@ -19,9 +19,9 @@ to = s.getOther( from ); assert.eq( 3 , from.getDB( "test1" ).foo.count() , "from doesn't have data before move" ); assert.eq( 0 , to.getDB( "test1" ).foo.count() , "to has data before move" ); -assert.eq( s.config.databases.findOne( { name : "test1" } ).primary , from.name , "not in db correctly to start" ); +assert.eq( s.config.databases.findOne( { _id : "test1" } ).primary , from.name , "not in db correctly to start" ); s.admin.runCommand( { moveprimary : "test1" , to : to.name } ); -assert.eq( s.config.databases.findOne( { name : "test1" } ).primary , to.name , "to in config db didn't change" ); +assert.eq( s.config.databases.findOne( { _id : "test1" } ).primary , to.name , "to in config db didn't change" ); assert.eq( 0 , from.getDB( "test1" ).foo.count() , "from still has data after move" ); diff --git a/jstests/sharding/shard1.js b/jstests/sharding/shard1.js index bbe1144bf07..47c0538af2d 100644 --- a/jstests/sharding/shard1.js +++ b/jstests/sharding/shard1.js @@ -18,7 +18,7 @@ s.adminCommand( { enablesharding : "test" } ); assert.eq( 3 , db.foo.find().length() , "after partitioning count failed" ); s.adminCommand( shardCommand ); -dbconfig = s.config.databases.findOne( { name : "test" } ); +dbconfig = s.config.databases.findOne( { _id : "test" } ); assert.eq( dbconfig.sharded["test.foo"] , { key : { num : 1 } , unique : false } , "Sharded content" ); assert.eq( 1 , s.config.chunks.count() ); diff --git a/s/config.cpp b/s/config.cpp index 509cdec65a9..e04b5c09d64 100644 --- a/s/config.cpp +++ b/s/config.cpp @@ -30,9 +30,13 @@ namespace mongo { - int ConfigServer::VERSION = 2; + int ConfigServer::VERSION = 3; Shard Shard::EMPTY; + string ShardNS::database = "config.databases"; + string ShardNS::chunk = "config.chunks"; + string ShardNS::shard = "config.shards"; + /* --- DBConfig --- */ string DBConfig::modelServer() { @@ -112,7 +116,7 @@ namespace mongo { } void DBConfig::serialize(BSONObjBuilder& to){ - to.append("name", _name); + to.append("_id", _name); to.appendBool("partitioned", _shardingEnabled ); to.append("primary", _primary ); @@ -129,7 +133,7 @@ namespace mongo { } void DBConfig::unserialize(const BSONObj& from){ - _name = from.getStringField("name"); + _name = from.getStringField("_id"); log(1) << "DBConfig unserialize: " << _name << " " << from << endl; _shardingEnabled = from.getBoolField("partitioned"); @@ -163,7 +167,7 @@ namespace mongo { bool DBConfig::doload(){ BSONObjBuilder b; - b.append("name", _name.c_str()); + b.append("_id", _name.c_str()); BSONObj q = b.done(); return load(q); } @@ -441,7 +445,7 @@ namespace mongo { uassert( 10189 , "should only have 1 thing in config.version" , ! c->more() ); } else { - if ( conn.count( "config.shard" ) || conn.count( "config.databases" ) ){ + if ( conn.count( ShardNS::shard ) || conn.count( ShardNS::database ) ){ version = 1; } } @@ -449,7 +453,7 @@ namespace mongo { return version; } - int ConfigServer::checkConfigVersion(){ + int ConfigServer::checkConfigVersion( bool upgrade ){ int cur = dbConfigVersion(); if ( cur == VERSION ) return 0; @@ -462,7 +466,111 @@ namespace mongo { conn.done(); return 0; } + + if ( cur == 2 ){ + + // need to upgrade + assert( VERSION == 3 ); + if ( ! upgrade ){ + log() << "newer version of mongo meta data\n" + << "need to --upgrade after shutting all mongos down" + << endl; + return -9; + } + + ShardConnection conn( _primary ); + + // do a backup + string backupName; + { + stringstream ss; + ss << "config-backup-" << terseCurrentTime(); + backupName = ss.str(); + } + log() << "backing up config to: " << backupName << endl; + conn->copyDatabase( "config" , backupName ); + + map<string,string> hostToShard; + set<string> shards; + // shards + { + unsigned n = 0; + auto_ptr<DBClientCursor> c = conn->query( ShardNS::shard , BSONObj() ); + while ( c->more() ){ + BSONObj o = c->next(); + string host = o["host"].String(); + + string name = ""; + + BSONElement id = o["_id"]; + if ( id.type() == String ){ + name = id.String(); + } + else { + stringstream ss; + ss << "shard" << hostToShard.size(); + name = ss.str(); + } + + hostToShard[host] = name; + shards.insert( name ); + n++; + } + + assert( n == hostToShard.size() ); + assert( n == shards.size() ); + + conn->remove( ShardNS::shard , BSONObj() ); + + for ( map<string,string>::iterator i=hostToShard.begin(); i != hostToShard.end(); i++ ){ + conn->insert( ShardNS::shard , BSON( "_id" << i->second << "host" << i->first ) ); + } + } + // databases + { + auto_ptr<DBClientCursor> c = conn->query( ShardNS::database , BSONObj() ); + map<string,BSONObj> newDBs; + unsigned n = 0; + while ( c->more() ){ + BSONObj old = c->next(); + + BSONObjBuilder b(old.objsize()); + b.appendAs( old["name"] , "_id" ); + + BSONObjIterator i(old); + while ( i.more() ){ + BSONElement e = i.next(); + if ( strcmp( "_id" , e.fieldName() ) == 0 || + strcmp( "name" , e.fieldName() ) == 0 ){ + continue; + } + + b.append( e ); + } + + BSONObj x = b.obj(); + cout << old << "\n\t" << x << endl; + newDBs[old["name"].String()] = x; + n++; + } + + assert( n == newDBs.size() ); + + conn->remove( ShardNS::database , BSONObj() ); + + for ( map<string,BSONObj>::iterator i=newDBs.begin(); i!=newDBs.end(); i++ ){ + conn->insert( ShardNS::database , i->second ); + } + + } + + conn->update( "config.version" , BSONObj() , BSON( "_id" << 1 << "version" << VERSION ) ); + conn.done(); + pool.flush(); + return 1; + } + log() << "don't know how to upgrade " << cur << " to " << VERSION << endl; return -8; } @@ -521,18 +629,7 @@ namespace mongo { } stringstream id; - id << ourHostname << "-"; - { - struct tm t; - time_t_to_Struct( time(0) , &t ); - id << ( 1900 + t.tm_year ) << "-" - << t.tm_mon << "-" - << t.tm_mday << "-" - << t.tm_hour << "-" - << t.tm_min << "-" - ; - } - id << num++; + id << ourHostname << "-" << terseCurrentTime() << "-" << num++; conn->insert( "config.changelog" , BSON( "_id" << id.str() << "server" << ourHostname << @@ -568,7 +665,7 @@ namespace mongo { void a(){ BSONObjBuilder b; - b << "name" << "abc"; + b << "_id" << "abc"; b.appendBool( "partitioned" , true ); b << "primary" << "myserver"; @@ -578,7 +675,7 @@ namespace mongo { void b(){ BSONObjBuilder b; - b << "name" << "abc"; + b << "_id" << "abc"; b.appendBool( "partitioned" , true ); b << "primary" << "myserver"; diff --git a/s/config.h b/s/config.h index c64ac809048..a3a1a2371d6 100644 --- a/s/config.h +++ b/s/config.h @@ -30,6 +30,13 @@ #include "shard.h" namespace mongo { + + struct ShardNS { + static string database; + static string shard; + static string chunk; + }; + class Grid; class ConfigServer; @@ -186,7 +193,7 @@ namespace mongo { /** * @return 0 = ok, otherwise error # */ - int checkConfigVersion(); + int checkConfigVersion( bool upgrade ); /** * log a change to config.changes diff --git a/s/server.cpp b/s/server.cpp index 696ff346cdb..4591b10222b 100644 --- a/s/server.cpp +++ b/s/server.cpp @@ -146,8 +146,9 @@ int main(int argc, char* argv[], char *envp[] ) { options.add_options() ( "configdb" , po::value<string>() , "1 or 3 comma separated config servers" ) ( "test" , "just run unit tests" ) + ( "upgrade" , "upgrade meta data version" ) ; - + // parse options po::variables_map params; @@ -222,9 +223,14 @@ int main(int argc, char* argv[], char *envp[] ) { return 8; } - int configError = configServer.checkConfigVersion(); + int configError = configServer.checkConfigVersion( params.count( "upgrade" ) ); if ( configError ){ - cout << "config server error: " << configError << endl; + if ( configError > 0 ){ + cout << "upgrade success!" << endl; + } + else { + cout << "config server error: " << configError << endl; + } return configError; } configServer.reloadSettings(); diff --git a/shell/servers.js b/shell/servers.js index 923214ab5ea..277e0c12457 100644 --- a/shell/servers.js +++ b/shell/servers.js @@ -181,7 +181,11 @@ ShardingTest.prototype.getDB = function( name ){ } ShardingTest.prototype.getServerName = function( dbname ){ - return this.config.databases.findOne( { name : dbname } ).primary; + var x = this.config.databases.findOne( { _id : dbname } ); + if ( x ) + return x.primary; + this.config.databases.find().forEach( printjson ); + throw "couldn't find dbname: " + dbname + " total: " + this.config.databases.count(); } ShardingTest.prototype.getServer = function( dbname ){ |