summaryrefslogtreecommitdiff
path: root/src/mongo/client/sdam/topology_state_machine_test.cpp
blob: 0ed51ce4b6cd261c23ae77bfcc95abbfe503b050 (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
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/**
 *    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.
 */
#include "mongo/client/sdam/topology_state_machine.h"

#include <boost/optional/optional_io.hpp>

#include "mongo/client/sdam/sdam_test_base.h"
#include "mongo/client/sdam/server_description.h"
#include "mongo/client/sdam/server_description_builder.h"
#include "mongo/client/sdam/topology_description.h"

namespace mongo::sdam {
class TopologyStateMachineTestFixture : public SdamTestFixture {
protected:
    static inline const auto kReplicaSetName = "replica_set";
    static inline const auto kLocalServer = "localhost:123";
    static inline const auto kLocalServer2 = "localhost:456";

    static inline const auto kTwoSeedConfig =
        SdamConfiguration(std::vector<ServerAddress>{kLocalServer, kLocalServer2},
                          TopologyType::kUnknown,
                          mongo::Milliseconds(500));
    static inline const auto kTwoSeedReplicaSetNoPrimaryConfig =
        SdamConfiguration(std::vector<ServerAddress>{kLocalServer, kLocalServer2},
                          TopologyType::kReplicaSetNoPrimary,
                          mongo::Milliseconds(500),
                          std::string("setName"));
    static inline const auto kSingleConfig =
        SdamConfiguration(std::vector<ServerAddress>{kLocalServer}, TopologyType::kSingle);

    // Given we in 'starting' state with initial config 'initialConfig'. We receive a
    // ServerDescription with type 'incoming', and expected the ending topology state to be
    // 'ending'.
    struct TopologyTypeTestCase {
        SdamConfiguration initialConfig;
        TopologyType starting;
        ServerType incoming;
        TopologyType ending;
    };

    // This function sets up the test scenario defined by the given TopologyTypeTestCase. It
    // simulates receiving a ServerDescription, and asserts that the final topology type is in the
    // correct state.
    void assertTopologyTypeTestCase(TopologyTypeTestCase testCase) {
        TopologyStateMachine stateMachine(testCase.initialConfig);

        // setup the initial state
        auto topologyDescription = std::make_shared<TopologyDescription>(testCase.initialConfig);
        topologyDescription->setType(testCase.starting);

        // create new ServerDescription and
        auto serverDescriptionBuilder =
            ServerDescriptionBuilder().withType(testCase.incoming).withAddress(kLocalServer);

        // update the known hosts in the ServerDescription
        if (testCase.initialConfig.getSeedList()) {
            for (auto address : *testCase.initialConfig.getSeedList()) {
                serverDescriptionBuilder.withHost(address);
            }
        }

        // set the primary if we are creating one
        if (testCase.incoming == ServerType::kRSPrimary) {
            serverDescriptionBuilder.withPrimary(kLocalServer);
        }

        // set the replica set name if appropriate
        const std::vector<ServerType>& replicaSetServerTypes = std::vector<ServerType>{
            ServerType::kRSOther, ServerType::kRSSecondary, ServerType::kRSArbiter};
        if (std::find(replicaSetServerTypes.begin(),
                      replicaSetServerTypes.end(),
                      testCase.incoming) != replicaSetServerTypes.end()) {
            serverDescriptionBuilder.withSetName(kReplicaSetName);
        }

        const auto serverDescription = serverDescriptionBuilder.instance();

        // simulate the ServerDescription being received
        stateMachine.onServerDescription(*topologyDescription, serverDescription);

        ASSERT_EQUALS(topologyDescription->getType(), testCase.ending);
    }

    std::vector<ServerType> allServerTypesExceptPrimary() {
        auto allExceptPrimary = allServerTypes();
        allExceptPrimary.erase(
            std::remove_if(allExceptPrimary.begin(),
                           allExceptPrimary.end(),
                           [](const ServerType t) { return t == ServerType::kRSPrimary; }),
            allExceptPrimary.end());
        return allExceptPrimary;
    }

    static auto getServerDescriptionAddress(const ServerDescriptionPtr& serverDescription) {
        return serverDescription->getAddress();
    };
};

TEST_F(TopologyStateMachineTestFixture, ShouldInstallServerDescriptionInSingleTopology) {
    TopologyStateMachine stateMachine(kSingleConfig);
    auto topologyDescription = std::make_shared<TopologyDescription>(kSingleConfig);

    auto updatedMeAddress = "foo:1234";
    auto serverDescription = ServerDescriptionBuilder()
                                 .withAddress(kLocalServer)
                                 .withMe(updatedMeAddress)
                                 .withType(ServerType::kStandalone)
                                 .instance();

    stateMachine.onServerDescription(*topologyDescription, serverDescription);
    ASSERT_EQUALS(static_cast<size_t>(1), topologyDescription->getServers().size());

    auto result = topologyDescription->findServerByAddress(kLocalServer);
    ASSERT(result);
    ASSERT_EQUALS(serverDescription, *result);
}

TEST_F(TopologyStateMachineTestFixture, ShouldRemoveServerDescriptionIfNotInHostsList) {
    const auto primary = (*kTwoSeedConfig.getSeedList()).front();
    const auto expectedRemovedServer = (*kTwoSeedConfig.getSeedList()).back();

    TopologyStateMachine stateMachine(kTwoSeedConfig);
    auto topologyDescription = std::make_shared<TopologyDescription>(kTwoSeedConfig);

    auto serverDescription = ServerDescriptionBuilder()
                                 .withAddress(primary)
                                 .withType(ServerType::kRSPrimary)
                                 .withPrimary(primary)
                                 .withHost(primary)
                                 .instance();

    ASSERT_EQUALS(static_cast<size_t>(2), topologyDescription->getServers().size());
    stateMachine.onServerDescription(*topologyDescription, serverDescription);
    ASSERT_EQUALS(static_cast<size_t>(1), topologyDescription->getServers().size());
    ASSERT_EQUALS(serverDescription, topologyDescription->getServers().front());
}


TEST_F(TopologyStateMachineTestFixture,
       ShouldNotRemoveReplicaSetMemberServerWhenTopologyIsReplicaSetNoPrimaryAndMeIsNotPresent) {
    const auto serverAddress = (*kTwoSeedReplicaSetNoPrimaryConfig.getSeedList()).front();

    TopologyStateMachine stateMachine(kTwoSeedReplicaSetNoPrimaryConfig);
    auto topologyDescription = std::make_shared<TopologyDescription>(kTwoSeedReplicaSetNoPrimaryConfig);

    auto serverDescription = ServerDescriptionBuilder()
                                 .withAddress(serverAddress)
                                 .withType(ServerType::kRSSecondary)
                                 .withSetName(*topologyDescription->getSetName())
                                 .instance();

    ASSERT_EQUALS(static_cast<size_t>(2), topologyDescription->getServers().size());
    auto serversBefore = map<ServerDescriptionPtr, ServerAddress>(topologyDescription->getServers(),
                                                                  getServerDescriptionAddress);

    stateMachine.onServerDescription(*topologyDescription, serverDescription);

    auto serversAfter = map<ServerDescriptionPtr, ServerAddress>(topologyDescription->getServers(),
                                                                 getServerDescriptionAddress);
    ASSERT_EQUALS(serversBefore, serversAfter);
}

TEST_F(TopologyStateMachineTestFixture,
       ShouldNotRemoveNonPrimaryServerWhenTopologyIsReplicaSetWithPrimaryAndMeIsNotPresent) {
    const auto serverAddress = (*kTwoSeedReplicaSetNoPrimaryConfig.getSeedList()).front();
    const auto primaryAddress = (*kTwoSeedReplicaSetNoPrimaryConfig.getSeedList()).back();

    TopologyStateMachine stateMachine(kTwoSeedReplicaSetNoPrimaryConfig);
    auto topologyDescription = std::make_shared<TopologyDescription>(kTwoSeedReplicaSetNoPrimaryConfig);

    auto primaryDescription = ServerDescriptionBuilder()
                                  .withAddress(primaryAddress)
                                  .withMe(primaryAddress)
                                  .withHost(primaryAddress)
                                  .withHost(serverAddress)
                                  .withSetName(*topologyDescription->getSetName())
                                  .withType(ServerType::kRSPrimary)
                                  .instance();

    auto serverDescription = ServerDescriptionBuilder()
                                 .withAddress(serverAddress)
                                 .withType(ServerType::kRSSecondary)
                                 .withSetName(*topologyDescription->getSetName())
                                 .instance();

    // change topology type to ReplicaSetWithPrimary
    stateMachine.onServerDescription(*topologyDescription, primaryDescription);
    ASSERT_EQUALS(topologyDescription->getType(), TopologyType::kReplicaSetWithPrimary);
    ASSERT_EQUALS(static_cast<size_t>(2), topologyDescription->getServers().size());

    auto serversBefore = map<ServerDescriptionPtr, ServerAddress>(topologyDescription->getServers(),
                                                                  getServerDescriptionAddress);

    stateMachine.onServerDescription(*topologyDescription, serverDescription);
    ASSERT_EQUALS(topologyDescription->getType(), TopologyType::kReplicaSetWithPrimary);

    auto serversAfter = map<ServerDescriptionPtr, ServerAddress>(topologyDescription->getServers(),
                                                                 getServerDescriptionAddress);
    ASSERT_EQUALS(serversBefore, serversAfter);
}

TEST_F(TopologyStateMachineTestFixture,
       ShouldRemoveNonPrimaryServerWhenTopologyIsReplicaSetNoPrimaryAndMeDoesntMatchAddress) {
    const auto serverAddress = (*kTwoSeedReplicaSetNoPrimaryConfig.getSeedList()).front();
    const auto expectedRemainingServerAddress =
        (*kTwoSeedReplicaSetNoPrimaryConfig.getSeedList()).back();
    const auto me = std::string("foo") + serverAddress;

    TopologyStateMachine stateMachine(kTwoSeedReplicaSetNoPrimaryConfig);
    auto topologyDescription = std::make_shared<TopologyDescription>(kTwoSeedReplicaSetNoPrimaryConfig);

    auto serverDescription = ServerDescriptionBuilder()
                                 .withAddress(serverAddress)
                                 .withMe(me)
                                 .withType(ServerType::kRSSecondary)
                                 .instance();

    ASSERT_EQUALS(static_cast<size_t>(2), topologyDescription->getServers().size());
    stateMachine.onServerDescription(*topologyDescription, serverDescription);
    ASSERT_EQUALS(static_cast<size_t>(1), topologyDescription->getServers().size());
    ASSERT_EQUALS(expectedRemainingServerAddress,
                  topologyDescription->getServers().front()->getAddress());
}

TEST_F(TopologyStateMachineTestFixture,
       ShouldAddServerDescriptionIfInHostsListButNotInTopologyDescription) {
    const auto primary = (*kTwoSeedConfig.getSeedList()).front();
    const auto secondary = (*kTwoSeedConfig.getSeedList()).back();
    const auto newHost = ServerAddress("newhost:123");

    TopologyStateMachine stateMachine(kTwoSeedConfig);
    auto topologyDescription = std::make_shared<TopologyDescription>(kTwoSeedConfig);

    auto serverDescription = ServerDescriptionBuilder()
                                 .withAddress(primary)
                                 .withType(ServerType::kRSPrimary)
                                 .withPrimary(primary)
                                 .withHost(primary)
                                 .withHost(secondary)
                                 .withHost(newHost)
                                 .instance();

    ASSERT_EQUALS(static_cast<size_t>(2), topologyDescription->getServers().size());
    stateMachine.onServerDescription(*topologyDescription, serverDescription);
    ASSERT_EQUALS(static_cast<size_t>(3), topologyDescription->getServers().size());

    auto newHostResult = topologyDescription->findServerByAddress(newHost);
    ASSERT(newHostResult);
    ASSERT_EQUALS(newHost, (*newHostResult)->getAddress());
    ASSERT_EQUALS(ServerType::kUnknown, (*newHostResult)->getType());
}

TEST_F(TopologyStateMachineTestFixture, ShouldSaveNewMaxSetVersion) {
    const auto primary = (*kTwoSeedConfig.getSeedList()).front();

    TopologyStateMachine stateMachine(kTwoSeedConfig);
    auto topologyDescription = std::make_shared<TopologyDescription>(kTwoSeedConfig);

    auto serverDescription = ServerDescriptionBuilder()
                                 .withType(ServerType::kRSPrimary)
                                 .withPrimary(primary)
                                 .withMe(primary)
                                 .withAddress(primary)
                                 .withHost(primary)
                                 .withSetVersion(100)
                                 .instance();

    stateMachine.onServerDescription(*topologyDescription, serverDescription);
    ASSERT_EQUALS(100, topologyDescription->getMaxSetVersion());

    auto serverDescriptionEvenBiggerSetVersion = ServerDescriptionBuilder()
                                                     .withType(ServerType::kRSPrimary)
                                                     .withPrimary(primary)
                                                     .withMe(primary)
                                                     .withAddress(primary)
                                                     .withHost(primary)
                                                     .withSetVersion(200)
                                                     .instance();

    stateMachine.onServerDescription(*topologyDescription, serverDescriptionEvenBiggerSetVersion);
    ASSERT_EQUALS(200, topologyDescription->getMaxSetVersion());
}

TEST_F(TopologyStateMachineTestFixture, ShouldSaveNewMaxElectionId) {
    const auto primary = (*kTwoSeedConfig.getSeedList()).front();
    auto topologyDescription = std::make_shared<TopologyDescription>(kTwoSeedConfig);
    TopologyStateMachine stateMachine(kTwoSeedConfig);

    const OID oidOne(std::string("000000000000000000000001"));
    const OID oidTwo(std::string("000000000000000000000002"));

    auto serverDescription = ServerDescriptionBuilder()
                                 .withType(ServerType::kRSPrimary)
                                 .withPrimary(primary)
                                 .withMe(primary)
                                 .withAddress(primary)
                                 .withHost(primary)
                                 .withSetVersion(1)
                                 .withElectionId(oidOne)
                                 .instance();

    stateMachine.onServerDescription(*topologyDescription, serverDescription);
    ASSERT_EQUALS(oidOne, topologyDescription->getMaxElectionId());

    auto serverDescriptionEvenBiggerElectionId = ServerDescriptionBuilder()
                                                     .withType(ServerType::kRSPrimary)
                                                     .withPrimary(primary)
                                                     .withMe(primary)
                                                     .withAddress(primary)
                                                     .withHost(primary)
                                                     .withSetVersion(1)
                                                     .withElectionId(oidTwo)
                                                     .instance();

    stateMachine.onServerDescription(*topologyDescription, serverDescriptionEvenBiggerElectionId);
    ASSERT_EQUALS(oidTwo, topologyDescription->getMaxElectionId());
}

// The following two tests (ShouldNotUpdateToplogyType, ShouldUpdateToCorrectToplogyType) assert
// that the topology type is correct given an initial state and a ServerType. Together, they
// cover all the cases specified in the SDAM spec here:
// https://github.com/mongodb/specifications/blob/master/source/server-discovery-and-monitoring/server-discovery-and-monitoring.rst#topologytype-table

TEST_F(TopologyStateMachineTestFixture, ShouldNotUpdateToplogyType) {
    using T = TopologyTypeTestCase;

    // test cases that should not change TopologyType
    std::vector<TopologyTypeTestCase> testCases{
        T{kTwoSeedConfig, TopologyType::kUnknown, ServerType::kUnknown, TopologyType::kUnknown},
        T{kTwoSeedConfig, TopologyType::kUnknown, ServerType::kStandalone, TopologyType::kUnknown},
        T{kTwoSeedConfig, TopologyType::kUnknown, ServerType::kRSGhost, TopologyType::kUnknown},
        T{kTwoSeedConfig,
          TopologyType::kReplicaSetNoPrimary,
          ServerType::kUnknown,
          TopologyType::kReplicaSetNoPrimary},
        T{kTwoSeedConfig,
          TopologyType::kReplicaSetNoPrimary,
          ServerType::kUnknown,
          TopologyType::kReplicaSetNoPrimary},
    };
    for (auto serverType : allServerTypes()) {
        testCases.push_back(
            T{kTwoSeedConfig, TopologyType::kSharded, serverType, TopologyType::kSharded});
    }

    const auto& allExceptPrimary = allServerTypesExceptPrimary();
    for (auto serverType : allExceptPrimary) {
        testCases.push_back(T{kTwoSeedConfig,
                              TopologyType::kReplicaSetNoPrimary,
                              serverType,
                              TopologyType::kReplicaSetNoPrimary});
    }

    int count = 0;
    for (auto testCase : testCases) {
        std::cout << "case " << ++count << " starting TopologyType: " << toString(testCase.starting)
                  << "; incoming ServerType: " << toString(testCase.incoming)
                  << "; expect ending TopologyType: " << toString(testCase.ending) << std::endl;

        assertTopologyTypeTestCase(testCase);
    }
}

TEST_F(TopologyStateMachineTestFixture, ShouldUpdateToCorrectToplogyType) {
    using T = TopologyTypeTestCase;

    // test cases that should change TopologyType
    const std::vector<TopologyTypeTestCase> testCases{
        T{kTwoSeedConfig, TopologyType::kUnknown, ServerType::kMongos, TopologyType::kSharded},
        T{kTwoSeedConfig,
          TopologyType::kUnknown,
          ServerType::kRSPrimary,
          TopologyType::kReplicaSetWithPrimary},
        T{kTwoSeedConfig,
          TopologyType::kUnknown,
          ServerType::kRSSecondary,
          TopologyType::kReplicaSetNoPrimary},
        T{kTwoSeedConfig,
          TopologyType::kUnknown,
          ServerType::kRSArbiter,
          TopologyType::kReplicaSetNoPrimary},
        T{kTwoSeedConfig,
          TopologyType::kUnknown,
          ServerType::kRSOther,
          TopologyType::kReplicaSetNoPrimary},
        T{kTwoSeedConfig,
          TopologyType::kReplicaSetNoPrimary,
          ServerType::kRSPrimary,
          TopologyType::kReplicaSetWithPrimary},
        T{kTwoSeedConfig,
          TopologyType::kReplicaSetWithPrimary,
          ServerType::kUnknown,
          TopologyType::kReplicaSetNoPrimary},
        T{kTwoSeedConfig,
          TopologyType::kReplicaSetWithPrimary,
          ServerType::kStandalone,
          TopologyType::kReplicaSetNoPrimary},
        T{kTwoSeedConfig,
          TopologyType::kReplicaSetWithPrimary,
          ServerType::kMongos,
          TopologyType::kReplicaSetNoPrimary},
        T{kTwoSeedConfig,
          TopologyType::kReplicaSetWithPrimary,
          ServerType::kRSPrimary,
          TopologyType::kReplicaSetWithPrimary},
        T{kTwoSeedConfig,
          TopologyType::kReplicaSetWithPrimary,
          ServerType::kRSSecondary,
          TopologyType::kReplicaSetNoPrimary},
        T{kTwoSeedConfig,
          TopologyType::kReplicaSetWithPrimary,
          ServerType::kRSOther,
          TopologyType::kReplicaSetNoPrimary},
        T{kTwoSeedConfig,
          TopologyType::kReplicaSetWithPrimary,
          ServerType::kRSArbiter,
          TopologyType::kReplicaSetNoPrimary},
        T{kTwoSeedConfig,
          TopologyType::kReplicaSetWithPrimary,
          ServerType::kRSGhost,
          TopologyType::kReplicaSetNoPrimary}};

    int count = 0;
    for (auto testCase : testCases) {
        std::cout << "case " << ++count << " starting TopologyType: " << toString(testCase.starting)
                  << "; incoming ServerType: " << toString(testCase.incoming)
                  << "; expect ending TopologyType: " << toString(testCase.ending) << std::endl;

        assertTopologyTypeTestCase(testCase);
    }
}
}  // namespace mongo::sdam