summaryrefslogtreecommitdiff
path: root/src/mongo/client/sdam/server_description.h
blob: 42e87ab34e81e3b568082e4936ecb042f3af3f96 (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
/**
 *    Copyright (C) 2019-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 <boost/algorithm/string.hpp>
#include <boost/optional.hpp>
#include <map>
#include <ostream>
#include <set>
#include <utility>

#include "mongo/bson/oid.h"
#include "mongo/client/sdam/sdam_datatypes.h"
#include "mongo/db/repl/optime.h"
#include "mongo/platform/basic.h"
#include "mongo/rpc/topology_version_gen.h"
#include "mongo/util/clock_source.h"

namespace mongo::sdam {
class ServerDescription {
    ServerDescription() = delete;

public:
    /**
     * Construct an unknown ServerDescription with default values except the server's address.
     */
    ServerDescription(ServerAddress address)
        : _address(std::move(address)), _type(ServerType::kUnknown) {}

    /**
     * Build a new ServerDescription according to the rules of the SDAM spec based on the
     * last RTT to the server and isMaster response.
     */
    ServerDescription(ClockSource* clockSource,
                      const IsMasterOutcome& isMasterOutcome,
                      boost::optional<IsMasterRTT> lastRtt = boost::none,
                      boost::optional<TopologyVersion> topologyVersion = boost::none,
                      boost::optional<int> poolResetCounter = boost::none);

    /**
     * This determines if a server description is equivalent according to the Server Discovery and
     * Monitoring specification. Members marked with (=) are used to determine equality. Note that
     * these members do not include RTT or the server's address.
     */
    bool isEquivalent(const ServerDescription& other) const;

    // server identity
    const ServerAddress& getAddress() const;
    ServerType getType() const;
    const boost::optional<ServerAddress>& getMe() const;
    const boost::optional<std::string>& getSetName() const;
    const std::map<std::string, std::string>& getTags() const;
    void appendBsonTags(BSONObjBuilder& builder) const;

    // network attributes
    const boost::optional<std::string>& getError() const;
    const boost::optional<IsMasterRTT>& getRtt() const;
    const boost::optional<int>& getLogicalSessionTimeoutMinutes() const;
    int getPoolResetCounter();

    // server capabilities
    int getMinWireVersion() const;
    int getMaxWireVersion() const;
    bool isDataBearingServer() const;
    bool isStreamable() const;

    // server 'time'
    const Date_t getLastUpdateTime() const;
    const boost::optional<Date_t>& getLastWriteDate() const;
    const boost::optional<repl::OpTime>& getOpTime() const;

    // topology membership
    const boost::optional<ServerAddress>& getPrimary() const;
    const std::set<ServerAddress>& getHosts() const;
    const std::set<ServerAddress>& getPassives() const;
    const std::set<ServerAddress>& getArbiters() const;
    const boost::optional<int>& getSetVersion() const;
    const boost::optional<OID>& getElectionId() const;
    const boost::optional<TopologyVersion>& getTopologyVersion() const;
    const boost::optional<TopologyDescriptionPtr> getTopologyDescription();

    BSONObj toBson() const;
    std::string toString() const;
    ServerDescriptionPtr cloneWithRTT(IsMasterRTT rtt);

private:
    /**
     * Classify the server's type based on the ismaster response.
     * @param isMaster - reply information for the ismaster command
     */
    void parseTypeFromIsMaster(const BSONObj isMaster);


    void calculateRtt(const IsMasterRTT currentRtt, const boost::optional<IsMasterRTT> lastRtt);
    void saveLastWriteInfo(BSONObj lastWriteBson);

    void storeHostListIfPresent(const std::string key,
                                const BSONObj response,
                                std::set<ServerAddress>& destination);
    void saveHosts(const BSONObj response);
    void saveTags(BSONObj tagsObj);
    void saveElectionId(BSONElement electionId);
    void saveTopologyVersion(BSONElement topologyVersionField);
    void saveStreamable(BSONElement streamableField);

    static inline const std::string kIsDbGrid = "isdbgrid";
    static inline const double kRttAlpha = 0.2;

    // address: the hostname or IP, and the port number, that the client connects to. Note that this
    // is not the server's ismaster.me field, in the case that the server reports an address
    // different from the address the client uses.
    ServerAddress _address;

    // topologyVersion: the server's topology version. Updated upon a significant topology change.
    boost::optional<TopologyVersion> _topologyVersion;

    // error: information about the last error related to this server. Default null.
    boost::optional<std::string> _error;

    // roundTripTime: the duration of the ismaster call. Default null.
    boost::optional<IsMasterRTT> _rtt;

    // lastWriteDate: a 64-bit BSON datetime or null. The "lastWriteDate" from the server's most
    // recent ismaster response.
    boost::optional<Date_t> _lastWriteDate;

    // opTime: an ObjectId or null. The last opTime reported by the server; an ObjectId or null.
    // (Only mongos and shard servers record this field when monitoring config servers as replica
    // sets.)
    boost::optional<repl::OpTime> _opTime;

    // (=) type: a ServerType enum value. Default Unknown.
    ServerType _type;

    // (=) minWireVersion, maxWireVersion: the wire protocol version range supported by the server.
    // Both default to 0. Use min and maxWireVersion only to determine compatibility.
    int _minWireVersion = 0;
    int _maxWireVersion = 0;

    // (=) me: The hostname or IP, and the port number, that this server was configured with in the
    // replica set. Default null.
    boost::optional<ServerAddress> _me;

    // (=) hosts, passives, arbiters: Sets of addresses. This server's opinion of the replica set's
    // members, if any. These hostnames are normalized to lower-case. Default empty. The client
    // monitors all three types of servers in a replica set.
    std::set<ServerAddress> _hosts;
    std::set<ServerAddress> _passives;
    std::set<ServerAddress> _arbiters;

    // (=) tags: map from string to string. Default empty.
    std::map<std::string, std::string> _tags;

    // (=) setName: string or null. Default null.
    boost::optional<std::string> _setName;

    // (=) setVersion: integer or null. Default null.
    boost::optional<int> _setVersion;

    // (=) electionId: an ObjectId, if this is a MongoDB 2.6+ replica set member that believes it is
    // primary. See using setVersion and electionId to detect stale primaries. Default null.
    boost::optional<OID> _electionId;

    // (=) primary: an address. This server's opinion of who the primary is. Default null.
    boost::optional<ServerAddress> _primary;

    // lastUpdateTime: when this server was last checked. Default "infinity ago".
    boost::optional<Date_t> _lastUpdateTime = Date_t::min();

    // (=) logicalSessionTimeoutMinutes: integer or null. Default null.
    boost::optional<int> _logicalSessionTimeoutMinutes;

    // (=) streamable: whether this server can stream isMaster responses. Default false.
    bool _streamable = false;

    // (=) poolResetCounter: integer, default 0. Initialized when client first creates connection
    // pool for server. Incremented on network error or timeout.
    int _poolResetCounter = 0;

    // The topology description of that we are a part of
    boost::optional<std::weak_ptr<TopologyDescription>> _topologyDescription;

    friend class TopologyDescription;
    friend class ServerDescriptionBuilder;
};

bool operator==(const mongo::sdam::ServerDescription& a, const mongo::sdam::ServerDescription& b);
bool operator!=(const mongo::sdam::ServerDescription& a, const mongo::sdam::ServerDescription& b);
std::ostream& operator<<(std::ostream& os, const ServerDescriptionPtr& description);
std::ostream& operator<<(std::ostream& os, const ServerDescription& description);
};  // namespace mongo::sdam