From d02951083c18df6a2d9bcce0e40621ba0603a6ff Mon Sep 17 00:00:00 2001 From: Eliot Horowitz Date: Wed, 11 Mar 2009 17:28:49 -0400 Subject: [PATCH] sharding version management for mongod --- SConstruct | 6 +- jstests/sharding/version1.js | 20 +++++ s/commands_db.cpp | 165 +++++++++++++++++++++++++++++++++++ 3 files changed, 190 insertions(+), 1 deletion(-) create mode 100644 jstests/sharding/version1.js create mode 100644 s/commands_db.cpp diff --git a/SConstruct b/SConstruct index b4f3e12a84a..38148b4e69d 100644 --- a/SConstruct +++ b/SConstruct @@ -139,6 +139,10 @@ coreServerFiles = [ "util/message_server_port.cpp" , "util/message_server_asio.c serverOnlyFiles = Split( "db/query.cpp db/introspect.cpp db/btree.cpp db/clientcursor.cpp db/javajs.cpp db/tests.cpp db/repl.cpp db/btreecursor.cpp db/cloner.cpp db/namespace.cpp db/matcher.cpp db/dbcommands.cpp db/dbeval.cpp db/dbwebserver.cpp db/dbinfo.cpp db/dbhelpers.cpp db/instance.cpp db/pdfile.cpp db/cursor.cpp db/security_commands.cpp db/security.cpp util/miniwebserver.cpp db/storage.cpp db/reccache.cpp db/queryoptimizer.cpp" ) +coreShardFiles = [] +shardServerFiles = coreShardFiles + Glob( "s/strategy*.cpp" ) + [ "s/commands.cpp" , "s/request.cpp" , "s/cursors.cpp" , "s/server.cpp" ] + [ "s/shard.cpp" , "s/shardkey.cpp" , "s/config.cpp" ] +serverOnlyFiles += coreShardFiles + [ "s/commands_db.cpp" ] + allClientFiles = commonFiles + coreDbFiles + [ "client/clientOnly.cpp" , "client/gridfs.cpp" ]; onlyServer = len( COMMAND_LINE_TARGETS ) == 0 or ( len( COMMAND_LINE_TARGETS ) == 1 and str( COMMAND_LINE_TARGETS[0] ) == "mongod" ) @@ -522,7 +526,7 @@ env.Program( "mongoimportjson" , allToolFiles + [ "tools/importJSON.cpp" ] ) env.Program( "mongofiles" , allToolFiles + [ "tools/files.cpp" ] ) # mongos -mongos = env.Program( "mongos" , commonFiles + coreDbFiles + coreServerFiles + Glob( "s/*.cpp" ) ) +mongos = env.Program( "mongos" , commonFiles + coreDbFiles + coreServerFiles + shardServerFiles ) # c++ library clientLibName = str( env.Library( "mongoclient" , allClientFiles )[0] ) diff --git a/jstests/sharding/version1.js b/jstests/sharding/version1.js new file mode 100644 index 00000000000..94b7580a478 --- /dev/null +++ b/jstests/sharding/version1.js @@ -0,0 +1,20 @@ +// version1.js + +s = new ShardingTest( "version1" , 1 , 2 ) + +a = s._connections[0].getDB( "admin" ); + +assert( a.runCommand( { "setShardVersion" : "alleyinsider.foo" , configdb : s._configDB } ).ok == 0 ); +assert( a.runCommand( { "setShardVersion" : "alleyinsider.foo" , configdb : s._configDB , version : "a" } ).ok == 0 ); +assert( a.runCommand( { "setShardVersion" : "alleyinsider.foo" , configdb : s._configDB , version : 2 } ).ok == 1 ); +assert( a.runCommand( { "setShardVersion" : "alleyinsider.foo" , configdb : "a" , version : 2 } ).ok == 0 ); + +assert( a.runCommand( { "setShardVersion" : "alleyinsider.foo" , configdb : s._configDB , version : 2 } ).ok == 1 ); +assert( a.runCommand( { "setShardVersion" : "alleyinsider.foo" , configdb : s._configDB , version : 1 } ).ok == 0 ); + +assert.eq( a.runCommand( { "setShardVersion" : "alleyinsider.foo" , configdb : s._configDB , version : 3 } ).oldVersion.i , 2 , "oldVersion" ); + +assert.eq( a.runCommand( { "getShardVersion" : "alleyinsider.foo" } ).mine.i , 3 , "my get version A" ); +assert.eq( a.runCommand( { "getShardVersion" : "alleyinsider.foo" } ).global.i , 3 , "my get version B" ); + +s.stop(); diff --git a/s/commands_db.cpp b/s/commands_db.cpp new file mode 100644 index 00000000000..4f11b2505f7 --- /dev/null +++ b/s/commands_db.cpp @@ -0,0 +1,165 @@ +// commands_db.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 . +*/ + + +/** + these are commands that live in mongod + mostly around shard management and checking + */ + +#include "stdafx.h" +#include +#include + +#include "../db/commands.h" +#include "../db/jsobj.h" + +using namespace std; + +namespace mongo { + + typedef map NSVersions; + + NSVersions myVersions; + boost::thread_specific_ptr clientShardVersions; + string shardConfigServer; + + + class MongodShardCommand : public Command { + public: + MongodShardCommand( const char * n ) : Command( n ){ + } + virtual bool slaveOk(){ + return false; + } + virtual bool adminOnly() { + return true; + } + }; + + // setShardVersion( ns ) + + class SetShardVersion : public MongodShardCommand { + public: + SetShardVersion() : MongodShardCommand("setShardVersion"){} + + virtual void help( stringstream& help ) const { + help << " example: { setShardVersion : 'alleyinsider.foo' , version : 1 , configdb : '' } "; + } + + bool run(const char *cmdns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool){ + + string configdb = cmdObj["configdb"].valuestrsafe(); + { // configdb checking + if ( configdb.size() == 0 ){ + errmsg = "no configdb"; + return false; + } + + if ( shardConfigServer.size() == 0 ){ + shardConfigServer = configdb; + } + else if ( shardConfigServer != configdb ){ + errmsg = "specified a different configdb!"; + return false; + } + } + + unsigned long long version; + { + BSONElement e = cmdObj["version"]; + if ( e.eoo() ){ + errmsg = "no version"; + return false; + } + else if ( e.isNumber() ) + version = (unsigned long long)e.number(); + else if ( e.type() == Date || e.type() == Timestamp ) + version = e.date(); + else { + errmsg = "version is not a numberic type"; + return false; + } + + } + + NSVersions * versions = clientShardVersions.get(); + + if ( ! versions ){ + log(1) << "entering shard mode for connection" << endl; + versions = new NSVersions(); + clientShardVersions.reset( versions ); + } + + string ns = cmdObj["setShardVersion"].valuestrsafe(); + if ( ns.size() == 0 ){ + errmsg = "need to speciy fully namespace"; + return false; + } + + unsigned long long& oldVersion = (*versions)[ns]; + if ( version < oldVersion ){ + errmsg = "going to older version for you!"; + return false; + } + + unsigned long long& myVersion = myVersions[ns]; + if ( version < myVersion ){ + errmsg = "going to older version for global"; + return false; + } + + result.appendTimestamp( "oldVersion" , oldVersion ); + oldVersion = version; + myVersion = version; + + result.append( "ok" , 1 ); + return 1; + } + + } setShardVersion; + + class GetShardVersion : public MongodShardCommand { + public: + GetShardVersion() : MongodShardCommand("getShardVersion"){} + + virtual void help( stringstream& help ) const { + help << " example: { getShardVersion : 'alleyinsider.foo' } "; + } + + bool run(const char *cmdns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool){ + string ns = cmdObj["getShardVersion"].valuestrsafe(); + if ( ns.size() == 0 ){ + errmsg = "need to speciy fully namespace"; + return false; + } + + result.appendTimestamp( "global" , myVersions[ns] ); + if ( clientShardVersions.get() ) + result.appendTimestamp( "mine" , (*clientShardVersions.get())[ns] ); + else + result.appendTimestamp( "mine" , 0 ); + + result.append( "ok" , 1 ); + return true; + } + + } getShardVersion; + + +}