summaryrefslogtreecommitdiff
path: root/src/mongo/executor/hedged_remote_command_runner_test.cpp
blob: 3daeded83f651cd7d8b7dea4d2a910f1e842e731 (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
/**
 *    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/executor/hedged_remote_command_runner.h"

#include "mongo/client/async_remote_command_targeter.h"
#include "mongo/client/read_preference.h"
#include "mongo/client/remote_command_targeter.h"
#include "mongo/client/remote_command_targeter_factory_mock.h"
#include "mongo/client/remote_command_targeter_mock.h"
#include "mongo/client/remote_command_targeter_rs.h"
#include "mongo/db/database_name.h"
#include "mongo/db/namespace_string.h"
#include "mongo/db/query/cursor_response.h"
#include "mongo/db/query/cursor_response_gen.h"
#include "mongo/db/query/find_command_gen.h"
#include "mongo/db/repl/hello_gen.h"
#include "mongo/executor/network_interface.h"
#include "mongo/executor/network_interface_mock.h"
#include "mongo/executor/network_test_env.h"
#include "mongo/executor/remote_command_request.h"
#include "mongo/executor/remote_command_response.h"
#include "mongo/executor/remote_command_runner.h"
#include "mongo/executor/remote_command_runner_test_fixture.h"
#include "mongo/executor/remote_command_targeter.h"
#include "mongo/s/mongos_server_parameters_gen.h"
#include "mongo/unittest/barrier.h"
#include "mongo/unittest/bson_test_util.h"
#include "mongo/unittest/unittest.h"
#include "mongo/util/assert_util.h"
#include "mongo/util/cancellation.h"
#include "mongo/util/duration.h"
#include "mongo/util/time_support.h"
#include <memory>
#include <vector>


namespace mongo {
namespace executor {
using namespace remote_command_runner;
namespace {

class HedgedCommandRunnerTest : public RemoteCommandRunnerTestFixture {
public:
    void setUp() {
        RemoteCommandRunnerTestFixture::setUp();
        ReadPreferenceSetting readPref;

        auto factory = RemoteCommandTargeterFactoryMock();
        _targeter_two_hosts = factory.create(ConnectionString::forStandalones(kTwoHosts));
        _targeter_three_hosts = factory.create(ConnectionString::forStandalones(kTwoHosts));
        _emptyTargeter = factory.create(ConnectionString::forStandalones(kTwoHosts));

        auto targeterMock = RemoteCommandTargeterMock::get(_targeter_two_hosts);
        targeterMock->setFindHostsReturnValue(kTwoHosts);

        auto threeTargeterMock = RemoteCommandTargeterMock::get(_targeter_three_hosts);
        threeTargeterMock->setFindHostsReturnValue(kThreeHosts);

        auto emptyTargeterMock = RemoteCommandTargeterMock::get(_emptyTargeter);
        emptyTargeterMock->setFindHostsReturnValue(std::vector<HostAndPort>{});
    }

    NetworkInterface::Counters getNetworkInterfaceCounters() {
        auto counters = getNetworkInterfaceMock()->getCounters();
        return counters;
    }

    std::shared_ptr<RemoteCommandTargeter> getTwoHostsTargeter() {
        return _targeter_two_hosts;
    }

    std::shared_ptr<RemoteCommandTargeter> getThreeHostsTargeter() {
        return _targeter_three_hosts;
    }

    std::shared_ptr<RemoteCommandTargeter> getEmptyTargeter() {
        return _emptyTargeter;
    }

    const std::vector<HostAndPort> kTwoHosts{HostAndPort("FakeHost1", 12345),
                                             HostAndPort("FakeHost2", 12345)};
    const std::vector<HostAndPort> kThreeHosts{HostAndPort("FakeHost1", 12345),
                                               HostAndPort("FakeHost2", 12345),
                                               HostAndPort("FakeHost3", 12345)};

private:
    std::shared_ptr<RemoteCommandTargeter> _targeter_two_hosts;
    std::shared_ptr<RemoteCommandTargeter> _targeter_three_hosts;
    std::shared_ptr<RemoteCommandTargeter> _emptyTargeter;
};

// TODO SERVER-68709: write more comprehensive test cases with the mock implementation.

/**
 * When we send a find command to the doHedgedRequest function, it sends out two requests and
 * cancels the second one once the first has responsed.
 */
TEST_F(HedgedCommandRunnerTest, FindHedgeRequestTwoHosts) {
    ReadPreferenceSetting readPref;
    std::shared_ptr<RemoteCommandTargeter> t = getTwoHostsTargeter();
    std::unique_ptr<RemoteCommandHostTargeter> targeter =
        std::make_unique<mongo::remote_command_runner::AsyncRemoteCommandTargeter>(readPref, t);
    FindCommandRequest findCmd(NamespaceString("testdb", "testcoll"));

    auto h = makeOperationContext();
    auto resultFuture = doHedgedRequest(
        findCmd, h.get(), std::move(targeter), getExecutorPtr(), CancellationToken::uncancelable());

    onCommand([&](const auto& request) {
        ASSERT(request.cmdObj["find"]);
        return CursorResponse(NamespaceString("testdb", "testcoll"), 0LL, {BSON("x" << 1)})
            .toBSON(CursorResponse::ResponseType::InitialResponse);
    });

    RemoteCommandRunnerResponse<CursorInitialReply> res = resultFuture.get();

    auto counters = getNetworkInterfaceCounters();
    ASSERT_EQ(counters.succeeded, 1);
    ASSERT_EQ(counters.canceled, 1);


    ASSERT_EQ(res.response.getCursor()->getNs(), NamespaceString("testdb", "testcoll"));
}

TEST_F(HedgedCommandRunnerTest, FindHedgeRequestThreeHosts) {
    ReadPreferenceSetting readPref;
    std::shared_ptr<RemoteCommandTargeter> t = getThreeHostsTargeter();
    std::unique_ptr<RemoteCommandHostTargeter> targeter =
        std::make_unique<mongo::remote_command_runner::AsyncRemoteCommandTargeter>(readPref, t);
    FindCommandRequest findCmd(NamespaceString("testdb", "testcoll"));

    auto opCtxHolder = makeOperationContext();
    auto resultFuture = doHedgedRequest(findCmd,
                                        opCtxHolder.get(),
                                        std::move(targeter),
                                        getExecutorPtr(),
                                        CancellationToken::uncancelable());

    onCommand([&](const auto& request) {
        ASSERT(request.cmdObj["find"]);
        return CursorResponse(NamespaceString("testdb", "testcoll"), 0LL, {BSON("x" << 1)})
            .toBSON(CursorResponse::ResponseType::InitialResponse);
    });

    RemoteCommandRunnerResponse<CursorInitialReply> res = resultFuture.get();

    auto counters = getNetworkInterfaceCounters();
    ASSERT_EQ(counters.succeeded, 1);
    ASSERT_EQ(counters.canceled, 2);

    // TODO SERVER-68767: ASSERT on actual BSONObj from getFirstBatch()
    ASSERT_EQ(res.response.getCursor()->getNs(), NamespaceString("testdb", "testcoll"));
}

/**
 * When we send a hello command to the doHedgedRequest function, it does not hedge and only sends
 * one request.
 */
TEST_F(HedgedCommandRunnerTest, HelloHedgeRequest) {
    HelloCommandReply helloReply = HelloCommandReply(TopologyVersion(OID::gen(), 0));
    HelloCommand helloCmd;
    initializeCommand(helloCmd);

    ReadPreferenceSetting readPref;
    std::shared_ptr<RemoteCommandTargeter> t = getTwoHostsTargeter();
    std::unique_ptr<RemoteCommandHostTargeter> targeter =
        std::make_unique<mongo::remote_command_runner::AsyncRemoteCommandTargeter>(readPref, t);

    auto opCtxHolder = makeOperationContext();
    auto resultFuture = doHedgedRequest(helloCmd,
                                        opCtxHolder.get(),
                                        std::move(targeter),
                                        getExecutorPtr(),
                                        CancellationToken::uncancelable());

    onCommand([&](const auto& request) {
        ASSERT(request.cmdObj["hello"]);
        return helloReply.toBSON();
    });

    auto counters = getNetworkInterfaceCounters();
    ASSERT_EQ(counters.succeeded, 1);
    ASSERT_EQ(counters.canceled, 0);

    RemoteCommandRunnerResponse<HelloCommandReply> res = resultFuture.get();

    ASSERT_BSONOBJ_EQ(res.response.toBSON(), helloReply.toBSON());
}

/**
 * When the targeter returns no hosts, we get a HostNotFound error.
 */
TEST_F(HedgedCommandRunnerTest, NoShardsFound) {
    HelloCommandReply helloReply = HelloCommandReply(TopologyVersion(OID::gen(), 0));
    HelloCommand helloCmd;
    initializeCommand(helloCmd);

    ReadPreferenceSetting readPref;
    std::shared_ptr<RemoteCommandTargeter> t = getEmptyTargeter();
    std::unique_ptr<RemoteCommandHostTargeter> targeter =
        std::make_unique<mongo::remote_command_runner::AsyncRemoteCommandTargeter>(readPref, t);


    auto opCtxHolder = makeOperationContext();
    auto resultFuture = doHedgedRequest(helloCmd,
                                        opCtxHolder.get(),
                                        std::move(targeter),
                                        getExecutorPtr(),
                                        CancellationToken::uncancelable());

    ASSERT_THROWS_CODE(resultFuture.get(), DBException, ErrorCodes::HostNotFound);
}

/**
 * When a hedged command is sent and one request resolves with a non-ignorable error, we propagate
 * that error upwards and cancel the other requests.
 */
TEST_F(HedgedCommandRunnerTest, FirstCommandFailsWithSignificantError) {
    FindCommandRequest findCmd(NamespaceString("testdb", "testcoll"));

    ReadPreferenceSetting readPref;
    std::shared_ptr<RemoteCommandTargeter> t = getTwoHostsTargeter();
    std::unique_ptr<RemoteCommandHostTargeter> targeter =
        std::make_unique<mongo::remote_command_runner::AsyncRemoteCommandTargeter>(readPref, t);


    auto opCtxHolder = makeOperationContext();
    auto resultFuture = doHedgedRequest(findCmd,
                                        opCtxHolder.get(),
                                        std::move(targeter),
                                        getExecutorPtr(),
                                        CancellationToken::uncancelable());

    onCommand([&](const auto& request) {
        ASSERT(request.cmdObj["find"]);
        return Status(ErrorCodes::NetworkTimeout, "mock");
    });

    auto counters = getNetworkInterfaceCounters();
    ASSERT_EQ(counters.failed, 1);
    ASSERT_EQ(counters.canceled, 1);

    ASSERT_THROWS_CODE(resultFuture.get(), DBException, ErrorCodes::NetworkTimeout);
}

/**
 * When a hedged command is sent and all requests fail with an "ignorable" error, that error
 * propagates upwards.
 */
TEST_F(HedgedCommandRunnerTest, BothCommandsFailWithSkippableError) {
    FindCommandRequest findCmd(NamespaceString("testdb", "testcoll"));

    ReadPreferenceSetting readPref;
    std::shared_ptr<RemoteCommandTargeter> t = getTwoHostsTargeter();
    std::unique_ptr<RemoteCommandHostTargeter> targeter =
        std::make_unique<mongo::remote_command_runner::AsyncRemoteCommandTargeter>(readPref, t);


    auto opCtxHolder = makeOperationContext();
    auto resultFuture = doHedgedRequest(findCmd,
                                        opCtxHolder.get(),
                                        std::move(targeter),
                                        getExecutorPtr(),
                                        CancellationToken::uncancelable());

    onCommand([&](const auto& request) {
        ASSERT(request.cmdObj["find"]);
        return Status(ErrorCodes::MaxTimeMSExpired, "mock");
    });

    onCommand([&](const auto& request) {
        ASSERT(request.cmdObj["find"]);
        return Status(ErrorCodes::MaxTimeMSExpired, "mock");
    });

    auto counters = getNetworkInterfaceCounters();
    ASSERT_EQ(counters.failed, 2);
    ASSERT_EQ(counters.canceled, 0);

    ASSERT_THROWS_CODE(resultFuture.get(), DBException, ErrorCodes::MaxTimeMSExpired);
}

TEST_F(HedgedCommandRunnerTest, AllCommandsFailWithSkippableError) {
    FindCommandRequest findCmd(NamespaceString("testdb", "testcoll"));

    ReadPreferenceSetting readPref;
    std::shared_ptr<RemoteCommandTargeter> t = getThreeHostsTargeter();
    std::unique_ptr<RemoteCommandHostTargeter> targeter =
        std::make_unique<mongo::remote_command_runner::AsyncRemoteCommandTargeter>(readPref, t);


    auto opCtxHolder = makeOperationContext();
    auto resultFuture = doHedgedRequest(findCmd,
                                        opCtxHolder.get(),
                                        std::move(targeter),
                                        getExecutorPtr(),
                                        CancellationToken::uncancelable());

    auto network = getNetworkInterfaceMock();
    auto now = getNetworkInterfaceMock()->now();
    network->enterNetwork();

    NetworkInterfaceMock::NetworkOperationIterator noi1 = network->getNextReadyRequest();
    NetworkInterfaceMock::NetworkOperationIterator noi2 = network->getNextReadyRequest();
    NetworkInterfaceMock::NetworkOperationIterator noi3 = network->getNextReadyRequest();

    auto firstRequest = (*noi1).getRequestOnAny();
    auto secondRequest = (*noi2).getRequestOnAny();
    auto thirdRequest = (*noi3).getRequestOnAny();

    if (firstRequest.target[0] == kThreeHosts[0]) {
        network->scheduleErrorResponse(
            noi1, now + Milliseconds(1000), Status(ErrorCodes::MaxTimeMSExpired, "mock"));
    } else {
        network->scheduleErrorResponse(noi1, Status(ErrorCodes::MaxTimeMSExpired, "mock"));
    }

    if (secondRequest.target[0] == kThreeHosts[0]) {
        network->scheduleErrorResponse(
            noi2, now + Milliseconds(1000), Status(ErrorCodes::MaxTimeMSExpired, "mock"));
    } else {
        network->scheduleErrorResponse(noi2, Status(ErrorCodes::MaxTimeMSExpired, "mock"));
    }

    if (thirdRequest.target[0] == kThreeHosts[0]) {
        network->scheduleErrorResponse(
            noi3, now + Milliseconds(1000), Status(ErrorCodes::MaxTimeMSExpired, "mock"));
    } else {
        network->scheduleErrorResponse(noi3, Status(ErrorCodes::MaxTimeMSExpired, "mock"));
    }

    network->runUntil(now + Milliseconds(1500));

    auto counters = network->getCounters();
    network->exitNetwork();

    ASSERT_EQ(counters.failed, 3);
    ASSERT_EQ(counters.canceled, 0);

    ASSERT_THROWS_CODE(resultFuture.get(), DBException, ErrorCodes::MaxTimeMSExpired);
}

/**
 * When a hedged command is sent and the first (hedged) request fails with an ignorable error and
 * the second (authoritative request) succeeds, we get the success result.
 */
TEST_F(HedgedCommandRunnerTest, FirstCommandFailsWithSkippableErrorNextSucceeds) {
    FindCommandRequest findCmd(NamespaceString("testdb", "testcoll"));

    ReadPreferenceSetting readPref;
    std::shared_ptr<RemoteCommandTargeter> t = getTwoHostsTargeter();
    std::unique_ptr<RemoteCommandHostTargeter> targeter =
        std::make_unique<mongo::remote_command_runner::AsyncRemoteCommandTargeter>(readPref, t);

    auto opCtxHolder = makeOperationContext();
    auto resultFuture = doHedgedRequest(findCmd,
                                        opCtxHolder.get(),
                                        std::move(targeter),
                                        getExecutorPtr(),
                                        CancellationToken::uncancelable());

    auto network = getNetworkInterfaceMock();
    auto now = getNetworkInterfaceMock()->now();

    RemoteCommandResponse successResponse{
        CursorResponse(NamespaceString("testdb", "testcoll"), 0LL, {BSON("x" << 1)})
            .toBSON(CursorResponse::ResponseType::InitialResponse),
        Milliseconds::zero()};

    network->enterNetwork();

    NetworkInterfaceMock::NetworkOperationIterator noi1 = network->getNextReadyRequest();
    NetworkInterfaceMock::NetworkOperationIterator noi2 = network->getNextReadyRequest();

    auto firstRequest = (*noi1).getRequestOnAny();
    auto secondRequest = (*noi2).getRequestOnAny();

    // if the first request is the authoritative one, send a delayed success response
    // otherwise send an ignorable error
    if (firstRequest.target[0] == kThreeHosts[0]) {
        network->scheduleSuccessfulResponse(noi1, now + Milliseconds(1000), successResponse);
    } else {
        network->scheduleErrorResponse(noi1, Status(ErrorCodes::MaxTimeMSExpired, "mock"));
    }

    // if the second request is the authoritative one, send a delayed success response
    // otherwise send an ignorable error
    if (secondRequest.target[0] == kThreeHosts[0]) {
        network->scheduleSuccessfulResponse(noi2, now + Milliseconds(1000), successResponse);
    } else {
        network->scheduleErrorResponse(noi2, Status(ErrorCodes::MaxTimeMSExpired, "mock"));
    }

    network->runUntil(now + Milliseconds(1500));

    auto counters = network->getCounters();
    network->exitNetwork();

    ASSERT_EQ(counters.failed, 1);
    ASSERT_EQ(counters.succeeded, 1);
    ASSERT_EQ(counters.canceled, 0);

    ASSERT_EQ(resultFuture.get().response.getCursor()->getNs(),
              NamespaceString("testdb", "testcoll"));
}

}  // namespace
}  // namespace executor
}  // namespace mongo