summaryrefslogtreecommitdiff
path: root/src/mongo/db/s/topology_time_ticker_test.cpp
blob: d676ee35b40db4979dc7f1b0a5fad89a662539c5 (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
/**
 *    Copyright (C) 2022-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/db/service_context.h"
#define MONGO_LOGV2_DEFAULT_COMPONENT ::mongo::logv2::LogComponent::kTest

#include "mongo/platform/basic.h"

#include "mongo/db/keys_collection_client_sharded.h"
#include "mongo/db/keys_collection_manager.h"
#include "mongo/db/logical_time_validator.h"
#include "mongo/db/s/config/config_server_test_fixture.h"
#include "mongo/db/s/topology_time_ticker.h"
#include "mongo/db/vector_clock_mutable.h"
#include "mongo/unittest/death_test.h"
#include "mongo/unittest/unittest.h"
#include "mongo/util/clock_source_mock.h"

namespace mongo {
namespace {

struct TickPoint {
    Timestamp topologyTime;
    Timestamp commitTime;
};

class TopologyTimeTickerConfigServer : public ConfigServerTestFixture {
protected:
    TopologyTimeTickerConfigServer() : ConfigServerTestFixture(Options{}.useMockClock(true)) {}

    void setUp() override {
        ConfigServerTestFixture::setUp();
    }

    void tearDown() override {
        ConfigServerTestFixture::tearDown();
    }

    /**
     * This function registers three tick points into the TopologyTimeTicker. Those three tick
     * points are also stored in the _ticks vector.
     */
    void registerThreeTickPoints(ServiceContext* sc) {
        auto& topologyTimeTicker = TopologyTimeTicker::get(sc);
        for (int i = 0; i < 3; ++i) {
            TickPoint newTick = {Timestamp(1, 2 * i), Timestamp(1, 2 * i + 1)};
            _ticks.push_back(newTick);
            topologyTimeTicker.onNewLocallyCommittedTopologyTimeAvailable(newTick.commitTime,
                                                                          newTick.topologyTime);
        }
    }

    void checkTopologyTimeAfterAdvancingMajorityCommitPoint(ServiceContext* sc,
                                                            const Timestamp& majorityTime,
                                                            const Timestamp& expectedTopologyTime) {
        auto& topologyTimeTicker = TopologyTimeTicker::get(sc);
        auto vc = VectorClockMutable::get(sc);
        topologyTimeTicker.onMajorityCommitPointUpdate(sc, repl::OpTime(majorityTime, /*term*/ -1));
        const auto time = vc->getTime();
        ASSERT_EQ(LogicalTime(expectedTopologyTime), time.topologyTime());
    }


    static const Timestamp kCommitTimePreTicks;
    static const Timestamp kCommitTimePostTicks;
    // The commit time of the different ticks must belong to the range (kCommitTimePreTicks,
    // kCommitTimePostTicks)
    std::vector<TickPoint> _ticks;
};

const Timestamp TopologyTimeTickerConfigServer::kCommitTimePreTicks = Timestamp(0, 1);
const Timestamp TopologyTimeTickerConfigServer::kCommitTimePostTicks = Timestamp(10, 10);

TEST_F(TopologyTimeTickerConfigServer, GossipingNewTopologyTimesWhenMajorityCommitted1) {
    auto sc = getServiceContext();

    registerThreeTickPoints(sc);

    // Checking initial topologyTime value
    checkTopologyTimeAfterAdvancingMajorityCommitPoint(sc, kCommitTimePreTicks, Timestamp(0, 1));

    // Checking that after advancing the commit point we also advance the topologyTime
    for (const auto& tick : _ticks) {
        checkTopologyTimeAfterAdvancingMajorityCommitPoint(sc, tick.commitTime, tick.topologyTime);
    }

    // The majority commit point is advanced a bit more but there are no tick points: the
    // topology time shouldn't change.
    checkTopologyTimeAfterAdvancingMajorityCommitPoint(
        sc, kCommitTimePostTicks, _ticks.back().topologyTime);
}

TEST_F(TopologyTimeTickerConfigServer, GossipingNewTopologyTimesWhenMajorityCommitted2) {
    auto sc = getServiceContext();

    registerThreeTickPoints(sc);

    // Checking initial topologyTime value
    checkTopologyTimeAfterAdvancingMajorityCommitPoint(sc, kCommitTimePreTicks, Timestamp(0, 1));

    // The majority commit point is advanced to a point that includes all the tick points. The
    // topology time should be the greatest one.
    checkTopologyTimeAfterAdvancingMajorityCommitPoint(
        sc, kCommitTimePostTicks, _ticks.back().topologyTime);
}

DEATH_TEST_F(TopologyTimeTickerConfigServer,
             InvalidonNewLocallyCommittedTopologyTimeAvailable,
             "invariant") {
    // This test verifies that the internal elements on the tick point vector are sorted.
    auto sc = getServiceContext();
    Timestamp topologyTime1(0, 8);
    Timestamp commitTime1(0, 10);
    auto& topologyTimeTicker = TopologyTimeTicker::get(sc);
    topologyTimeTicker.onNewLocallyCommittedTopologyTimeAvailable(commitTime1, topologyTime1);

    Timestamp topologyTime2(0, 5);
    Timestamp commitTime2(0, 6);
    // Newer tick points must have a greater commit time than older ones
    topologyTimeTicker.onNewLocallyCommittedTopologyTimeAvailable(commitTime2, topologyTime2);
}

TEST_F(TopologyTimeTickerConfigServer, RollbackingAllTickPoints) {
    auto sc = getServiceContext();

    registerThreeTickPoints(sc);

    // Checking initial topologyTime value
    checkTopologyTimeAfterAdvancingMajorityCommitPoint(sc, kCommitTimePreTicks, Timestamp(0, 1));

    // We rollback all tick points
    auto& topologyTimeTicker = TopologyTimeTicker::get(sc);
    topologyTimeTicker.onReplicationRollback(repl::OpTime(kCommitTimePreTicks, /*term*/ -1));

    // The majority commit point is advanced to a point that would have included all our tick
    // points, but because they were rollbacked the topology time should still be Timestamp(0, 1).
    checkTopologyTimeAfterAdvancingMajorityCommitPoint(sc, kCommitTimePostTicks, Timestamp(0, 1));
}

TEST_F(TopologyTimeTickerConfigServer, PartialRollbackingTickPoints) {
    auto sc = getServiceContext();

    registerThreeTickPoints(sc);

    // We rollback the last two tick points
    auto& topologyTimeTicker = TopologyTimeTicker::get(sc);
    topologyTimeTicker.onReplicationRollback(repl::OpTime(_ticks.front().commitTime, /*term*/ -1));

    // The majority commit point is advanced to a point that includes all the tick points. The
    // topology time should be the one from the first tick since the other two were rollbacked.
    checkTopologyTimeAfterAdvancingMajorityCommitPoint(
        sc, kCommitTimePostTicks, _ticks.front().topologyTime);
}

}  // namespace
}  // namespace mongo