0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-12-01 09:32:32 +01:00

replsetcnfig work

This commit is contained in:
Dwight 2010-04-27 14:22:46 -04:00
parent a53f68340b
commit 4ea1fe91d3
7 changed files with 94 additions and 45 deletions

View File

@ -1478,6 +1478,8 @@ namespace mongo {
try { try {
unsigned u = stringToNum(f); unsigned u = stringToNum(f);
assert( u < 4096 ); assert( u < 4096 );
if( u >= v.size() )
v.resize(u+1);
v[u] = e; v[u] = e;
} }
catch(unsigned) { } catch(unsigned) { }

View File

@ -23,11 +23,11 @@ namespace mongo {
struct HealthOptions { struct HealthOptions {
HealthOptions() { HealthOptions() {
heartbeatSleepMillis = 2000; heartbeatSleepMillis = 2000;
heartbeatTimeoutMillis = 10; heartbeatTimeoutMillis = 10000;
heartbeatConnRetries = 3; heartbeatConnRetries = 3;
} }
bool isDefault() const { bool isDefault() const {
return heartbeatSleepMillis != 2000 || heartbeatTimeoutMillis != 10 || heartbeatConnRetries != 3; return !( heartbeatSleepMillis != 2000 || heartbeatTimeoutMillis != 10000 || heartbeatConnRetries != 3 );
} }
// see http://www.mongodb.org/display/DOCS/Replica+Set+Internals // see http://www.mongodb.org/display/DOCS/Replica+Set+Internals

View File

@ -34,7 +34,7 @@ namespace mongo {
class ReplSet { class ReplSet {
public: public:
bool isMaster(const char *client) { bool isMaster(const char *client) {
// //zzz
return false; return false;
} }
void fillIsMaster(BSONObjBuilder&); void fillIsMaster(BSONObjBuilder&);

View File

@ -54,9 +54,17 @@ namespace mongo {
return false; return false;
} }
if( cmdObj["replSetInitiate"].type() != Object ) {
errmsg = "no configuration specified";
return false;
}
ReplSetConfig newConfig(cmdObj["replSetInitiate"].Obj()); ReplSetConfig newConfig(cmdObj["replSetInitiate"].Obj());
return true; log() << newConfig.toString() << endl;
errmsg = "not yet implemented";
return false;
} }
} cmdReplSetInitiate; } cmdReplSetInitiate;

View File

@ -26,15 +26,35 @@ using namespace bson;
namespace mongo { namespace mongo {
bo ReplSetConfig::Member::asBson() const {
bob b;
b << "_id" << _id;
b.append("host", h.toString());
if( votes != 1 ) b << "votes" << votes;
if( priority != 1.0 ) b << "priority" << priority;
if( arbiterOnly ) b << "arbiterOnly" << true;
return b.obj();
}
bo ReplSetConfig::asBson() const { bo ReplSetConfig::asBson() const {
bob b; bob b;
b.append("_id", _id).append("version", version); b.append("_id", _id).append("version", version);
if( !ho.isDefault() ) { if( !ho.isDefault() || !getLastErrorDefaults.isEmpty() ) {
b << "settings" << bob settings;
(bob() << "heartbeatConnRetries " << ho.heartbeatConnRetries << if( !ho.isDefault() )
"heartbeatSleep" << ho.heartbeatSleepMillis / 1000 << settings << "heartbeatConnRetries " << ho.heartbeatConnRetries <<
"heartbeatTimeout" << ho.heartbeatTimeoutMillis / 1000).obj(); "heartbeatSleep" << ho.heartbeatSleepMillis / 1000 <<
"heartbeatTimeout" << ho.heartbeatTimeoutMillis / 1000;
if( !getLastErrorDefaults.isEmpty() )
settings << "getLastErrorDefaults" << getLastErrorDefaults;
b << "settings" << settings.obj();
} }
BSONArrayBuilder a;
for( unsigned i = 0; i < members.size(); i++ )
a.append( members[i].asBson() );
b.append("members", a.arr());
return b.obj(); return b.obj();
} }
@ -70,28 +90,40 @@ namespace mongo {
if( settings["heartbeatTimeout"].ok() ) if( settings["heartbeatTimeout"].ok() )
ho.heartbeatTimeoutMillis = (unsigned) (settings["heartbeatTimeout"].Number() * 1000); ho.heartbeatTimeoutMillis = (unsigned) (settings["heartbeatTimeout"].Number() * 1000);
ho.check(); ho.check();
try { getLastErrorDefaults = settings["getLastErrorDefaults"].Obj(); } catch(...) { }
} }
set<string> hosts; set<string> hosts;
set<int> ords; set<int> ords;
vector<BSONElement> members = o["members"].Array(); vector<BSONElement> members;
try {
members = o["members"].Array();
}
catch(...) {
uasserted(13130, "error parsing replSet configuration object 'members' field");
}
for( unsigned i = 0; i < members.size(); i++ ) { for( unsigned i = 0; i < members.size(); i++ ) {
BSONObj mobj = members[i].Obj(); BSONObj mobj = members[i].Obj();
Member m; Member m;
string s = mobj["host"].String();
try { try {
m._id = (int) mobj["_id"].Number();
string s = mobj["host"].String();
m.h = HostAndPort::fromString(s); m.h = HostAndPort::fromString(s);
m.arbiterOnly = mobj.getBoolField("arbiterOnly"); m.arbiterOnly = mobj.getBoolField("arbiterOnly");
try { m.priority = mobj["priority"].Number(); } catch(...) { } try { m.priority = mobj["priority"].Number(); } catch(...) { }
try { m.votes = (unsigned) mobj["votes"].Number(); } catch(...) { } try { m.votes = (unsigned) mobj["votes"].Number(); } catch(...) { }
m.check(); m.check();
} }
catch(...) { catch(DBException& e) {
uassert(13107, "bad local.system.replset config", false); log() << "replSet cfg parsing exception for members[" << i << "] " << e.what() << endl;
stringstream ss;
ss << "replSet members[" << i << "] bad config object";
uassert(13107, ss.str(), false);
} }
uassert(13108, "bad local.system.replset config dups?", ords.count(m._id) == 0 && hosts.count(m.h.toString()) == 0); uassert(13108, "bad local.system.replset config dups?", ords.count(m._id) == 0 && hosts.count(m.h.toString()) == 0);
hosts.insert(m.h.toString()); hosts.insert(m.h.toString());
ords.insert(m._id); ords.insert(m._id);
this->members.push_back(m);
} }
uassert(13117, "bad local.system.replset config", !_id.empty()); uassert(13117, "bad local.system.replset config", !_id.empty());
} }

View File

@ -41,49 +41,56 @@ Additionally an object in this collection holds global configuration settings fo
{ _id : <logical_set_name>, settings: { _id : <logical_set_name>, settings:
{ [heartbeatSleep : <seconds>] { [heartbeatSleep : <seconds>]
[, heartbeatTimeout : <seconds>] [, heartbeatTimeout : <seconds>]
[, heartbeatConnRetries : <n>] [, heartbeatConnRetries : <n>]
[, getLastErrorDefaults: <defaults>] [, getLastErrorDefaults: <defaults>]
} }
} }
*/ */
class ReplSetConfig { class ReplSetConfig {
public: public:
/* if something is misconfigured, throws an exception. /* if something is misconfigured, throws an exception.
if couldn't be queried or is just blank, ok() will be false. if couldn't be queried or is just blank, ok() will be false.
*/ */
ReplSetConfig(const HostAndPort& h); ReplSetConfig(const HostAndPort& h);
ReplSetConfig(BSONObj cfg); ReplSetConfig(BSONObj cfg);
bool ok() const { return _ok; } bool ok() const { return _ok; }
struct Member { struct Member {
Member() : _id(-1), votes(1), priority(1.0), arbiterOnly(false) { } Member() : _id(-1), votes(1), priority(1.0), arbiterOnly(false) { }
int _id; /* ordinal */ int _id; /* ordinal */
unsigned votes; /* how many votes this node gets. default 1. */ unsigned votes; /* how many votes this node gets. default 1. */
HostAndPort h; HostAndPort h;
double priority; /* 0 means can never be primary */ double priority; /* 0 means can never be primary */
bool arbiterOnly; bool arbiterOnly;
void check() const; /* check validity, assert if not. */ void check() const; /* check validity, assert if not. */
}; BSONObj asBson() const;
vector<Member> members; };
string _id; vector<Member> members;
int version; string _id;
HealthOptions ho; int version;
string md5; HealthOptions ho;
string md5;
BSONObj getLastErrorDefaults;
// true if could connect, and there is no cfg object there at all // true if could connect, and there is no cfg object there at all
bool empty() { return version == -2; } bool empty() { return version == -2; }
/* TODO: add getLastErrorDefaults */ /* TODO: add getLastErrorDefaults */
string toString() const { return asBson().toString(); }
/*@ validate the settings. does not call check() on each member, you have to do that separately. */
void check() const;
private: private:
bool _ok; bool _ok;
void from(BSONObj); void from(BSONObj);
void clear(); void clear();
BSONObj asBson() const; BSONObj asBson() const;
}; };
} }

View File

@ -44,7 +44,7 @@ namespace mongo {
return HostAndPort(string(p,colon-p),port); return HostAndPort(string(p,colon-p),port);
} }
// no port specified. // no port specified.
return HostAndPort(p, cmdLine.port); return HostAndPort(p);
} }
bool operator<(const HostAndPort& r) const { return _host < r._host || (_host==r._host&&_port<r._port); } bool operator<(const HostAndPort& r) const { return _host < r._host || (_host==r._host&&_port<r._port); }
@ -58,7 +58,7 @@ namespace mongo {
string toString() const; string toString() const;
string host() const { return _host; } string host() const { return _host; }
int port() const { return _port; } int port() const { return _port >= 0 ? _port : cmdLine.port; }
private: private:
// invariant (except full obj assignment): // invariant (except full obj assignment):