Files
mongo/db/repl.h

192 lines
6.4 KiB
C
Raw Normal View History

// repl.h - replication
2008-07-27 18:36:47 -04:00
/**
* Copyright (C) 2008 10gen Inc.
2008-12-28 20:28:49 -05:00
*
2008-07-27 18:36:47 -04:00
* 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.
2008-12-28 20:28:49 -05:00
*
2008-07-27 18:36:47 -04:00
* 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.
2008-12-28 20:28:49 -05:00
*
2008-07-27 18:36:47 -04:00
* 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/>.
*/
/* replication data overview
2008-12-28 20:28:49 -05:00
at the slave:
local.sources { host: ..., source: ..., only: ..., syncedTo: ..., localLogTs: ..., dbsNextPass: { ... }, incompleteCloneDbs: { ... } }
2008-07-28 17:52:44 -04:00
at the master:
local.oplog.$<source>
*/
#pragma once
2009-04-23 18:01:24 -04:00
#include "pdfile.h"
#include "db.h"
#include "dbhelpers.h"
2008-11-30 21:00:54 -05:00
#include "../client/dbclient.h"
2009-04-23 18:01:24 -04:00
#include "../util/optime.h"
2010-04-13 13:50:09 -04:00
#include "oplog.h"
2010-07-05 23:10:51 -04:00
#include "../util/concurrency/thread_pool.h"
2010-07-18 13:34:16 -04:00
#include "oplogreader.h"
#include "cloner.h"
2009-01-14 17:09:51 -05:00
2010-04-13 13:50:09 -04:00
namespace mongo {
2009-08-17 21:46:18 -04:00
2011-03-29 16:56:21 -07:00
/* replication slave? (possibly with slave)
2009-08-17 21:46:18 -04:00
--slave cmd line setting -> SimpleSlave
2011-01-04 00:40:41 -05:00
*/
2011-03-29 16:56:21 -07:00
typedef enum { NotSlave=0, SimpleSlave } SlaveTypes;
2009-08-17 21:46:18 -04:00
class ReplSettings {
public:
SlaveTypes slave;
2011-03-29 16:56:21 -07:00
/** true means we are master and doing replication. if we are not writing to oplog, this won't be true. */
bool master;
bool fastsync;
2011-01-04 00:40:41 -05:00
bool autoresync;
2011-01-04 00:40:41 -05:00
2010-03-01 13:55:31 -08:00
int slavedelay;
set<string> discoveredSeeds;
2011-06-22 10:23:09 -04:00
BSONObj reconfig;
ReplSettings()
: slave(NotSlave) , master(false) , fastsync() , autoresync(false), slavedelay(), discoveredSeeds() {
}
};
2009-08-17 21:46:18 -04:00
extern ReplSettings replSettings;
2011-01-04 00:40:41 -05:00
/* A replication exception */
2009-02-06 16:21:49 -05:00
class SyncException : public DBException {
public:
2011-01-04 00:40:41 -05:00
SyncException() : DBException( "sync exception" , 10001 ) {}
};
/* A Source is a source from which we can pull (replicate) data.
stored in collection local.sources.
Can be a group of things to replicate for several databases.
2008-07-29 17:54:54 -04:00
2011-03-29 16:56:21 -07:00
{ host: ..., source: ..., only: ..., syncedTo: ..., dbsNextPass: { ... }, incompleteCloneDbs: { ... } }
2008-10-10 16:55:21 -04:00
'source' defaults to 'main'; support for multiple source names is
not done (always use main for now).
2008-12-28 20:28:49 -05:00
*/
class ReplSource {
2011-06-26 01:14:06 -04:00
shared_ptr<ThreadPool> tp;
2010-07-06 17:49:20 -04:00
void resync(string db);
/** @param alreadyLocked caller already put us in write lock if true */
2011-03-29 16:56:21 -07:00
void sync_pullOpLog_applyOperation(BSONObj& op, bool alreadyLocked);
/* pull some operations from the master's oplog, and apply them.
calls sync_pullOpLog_applyOperation
*/
int sync_pullOpLog(int& nApplied);
/* we only clone one database per pass, even if a lot need done. This helps us
avoid overflowing the master's transaction log by doing too much work before going
back to read more transactions. (Imagine a scenario of slave startup where we try to
clone 100 databases in one pass.)
*/
set<string> addDbNextPass;
2009-04-16 11:36:06 -04:00
set<string> incompleteCloneDbs;
ReplSource();
2011-01-04 00:40:41 -05:00
// returns the dummy ns used to do the drop
string resyncDrop( const char *db, const char *requester );
2009-04-22 17:44:23 -04:00
// call without the db mutex
void syncToTailOfRemoteLog();
string ns() const { return string( "local.oplog.$" ) + sourceName(); }
unsigned _sleepAdviceTime;
2011-01-04 00:40:41 -05:00
/**
* If 'db' is a new database and its name would conflict with that of
* an existing database, synchronize these database names with the
* master.
* @return true iff an op with the specified ns may be applied.
*/
bool handleDuplicateDbName( const BSONObj &op, const char *ns, const char *db );
public:
OplogReader oplogReader;
void applyOperation(const BSONObj& op);
string hostName; // ip addr or hostname plus optionally, ":<port>"
string _sourceName; // a logical source name.
string sourceName() const { return _sourceName.empty() ? "main" : _sourceName; }
string only; // only a certain db. note that in the sources collection, this may not be changed once you start replicating.
2010-01-04 17:41:12 -05:00
/* the last time point we have already synced up to (in the remote/master's oplog). */
OpTime syncedTo;
2010-01-04 17:41:12 -05:00
int nClonedThisPass;
2009-04-01 16:00:56 -04:00
typedef vector< shared_ptr< ReplSource > > SourceVector;
static void loadAll(SourceVector&);
2009-03-03 18:09:03 -05:00
explicit ReplSource(BSONObj);
/* -1 = error */
int sync(int& nApplied);
void save(); // write ourself to local.sources
// make a jsobj from our member fields of the form
// { host: ..., source: ..., syncedTo: ... }
BSONObj jsobj();
bool operator==(const ReplSource&r) const {
return hostName == r.hostName && sourceName() == r.sourceName();
}
string toString() const { return sourceName() + "@" + hostName; }
2011-01-04 00:40:41 -05:00
bool haveMoreDbsToSync() const { return !addDbNextPass.empty(); }
2010-03-01 13:55:31 -08:00
int sleepAdvice() const {
if ( !_sleepAdviceTime )
return 0;
int wait = _sleepAdviceTime - unsigned( time( 0 ) );
2010-03-01 13:55:31 -08:00
return wait > 0 ? wait : 0;
}
2011-01-04 00:40:41 -05:00
2009-02-02 11:15:24 -05:00
static bool throttledForceResyncDead( const char *requester );
static void forceResyncDead( const char *requester );
void forceResync( const char *requester );
};
bool anyReplEnabled();
2010-02-10 14:18:57 -05:00
void appendReplicationInfo( BSONObjBuilder& result , bool authed , int level = 0 );
2011-01-04 00:40:41 -05:00
/**
* Helper class used to set and query an ignore state for a named database.
* The ignore state will expire after a specified OpTime.
*/
class DatabaseIgnorer {
public:
/** Indicate that operations for 'db' should be ignored until after 'futureOplogTime' */
void doIgnoreUntilAfter( const string &db, const OpTime &futureOplogTime );
/**
* Query ignore state of 'db'; if 'currentOplogTime' is after the ignore
* limit, the ignore state will be cleared.
*/
bool ignoreAt( const string &db, const OpTime &currentOplogTime );
private:
map< string, OpTime > _ignores;
};
2011-01-04 00:40:41 -05:00
2009-01-14 17:09:51 -05:00
} // namespace mongo