Files
mongo/db/repl/rs_initialsync.cpp

293 lines
10 KiB
C++
Raw Normal View History

2010-07-18 12:53:40 -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/>.
*/
#include "pch.h"
2010-11-02 18:47:04 -04:00
#include "../repl.h"
2010-07-18 12:53:40 -04:00
#include "../client.h"
#include "../../client/dbclient.h"
#include "rs.h"
2010-07-18 13:34:16 -04:00
#include "../oplogreader.h"
2010-07-18 15:02:46 -04:00
#include "../../util/mongoutils/str.h"
2010-07-19 09:35:13 -04:00
#include "../dbhelpers.h"
#include "rs_optime.h"
2010-08-02 12:16:01 -04:00
#include "../oplog.h"
2010-07-18 12:53:40 -04:00
namespace mongo {
2010-07-18 15:02:46 -04:00
using namespace mongoutils;
using namespace bson;
2010-07-18 15:02:46 -04:00
2010-07-18 12:53:40 -04:00
void dropAllDatabasesExceptLocal();
2010-07-18 15:02:46 -04:00
// add try/catch with sleep
2011-01-04 00:40:41 -05:00
void isyncassert(const char *msg, bool expr) {
if( !expr ) {
2010-07-18 15:02:46 -04:00
string m = str::stream() << "initial sync " << msg;
theReplSet->sethbmsg(m, 0);
2010-07-22 14:20:29 -04:00
uasserted(13404, m);
2010-07-18 15:02:46 -04:00
}
}
2011-01-04 00:40:41 -05:00
void ReplSetImpl::syncDoInitialSync() {
createOplog();
2011-01-04 00:40:41 -05:00
2010-07-18 15:02:46 -04:00
while( 1 ) {
try {
_syncDoInitialSync();
break;
}
2010-07-19 18:05:44 -04:00
catch(DBException& e) {
sethbmsg("initial sync exception " + e.toString(), 0);
2010-07-18 15:02:46 -04:00
sleepsecs(30);
}
}
}
2011-01-04 00:40:41 -05:00
bool cloneFrom(const char *masterHost, string& errmsg, const string& fromdb, bool logForReplication,
bool slaveOk, bool useReplAuth, bool snapshot);
2010-07-19 09:29:49 -04:00
/* todo : progress metering to sethbmsg. */
static bool clone(const char *master, string db) {
string err;
return cloneFrom(master, err, db, false,
2011-01-04 00:40:41 -05:00
/* slave_ok */ true, true, false);
2010-07-19 09:29:49 -04:00
}
void _logOpObjRS(const BSONObj& op);
2010-08-23 15:48:13 -04:00
bool copyCollectionFromRemote(const string& host, const string& ns, const BSONObj& query, string &errmsg, bool logforrepl);
static void emptyOplog() {
writelock lk(rsoplog);
Client::Context ctx(rsoplog);
2011-01-04 00:40:41 -05:00
NamespaceDetails *d = nsdetails(rsoplog);
2010-08-02 12:16:01 -04:00
2011-01-04 00:40:41 -05:00
// temp
if( d && d->stats.nrecords == 0 )
return; // already empty, ok.
2010-08-02 12:16:01 -04:00
log(1) << "replSet empty oplog" << rsLog;
d->emptyCappedCollection(rsoplog);
2010-08-02 12:16:01 -04:00
/*
string errmsg;
bob res;
dropCollection(rsoplog, errmsg, res);
2011-01-04 00:40:41 -05:00
log() << "replSet recreated oplog so it is empty. todo optimize this..." << rsLog;
createOplog();*/
2010-08-02 12:16:01 -04:00
2011-01-04 00:40:41 -05:00
// TEMP: restart to recreate empty oplog
2010-08-02 12:16:01 -04:00
//log() << "replSet FATAL error during initial sync. mongod restart required." << rsLog;
//dbexit( EXIT_CLEAN );
/*
writelock lk(rsoplog);
Client::Context c(rsoplog, dbpath, 0, doauth/false);
NamespaceDetails *oplogDetails = nsdetails(rsoplog);
uassert(13412, str::stream() << "replSet error " << rsoplog << " is missing", oplogDetails != 0);
oplogDetails->cappedTruncateAfter(rsoplog, h.commonPointOurDiskloc, false);
*/
}
/**
2011-01-04 00:40:41 -05:00
* Choose a member to sync from.
*
* The initalSync option is an object with 1 k/v pair:
2011-01-04 00:40:41 -05:00
*
* "state" : 1|2
* "name" : "host"
* "_id" : N
* "optime" : t
*
* All except optime are exact matches. "optime" will find a secondary with
* an optime >= to the optime given.
*/
const Member* ReplSetImpl::getMemberToSyncTo() {
BSONObj sync = myConfig().initialSync;
bool secondaryOnly = false, isOpTime = false;
char *name = 0;
int id = -1;
OpTime optime;
2010-07-18 15:02:46 -04:00
2010-07-22 20:21:23 -04:00
StateBox::SP sp = box.get();
assert( !sp.state.primary() ); // wouldn't make sense if we were.
2010-07-18 15:02:46 -04:00
// if it exists, we've already checked that these fields are valid in
// rs_config.cpp
if ( !sync.isEmpty() ) {
if (sync.hasElement("state")) {
if (sync["state"].Number() == 1) {
if (sp.primary) {
sethbmsg( str::stream() << "syncing to primary: " << sp.primary->fullName(), 0);
return const_cast<Member*>(sp.primary);
}
else {
sethbmsg("couldn't clone from primary");
return NULL;
}
}
else {
secondaryOnly = true;
}
}
if (sync.hasElement("name")) {
name = (char*)sync["name"].valuestr();
}
if (sync.hasElement("_id")) {
2010-11-17 18:46:40 -05:00
id = (int)sync["_id"].Number();
}
if (sync.hasElement("optime")) {
isOpTime = true;
optime = sync["optime"]._opTime();
}
}
2011-01-04 00:40:41 -05:00
for( Member *m = head(); m; m = m->next() ) {
if (!m->hbinfo().up() ||
2011-01-04 00:40:41 -05:00
(m->state() != MemberState::RS_SECONDARY &&
m->state() != MemberState::RS_PRIMARY) ||
(secondaryOnly && m->state() != MemberState::RS_SECONDARY) ||
(id != -1 && (int)m->id() != id) ||
(name != 0 && strcmp(name, m->fullName().c_str()) != 0) ||
(isOpTime && optime >= m->hbinfo().opTime)) {
continue;
}
sethbmsg( str::stream() << "syncing to: " << m->fullName(), 0);
return const_cast<Member*>(m);
}
2011-01-04 00:40:41 -05:00
2011-03-24 15:39:32 -04:00
sethbmsg( str::stream() << "couldn't find a member matching the sync criteria:" <<
" state? " << (secondaryOnly ? "2" : "none") <<
" name? " << (name ? name : "none") <<
" _id? " << id <<
" optime? " << optime.toStringPretty() );
2011-01-04 00:40:41 -05:00
return NULL;
}
2011-01-04 00:40:41 -05:00
/**
* Do the initial sync for this member.
*/
2011-01-04 00:40:41 -05:00
void ReplSetImpl::_syncDoInitialSync() {
sethbmsg("initial sync pending",0);
2011-01-04 00:40:41 -05:00
const Member *source = getMemberToSyncTo();
if (!source) {
sethbmsg("initial sync need a member to be primary or secondary to do our initial sync", 0);
sleepsecs(15);
return;
}
2011-01-04 00:40:41 -05:00
string sourceHostname = source->h().toString();
2010-07-18 13:34:16 -04:00
OplogReader r;
if( !r.connect(sourceHostname) ) {
sethbmsg( str::stream() << "initial sync couldn't connect to " << source->h().toString() , 0);
2010-07-18 15:02:46 -04:00
sleepsecs(15);
return;
}
BSONObj lastOp = r.getLastOp(rsoplog);
2011-01-04 00:40:41 -05:00
if( lastOp.isEmpty() ) {
2010-07-19 18:05:44 -04:00
sethbmsg("initial sync couldn't read remote oplog", 0);
2010-07-19 09:29:49 -04:00
sleepsecs(15);
return;
}
OpTime startingTS = lastOp["ts"]._opTime();
2011-01-04 00:40:41 -05:00
2010-11-02 18:47:04 -04:00
if (replSettings.fastsync) {
log() << "fastsync: skipping database clone" << rsLog;
}
else {
sethbmsg("initial sync drop all databases", 0);
dropAllDatabasesExceptLocal();
sethbmsg("initial sync clone all databases", 0);
list<string> dbs = r.conn()->getDatabaseNames();
for( list<string>::iterator i = dbs.begin(); i != dbs.end(); i++ ) {
string db = *i;
if( db != "local" ) {
sethbmsg( str::stream() << "initial sync cloning db: " << db , 0);
bool ok;
{
writelock lk(db);
Client::Context ctx(db);
ok = clone(sourceHostname.c_str(), db);
2010-11-02 18:47:04 -04:00
}
2011-01-04 00:40:41 -05:00
if( !ok ) {
2010-11-02 18:47:04 -04:00
sethbmsg( str::stream() << "initial sync error clone of " << db << " failed sleeping 5 minutes" ,0);
sleepsecs(300);
return;
}
2010-07-19 09:29:49 -04:00
}
}
}
2010-07-19 09:35:13 -04:00
2010-07-19 18:05:44 -04:00
sethbmsg("initial sync query minValid",0);
2010-07-19 09:35:13 -04:00
isyncassert( "initial sync source must remain readable throughout our initial sync", source->state().readable() );
2010-11-07 15:02:48 -05:00
2011-01-04 00:40:41 -05:00
/* our cloned copy will be strange until we apply oplog events that occurred
2010-07-19 09:35:13 -04:00
through the process. we note that time point here. */
BSONObj minValid = r.getLastOp(rsoplog);
2010-11-07 15:02:48 -05:00
isyncassert( "getLastOp is empty ", !minValid.isEmpty() );
OpTime mvoptime = minValid["ts"]._opTime();
assert( !mvoptime.isNull() );
2010-07-19 09:35:13 -04:00
2011-01-04 00:40:41 -05:00
/* apply relevant portion of the oplog
*/
{
isyncassert( "initial sync source must remain readable throughout our initial sync [2]", source->state().readable() );
if( ! initialSyncOplogApplication(source, /*applyGTE*/startingTS, /*minValid*/mvoptime) ) { // note we assume here that this call does not throw
2010-08-02 18:07:55 -04:00
log() << "replSet initial sync failed during applyoplog" << rsLog;
emptyOplog(); // otherwise we'll be up!
2011-01-04 00:40:41 -05:00
lastOpTimeWritten = OpTime();
lastH = 0;
2010-08-02 18:07:55 -04:00
log() << "replSet cleaning up [1]" << rsLog;
{
writelock lk("local.");
Client::Context cx( "local." );
2011-01-04 00:40:41 -05:00
cx.db()->flushFiles(true);
}
2010-08-02 18:07:55 -04:00
log() << "replSet cleaning up [2]" << rsLog;
2010-10-30 14:41:49 -04:00
sleepsecs(5);
return;
}
}
sethbmsg("initial sync finishing up",0);
2011-01-04 00:40:41 -05:00
2010-07-22 20:21:23 -04:00
assert( !box.getState().primary() ); // wouldn't make sense if we were.
2010-07-19 09:29:49 -04:00
{
writelock lk("local.");
Client::Context cx( "local." );
2011-01-04 00:40:41 -05:00
cx.db()->flushFiles(true);
try {
log() << "replSet set minValid=" << minValid["ts"]._opTime().toString() << rsLog;
}
catch(...) { }
2010-07-19 09:35:13 -04:00
Helpers::putSingleton("local.replset.minvalid", minValid);
cx.db()->flushFiles(true);
2010-07-19 09:29:49 -04:00
}
2010-07-19 18:05:44 -04:00
sethbmsg("initial sync done",0);
2010-07-18 12:53:40 -04:00
}
}