summaryrefslogtreecommitdiff
path: root/db/commands/cloud.cpp
blob: 8f9d9d2e4b5089f60a1f1ce3fe52ad38279e6701 (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
#include "../commands.h"
#include <map>
#include "../../util/concurrency/value.h"
#include "../../util/mongoutils/str.h"
#include "../../util/net/hostandport.h"

using namespace mongoutils;

namespace mongo {

    mapsf<string,string> dynHostNames;
    extern DiagStr _hostNameCached;

    string dynHostMyName() {
        if( !str::startsWith(_hostNameCached, '#') )
            return "";
        return _hostNameCached; 
    }

    void dynHostResolve(string& name, int& port) {
        assert( !name.empty() );
        assert( !str::contains(name, ':') );
        assert( str::startsWith(name, '#') );
        string s = dynHostNames.get(name);
        if( s.empty() ) { 
            name.clear();
            return;
        }
        assert( !str::startsWith(s, '#') );
        HostAndPort hp(s);
        if( hp.hasPort() ) {
            port = hp.port();
            log() << "info: dynhost in:" << name << " out:" << hp.toString() << endl;
        }
        name = hp.host();
    }

    /** 
      { cloud:1, nodes: {
          name : <ip>, ...
        },
        me : <mylogicalname>
      }
    */
    class CmdCloud  : public Command {
    public:
    virtual LockType locktype() const { return NONE; }
        virtual bool logTheOp() { return false; }
        virtual bool adminOnly() const { return true; } // very important
        virtual bool localHostOnlyIfNoAuth(const BSONObj&) { return true; }
        virtual bool slaveOk() const { return true; }
        virtual void help( stringstream& help ) const { 
            help << "internal\n"; 
            help << "{cloud:1,nodes:...,me:<my_logical_name>}";
        }
        CmdCloud() : Command("cloud") {}
        bool run(const string& dbname, BSONObj& cmdObj, int, string& errmsg, BSONObjBuilder& result, bool fromRepl) {
            assert(!fromRepl);
            BSONObj nodes = cmdObj["nodes"].Obj();
            map<string,string> ipmap;
            for( BSONObj::iterator i(nodes); i.more(); ) { 
                BSONElement e = i.next();
                assert( *e.fieldName() == '#' );
                ipmap[e.fieldName()] = e.String();
            }

            string me = cmdObj["me"].String();
            assert( !me.empty() && me[0] == '#' );
            
            log(/*1*/) << "CmdCloud" << endl;

            if( me != _hostNameCached.get() ) { 
                log() << "CmdCloud new 'me' value:" << me << endl;
                _hostNameCached = me;
            }

            dynHostNames.swap(ipmap);
            return true;
        }
    } cmdCloud;

    BSONObj fromjson(const string &str);

    void cloudCmdLineParamIs(string cmd) {
        string errmsg;
        BSONObjBuilder res;
        BSONObj o = fromjson(cmd);
        cmdCloud.run("", o, 0, errmsg, res, false);
    }
}