summaryrefslogtreecommitdiff
path: root/src/mongo/db/repl/replication_coordinator_test_fixture.cpp
blob: 2479c5d4724729fbd7f919b6bef733a6bcd1a443 (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
/**
 *    Copyright (C) 2014 MongoDB Inc.
 *
 *    This program is free software: you can redistribute it and/or  modify
 *    it under the terms of the GNU Affero General Public License, version 3,
 *    as published by the Free Software Foundation.
 *
 *    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
 *    GNU Affero General Public License for more details.
 *
 *    You should have received a copy of the GNU Affero General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *    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 GNU Affero General 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::kReplication

#include "mongo/platform/basic.h"

#include "mongo/db/repl/replication_coordinator_test_fixture.h"

#include "mongo/db/operation_context_noop.h"
#include "mongo/db/repl/is_master_response.h"
#include "mongo/db/repl/network_interface_mock.h"
#include "mongo/db/repl/operation_context_repl_mock.h"
#include "mongo/db/repl/repl_settings.h"
#include "mongo/db/repl/repl_set_heartbeat_args.h"
#include "mongo/db/repl/replication_coordinator_external_state_mock.h"
#include "mongo/db/repl/replication_coordinator_impl.h"
#include "mongo/db/repl/topology_coordinator_impl.h"
#include "mongo/stdx/functional.h"
#include "mongo/unittest/unittest.h"
#include "mongo/util/log.h"

namespace mongo {
namespace repl {

namespace {
    bool stringContains(const std::string &haystack, const std::string& needle) {
        return haystack.find(needle) != std::string::npos;
    }
}  // namespace

    ReplicaSetConfig ReplCoordTest::assertMakeRSConfig(const BSONObj& configBson) {
        ReplicaSetConfig config;
        ASSERT_OK(config.initialize(configBson));
        ASSERT_OK(config.validate());
        return config;
    }

    ReplCoordTest::ReplCoordTest() : _callShutdown(false) {}
    ReplCoordTest::~ReplCoordTest() {}

    void ReplCoordTest::setUp() {
        _settings.replSet = "mySet/node1:12345,node2:54321";
    }

    void ReplCoordTest::tearDown() {
        if (_externalState) {
            _externalState->setStoreLocalConfigDocumentToHang(false);
        }
        if (_callShutdown) {
            shutdown();
        }
    }

    void ReplCoordTest::assertRunUntil(Date_t newTime) {
        this->_net->runUntil(newTime);
        ASSERT_EQUALS(newTime, getNet()->now());
    }

    void ReplCoordTest::enterNetwork() {
        getNet()->enterNetwork();
    }

    void ReplCoordTest::exitNetwork() {
        getNet()->exitNetwork();
    }

    void ReplCoordTest::addSelf(const HostAndPort& selfHost) {
        getExternalState()->addSelf(selfHost);
    }

    void ReplCoordTest::init() {
        invariant(!_repl);
        invariant(!_callShutdown);

        // PRNG seed for tests.
        const int64_t seed = 0;

        _topo = new TopologyCoordinatorImpl(Seconds(0));
        _net = new NetworkInterfaceMock;
        _externalState = new ReplicationCoordinatorExternalStateMock;
        _repl.reset(new ReplicationCoordinatorImpl(_settings,
                                                   _externalState,
                                                   _net,
                                                   _topo,
                                                   seed));
    }

    void ReplCoordTest::init(const ReplSettings& settings) {
        _settings = settings;
        init();
    }

    void ReplCoordTest::init(const std::string& replSet) {
        _settings.replSet = replSet;
        init();
    }

    void ReplCoordTest::start() {
        invariant(!_callShutdown);
        // if we haven't initialized yet, do that first.
        if (!_repl) {
            init();
        }

        OperationContextNoop txn;
        _repl->startReplication(&txn);
        _repl->waitForStartUpComplete();
        _callShutdown = true;
    }

    void ReplCoordTest::start(const BSONObj& configDoc, const HostAndPort& selfHost) {
        if (!_repl) {
            init();
        }
        _externalState->setLocalConfigDocument(StatusWith<BSONObj>(configDoc));
        _externalState->addSelf(selfHost);
        start();
    }

    void ReplCoordTest::start(const HostAndPort& selfHost) {
        if (!_repl) {
            init();
        }
        _externalState->addSelf(selfHost);
        start();
    }

    void ReplCoordTest::assertStartSuccess(
            const BSONObj& configDoc,
            const HostAndPort& selfHost) {
        start(configDoc, selfHost);
        ASSERT_NE(MemberState::RS_STARTUP, getReplCoord()->getMemberState().s);
    }

    ResponseStatus ReplCoordTest::makeResponseStatus(const BSONObj& doc, Milliseconds millis) {
        log() << "Responding with " << doc;
        return ResponseStatus(ReplicationExecutor::RemoteCommandResponse(doc, millis));
    }

    void ReplCoordTest::simulateSuccessfulElection() {
        OperationContextReplMock txn;
        ReplicationCoordinatorImpl* replCoord = getReplCoord();
        NetworkInterfaceMock* net = getNet();
        ReplicaSetConfig rsConfig = replCoord->getReplicaSetConfig_forTest();
        ASSERT(replCoord->getMemberState().secondary()) <<
            replCoord->getMemberState().toString();
        while (!replCoord->getMemberState().primary()) {
            log() << "Waiting on network in state " << replCoord->getMemberState();
            getNet()->enterNetwork();
            const NetworkInterfaceMock::NetworkOperationIterator noi = net->getNextReadyRequest();
            const ReplicationExecutor::RemoteCommandRequest& request = noi->getRequest();
            log() << request.target.toString() << " processing " << request.cmdObj;
            ReplSetHeartbeatArgs hbArgs;
            if (hbArgs.initialize(request.cmdObj).isOK()) {
                ReplSetHeartbeatResponse hbResp;
                hbResp.setSetName(rsConfig.getReplSetName());
                hbResp.setState(MemberState::RS_SECONDARY);
                hbResp.setVersion(rsConfig.getConfigVersion());
                BSONObjBuilder respObj;
                respObj << "ok" << 1;
                hbResp.addToBSON(&respObj);
                net->scheduleResponse(noi, net->now(), makeResponseStatus(respObj.obj()));
            }
            else if (request.cmdObj.firstElement().fieldNameStringData() == "replSetFresh") {
                net->scheduleResponse(noi, net->now(), makeResponseStatus(
                                              BSON("ok" << 1 <<
                                                   "fresher" << false <<
                                                   "opTime" << Date_t(OpTime(0, 0).asDate()) <<
                                                   "veto" << false)));
            }
            else if (request.cmdObj.firstElement().fieldNameStringData() == "replSetElect") {
                net->scheduleResponse(noi, net->now(), makeResponseStatus(
                                              BSON("ok" << 1 <<
                                                   "vote" << 1 <<
                                                   "round" << request.cmdObj["round"].OID())));
            }
            else {
                error() << "Black holing unexpected request to " << request.target << ": " <<
                    request.cmdObj;
                net->blackHole(noi);
            }
            net->runReadyNetworkOperations();
            getNet()->exitNetwork();
        }
        ASSERT(replCoord->isWaitingForApplierToDrain());
        ASSERT(replCoord->getMemberState().primary()) <<
            replCoord->getMemberState().toString();

        IsMasterResponse imResponse;
        replCoord->fillIsMasterForReplSet(&imResponse);
        ASSERT_FALSE(imResponse.isMaster()) << imResponse.toBSON().toString();
        ASSERT_TRUE(imResponse.isSecondary()) << imResponse.toBSON().toString();
        replCoord->signalDrainComplete(&txn);
        replCoord->fillIsMasterForReplSet(&imResponse);
        ASSERT_TRUE(imResponse.isMaster()) << imResponse.toBSON().toString();
        ASSERT_FALSE(imResponse.isSecondary()) << imResponse.toBSON().toString();

        ASSERT(replCoord->getMemberState().primary()) <<
            replCoord->getMemberState().toString();
    }

    void ReplCoordTest::simulateStepDownOnIsolation() {
        ReplicationCoordinatorImpl* replCoord = getReplCoord();
        NetworkInterfaceMock* net = getNet();
        ReplicaSetConfig rsConfig = replCoord->getReplicaSetConfig_forTest();
        ASSERT(replCoord->getMemberState().primary()) <<
            replCoord->getMemberState().toString();
        while (replCoord->getMemberState().primary()) {
            log() << "Waiting on network in state " << replCoord->getMemberState();
            getNet()->enterNetwork();
            net->runUntil(net->now() + 10000);
            const NetworkInterfaceMock::NetworkOperationIterator noi = net->getNextReadyRequest();
            const ReplicationExecutor::RemoteCommandRequest& request = noi->getRequest();
            log() << request.target.toString() << " processing " << request.cmdObj;
            ReplSetHeartbeatArgs hbArgs;
            if (hbArgs.initialize(request.cmdObj).isOK()) {
                net->scheduleResponse(noi,
                                      net->now(),
                                      ResponseStatus(ErrorCodes::NetworkTimeout, "Nobody's home"));
            }
            else {
                error() << "Black holing unexpected request to " << request.target << ": " <<
                    request.cmdObj;
                net->blackHole(noi);
            }
            net->runReadyNetworkOperations();
            getNet()->exitNetwork();
        }
    }

    void ReplCoordTest::shutdown() {
        invariant(_callShutdown);
        _net->exitNetwork();
        _repl->shutdown();
        _callShutdown = false;
    }

    int64_t ReplCoordTest::countLogLinesContaining(const std::string& needle) {
        return std::count_if(getCapturedLogMessages().begin(),
                             getCapturedLogMessages().end(),
                             stdx::bind(stringContains,
                                        stdx::placeholders::_1,
                                        needle));
    }

}  // namespace repl
}  // namespace mongo