0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-12-01 09:32:32 +01:00
mongodb/db/repl.h

139 lines
4.1 KiB
C
Raw Normal View History

// repl.h - replication
2008-07-28 00:36:47 +02: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/>.
*/
/* replication data overview
at the slave:
2008-07-29 23:54:54 +02:00
local.sources { host: ..., source: ..., syncedTo: ..., dbs: { ... } }
2008-07-28 23:52:44 +02:00
at the master:
local.oplog.$<source>
local.oplog.$main is the default
*/
#pragma once
2008-07-29 23:54:54 +02:00
class DBClientConnection;
2008-08-19 20:39:44 +02:00
class DBClientCursor;
2008-07-28 23:52:44 +02:00
extern bool slave;
extern bool master;
bool cloneFrom(const char *masterHost, string& errmsg);
#pragma pack(push,4)
2008-08-04 13:00:48 +02:00
class OpTime {
2008-08-19 00:00:17 +02:00
unsigned i;
unsigned secs;
2008-08-04 13:00:48 +02:00
public:
2008-08-19 00:00:17 +02:00
OpTime(unsigned long long date) {
reinterpret_cast<unsigned long long&>(*this) = date;
}
OpTime(unsigned a, unsigned b) { secs = a; i = b; }
OpTime() { secs = 0; i = 0; }
static OpTime now();
/* We store OpTime's in the database as Javascript Date datatype -- we needed some sort of
64 bit "container" for these values. While these are not really "Dates", that seems a
better choice for now than say, Number, which is floating point. Note the BinData type
is perhaps the cleanest choice, lacking a true unsigned64 datatype, but BinData has a
couple bytes of overhead.
*/
2008-08-19 00:00:17 +02:00
unsigned long long asDate() const { return *((unsigned long long *) &i); }
// unsigned long long& asDate() { return *((unsigned long long *) &i); }
2008-08-04 13:00:48 +02:00
bool isNull() { return secs == 0; }
string toString() const {
stringstream ss;
ss << hex << secs << ':' << i;
return ss.str();
}
bool operator==(const OpTime& r) const {
return i == r.i && secs == r.secs;
}
bool operator!=(const OpTime& r) const { return !(*this == r); }
bool operator<(const OpTime& r) const {
if( secs != r.secs )
return secs < r.secs;
return i < r.i;
}
};
#pragma pack(pop)
/* A replication exception */
2008-07-29 23:54:54 +02:00
struct SyncException {
};
/* 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 23:54:54 +02:00
{ host: ..., source: ..., syncedTo: ..., dbs: { ... } }
*/
class ReplSource {
2008-07-29 23:54:54 +02:00
bool resync(string db);
void sync_pullOpLog();
void sync_pullOpLog_applyOperation(JSObj& op);
auto_ptr<DBClientConnection> conn;
2008-08-19 20:39:44 +02:00
auto_ptr<DBClientCursor> cursor;
public:
2008-07-28 23:52:44 +02:00
string hostName; // ip addr or hostname
string sourceName; // a logical source name.
2008-08-25 22:46:39 +02:00
string only; // only a certain db. note that in the sources collection, this may not be changed once you start replicating.
2008-07-28 23:52:44 +02:00
/* the last time point we have already synced up to. */
OpTime syncedTo;
2008-07-28 23:52:44 +02:00
2008-07-29 23:54:54 +02:00
/* list of databases that we have synced.
we need this so that if we encounter a new one, we know
to go fetch the old data.
*/
set<string> dbs;
int nClonedThisPass;
static void loadAll(vector<ReplSource*>&);
static void cleanup(vector<ReplSource*>&);
ReplSource(JSObj);
bool sync();
2008-07-29 23:54:54 +02:00
void save(); // write ourself to local.sources
void resetConnection() { conn = auto_ptr<DBClientConnection>(0); }
2008-07-29 23:54:54 +02:00
// make a jsobj from our member fields of the form
2008-08-19 20:39:44 +02:00
// { host: ..., source: ..., syncedTo: ... }
2008-07-29 23:54:54 +02:00
JSObj jsobj();
bool operator==(const ReplSource&r) const {
return hostName == r.hostName && sourceName == r.sourceName;
}
};
2008-07-28 23:52:44 +02:00
/* Write operation to the log (local.oplog.$main)
2008-08-25 22:53:19 +02:00
"i" insert
"u" update
"d" delete
"c" db cmd
"db" declares presence of a database (ns is set to the db name + '.')
2008-07-28 23:52:44 +02:00
*/
void _logOp(const char *opstr, const char *ns, JSObj& obj, JSObj *patt, bool *b);
inline void logOp(const char *opstr, const char *ns, JSObj& obj, JSObj *patt = 0, bool *b = 0) {
if( master )
_logOp(opstr, ns, obj, patt, b);
}