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 {
unsigned u = stringToNum(f);
assert( u < 4096 );
if( u >= v.size() )
v.resize(u+1);
v[u] = e;
}
catch(unsigned) { }

View File

@ -23,11 +23,11 @@ namespace mongo {
struct HealthOptions {
HealthOptions() {
heartbeatSleepMillis = 2000;
heartbeatTimeoutMillis = 10;
heartbeatTimeoutMillis = 10000;
heartbeatConnRetries = 3;
}
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

View File

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

View File

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

View File

@ -26,15 +26,35 @@ using namespace bson;
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 {
bob b;
b.append("_id", _id).append("version", version);
if( !ho.isDefault() ) {
b << "settings" <<
(bob() << "heartbeatConnRetries " << ho.heartbeatConnRetries <<
"heartbeatSleep" << ho.heartbeatSleepMillis / 1000 <<
"heartbeatTimeout" << ho.heartbeatTimeoutMillis / 1000).obj();
if( !ho.isDefault() || !getLastErrorDefaults.isEmpty() ) {
bob settings;
if( !ho.isDefault() )
settings << "heartbeatConnRetries " << ho.heartbeatConnRetries <<
"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();
}
@ -70,28 +90,40 @@ namespace mongo {
if( settings["heartbeatTimeout"].ok() )
ho.heartbeatTimeoutMillis = (unsigned) (settings["heartbeatTimeout"].Number() * 1000);
ho.check();
try { getLastErrorDefaults = settings["getLastErrorDefaults"].Obj(); } catch(...) { }
}
set<string> hosts;
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++ ) {
BSONObj mobj = members[i].Obj();
Member m;
string s = mobj["host"].String();
try {
m._id = (int) mobj["_id"].Number();
string s = mobj["host"].String();
m.h = HostAndPort::fromString(s);
m.arbiterOnly = mobj.getBoolField("arbiterOnly");
try { m.priority = mobj["priority"].Number(); } catch(...) { }
try { m.votes = (unsigned) mobj["votes"].Number(); } catch(...) { }
m.check();
}
catch(...) {
uassert(13107, "bad local.system.replset config", false);
catch(DBException& e) {
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);
hosts.insert(m.h.toString());
ords.insert(m._id);
this->members.push_back(m);
}
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:
{ [heartbeatSleep : <seconds>]
[, heartbeatTimeout : <seconds>]
[, heartbeatConnRetries : <n>]
[, getLastErrorDefaults: <defaults>]
[, heartbeatTimeout : <seconds>]
[, heartbeatConnRetries : <n>]
[, getLastErrorDefaults: <defaults>]
}
}
*/
class ReplSetConfig {
public:
/* if something is misconfigured, throws an exception.
if couldn't be queried or is just blank, ok() will be false.
*/
ReplSetConfig(const HostAndPort& h);
/* if something is misconfigured, throws an exception.
if couldn't be queried or is just blank, ok() will be false.
*/
ReplSetConfig(const HostAndPort& h);
ReplSetConfig(BSONObj cfg);
ReplSetConfig(BSONObj cfg);
bool ok() const { return _ok; }
bool ok() const { return _ok; }
struct Member {
Member() : _id(-1), votes(1), priority(1.0), arbiterOnly(false) { }
int _id; /* ordinal */
unsigned votes; /* how many votes this node gets. default 1. */
HostAndPort h;
double priority; /* 0 means can never be primary */
bool arbiterOnly;
void check() const; /* check validity, assert if not. */
};
vector<Member> members;
string _id;
int version;
HealthOptions ho;
string md5;
struct Member {
Member() : _id(-1), votes(1), priority(1.0), arbiterOnly(false) { }
int _id; /* ordinal */
unsigned votes; /* how many votes this node gets. default 1. */
HostAndPort h;
double priority; /* 0 means can never be primary */
bool arbiterOnly;
void check() const; /* check validity, assert if not. */
BSONObj asBson() const;
};
vector<Member> members;
string _id;
int version;
HealthOptions ho;
string md5;
BSONObj getLastErrorDefaults;
// true if could connect, and there is no cfg object there at all
bool empty() { return version == -2; }
// true if could connect, and there is no cfg object there at all
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:
bool _ok;
void from(BSONObj);
void clear();
BSONObj asBson() const;
bool _ok;
void from(BSONObj);
void clear();
BSONObj asBson() const;
};
}

View File

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