summaryrefslogtreecommitdiff
path: root/src/mongo/s/service_entry_point_mongos.cpp
blob: 36a2d0aa13c25dee3a38b80ec386f68b1646fef7 (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
/**
 *    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.
 */


#include <memory>

#include "mongo/platform/basic.h"

#include "mongo/s/service_entry_point_mongos.h"

#include "mongo/client/server_discovery_monitor.h"
#include "mongo/db/auth/authorization_session.h"
#include "mongo/db/commands.h"
#include "mongo/db/curop.h"
#include "mongo/db/dbmessage.h"
#include "mongo/db/not_primary_error_tracker.h"
#include "mongo/db/operation_context.h"
#include "mongo/db/request_execution_context.h"
#include "mongo/db/service_context.h"
#include "mongo/db/session_catalog.h"
#include "mongo/db/stats/counters.h"
#include "mongo/logv2/log.h"
#include "mongo/rpc/message.h"
#include "mongo/rpc/warn_unsupported_wire_ops.h"
#include "mongo/s/commands/strategy.h"
#include "mongo/s/grid.h"
#include "mongo/s/load_balancer_support.h"
#include "mongo/s/query/cluster_cursor_manager.h"
#include "mongo/s/transaction_router.h"

#define MONGO_LOGV2_DEFAULT_COMPONENT ::mongo::logv2::LogComponent::kNetwork


namespace mongo {

namespace {

BSONObj buildErrReply(const DBException& ex) {
    BSONObjBuilder errB;
    errB.append("$err", ex.what());
    errB.append("code", ex.code());
    return errB.obj();
}

}  // namespace

// Allows for decomposing `handleRequest` into parts and simplifies composing the future-chain.
struct HandleRequest : public std::enable_shared_from_this<HandleRequest> {
    struct OpRunnerBase;

    HandleRequest(OperationContext* opCtx, const Message& message)
        : rec(std::make_shared<RequestExecutionContext>(opCtx, message)),
          op(message.operation()),
          msgId(message.header().getId()),
          nsString(getNamespaceString(rec->getDbMessage())) {}

    // Prepares the environment for handling the request.
    void setupEnvironment();

    // Returns a future that does the heavy lifting of running client commands.
    Future<DbResponse> handleRequest();

    // Runs on successful execution of the future returned by `handleRequest`.
    void onSuccess(const DbResponse&);

    // Returns a future-chain to handle the request and prepare the response.
    Future<DbResponse> run();

    static NamespaceString getNamespaceString(const DbMessage& dbmsg) {
        if (!dbmsg.messageShouldHaveNs())
            return {};
        return NamespaceString(dbmsg.getns());
    }

    const std::shared_ptr<RequestExecutionContext> rec;
    const NetworkOp op;
    const int32_t msgId;
    const NamespaceString nsString;

    boost::optional<long long> slowMsOverride;
};

void HandleRequest::setupEnvironment() {
    using namespace fmt::literals;
    auto opCtx = rec->getOpCtx();

    // This exception will not be returned to the caller, but will be logged and will close the
    // connection
    uassert(ErrorCodes::IllegalOperation,
            "Message type {} is not supported."_format(op),
            isSupportedRequestNetworkOp(op) &&
                op != dbCompressed);  // Decompression should be handled above us.

    // Start a new NotPrimaryErrorTracker session. Any exceptions thrown from here onwards will be
    // returned to the caller (if the type of the message permits it).
    auto client = opCtx->getClient();
    NotPrimaryErrorTracker::get(client).startRequest();
    AuthorizationSession::get(client)->startRequest(opCtx);

    CurOp::get(opCtx)->ensureStarted();
}

// The base for various operation runners that handle the request, and often generate a DbResponse.
struct HandleRequest::OpRunnerBase {
    explicit OpRunnerBase(std::shared_ptr<HandleRequest> hr) : hr(std::move(hr)) {}
    virtual ~OpRunnerBase() = default;
    virtual Future<DbResponse> run() = 0;
    const std::shared_ptr<HandleRequest> hr;
};

struct CommandOpRunner final : public HandleRequest::OpRunnerBase {
    using HandleRequest::OpRunnerBase::OpRunnerBase;
    Future<DbResponse> run() override {
        return Strategy::clientCommand(hr->rec);
    }
};

Future<DbResponse> HandleRequest::handleRequest() {
    switch (op) {
        case dbQuery:
            if (!nsString.isCommand()) {
                globalOpCounters.gotQueryDeprecated();
                warnUnsupportedOp(*(rec->getOpCtx()->getClient()), networkOpToString(dbQuery));
                return Future<DbResponse>::makeReady(
                    makeErrorResponseToUnsupportedOpQuery("OP_QUERY is no longer supported"));
            }
            [[fallthrough]];  // It's a query containing a command
        case dbMsg:
            return std::make_unique<CommandOpRunner>(shared_from_this())->run();
        case dbGetMore: {
            globalOpCounters.gotGetMoreDeprecated();
            warnUnsupportedOp(*(rec->getOpCtx()->getClient()), networkOpToString(dbGetMore));
            return Future<DbResponse>::makeReady(
                makeErrorResponseToUnsupportedOpQuery("OP_GET_MORE is no longer supported"));
        }
        case dbKillCursors:
            globalOpCounters.gotKillCursorsDeprecated();
            warnUnsupportedOp(*(rec->getOpCtx()->getClient()), networkOpToString(op));
            uasserted(5745707, "OP_KILL_CURSORS is no longer supported");
        case dbInsert: {
            auto opInsert = InsertOp::parseLegacy(rec->getMessage());
            globalOpCounters.gotInsertsDeprecated(opInsert.getDocuments().size());
            warnUnsupportedOp(*(rec->getOpCtx()->getClient()), networkOpToString(op));
            uasserted(5745706, "OP_INSERT is no longer supported");
        }
        case dbUpdate:
            globalOpCounters.gotUpdateDeprecated();
            warnUnsupportedOp(*(rec->getOpCtx()->getClient()), networkOpToString(op));
            uasserted(5745705, "OP_UPDATE is no longer supported");
        case dbDelete:
            globalOpCounters.gotDeleteDeprecated();
            warnUnsupportedOp(*(rec->getOpCtx()->getClient()), networkOpToString(op));
            uasserted(5745704, "OP_DELETE is no longer supported");
        default:
            MONGO_UNREACHABLE;
    }
}

void HandleRequest::onSuccess(const DbResponse& dbResponse) {
    auto opCtx = rec->getOpCtx();
    // Mark the op as complete, populate the response length, and log it if appropriate.
    CurOp::get(opCtx)->completeAndLogOperation(
        opCtx, logv2::LogComponent::kCommand, dbResponse.response.size(), slowMsOverride);
}

Future<DbResponse> HandleRequest::run() {
    auto fp = makePromiseFuture<void>();
    auto future = std::move(fp.future)
                      .then([this, anchor = shared_from_this()] { setupEnvironment(); })
                      .then([this, anchor = shared_from_this()] { return handleRequest(); })
                      .tap([this, anchor = shared_from_this()](const DbResponse& dbResponse) {
                          onSuccess(dbResponse);
                      })
                      .tapError([](Status status) {
                          LOGV2(4879803, "Failed to handle request", "error"_attr = redact(status));
                      });
    fp.promise.emplaceValue();
    return future;
}

Future<DbResponse> ServiceEntryPointMongos::handleRequestImpl(OperationContext* opCtx,
                                                              const Message& message) noexcept {
    auto hr = std::make_shared<HandleRequest>(opCtx, message);
    return hr->run();
}

Future<DbResponse> ServiceEntryPointMongos::handleRequest(OperationContext* opCtx,
                                                          const Message& message) noexcept {
    return handleRequestImpl(opCtx, message);
}

void ServiceEntryPointMongos::onClientConnect(Client* client) {
    if (load_balancer_support::isFromLoadBalancer(client)) {
        _loadBalancedConnections.increment();
    }
}

void ServiceEntryPointMongos::derivedOnClientDisconnect(Client* client) {
    if (load_balancer_support::isFromLoadBalancer(client)) {
        _loadBalancedConnections.decrement();

        auto killerOperationContext = client->makeOperationContext();

        // Kill any cursors opened by the given Client.
        auto ccm = Grid::get(client->getServiceContext())->getCursorManager();
        ccm->killCursorsSatisfying(killerOperationContext.get(),
                                   [&](CursorId, const ClusterCursorManager::CursorEntry& entry) {
                                       return entry.originatingClientUuid() == client->getUUID();
                                   });

        // Kill any in-progress transactions over this Client connection.
        auto lsid = load_balancer_support::getMruSession(client);

        auto killToken = [&]() -> boost::optional<SessionCatalog::KillToken> {
            try {
                return SessionCatalog::get(killerOperationContext.get())->killSession(lsid);
            } catch (const ExceptionFor<ErrorCodes::NoSuchSession>&) {
                return boost::none;
            }
        }();
        if (!killToken) {
            // There was no entry in the SessionCatalog for the session most recently used by the
            // disconnecting client, so we have no transaction state to clean up.
            return;
        }
        OperationContextSession sessionCtx(killerOperationContext.get(), std::move(*killToken));
        invariant(lsid ==
                  OperationContextSession::get(killerOperationContext.get())->getSessionId());

        auto txnRouter = TransactionRouter::get(killerOperationContext.get());
        if (txnRouter && txnRouter.isInitialized() && !txnRouter.isTrackingOver()) {
            txnRouter.implicitlyAbortTransaction(
                killerOperationContext.get(),
                {ErrorCodes::Interrupted,
                 "aborting in-progress transaction because load-balanced client disconnected"});
        }
    }
}

void ServiceEntryPointMongos::appendStats(BSONObjBuilder* bob) const {
    ServiceEntryPointImpl::appendStats(bob);
    if (load_balancer_support::isEnabled()) {
        bob->append("loadBalanced", _loadBalancedConnections);
    }
}
}  // namespace mongo