From b9d61d202c7acebac58ce55c1da89f8e67dd29a7 Mon Sep 17 00:00:00 2001 From: Eliot Horowitz Date: Thu, 10 Sep 2009 00:39:30 -0400 Subject: sharding config db versioning SHARDING-33 --- s/config.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++++ s/config.h | 12 +++++++++++- s/server.cpp | 8 +++++++- 3 files changed, 62 insertions(+), 2 deletions(-) diff --git a/s/config.cpp b/s/config.cpp index b830b83664a..51334d79b47 100644 --- a/s/config.cpp +++ b/s/config.cpp @@ -29,6 +29,8 @@ namespace mongo { + int ConfigServer::VERSION = 2; + /* --- DBConfig --- */ string DBConfig::modelServer() { @@ -314,6 +316,48 @@ namespace mongo { return true; } + + int ConfigServer::dbConfigVersion(){ + ScopedDbConnection conn( _primary ); + int version = dbConfigVersion( conn.conn() ); + conn.done(); + return version; + } + + int ConfigServer::dbConfigVersion( DBClientBase& conn ){ + auto_ptr c = conn.query( "config.version" , BSONObj() ); + int version = 0; + if ( c->more() ){ + BSONObj o = c->next(); + version = o["version"].numberInt(); + uassert( "should only have 1 thing in config.version" , ! c->more() ); + } + else { + if ( conn.count( "config.shard" ) || conn.count( "config.databases" ) ){ + version = 1; + } + } + + return version; + } + + int ConfigServer::checkConfigVersion(){ + int cur = dbConfigVersion(); + if ( cur == VERSION ) + return 0; + + if ( cur == 0 ){ + ScopedDbConnection conn( _primary ); + conn->insert( "config.version" , BSON( "version" << VERSION ) ); + pool.flush(); + assert( VERSION == dbConfigVersion( conn.conn() ) ); + conn.done(); + return 0; + } + + log() << "don't know how to upgrade " << cur << " to " << VERSION << endl; + return -8; + } string ConfigServer::getHost( string name , bool withPort ){ if ( name.find( ":" ) ){ diff --git a/s/config.h b/s/config.h index 9e34c9fc243..edf813cce64 100644 --- a/s/config.h +++ b/s/config.h @@ -29,7 +29,7 @@ #include "shardkey.h" namespace mongo { - + class Grid; class ConfigServer; @@ -150,6 +150,16 @@ namespace mongo { call at startup, this will initiate connection to the grid db */ bool init( vector configHosts , bool infer ); + + int dbConfigVersion(); + int dbConfigVersion( DBClientBase& conn ); + + /** + * @return 0 = ok, otherwise error # + */ + int checkConfigVersion(); + + static int VERSION; private: string getHost( string name , bool withPort ); diff --git a/s/server.cpp b/s/server.cpp index 8a8177498f4..f878c1372c8 100644 --- a/s/server.cpp +++ b/s/server.cpp @@ -164,8 +164,14 @@ int main(int argc, char* argv[], char *envp[] ) { cerr << "couldn't connectd to config db" << endl; return 7; } - + assert( configServer.ok() ); + + int configError = configServer.checkConfigVersion(); + if ( configError ){ + cerr << "config server error: " << configError << endl; + return configError; + } init(); start(); -- cgit v1.2.1