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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
|
/**
* 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.
*/
#define MONGO_LOG_DEFAULT_COMPONENT ::mongo::logger::LogComponent::kDefault
#include "mongo/client/sdam/sdam_test_base.h"
#include "mongo/client/sdam/topology_description.h"
#include <boost/optional/optional_io.hpp>
#include "mongo/client/sdam/server_description.h"
#include "mongo/client/sdam/server_description_builder.h"
#include "mongo/db/wire_version.h"
#include "mongo/logv2/log.h"
#include "mongo/unittest/death_test.h"
#include "mongo/util/log.h"
namespace mongo {
template std::ostream& operator<<(std::ostream& os,
const std::vector<mongo::sdam::ServerAddress>& s);
namespace sdam {
using mongo::operator<<;
class TopologyDescriptionTestFixture : public SdamTestFixture {
protected:
void assertDefaultConfig(const TopologyDescription& topologyDescription);
static inline const auto kSetName = std::string("mySetName");
static inline const std::vector<ServerAddress> kOneServer{"foo:1234"};
static inline const std::vector<ServerAddress> kTwoServersVaryCase{"FoO:1234", "BaR:1234"};
static inline const std::vector<ServerAddress> kTwoServersNormalCase{"foo:1234", "bar:1234"};
static inline const std::vector<ServerAddress> kThreeServers{
"foo:1234", "bar:1234", "baz:1234"};
static inline const auto kDefaultConfig = SdamConfiguration();
static inline const auto kSingleSeedConfig =
SdamConfiguration(kOneServer, TopologyType::kSingle);
};
void TopologyDescriptionTestFixture::assertDefaultConfig(
const TopologyDescription& topologyDescription) {
ASSERT_EQUALS(boost::none, topologyDescription.getSetName());
ASSERT_EQUALS(boost::none, topologyDescription.getMaxElectionId());
auto expectedDefaultServer = ServerDescription("localhost:27017");
ASSERT_EQUALS(expectedDefaultServer, *topologyDescription.getServers().front());
ASSERT_EQUALS(static_cast<std::size_t>(1), topologyDescription.getServers().size());
ASSERT_EQUALS(true, topologyDescription.isWireVersionCompatible());
ASSERT_EQUALS(boost::none, topologyDescription.getWireVersionCompatibleError());
ASSERT_EQUALS(boost::none, topologyDescription.getLogicalSessionTimeoutMinutes());
}
TEST_F(TopologyDescriptionTestFixture, ShouldHaveCorrectDefaultValues) {
assertDefaultConfig(TopologyDescription(kDefaultConfig));
assertDefaultConfig(TopologyDescription());
}
TEST_F(TopologyDescriptionTestFixture, ShouldNormalizeInitialSeedList) {
auto config = SdamConfiguration(kTwoServersVaryCase);
TopologyDescription topologyDescription(config);
auto expectedAddresses = kTwoServersNormalCase;
auto serverAddresses = map<ServerDescriptionPtr, ServerAddress>(
topologyDescription.getServers(),
[](const ServerDescriptionPtr& description) { return description->getAddress(); });
ASSERT_EQUALS(expectedAddresses, serverAddresses);
}
TEST_F(TopologyDescriptionTestFixture, ShouldAllowTypeSingleWithASingleSeed) {
TopologyDescription topologyDescription(kSingleSeedConfig);
ASSERT(TopologyType::kSingle == topologyDescription.getType());
auto servers = map<ServerDescriptionPtr, ServerAddress>(
topologyDescription.getServers(),
[](const ServerDescriptionPtr& desc) { return desc->getAddress(); });
ASSERT_EQUALS(kOneServer, servers);
}
TEST_F(TopologyDescriptionTestFixture, DoesNotAllowMultipleSeedsWithSingle) {
ASSERT_THROWS_CODE(TopologyDescription({kTwoServersNormalCase, TopologyType::kSingle}),
DBException,
ErrorCodes::InvalidSeedList);
}
TEST_F(TopologyDescriptionTestFixture, ShouldSetTheReplicaSetName) {
auto expectedSetName = kSetName;
auto config = SdamConfiguration(
kOneServer, TopologyType::kReplicaSetNoPrimary, mongo::Seconds(10), expectedSetName);
TopologyDescription topologyDescription(config);
ASSERT_EQUALS(expectedSetName, *topologyDescription.getSetName());
}
TEST_F(TopologyDescriptionTestFixture, ShouldNotAllowSettingTheReplicaSetNameWithWrongType) {
ASSERT_THROWS_CODE(
TopologyDescription({kOneServer, TopologyType::kUnknown, mongo::Seconds(10), kSetName}),
DBException,
ErrorCodes::InvalidTopologyType);
}
TEST_F(TopologyDescriptionTestFixture, ShouldNotAllowTopologyTypeRSNoPrimaryWithoutSetName) {
ASSERT_THROWS_CODE(
SdamConfiguration(
kOneServer, TopologyType::kReplicaSetNoPrimary, mongo::Seconds(10), boost::none),
DBException,
ErrorCodes::TopologySetNameRequired);
}
TEST_F(TopologyDescriptionTestFixture, ShouldOnlyAllowSingleAndRsNoPrimaryWithSetName) {
auto topologyTypes = allTopologyTypes();
topologyTypes.erase(std::remove_if(topologyTypes.begin(),
topologyTypes.end(),
[](const TopologyType& topologyType) {
return topologyType == TopologyType::kSingle ||
topologyType == TopologyType::kReplicaSetNoPrimary;
}),
topologyTypes.end());
for (const auto topologyType : topologyTypes) {
LOGV2(20217,
"Check TopologyType {topologyType} with setName value.",
"topologyType"_attr = toString(topologyType));
ASSERT_THROWS_CODE(
SdamConfiguration(kOneServer, topologyType, mongo::Seconds(10), kSetName),
DBException,
ErrorCodes::InvalidTopologyType);
}
}
TEST_F(TopologyDescriptionTestFixture, ShouldDefaultHeartbeatToTenSecs) {
SdamConfiguration config;
ASSERT_EQUALS(SdamConfiguration::kDefaultHeartbeatFrequencyMs, config.getHeartBeatFrequency());
}
TEST_F(TopologyDescriptionTestFixture, ShouldAllowSettingTheHeartbeatFrequency) {
const auto expectedHeartbeatFrequency = mongo::Milliseconds(20000);
SdamConfiguration config(boost::none, TopologyType::kUnknown, expectedHeartbeatFrequency);
ASSERT_EQUALS(expectedHeartbeatFrequency, config.getHeartBeatFrequency());
}
TEST_F(TopologyDescriptionTestFixture, ShouldNotAllowChangingTheHeartbeatFrequencyBelow500Ms) {
auto belowThresholdFrequency =
mongo::Milliseconds(SdamConfiguration::kMinHeartbeatFrequencyMS.count() - 1);
ASSERT_THROWS_CODE(
SdamConfiguration(boost::none, TopologyType::kUnknown, belowThresholdFrequency),
DBException,
ErrorCodes::InvalidHeartBeatFrequency);
}
TEST_F(TopologyDescriptionTestFixture,
ShouldSetWireCompatibilityErrorForMinWireVersionWhenMinWireVersionIsGreater) {
const auto outgoingMaxWireVersion = WireSpec::instance().outgoing.maxWireVersion;
const auto config = SdamConfiguration(kOneServer, TopologyType::kUnknown, mongo::Seconds(10));
TopologyDescription topologyDescription(config);
const auto serverDescriptionMinVersion = ServerDescriptionBuilder()
.withAddress(kOneServer[0])
.withMe(kOneServer[0])
.withType(ServerType::kRSSecondary)
.withMinWireVersion(outgoingMaxWireVersion + 1)
.instance();
ASSERT_EQUALS(boost::none, topologyDescription.getWireVersionCompatibleError());
topologyDescription.installServerDescription(serverDescriptionMinVersion);
ASSERT_NOT_EQUALS(boost::none, topologyDescription.getWireVersionCompatibleError());
}
TEST_F(TopologyDescriptionTestFixture,
ShouldSetWireCompatibilityErrorForMinWireVersionWhenMaxWireVersionIsLess) {
const auto outgoingMinWireVersion = WireSpec::instance().outgoing.minWireVersion;
const auto config = SdamConfiguration(kOneServer, TopologyType::kUnknown, mongo::Seconds(10));
TopologyDescription topologyDescription(config);
const auto serverDescriptionMaxVersion = ServerDescriptionBuilder()
.withAddress(kOneServer[0])
.withMe(kOneServer[0])
.withType(ServerType::kRSSecondary)
.withMaxWireVersion(outgoingMinWireVersion - 1)
.instance();
ASSERT_EQUALS(boost::none, topologyDescription.getWireVersionCompatibleError());
topologyDescription.installServerDescription(serverDescriptionMaxVersion);
ASSERT_NOT_EQUALS(boost::none, topologyDescription.getWireVersionCompatibleError());
}
TEST_F(TopologyDescriptionTestFixture, ShouldNotSetWireCompatibilityErrorWhenServerTypeIsUnknown) {
const auto outgoingMinWireVersion = WireSpec::instance().outgoing.minWireVersion;
const auto config = SdamConfiguration(kOneServer, TopologyType::kUnknown, mongo::Seconds(10));
TopologyDescription topologyDescription(config);
const auto serverDescriptionMaxVersion =
ServerDescriptionBuilder().withMaxWireVersion(outgoingMinWireVersion - 1).instance();
ASSERT_EQUALS(boost::none, topologyDescription.getWireVersionCompatibleError());
topologyDescription.installServerDescription(serverDescriptionMaxVersion);
ASSERT_EQUALS(boost::none, topologyDescription.getWireVersionCompatibleError());
}
TEST_F(TopologyDescriptionTestFixture, ShouldSetLogicalSessionTimeoutToMinOfAllServerDescriptions) {
const auto config = SdamConfiguration(kThreeServers);
TopologyDescription topologyDescription(config);
const auto logicalSessionTimeouts = std::vector{300, 100, 200};
auto timeoutIt = logicalSessionTimeouts.begin();
const auto serverDescriptionsWithTimeouts = map<ServerDescriptionPtr, ServerDescriptionPtr>(
topologyDescription.getServers(), [&timeoutIt](const ServerDescriptionPtr& description) {
auto newInstanceBuilder = ServerDescriptionBuilder()
.withType(ServerType::kRSSecondary)
.withAddress(description->getAddress())
.withMe(description->getAddress())
.withLogicalSessionTimeoutMinutes(*timeoutIt);
timeoutIt++;
return newInstanceBuilder.instance();
});
for (auto description : serverDescriptionsWithTimeouts) {
topologyDescription.installServerDescription(description);
}
int expectedLogicalSessionTimeout =
*std::min_element(logicalSessionTimeouts.begin(), logicalSessionTimeouts.end());
ASSERT_EQUALS(expectedLogicalSessionTimeout,
topologyDescription.getLogicalSessionTimeoutMinutes());
}
TEST_F(TopologyDescriptionTestFixture,
ShouldSetLogicalSessionTimeoutToNoneIfAnyServerDescriptionHasNone) {
const auto config = SdamConfiguration(kThreeServers);
TopologyDescription topologyDescription(config);
const auto logicalSessionTimeouts = std::vector{300, 100, 200};
auto timeoutIt = logicalSessionTimeouts.begin();
const auto serverDescriptionsWithTimeouts = map<ServerDescriptionPtr, ServerDescriptionPtr>(
topologyDescription.getServers(), [&](const ServerDescriptionPtr& description) {
auto timeoutValue = (timeoutIt == logicalSessionTimeouts.begin())
? boost::none
: boost::make_optional(*timeoutIt);
auto newInstance = ServerDescriptionBuilder()
.withType(ServerType::kRSSecondary)
.withAddress(description->getAddress())
.withMe(description->getAddress())
.withLogicalSessionTimeoutMinutes(timeoutValue)
.instance();
++timeoutIt;
return newInstance;
});
for (auto description : serverDescriptionsWithTimeouts) {
topologyDescription.installServerDescription(description);
}
ASSERT_EQUALS(boost::none, topologyDescription.getLogicalSessionTimeoutMinutes());
}
TEST_F(TopologyDescriptionTestFixture, ShouldUpdateTopologyVersionOnSuccess) {
const auto config = SdamConfiguration(kThreeServers);
TopologyDescription topologyDescription(config);
// Deafult topologyVersion is null
ASSERT_EQUALS(topologyDescription.getServers().size(), 3);
auto serverDescription = topologyDescription.getServers()[1];
ASSERT(serverDescription->getTopologyVersion() == boost::none);
// Create new serverDescription with topologyVersion, topologyDescription should have the new
// topologyVersion
auto processId = OID("000000000000000000000001");
auto newDescription = ServerDescriptionBuilder()
.withType(ServerType::kRSSecondary)
.withAddress(serverDescription->getAddress())
.withMe(serverDescription->getAddress())
.withTopologyVersion(TopologyVersion(processId, 1))
.instance();
topologyDescription.installServerDescription(newDescription);
ASSERT_EQUALS(topologyDescription.getServers().size(), 3);
auto topologyVersion = topologyDescription.getServers()[1]->getTopologyVersion();
ASSERT(topologyVersion == TopologyVersion(processId, 1));
}
TEST_F(TopologyDescriptionTestFixture, ShouldNotUpdateTopologyVersionOnError) {
const auto config = SdamConfiguration(kThreeServers);
TopologyDescription topologyDescription(config);
// Deafult topologyVersion is null
ASSERT_EQUALS(topologyDescription.getServers().size(), 3);
auto serverDescription = topologyDescription.getServers()[1];
ASSERT(serverDescription->getTopologyVersion() == boost::none);
auto newDescription = ServerDescriptionBuilder()
.withAddress(serverDescription->getAddress())
.withError("error")
.instance();
topologyDescription.installServerDescription(newDescription);
ASSERT_EQUALS(topologyDescription.getServers().size(), 3);
auto topologyVersion = topologyDescription.getServers()[1]->getTopologyVersion();
ASSERT(topologyVersion == boost::none);
}
}; // namespace sdam
}; // namespace mongo
|