summaryrefslogtreecommitdiff
path: root/db/replset.h
blob: 795dbc5193c4d0c625cffe0223ce3d361c189890 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/**
*    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 "db.h"
#include "dbhelpers.h"
#include "json.h"
#include "../client/dbclient.h"

namespace mongo {

    extern int port;
    extern const char *allDead;

    /* ReplPair is a pair of db servers replicating to one another and cooperating.

       Only one member of the pair is active at a time; so this is a smart master/slave
       configuration basically.

       You may read from the slave at anytime though (if you don't mind the slight lag).

       todo: Could be extended to be more than a pair, thus the name 'Set' -- for example,
       a set of 3...
    */

    class ReplPair {
    public:
        enum {
            State_CantArb = -3,
            State_Confused = -2,
            State_Negotiating = -1,
            State_Slave = 0,
            State_Master = 1
        };

        int state;
        string info; // commentary about our current state
        string arbHost;  // "-" for no arbiter.  "host[:port]"
        int remotePort;
        string remoteHost;
        string remote; // host:port if port specified.
//    int date; // -1 not yet set; 0=slave; 1=master

        string getInfo() {
            stringstream ss;
            ss << "  state:   ";
            if ( state == 1 ) ss << "1 State_Master ";
            else if ( state == 0 ) ss << "0 State_Slave";
            else
                ss << "<b>" << state << "</b>";
            ss << '\n';
            ss << "  info:    " << info << '\n';
            ss << "  arbhost: " << arbHost << '\n';
            ss << "  remote:  " << remoteHost << ':' << remotePort << '\n';
//        ss << "  date:    " << date << '\n';
            return ss.str();
        }

        ReplPair(const char *remoteEnd, const char *arbiter);

        bool dominant(const string& myname) {
            if ( myname == remoteHost )
                return port > remotePort;
            return myname > remoteHost;
        }

        void setMasterLocked( int n, const char *_comment = "" ) {
            dblock p;
            setMaster( n, _comment );
        }

        void setMaster(int n, const char *_comment = "");

        /* negotiate with our peer who is master */
        void negotiate(DBClientConnection *conn, string method);

        /* peer unreachable, try our arbitrator */
        void arbitrate();

        virtual
        DBClientConnection *newClientConnection() const {
            return new DBClientConnection();
        }
    };

    extern ReplPair *replPair;

    /* note we always return true for the "local" namespace.

       we should not allow most operations when not the master
       also we report not master if we are "dead".

       See also CmdIsMaster.

    */
    inline bool isMaster() {
        if ( allDead ) {
            return database->name == "local";
        }

        if ( replPair == 0 || replPair->state == ReplPair::State_Master )
            return true;

        return database->name == "local";
    }

    inline ReplPair::ReplPair(const char *remoteEnd, const char *arb) {
        state = -1;
        remote = remoteEnd;
        remotePort = DBPort;
        remoteHost = remoteEnd;
        const char *p = strchr(remoteEnd, ':');
        if ( p ) {
            remoteHost = string(remoteEnd, p-remoteEnd);
            remotePort = atoi(p+1);
            uassert("bad port #", remotePort > 0 && remotePort < 0x10000 );
            if ( remotePort == DBPort )
                remote = remoteHost; // don't include ":27017" as it is default; in case ran in diff ways over time to normalizke the hostname format in sources collection
        }

        uassert("arbiter parm is missing, use '-' for none", arb);
        arbHost = arb;
        uassert("arbiter parm is empty", !arbHost.empty());
    }

    /* This is set to true if we have EVER been up to date -- this way a new pair member
     which is a replacement won't go online as master until we have initially fully synced.
     */
    class PairSync {
        int initialsynccomplete;
    public:
        PairSync() {
            initialsynccomplete = -1;
        }

        /* call before using the class.  from dbmutex */
        void init() {
            BSONObj o;
            initialsynccomplete = 0;
            if ( Helpers::getSingleton("local.pair.sync", o) )
                initialsynccomplete = 1;
        }

        bool initialSyncCompleted() {
            return initialsynccomplete != 0;
        }

        void setInitialSyncCompleted() {
            BSONObj o = fromjson("{\"initialsynccomplete\":1}");
            Helpers::putSingleton("local.pair.sync", o);
            initialsynccomplete = 1;
        }

        void setInitialSyncCompletedLocking() {
            if ( initialsynccomplete == 1 )
                return;
            dblock lk;
            BSONObj o = fromjson("{\"initialsynccomplete\":1}");
            Helpers::putSingleton("local.pair.sync", o);
            initialsynccomplete = 1;
        }
    };


} // namespace mongo