summaryrefslogtreecommitdiff
path: root/src/mongo/executor/pinned_connection_task_executor_test.cpp
blob: d5629c898ab6edceb41dcb69ba04b637009711f0 (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
/**
 *    Copyright (C) 2023-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/rpc/get_status_from_command_result.h"
#include "mongo/rpc/op_msg_rpc_impls.h"
#include "mongo/unittest/death_test.h"
#include "mongo/unittest/unittest.h"

#include "pinned_connection_task_executor_test_fixture.h"

namespace mongo::executor {
namespace {

RemoteCommandRequest makeRCR(HostAndPort remote, BSONObj extraFields) {
    return RemoteCommandRequest(remote, "admin", BSON("hello" << 1), extraFields, nullptr);
};

void assertMessageBodyCameFromRequest(Message m, RemoteCommandRequest rcr) {
    auto opMsg = OpMsgRequest::parse(m);
    auto expectedOpMsg = OpMsgRequest::fromDBAndBody(
        std::move(rcr.dbname), std::move(rcr.cmdObj), std::move(rcr.metadata));
    ASSERT_BSONOBJ_EQ(opMsg.body, expectedOpMsg.body);
}

void assertMessageBodyAndDBName(Message m, BSONObj body, BSONObj metadata, std::string dbName) {
    auto opMsg = OpMsgRequest::parse(m);
    auto expectedOpMsg = OpMsgRequest::fromDBAndBody(dbName, body, metadata);
    ASSERT_BSONOBJ_EQ(opMsg.body, expectedOpMsg.body);
}

Message makeOkReplyMessage() {
    rpc::OpMsgReplyBuilder replyBuilder;
    replyBuilder.setCommandReply(BSONObj());
    return replyBuilder.done();
}

Message makeErrorReplyMessage(Status error) {
    rpc::OpMsgReplyBuilder replyBuilder;
    replyBuilder.setCommandReply(error);
    return replyBuilder.done();
}

TEST_F(PinnedConnectionTaskExecutorTest, RunSingleCommandOverSession) {
    auto pinnedTE = makePinnedConnTaskExecutor();
    HostAndPort remote("mock");

    auto rcr = makeRCR(remote, BSONObj());
    auto pf = makePromiseFuture<void>();

    ASSERT_OK(pinnedTE->scheduleRemoteCommand(
        rcr, [&](const TaskExecutor::RemoteCommandCallbackArgs& args) {
            pf.promise.setWith([&] { return args.response.status; });
        }));
    // We first expect sink message to be called and to see the hello
    int32_t responseToId;
    expectSinkMessage([&](Message m) {
        responseToId = m.header().getId();
        assertMessageBodyCameFromRequest(m, rcr);
        return Status::OK();
    });
    // Now we expect source message to be called and provide the response
    expectSourceMessage([&]() {
        auto message = makeOkReplyMessage();
        message.header().setResponseToMsgId(responseToId);
        return message;
    });

    ASSERT_OK(pf.future.getNoThrow());
    pinnedTE->shutdown();
    pinnedTE->join();
}

// Test we can schedule multiple RPC on the executor, and that they then
// run serially over the same transport session.
TEST_F(PinnedConnectionTaskExecutorTest, RunTwoRemoteCommandsSimultaneously) {
    auto pinnedTE = makePinnedConnTaskExecutor();
    HostAndPort remote("mock");

    // Schedule two RPCs
    std::vector<Future<void>> results;
    for (int i = 0; i < 2; ++i) {
        auto promise = std::make_shared<Promise<void>>(NonNullPromiseTag{});
        results.push_back(promise->getFuture());
        auto extraFields = BSON("forTest" << i);
        ASSERT_OK(pinnedTE->scheduleRemoteCommand(
            makeRCR(remote, extraFields),
            [p = std::move(promise)](const TaskExecutor::RemoteCommandCallbackArgs& args) {
                p->setWith([&] { return args.response.status; });
            }));
    }
    ASSERT_EQ(2, results.size());
    for (int i = 0; i < 2; ++i) {
        auto pf = makePromiseFuture<void>();
        // We first expect sink message to be called and to see the i'th request
        // (All i requests should appear on our same mocked session).
        int32_t responseToId;
        expectSinkMessage([&](Message m) {
            responseToId = m.header().getId();
            assertMessageBodyAndDBName(m, BSON("hello" << 1), BSON("forTest" << i), "admin");
            pf.promise.emplaceValue();
            return Status::OK();
        });
        pf.future.get();
        // Now we expect source message to be called and provide the response
        expectSourceMessage([&]() {
            auto message = makeOkReplyMessage();
            message.header().setResponseToMsgId(responseToId);
            return message;
        });
        // I'th command should be completed:
        ASSERT_OK(results[i].getNoThrow());
    }
    pinnedTE->shutdown();
    pinnedTE->join();
}

TEST_F(PinnedConnectionTaskExecutorTest, FailCommandRemotelyDoesntBreakOtherCommands) {
    auto pinnedTE = makePinnedConnTaskExecutor();
    HostAndPort remote("mock");
    //
    // Schedule two RPCs
    std::vector<Future<BSONObj>> results;
    for (int i = 0; i < 2; ++i) {
        auto promise = std::make_shared<Promise<BSONObj>>(NonNullPromiseTag{});
        results.push_back(promise->getFuture());
        auto extraFields = BSON("forTest" << i);
        ASSERT_OK(pinnedTE->scheduleRemoteCommand(
            makeRCR(remote, extraFields),
            [p = std::move(promise)](const TaskExecutor::RemoteCommandCallbackArgs& args) {
                if (args.response.isOK()) {
                    p->emplaceValue(args.response.data);
                } else {
                    p->setError(args.response.status);
                }
            }));
    }
    ASSERT_EQ(2, results.size());

    int32_t responseToId;
    expectSinkMessage([&](Message m) {
        responseToId = m.header().getId();
        assertMessageBodyAndDBName(m, BSON("hello" << 1), BSON("forTest" << 0), "admin");
        return Status::OK();
    });
    // Fail the first request
    Status testFailure{ErrorCodes::BadValue, "test failure"};
    expectSourceMessage([&]() {
        auto message = makeErrorReplyMessage(testFailure);
        message.header().setResponseToMsgId(responseToId);
        return message;
    });
    auto remoteErr = results[0].getNoThrow().getValue();
    ASSERT_EQ(getStatusFromCommandResult(remoteErr), testFailure);

    // Second command should still be able to succeed:
    expectSinkMessage([&](Message m) {
        responseToId = m.header().getId();
        assertMessageBodyAndDBName(m, BSON("hello" << 1), BSON("forTest" << 1), "admin");
        return Status::OK();
    });
    expectSourceMessage([&]() {
        auto message = makeOkReplyMessage();
        message.header().setResponseToMsgId(responseToId);
        return message;
    });
    auto success = results[1].getNoThrow().getValue();
    ASSERT_EQ(Status::OK(), getStatusFromCommandResult(success));

    pinnedTE->shutdown();
    pinnedTE->join();
}

DEATH_TEST_REGEX_F(
    PinnedConnectionTaskExecutorTest,
    SchedulingCommandOnDifferentHostFails,
    R"#(Attempted to schedule RPC to (\S+):(\d+) on TaskExecutor that had pinned connection to (\S+):(\d+))#") {
    auto pinnedTE = makePinnedConnTaskExecutor();
    HostAndPort remote("mock");
    HostAndPort otherRemote("otherHost");

    // Schedule two RPCs
    auto pf = makePromiseFuture<void>();
    ASSERT_OK(pinnedTE->scheduleRemoteCommand(
        makeRCR(remote, {}), [&](const TaskExecutor::RemoteCommandCallbackArgs& args) {
            pf.promise.setWith([&] { return args.response.status; });
        }));
    auto pfTwo = makePromiseFuture<void>();
    ASSERT_OK(pinnedTE->scheduleRemoteCommand(
        makeRCR(otherRemote, {}), [&](const TaskExecutor::RemoteCommandCallbackArgs& args) {
            pfTwo.promise.setWith([&] { return args.response.status; });
        }));
    // first command runs OK
    int32_t responseToId;
    expectSinkMessage([&](Message m) {
        responseToId = m.header().getId();
        return Status::OK();
    });
    expectSourceMessage([&]() {
        auto reply = makeOkReplyMessage();
        reply.header().setResponseToMsgId(responseToId);
        return reply;
    });
    ASSERT_OK(pf.future.getNoThrow());

    // Second command should invariant once the PCTE attempts to run it, because it has a different
    // remote target.
    // Should never be fulfilled.
    ASSERT_OK(pfTwo.future.getNoThrow());
}

TEST_F(PinnedConnectionTaskExecutorTest, CancelRPC) {
    auto pinnedTE = makePinnedConnTaskExecutor();
    HostAndPort remote("mock");

    auto rcr = makeRCR(remote, BSONObj());
    auto pf = makePromiseFuture<void>();

    // Schedule a command.
    auto swCbHandle = pinnedTE->scheduleRemoteCommand(
        std::move(rcr), [&](const TaskExecutor::RemoteCommandCallbackArgs& args) {
            pf.promise.setWith([&] { return args.response.status; });
        });
    ASSERT_OK(swCbHandle);
    auto cbHandle = swCbHandle.getValue();
    pinnedTE->cancel(cbHandle);
    ASSERT_EQ(pf.future.getNoThrow(), TaskExecutor::kCallbackCanceledErrorStatus);

    pinnedTE->shutdown();
    pinnedTE->join();
}

TEST_F(PinnedConnectionTaskExecutorTest, ShutdownWithRPCInProgress) {
    auto pinnedTE = makePinnedConnTaskExecutor();
    auto pf = makePromiseFuture<void>();
    ASSERT_OK(pinnedTE->scheduleRemoteCommand(
        makeRCR(HostAndPort("mock"), BSONObj()),
        [&](const TaskExecutor::RemoteCommandCallbackArgs& args) {
            pf.promise.setWith([&] { return args.response.status; });
        }));
    pinnedTE->shutdown();
    ASSERT_EQ(pf.future.getNoThrow(), TaskExecutor::kCallbackCanceledErrorStatus);
    pinnedTE->join();
}

TEST_F(PinnedConnectionTaskExecutorTest, CancelNonRPC) {
    auto pinnedTE = makePinnedConnTaskExecutor();

    auto pf = makePromiseFuture<void>();
    // Schedule some work
    auto now = getNet()->now();
    auto swCbHandle = pinnedTE->scheduleWorkAt(now + Milliseconds(10), [&](auto&& cbArgs) {
        pf.promise.setWith([&] { return cbArgs.status; });
    });

    ASSERT_OK(swCbHandle);
    auto cbHandle = swCbHandle.getValue();
    pinnedTE->cancel(cbHandle);

    ASSERT_EQ(pf.future.getNoThrow(), TaskExecutor::kCallbackCanceledErrorStatus);

    pinnedTE->shutdown();
    pinnedTE->join();
}

TEST_F(PinnedConnectionTaskExecutorTest, EnsureStreamIsUpdatedAfterUse) {
    auto pinnedTE = makePinnedConnTaskExecutor();
    HostAndPort remote("mock");

    auto rcr = makeRCR(remote, BSONObj());
    auto pf = makePromiseFuture<void>();
    // We haven't done any RPCs, so we shouldn't have touched any of the stream counters.
    ASSERT_EQ(_indicateSuccessCalls.load(), 0);
    ASSERT_EQ(_indicateUsedCalls.load(), 0);
    ASSERT_EQ(_indicateFailureCalls.load(), 0);

    ASSERT_OK(pinnedTE->scheduleRemoteCommand(
        rcr, [&](const TaskExecutor::RemoteCommandCallbackArgs& args) {
            pf.promise.setWith([&] { return args.response.status; });
        }));
    int32_t responseToId;
    expectSinkMessage([&](Message m) {
        responseToId = m.header().getId();
        assertMessageBodyCameFromRequest(m, rcr);
        return Status::OK();
    });
    expectSourceMessage([&]() {
        auto message = makeOkReplyMessage();
        message.header().setResponseToMsgId(responseToId);
        return message;
    });

    ASSERT_OK(pf.future.getNoThrow());

    pinnedTE->shutdown();
    pinnedTE->join();

    // We have compelted an RPC successfully using the leased stream:
    ASSERT_EQ(_indicateSuccessCalls.load(), 1);
    ASSERT_EQ(_indicateUsedCalls.load(), 1);
    ASSERT_EQ(_indicateFailureCalls.load(), 0);
}

TEST_F(PinnedConnectionTaskExecutorTest, StreamFailureShutsDownAndCancels) {
    auto pinnedTE = makePinnedConnTaskExecutor();
    HostAndPort remote("mock");

    // We haven't done any RPCs, so we shouldn't have touched any of the stream counters.
    ASSERT_EQ(_indicateSuccessCalls.load(), 0);
    ASSERT_EQ(_indicateUsedCalls.load(), 0);
    ASSERT_EQ(_indicateFailureCalls.load(), 0);


    // Schedule two RPCs
    std::vector<Future<BSONObj>> results;
    for (int i = 0; i < 2; ++i) {
        auto promise = std::make_shared<Promise<BSONObj>>(NonNullPromiseTag{});
        results.push_back(promise->getFuture());
        auto extraFields = BSON("forTest" << i);
        ASSERT_OK(pinnedTE->scheduleRemoteCommand(
            makeRCR(remote, extraFields),
            [p = std::move(promise)](const TaskExecutor::RemoteCommandCallbackArgs& args) {
                if (args.response.isOK()) {
                    p->emplaceValue(args.response.data);
                } else {
                    p->setError(args.response.status);
                }
            }));
    }
    ASSERT_EQ(2, results.size());

    int32_t responseToId;
    expectSinkMessage([&](Message m) {
        responseToId = m.header().getId();
        assertMessageBodyAndDBName(m, BSON("hello" << 1), BSON("forTest" << 0), "admin");
        return Status::OK();
    });

    // Fail the first request
    Status testFailure{ErrorCodes::BadValue, "test failure"};
    expectSourceMessage([&]() { return testFailure; });
    auto localErr = results[0].getNoThrow().getStatus();
    ASSERT_EQ(localErr, testFailure);

    // The second should be cancelled automatically by shutdown.
    ASSERT_EQ(results[1].getNoThrow(), TaskExecutor::kCallbackCanceledErrorStatus);
    ASSERT(pinnedTE->isShuttingDown());

    // We failed.
    ASSERT_EQ(_indicateSuccessCalls.load(), 0);
    ASSERT_EQ(_indicateUsedCalls.load(), 0);
    ASSERT_EQ(_indicateFailureCalls.load(), 1);
    pinnedTE->join();
}
}  // namespace
}  // namespace mongo::executor