summaryrefslogtreecommitdiff
path: root/src/mongo/transport/transport_layer_asio_test.cpp
blob: 7bfc2e3f5c4086ecff3c497308d018bec382cb86 (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
/**
 *    Copyright (C) 2018-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.
 */
#define MONGO_LOGV2_DEFAULT_COMPONENT ::mongo::logv2::LogComponent::kTest

#include "mongo/platform/basic.h"

#include "mongo/transport/transport_layer_asio.h"

#include "mongo/db/server_options.h"
#include "mongo/logv2/log.h"
#include "mongo/rpc/op_msg.h"
#include "mongo/transport/service_entry_point.h"
#include "mongo/unittest/unittest.h"
#include "mongo/util/assert_util.h"
#include "mongo/util/net/sock.h"

#include "asio.hpp"

namespace mongo {
namespace {

class ServiceEntryPointUtil : public ServiceEntryPoint {
public:
    void startSession(transport::SessionHandle session) override {
        stdx::unique_lock<Latch> lk(_mutex);
        _sessions.push_back(std::move(session));
        LOGV2(2303201, "started session");
        _cv.notify_one();
    }

    void endAllSessions(transport::Session::TagMask tags) override {
        LOGV2(2303301, "end all sessions");
        std::vector<transport::SessionHandle> old_sessions;
        {
            stdx::unique_lock<Latch> lock(_mutex);
            old_sessions.swap(_sessions);
        }
        old_sessions.clear();
    }

    Status start() override {
        return Status::OK();
    }

    bool shutdown(Milliseconds timeout) override {
        return true;
    }

    void appendStats(BSONObjBuilder*) const override {}

    size_t numOpenSessions() const override {
        stdx::unique_lock<Latch> lock(_mutex);
        return _sessions.size();
    }

    Future<DbResponse> handleRequest(OperationContext* opCtx,
                                     const Message& request) noexcept override {
        MONGO_UNREACHABLE;
    }

    void setTransportLayer(transport::TransportLayer* tl) {
        _transport = tl;
    }

    void waitForConnect() {
        stdx::unique_lock<Latch> lock(_mutex);
        _cv.wait(lock, [&] { return !_sessions.empty(); });
    }

private:
    mutable Mutex _mutex = MONGO_MAKE_LATCH("::_mutex");
    stdx::condition_variable _cv;
    std::vector<transport::SessionHandle> _sessions;
    transport::TransportLayer* _transport = nullptr;
};

class SimpleConnectionThread {
public:
    explicit SimpleConnectionThread(int port) : _port(port) {
        _thr = stdx::thread{[&] {
            auto sa = SockAddr::create("localhost", _port, AF_INET);
            _s.connect(sa);
            LOGV2(23034, "connection: port {port}", "port"_attr = _port);
            stdx::unique_lock<Latch> lk(_mutex);
            _cv.wait(lk, [&] { return _stop; });
            LOGV2(23035, "connection: Rx stop request");
        }};
    }

    void forceCloseSocket() {
        // Setting linger on to a zero-value timeout causes the socket to send an RST packet to the
        // recipient side when closing the connection.
        struct linger sl = {1, 0};
#ifdef _WIN32
        char* pval = reinterpret_cast<char*>(&sl);
#else
        void* pval = &sl;
#endif
        ASSERT(!setsockopt(_s.rawFD(), SOL_SOCKET, SO_LINGER, pval, sizeof(sl)));
        _s.close();
    }

    void stop() {
        {
            stdx::unique_lock<Latch> lk(_mutex);
            _stop = true;
        }
        LOGV2(23036, "connection: Tx stop request");
        _cv.notify_one();
        _thr.join();
        LOGV2(23037, "connection: stopped");
    }

private:
    Mutex _mutex = MONGO_MAKE_LATCH("SimpleConnectionThread::_mutex");
    stdx::condition_variable _cv;
    stdx::thread _thr;
    bool _stop = false;

    Socket _s;
    int _port;
};

std::unique_ptr<transport::TransportLayerASIO> makeAndStartTL(ServiceEntryPoint* sep) {
    auto options = [] {
        ServerGlobalParams params;
        params.noUnixSocket = true;
        transport::TransportLayerASIO::Options opts(&params);

        // TODO SERVER-30212 should clean this up and assign a port from the supplied port range
        // provided by resmoke.
        opts.port = 0;
        return opts;
    }();

    auto tla = std::make_unique<transport::TransportLayerASIO>(options, sep);
    ASSERT_OK(tla->setup());
    ASSERT_OK(tla->start());

    return tla;
}

TEST(TransportLayerASIO, PortZeroConnect) {
    ServiceEntryPointUtil sepu;
    auto tla = makeAndStartTL(&sepu);

    int port = tla->listenerPort();
    ASSERT_GT(port, 0);
    LOGV2(23038, "TransportLayerASIO.listenerPort() is {port}", "port"_attr = port);

    SimpleConnectionThread connect_thread(port);
    sepu.waitForConnect();

    ASSERT_EQ(sepu.numOpenSessions(), 1);
    connect_thread.stop();
    sepu.endAllSessions({});
    tla->shutdown();
}

TEST(TransportLayerASIO, TCPResetAfterConnectionIsSilentlySwallowed) {
    ServiceEntryPointUtil sepu;
    auto tla = makeAndStartTL(&sepu);

    auto hangBeforeAcceptFp = globalFailPointRegistry().find("transportLayerASIOhangBeforeAccept");
    auto timesEntered = hangBeforeAcceptFp->setMode(FailPoint::alwaysOn);

    SimpleConnectionThread connect_thread(tla->listenerPort());
    hangBeforeAcceptFp->waitForTimesEntered(timesEntered + 1);

    connect_thread.forceCloseSocket();
    hangBeforeAcceptFp->setMode(FailPoint::off);

    ASSERT_EQ(sepu.numOpenSessions(), 0);
    connect_thread.stop();
    tla->shutdown();
}

class TimeoutSEP : public ServiceEntryPoint {
public:
    ~TimeoutSEP() override {
        // This should shutdown immediately, so give the maximum timeout
        shutdown(Milliseconds::max());
    }

    void endAllSessions(transport::Session::TagMask tags) override {
        MONGO_UNREACHABLE;
    }

    bool shutdown(Milliseconds timeout) override {
        LOGV2(23039, "Joining all worker threads");
        for (auto& thread : _workerThreads) {
            thread.join();
        }
        return true;
    }

    Status start() override {
        return Status::OK();
    }

    void appendStats(BSONObjBuilder*) const override {}

    size_t numOpenSessions() const override {
        return 0;
    }

    Future<DbResponse> handleRequest(OperationContext* opCtx,
                                     const Message& request) noexcept override {
        MONGO_UNREACHABLE;
    }

    bool waitForTimeout(boost::optional<Milliseconds> timeout = boost::none) {
        stdx::unique_lock<Latch> lk(_mutex);
        bool ret = true;
        if (timeout) {
            ret = _cond.wait_for(lk, timeout->toSystemDuration(), [this] { return _finished; });
        } else {
            _cond.wait(lk, [this] { return _finished; });
        }

        _finished = false;
        return ret;
    }

protected:
    void notifyComplete() {
        stdx::unique_lock<Latch> lk(_mutex);
        _finished = true;
        _cond.notify_one();
    }

    template <typename FunT>
    void startWorkerThread(FunT&& fun) {
        _workerThreads.emplace_back(std::forward<FunT>(fun));
    }

private:
    Mutex _mutex = MONGO_MAKE_LATCH("TimeoutSEP::_mutex");

    stdx::condition_variable _cond;
    bool _finished = false;

    std::vector<stdx::thread> _workerThreads;
};

class TimeoutSyncSEP : public TimeoutSEP {
public:
    enum Mode { kShouldTimeout, kNoTimeout };
    TimeoutSyncSEP(Mode mode) : _mode(mode) {}

    void startSession(transport::SessionHandle session) override {
        LOGV2(23040, "Accepted connection", "remote"_attr = session->remote());
        startWorkerThread([this, session = std::move(session)]() mutable {
            LOGV2(23041, "waiting for message");
            session->setTimeout(Milliseconds{500});
            auto status = session->sourceMessage().getStatus();
            if (_mode == kShouldTimeout) {
                ASSERT_EQ(status, ErrorCodes::NetworkTimeout);
                LOGV2(23042, "message timed out");
            } else {
                ASSERT_OK(status);
                LOGV2(23043, "message received okay");
            }

            session.reset();
            notifyComplete();
        });
    }

private:
    Mode _mode;
};

class TimeoutConnector {
public:
    TimeoutConnector(int port, bool sendRequest)
        : _ctx(), _sock(_ctx), _endpoint(asio::ip::address_v4::loopback(), port) {
        std::error_code ec;
        _sock.connect(_endpoint, ec);
        ASSERT_EQ(ec, std::error_code());

        if (sendRequest) {
            sendMessage();
        }
    }

    void sendMessage() {
        OpMsgBuilder builder;
        builder.setBody(BSON("ping" << 1));
        Message msg = builder.finish();
        msg.header().setResponseToMsgId(0);
        msg.header().setId(0);
        OpMsg::appendChecksum(&msg);

        std::error_code ec;
        asio::write(_sock, asio::buffer(msg.buf(), msg.size()), ec);
        ASSERT_FALSE(ec);
    }

private:
    asio::io_context _ctx;
    asio::ip::tcp::socket _sock;
    asio::ip::tcp::endpoint _endpoint;
};

/* check that timeouts actually time out */
TEST(TransportLayerASIO, SourceSyncTimeoutTimesOut) {
    TimeoutSyncSEP sep(TimeoutSyncSEP::kShouldTimeout);
    auto tla = makeAndStartTL(&sep);

    TimeoutConnector connector(tla->listenerPort(), false);

    sep.waitForTimeout();
    tla->shutdown();
}

/* check that timeouts don't time out unless there's an actual timeout */
TEST(TransportLayerASIO, SourceSyncTimeoutSucceeds) {
    TimeoutSyncSEP sep(TimeoutSyncSEP::kNoTimeout);
    auto tla = makeAndStartTL(&sep);

    TimeoutConnector connector(tla->listenerPort(), true);

    sep.waitForTimeout();
    tla->shutdown();
}

/* check that switching from timeouts to no timeouts correctly resets the timeout to unlimited */
class TimeoutSwitchModesSEP : public TimeoutSEP {
public:
    void startSession(transport::SessionHandle session) override {
        LOGV2(23044, "Accepted connection", "remote"_attr = session->remote());
        startWorkerThread([this, session = std::move(session)]() mutable {
            LOGV2(23045, "waiting for message");
            auto sourceMessage = [&] { return session->sourceMessage().getStatus(); };

            // the first message we source should time out.
            session->setTimeout(Milliseconds{500});
            ASSERT_EQ(sourceMessage(), ErrorCodes::NetworkTimeout);
            notifyComplete();

            LOGV2(23046, "timed out successfully");

            // get the session back in a known state with the timeout still in place
            ASSERT_OK(sourceMessage());
            notifyComplete();

            LOGV2(23047, "waiting for message without a timeout");

            // this should block and timeout the waitForComplete mutex, and the session should wait
            // for a while to make sure this isn't timing out and then send a message to unblock
            // the this call to recv
            session->setTimeout(boost::none);
            ASSERT_OK(sourceMessage());

            session.reset();
            notifyComplete();
            LOGV2(23048, "ending test");
        });
    }
};

TEST(TransportLayerASIO, SwitchTimeoutModes) {
    TimeoutSwitchModesSEP sep;
    auto tla = makeAndStartTL(&sep);

    TimeoutConnector connector(tla->listenerPort(), false);

    ASSERT_TRUE(sep.waitForTimeout());

    connector.sendMessage();
    ASSERT_TRUE(sep.waitForTimeout());

    ASSERT_FALSE(sep.waitForTimeout(Milliseconds{1000}));
    connector.sendMessage();
    ASSERT_TRUE(sep.waitForTimeout());

    tla->shutdown();
}

}  // namespace
}  // namespace mongo