summaryrefslogtreecommitdiff
path: root/src/mongo/db/repl/hello_response.h
blob: 953db13c5703caed7921d3a5cb7fea3d187ef5eb (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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/**
 *    Copyright (C) 2018-present MongoDB, Inc.
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the Server Side Public License, version 1,
 *    as published by MongoDB, Inc.
 *
 *    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
 *    Server Side Public License for more details.
 *
 *    You should have received a copy of the Server Side Public License
 *    along with this program. If not, see
 *    <http://www.mongodb.com/licensing/server-side-public-license>.
 *
 *    As a special exception, the copyright holders give permission to link the
 *    code of portions of this program with the OpenSSL library under certain
 *    conditions as described in each individual source file and distribute
 *    linked combinations including the program with the OpenSSL library. You
 *    must comply with the Server Side Public License in all respects for
 *    all of the code used other than as permitted herein. If you modify file(s)
 *    with this exception, you may extend this exception to your version of the
 *    file(s), but you are not obligated to do so. If you do not wish to do so,
 *    delete this exception statement from your version. If you delete this
 *    exception statement from all source files in the program, then also delete
 *    it in the license file.
 */

#pragma once

#include <string>
#include <vector>

#include "mongo/bson/oid.h"
#include "mongo/db/repl/optime.h"
#include "mongo/db/repl/optime_with.h"
#include "mongo/rpc/topology_version_gen.h"
#include "mongo/stdx/unordered_map.h"
#include "mongo/util/net/hostandport.h"
#include "mongo/util/time_support.h"

namespace mongo {

class BSONObj;
class BSONObjBuilder;
class Status;

namespace repl {

/**
 * Response structure for the hello command. Only handles responses from nodes
 * that are in replset mode.
 */
class HelloResponse {
public:
    HelloResponse();

    /**
     * Initializes this HelloResponse from the contents of "doc".
     */
    Status initialize(const BSONObj& doc);

    /**
     * Appends all non-default values to "builder". When true, "useLegacyResponseFields" indicates
     * that we are responding to an isMaster command and not a hello command. Attach the legacy
     * "ismaster" field if true, and the "isWritablePrimary" field otherwise. There are two values
     * that are handled specially: if _inShutdown is true or _configSet is false, we will add a
     * standard response to "builder" indicating either that we are in the middle of shutting down
     * or we do not have a valid replica set config, ignoring the values of all other member
     * variables.
     */
    void addToBSON(BSONObjBuilder* builder, bool useLegacyResponseFields) const;

    /**
     * Returns a BSONObj consisting the results of calling addToBSON on an otherwise empty
     * BSONObjBuilder.
     */
    BSONObj toBSON(bool useLegacyResponseFields = true) const;


    // ===================== Accessors for member variables ================================= //

    bool isWritablePrimary() const {
        return _isWritablePrimary;
    }

    bool isSecondary() const {
        return _secondary;
    }

    const std::string& getReplSetName() const {
        return _setName;
    }

    long long getReplSetVersion() const {
        return _setVersion;
    }

    const std::vector<HostAndPort>& getHosts() const {
        return _hosts;
    }

    const std::vector<HostAndPort>& getPassives() const {
        return _passives;
    }

    const std::vector<HostAndPort>& getArbiters() const {
        return _arbiters;
    }

    const HostAndPort& getPrimary() const {
        return _primary;
    }

    bool hasPrimary() const {
        return _primarySet;
    }

    bool isArbiterOnly() const {
        return _arbiterOnly;
    }

    bool isPassive() const {
        return _passive;
    }

    bool isHidden() const {
        return _hidden;
    }

    bool shouldBuildIndexes() const {
        return _buildIndexes;
    }

    Seconds getSecondaryDelaySecs() const {
        return _secondaryDelaySecs;
    }

    stdx::unordered_map<std::string, std::string> getTags() const {
        return _tags;
    }

    const HostAndPort& getMe() const {
        return _me;
    }

    const OID& getElectionId() const {
        return _electionId;
    }

    OpTime getLastWriteOpTime() const {
        if (!_lastWrite) {
            return OpTime();
        }
        return _lastWrite->opTime;
    }

    time_t getLastWriteDate() const {
        if (!_lastWrite) {
            return 0;
        }
        return _lastWrite->value;
    }

    OpTime getLastMajorityWriteOpTime() const {
        if (!_lastMajorityWrite) {
            return OpTime();
        }
        return _lastMajorityWrite->opTime;
    }

    time_t getLastMajorityWriteDate() const {
        if (!_lastMajorityWrite) {
            return 0;
        }
        return _lastMajorityWrite->value;
    }

    boost::optional<TopologyVersion> getTopologyVersion() const {
        return _topologyVersion;
    }

    /**
     * If false, calls to toBSON/addToBSON will ignore all other fields and add a specific
     * message to indicate that we have no replica set config.
     */
    bool isConfigSet() const {
        return _configSet;
    }

    /**
     * If false, calls to toBSON/addToBSON will ignore all other fields and add a specific
     * message to indicate that we are in the middle of shutting down.
     */
    bool isShutdownInProgress() const {
        return _shutdownInProgress;
    }


    // ===================== Mutators for member variables ================================= //

    void setIsWritablePrimary(bool isWritablePrimary);

    void setIsSecondary(bool secondary);

    void setReplSetName(StringData setName);

    void setReplSetVersion(long long version);

    void addHost(const HostAndPort& host);

    void addPassive(const HostAndPort& passive);

    void addArbiter(const HostAndPort& arbiter);

    void setPrimary(const HostAndPort& primary);

    void setIsArbiterOnly(bool arbiterOnly);

    void setIsPassive(bool passive);

    void setIsHidden(bool hidden);

    void setShouldBuildIndexes(bool buildIndexes);

    void setTopologyVersion(TopologyVersion topologyVersion);

    void setSecondaryDelaySecs(Seconds secondaryDelaySecs);

    void addTag(const std::string& tagKey, const std::string& tagValue);

    void setMe(const HostAndPort& me);

    void setElectionId(const OID& electionId);

    void setLastWrite(const OpTime& lastWriteOpTime, time_t lastWriteDate);

    void setLastMajorityWrite(const OpTime& lastMajorityWriteOpTime, time_t lastMajorityWriteDate);

    /**
     * Marks _configSet as false, which will cause future calls to toBSON/addToBSON to ignore
     * all other member variables and output a hardcoded response indicating that we have no
     * valid replica set config.
     */
    void markAsNoConfig();

    /**
     * Marks _shutdownInProgress as true, which will cause future calls to toBSON/addToBSON to
     * ignore all other member variables and output a hardcoded response indicating that we are
     * in the middle of shutting down.
     */
    void markAsShutdownInProgress();

private:
    bool _isWritablePrimary;
    bool _isWritablePrimarySet;
    bool _secondary;
    bool _isSecondarySet;
    std::string _setName;
    bool _setNameSet;
    long long _setVersion;
    bool _setVersionSet;
    std::vector<HostAndPort> _hosts;
    bool _hostsSet;
    std::vector<HostAndPort> _passives;
    bool _passivesSet;
    std::vector<HostAndPort> _arbiters;
    bool _arbitersSet;
    HostAndPort _primary;
    bool _primarySet;
    bool _arbiterOnly;
    bool _arbiterOnlySet;
    bool _passive;
    bool _passiveSet;
    bool _hidden;
    bool _hiddenSet;
    bool _buildIndexes;
    bool _buildIndexesSet;
    Seconds _secondaryDelaySecs;
    bool _secondaryDelaySecsSet;
    stdx::unordered_map<std::string, std::string> _tags;
    bool _tagsSet;
    HostAndPort _me;
    bool _meSet;
    OID _electionId;
    boost::optional<OpTimeWith<time_t>> _lastWrite;
    boost::optional<OpTimeWith<time_t>> _lastMajorityWrite;
    boost::optional<TopologyVersion> _topologyVersion;

    // If _configSet is false this means we don't have a valid repl set config, so toBSON
    // will return a set of hardcoded values that indicate this.
    bool _configSet;
    // If _shutdownInProgress is true toBSON will return a set of hardcoded values to indicate
    // that we are mid shutdown
    bool _shutdownInProgress;
};

}  // namespace repl
}  // namespace mongo