Files
mongo/db/repl/rs_initialsync.cpp

215 lines
7.0 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"
#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
void isyncassert(const char *msg, bool expr) {
if( !expr ) {
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
}
}
2010-07-18 12:53:40 -04:00
void ReplSetImpl::syncDoInitialSync() {
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);
}
}
}
2010-07-19 09:29:49 -04:00
bool cloneFrom(const char *masterHost, string& errmsg, const string& fromdb, bool logForReplication,
bool slaveOk, bool useReplAuth, bool snapshot);
/* todo : progress metering to sethbmsg. */
static bool clone(const char *master, string db) {
string err;
return cloneFrom(master, err, db, false,
/*slaveok later can be true*/ false, true, false);
}
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);
2010-08-02 12:16:01 -04:00
NamespaceDetails *d = nsdetails(rsoplog);
// temp
2010-09-27 12:35:22 -04:00
if( d && d->stats.nrecords == 0 )
2010-08-02 12:16:01 -04:00
return; // already empty, ok.
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);
2010-08-02 12:16:01 -04:00
log() << "replSet recreated oplog so it is empty. todo optimize this..." << rsLog;
createOplog();*/
2010-08-02 12:16:01 -04:00
// TEMP: restart to recreate empty oplog
//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);
*/
}
2010-07-18 15:02:46 -04:00
void ReplSetImpl::_syncDoInitialSync() {
2010-07-19 18:05:44 -04:00
sethbmsg("initial sync pending",0);
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
2010-07-22 20:21:23 -04:00
const Member *cp = sp.primary;
2010-07-18 15:02:46 -04:00
if( cp == 0 ) {
2010-07-19 18:05:44 -04:00
sethbmsg("initial sync need a member to be primary",0);
2010-07-18 15:02:46 -04:00
sleepsecs(15);
return;
}
2010-07-18 13:34:16 -04:00
2010-07-19 09:29:49 -04:00
string masterHostname = cp->h().toString();
2010-07-18 13:34:16 -04:00
OplogReader r;
2010-07-19 09:29:49 -04:00
if( !r.connect(masterHostname) ) {
2010-07-19 18:05:44 -04:00
sethbmsg( str::stream() << "initial sync couldn't connect to " << cp->h().toString() , 0);
2010-07-18 15:02:46 -04:00
sleepsecs(15);
return;
}
BSONObj lastOp = r.getLastOp(rsoplog);
2010-07-19 09:29:49 -04: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();
2010-07-18 15:02:46 -04:00
{
/* make sure things aren't too flappy */
sleepsecs(5);
2010-07-22 20:21:23 -04:00
isyncassert( "flapping?", box.getPrimary() == cp );
2010-07-19 09:29:49 -04:00
BSONObj o = r.getLastOp(rsoplog);
isyncassert( "flapping [2]?", !o.isEmpty() );
2010-07-18 15:02:46 -04:00
}
2010-07-18 13:34:16 -04:00
2010-07-19 18:05:44 -04:00
sethbmsg("initial sync drop all databases", 0);
2010-07-18 12:53:40 -04:00
dropAllDatabasesExceptLocal();
// sethbmsg("initial sync drop oplog", 0);
// emptyOplog();
2010-07-18 15:02:46 -04:00
2010-07-19 09:29:49 -04:00
list<string> dbs = r.conn()->getDatabaseNames();
2010-07-19 18:05:44 -04:00
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(masterHostname.c_str(), db);
}
if( !ok ) {
sethbmsg( str::stream() << "initial sync error clone of " << db << " failed sleeping 5 minutes" ,0);
2010-07-19 09:29:49 -04:00
sleepsecs(300);
return;
}
}
}
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
/* our cloned copy will be strange until we apply oplog events that occurred
through the process. we note that time point here. */
BSONObj minValid = r.getLastOp(rsoplog);
assert( !minValid.isEmpty() );
OpTime mvoptime = minValid["ts"]._opTime();
assert( !mvoptime.isNull() );
2010-07-19 09:35:13 -04:00
/* copy the oplog
*/
{
sethbmsg("initial sync copy+apply oplog");
2010-08-02 18:48:24 -04:00
if( ! initialSyncOplogApplication(masterHostname, cp, startingTS, 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!
2010-08-02 12:16:01 -04: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." );
cx.db()->flushFiles(true);
}
2010-08-02 18:07:55 -04:00
log() << "replSet cleaning up [2]" << rsLog;
sleepsecs(2);
return;
}
}
sethbmsg("initial sync finishing up",0);
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." );
2010-07-29 15:01:41 -04: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
}
}