0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-12-01 09:32:32 +01:00
This commit is contained in:
Dwight 2010-05-20 14:50:36 -04:00
parent 270f831f58
commit 5fd674d66f
6 changed files with 99 additions and 50 deletions

View File

@ -580,6 +580,7 @@
<ClInclude Include="reci.h" />
<ClInclude Include="recstore.h" />
<ClInclude Include="repl\connections.h" />
<ClInclude Include="repl\multicmd.h" />
<ClInclude Include="repl\rsmember.h" />
<ClInclude Include="repl\rstime.h" />
<ClInclude Include="storage.h" />

View File

@ -683,6 +683,9 @@
<ClInclude Include="..\boost_pieces\any.hpp">
<Filter>util\boost_pieces</Filter>
</ClInclude>
<ClInclude Include="repl\multicmd.h">
<Filter>replset</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Filter Include="libs">

View File

@ -16,17 +16,15 @@
#include "pch.h"
#include "replset.h"
#include "connections.h"
#include "../../util/background.h"
#include "multicmd.h"
namespace mongo {
int ReplSet::Consensus::totalVotes() const {
static int complain = 0;
int vTot = rs._self->config().votes;
for( Member *m = rs.head(); m; m=m->next() ) {
for( Member *m = rs.head(); m; m=m->next() )
vTot += m->config().votes;
}
if( vTot % 2 == 0 && vTot && complain++ == 0 )
log() << "replSet warning: total number of votes is even - considering giving one member an extra vote" << rsLog;
return vTot;
@ -34,62 +32,36 @@ namespace mongo {
bool ReplSet::Consensus::aMajoritySeemsToBeUp() const {
int vUp = rs._self->config().votes;
for( Member *m = rs.head(); m; m=m->next() ) {
for( Member *m = rs.head(); m; m=m->next() )
vUp += m->hbinfo().up() ? m->config().votes : 0;
}
return vUp * 2 > totalVotes();
}
static BSONObj electCmd;
class E : public BackgroundJob {
string name() { return "Consensus::E"; }
void run() {
ok = 0;
log() << "not done" << rsLog;
try {
ScopedConn c(m->fullName());
if( c->runCommand("admin", electCmd, result) )
ok++;
}
catch(DBException&) {
DEV log() << "replSet dev caught dbexception on electCmd" << rsLog;
}
}
public:
BSONObj result;
int ok;
ReplSet::Member *m;
};
typedef shared_ptr<E> eptr;
static const int VETO = -10000;
void ReplSet::Consensus::_electSelf() {
ReplSet::Member& me = *rs._self;
electCmd = BSON(
BSONObj electCmd = BSON(
"replSetElect" << 1 <<
"set" << rs.name() <<
"who" << me.fullName() <<
"whoid" << me.hbinfo().id() <<
"cfgver" << rs._cfg->version
);
list<eptr> jobs;
list<BackgroundJob*> _jobs;
for( Member *m = rs.head(); m; m=m->next() ) if( m->hbinfo().up() ) {
E *e = new E();
e->m = m;
jobs.push_back(eptr(e)); _jobs.push_back(e);
}
list<Target> L;
for( Member *m = rs.head(); m; m=m->next() )
if( m->hbinfo().up() )
L.push_back( Target(m->fullName()) );
time_t start = time(0);
BackgroundJob::go(_jobs);
BackgroundJob::wait(_jobs,5);
multiCommand(electCmd, L);
int tally = me.config().votes; // me votes yes.
for( list<eptr>::iterator i = jobs.begin(); i != jobs.end(); i++ ) {
if( (*i)->ok ) {
int v = (*i)->result["vote"].Int();
int tally = me.config().votes; // we vote yes. ?
for( list<Target>::iterator i = L.begin(); i != L.end(); i++ ) {
cout << "TEMP electres: " << i->result.toString() << endl;
if( i->ok ) {
int v = i->result["vote"].Int();
tally += v;
}
}

73
db/repl/multicmd.h Executable file
View File

@ -0,0 +1,73 @@
// @file multicmd.h
/**
* 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/>.
*/
#pragma once
#include "../../util/background.h"
#include "connections.h"
namespace mongo {
struct Target {
Target(string hostport) : toHost(hostport), ok(false) { }
Target() : ok(false) { }
string toHost;
bool ok;
BSONObj result;
};
/** send a command in parallel to many servers, and collect the results. */
void multiCommand(BSONObj cmd, list<Target>& L);
/* -- implementation ------------- */
class _MultiCommandJob : public BackgroundJob {
public:
BSONObj& cmd;
Target& d;
_MultiCommandJob(BSONObj& _cmd, Target& _d) : cmd(_cmd), d(_d) { }
private:
string name() { return "MultiCommandJob"; }
void run() {
try {
ScopedConn c(d.toHost);
c->runCommand("admin", cmd, d.result);
}
catch(DBException&) {
DEV log() << "dev caught dbexception on multiCommand " << d.toHost << rsLog;
}
}
};
inline void multiCommand(BSONObj cmd, list<Target>& L) {
typedef shared_ptr<_MultiCommandJob> P;
list<P> jobs;
list<BackgroundJob *> _jobs;
for( list<Target>::iterator i = L.begin(); i != L.end(); i++ ) {
Target& d = *i;
_MultiCommandJob *j = new _MultiCommandJob(cmd, d);
jobs.push_back(P(j));
_jobs.push_back(j);
}
BackgroundJob::go(_jobs);
BackgroundJob::wait(_jobs,5);
}
}

View File

@ -28,7 +28,8 @@ namespace mongo {
void ReplSet::updateHBInfo(const HeartbeatInfo& h) {
for( Member *m = _members.head(); m; m=m->next() ) {
if( m->id() == h.id() ) {
m->_m = h;
m->_hbinfo = h;
return;
}
}
}

View File

@ -98,18 +98,17 @@ namespace mongo {
string fullName() const { return h().toString(); }
const ReplSetConfig::MemberCfg& config() const { return *_config; }
void summarizeAsHtml(stringstream& s) const;
const HeartbeatInfo& hbinfo() const { return _m; }
string lhb() { return _m.lastHeartbeatMsg; }
const HeartbeatInfo& hbinfo() const { return _hbinfo; }
string lhb() { return _hbinfo.lastHeartbeatMsg; }
MemberState state() const { return _state; }
const HostAndPort& h() const { return _h; }
unsigned id() const { return _m.id(); }
unsigned id() const { return _hbinfo.id(); }
friend class ReplSet;
private:
const ReplSetConfig::MemberCfg *_config; /* todo: when this changes??? */
HostAndPort _h;
MemberState _state;
HeartbeatInfo _m;
HeartbeatInfo _hbinfo;
};
list<HostAndPort> memberHostnames() const;
const Member* currentPrimary() const { return _currentPrimary; }
@ -153,7 +152,7 @@ namespace mongo {
{ _myState = FATAL; log() << "replSet error fatal error, stopping replication" << rsLog; }
inline ReplSet::Member::Member(HostAndPort h, unsigned ord, const ReplSetConfig::MemberCfg *c) :
_h(h), _config(c), _m(ord) { }
_h(h), _config(c), _hbinfo(ord) { }
inline bool ReplSet::isMaster(const char *client) {
/* todo replset */