summaryrefslogtreecommitdiff
path: root/db/repl/rs_config.cpp
blob: dbe0e619918954474d6c643d0978f737002fd6c0 (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
// rs_config.cpp

/**
*    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 "stdafx.h"
#include "rs_config.h"
#include "replset.h"
#include "../../client/dbclient.h"
#include "../../util/hostandport.h"

namespace mongo { 

    void ReplSetConfig::from(BSONObj o) {
        md5 = o.md5();
        _id = o["_id"].String();
        int v = o["version"].numberInt();
        uassert(13115, "bad local.system.replset config: version", v > 0);
        version = v;

        if( o["settings"].ok() ) {
            BSONObj settings = o["settings"].Obj();
            if( settings["connRetries"].ok() )
                healthOptions.connRetries = settings["connRetries"].numberInt();
            if( settings["heartbeatSleep"].ok() )
                healthOptions.heartbeatSleepMillis = (unsigned) (settings["heartbeatSleep"].Number() * 1000);
            if( settings["heartbeatTimeout"].ok() )
                healthOptions.heartbeatTimeoutMillis = (unsigned) (settings["heartbeatTimeout"].Number() * 1000);
            healthOptions.check();
        }

        set<string> hosts;
        vector<BSONElement> members = o["members"].Array();
        for( unsigned i = 0; i < members.size(); i++ ) {
            BSONObj mobj = members[i].Obj();
            Member m;
            string s = mobj["host"].String();
            try {
                m.h = HostAndPort::fromString(s);
                m.arbiterOnly = mobj.getBoolField("arbiterOnly");
                BSONElement pri = mobj["priority"];
                m.priority = pri.ok() ? pri.number() : 1.0;
            }
            catch(...) { 
                uassert(13107, "bad local.system.replset config", false);
            }
            uassert(13108, "bad local.system.replset config dups?", hosts.count(m.h.toString()) == 0);
            hosts.insert(m.h.toString());
        }
        uassert(13117, "bad local.system.replset config", !_id.empty());
    }

    static inline void configAssert(bool expr) {
        uassert(13122, "bad local.system.replset config", expr);
    }

    ReplSetConfig::ReplSetConfig(const HostAndPort& h) {
        version = -5;
        int level = 2;
        DEV level = 0;
        _ok = false;
        log(0) << "replSet load config from: " << h.toString() << endl;

        auto_ptr<DBClientCursor> c;
        try {
            DBClientConnection conn(false, 0, 20);
            conn._logLevel = 2;
            string err;
            conn.connect(h.toString());
            version = -4;

            {
                /* first, make sure other node is configured to be a replset. just to be safe. */
                BSONObj cmd = BSON( "replSetHeartbeat" << "preloadconfig?" );
                BSONObj info;
                bool ok = conn.runCommand("admin", cmd, info);
                cout << h.toString() << " " << ok << " " << info.toString() << endl;
                if( !info["rs"].trueValue() ) { 
                    stringstream ss;
                    ss << "replSet error: member " << h.toString() << " is not in --replSet mode";
                    msgassertedNoTrace(10000, ss.str().c_str()); // not caught as not a user exception - we want it not caught
                }
            }

            version = -3;

            c = conn.query("local.system.replset");
            if( !c->more() ) {
                version = -2; /* -2 is a sentinel - see ReplSetConfig::empty() */
                return;
            }
            version = -1;
        }
        catch( UserException& e) { 
            log(level) << "replSet couldn't load config " << h.toString() << ' ' << e.what() << endl;
            return;
        }

        BSONObj o = c->nextSafe();
        uassert(13109, "multiple rows in local.system.replset not supported", !c->more());
        from(o);
        _ok = true;
        log(level) << "replSet load ok" << endl;
    }

}