Files
mongo/db/repl/replset_commands.cpp

132 lines
5.2 KiB
C++
Raw Normal View History

2010-04-14 17:30:23 -04:00
/**
* 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/>.
*/
2010-04-27 15:27:52 -04:00
#include "pch.h"
2010-04-14 17:30:23 -04:00
#include "../cmdline.h"
#include "../commands.h"
2010-05-08 14:12:24 -04:00
#include "health.h"
2010-05-29 15:45:47 -04:00
#include "rs.h"
2010-04-23 17:35:05 -04:00
#include "rs_config.h"
2010-04-14 17:30:23 -04:00
namespace mongo {
2010-07-08 14:41:49 -04:00
void checkAllMembersUpForConfigChange(const ReplSetConfig& cfg);
2010-04-18 12:30:40 -04:00
/* commands in other files:
replSetHeartbeat - health.cpp
2010-05-08 14:12:24 -04:00
replSetInitiate - rs_mod.cpp
2010-04-22 18:43:37 -04:00
*/
2010-04-18 12:30:40 -04:00
2010-07-08 14:41:49 -04:00
using namespace bson;
2010-06-01 15:17:38 -04:00
class CmdReplSetGetStatus : public ReplSetCommand {
2010-04-14 17:30:23 -04:00
public:
2010-04-23 15:50:49 -04:00
virtual void help( stringstream &help ) const {
help << "Report status of a replica set from the POV of this server\n";
help << "{ replSetGetStatus : 1 }";
2010-04-25 16:13:21 -04:00
help << "\nhttp://www.mongodb.org/display/DOCS/Replica+Set+Commands";
2010-04-23 15:50:49 -04:00
}
2010-06-01 15:17:38 -04:00
CmdReplSetGetStatus() : ReplSetCommand("replSetGetStatus", true) { }
virtual bool run(const string& , BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool fromRepl) {
2010-06-01 15:17:38 -04:00
if( !check(errmsg, result) )
2010-04-20 12:29:00 -04:00
return false;
theReplSet->summarizeStatus(result);
2010-04-14 17:30:23 -04:00
return true;
}
} cmdReplSetGetStatus;
2010-07-08 12:26:49 -04:00
class CmdReplSetReconfig : public ReplSetCommand {
public:
virtual void help( stringstream &help ) const {
help << "Adjust configuration of a replica set\n";
help << "{ replSetReconfig : config_object }";
help << "\nhttp://www.mongodb.org/display/DOCS/Replica+Set+Commands";
}
CmdReplSetReconfig() : ReplSetCommand("replSetReconfig") { }
virtual bool run(const string& , BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool fromRepl) {
if( !check(errmsg, result) )
return false;
if( !theReplSet->isPrimary() ) {
errmsg = "replSetReconfig command must be sent to the current replica set primary.";
return false;
}
2010-07-08 14:41:49 -04:00
{
// just make sure we can get a write lock before doing anything else. we'll reacquire one
// later. of course it could be stuck then, but this check lowers the risk if weird things
// are up - we probably don't want a change to apply 30 minutes after the initial attempt.
time_t t = time(0);
writelock lk("");
if( time(0)-t > 20 ) {
errmsg = "took a long time to get write lock, so not initiating. Initiate when server less busy?";
return false;
}
}
if( cmdObj["replSetReconfig"].type() != Object ) {
errmsg = "no configuration specified";
return false;
}
/** TODO
Support changes when a majority, but not all, members of a set are up.
Determine what changes should not be allowed as they would cause erroneous states.
What should be possible when a majority is not up?
*/
try {
ReplSetConfig newConfig(cmdObj["replSetReconfig"].Obj());
log() << "replSet replSetReconfig config object parses ok, " << newConfig.members.size() << " members specified" << rsLog;
if( !ReplSetConfig::legalChange(theReplSet->getConfig(), newConfig, errmsg) ) {
return false;
}
checkAllMembersUpForConfigChange(newConfig);
2010-07-14 15:08:59 -04:00
log() << "replSet replSetReconfig all members seem up" << rsLog;
2010-07-08 14:41:49 -04:00
2010-07-12 22:12:57 -04:00
theReplSet->haveNewConfig(newConfig);
2010-07-12 09:13:38 -04:00
ReplSet::startupStatusMsg = "replSetReconfig'd";
2010-07-08 14:41:49 -04:00
}
catch( DBException& e ) {
log() << "replSet replSetReconfig exception: " << e.what() << rsLog;
throw;
}
2010-07-12 09:13:38 -04:00
return true;
2010-07-08 12:26:49 -04:00
}
} cmdReplSetReconfig;
2010-06-01 15:17:38 -04:00
class CmdReplSetFreeze : public ReplSetCommand {
2010-04-25 16:13:21 -04:00
public:
virtual void help( stringstream &help ) const {
help << "Enable / disable failover for the set - locks current primary as primary even if issues occur.\nFor use during system maintenance.\n";
help << "{ replSetFreeze : <bool> }";
help << "\nhttp://www.mongodb.org/display/DOCS/Replica+Set+Commands";
}
2010-06-01 15:17:38 -04:00
CmdReplSetFreeze() : ReplSetCommand("replSetFreeze", true) { }
virtual bool run(const string& , BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool fromRepl) {
2010-06-01 15:17:38 -04:00
if( !check(errmsg, result) )
2010-04-25 16:13:21 -04:00
return false;
errmsg = "not yet implemented"; /*TODO*/
return false;
}
} cmdReplSetFreeze;
2010-04-14 17:30:23 -04:00
}