summaryrefslogtreecommitdiff
path: root/src/mongo/executor/remote_command_runner.h
blob: 98ed870b90f66c59f2175c9e898ba67caf41f122 (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
/**
 *    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.
 */

#pragma once

#include "mongo/bson/bsonobj.h"
#include "mongo/db/operation_context.h"
#include "mongo/executor/remote_command_response.h"
#include "mongo/executor/remote_command_runner_error_info.h"
#include "mongo/executor/remote_command_targeter.h"
#include "mongo/executor/task_executor.h"
#include "mongo/rpc/get_status_from_command_result.h"
#include "mongo/util/assert_util.h"
#include "mongo/util/cancellation.h"
#include "mongo/util/future.h"
#include "mongo/util/net/hostandport.h"
#include <memory>

namespace mongo::executor::remote_command_runner {
namespace detail {
struct RemoteCommandInternalResponse {
    BSONObj response;
    HostAndPort targetUsed;
};

/**
 * The RemoteCommandRunner class stores the implementation details used by the free function
 * remote_command_runner::doRequest defined below. It is a decoration on the service context,
 * and appropriate static getters/setters are provided to access/set the decoration. It has
 * a single virtual function, which takes a command specified as BSON and runs it on the
 * provided HostAndPort/database name asynchronously, using the given executor. It keeps
 * the executor alive for the duration of the command's execution; to cancel the command's
 * execution, use the provided cancellation token.
 *
 * This is *not* part of the public API, and is deliberately in the detail namespace.
 * Use the remote_command_runner::doRequest free-function/public API below instead,
 * which contains additional functionality and type checking.
 */
class RemoteCommandRunner {
public:
    virtual ~RemoteCommandRunner() = default;
    virtual ExecutorFuture<RemoteCommandInternalResponse> _doRequest(
        StringData dbName,
        BSONObj cmdBSON,
        std::unique_ptr<RemoteCommandHostTargeter> targeter,
        OperationContext* opCtx,
        std::shared_ptr<TaskExecutor> exec,
        CancellationToken token) = 0;
    static RemoteCommandRunner* get(ServiceContext* serviceContext);
    static void set(ServiceContext* serviceContext, std::unique_ptr<RemoteCommandRunner> theRunner);
};

/**
 * Returns a RemoteCommandExecutionError with ErrorExtraInfo populated to contain
 * details about any error, local or remote, contained in `r`.
 */
inline Status makeErrorIfNeeded(TaskExecutor::ResponseOnAnyStatus r) {
    if (r.status.isOK() && getStatusFromCommandResult(r.data).isOK() &&
        getWriteConcernStatusFromCommandResult(r.data).isOK() &&
        getFirstWriteErrorStatusFromCommandResult(r.data).isOK()) {
        return Status::OK();
    }
    return {RemoteCommandExecutionErrorInfo(r), "Remote command execution failed"};
}
}  // namespace detail

template <typename CommandReplyType>
struct RemoteCommandRunnerResponse {
    CommandReplyType response;
    HostAndPort targetUsed;
};

/**
 * Execute the command asynchronously on the given target with the provided executor.
 * Returns a SemiFuture with the reply from the IDL command, or throws an error.
 */
template <typename CommandType>
SemiFuture<RemoteCommandRunnerResponse<typename CommandType::Reply>> doRequest(
    CommandType cmd,
    OperationContext* opCtx,
    std::unique_ptr<RemoteCommandHostTargeter> targeter,
    std::shared_ptr<executor::TaskExecutor> exec,
    CancellationToken token) {
    /* Execute the command after extracting the db name and bson from the CommandType. Wrapping this
     * function allows us to seperate the CommandType parsing logic from the implementation details
     * of executing the remote command asynchronously.
     */
    auto resFuture =
        detail::RemoteCommandRunner::get(opCtx->getServiceContext())
            ->_doRequest(
                cmd.getDbName().db(), cmd.toBSON({}), std::move(targeter), opCtx, exec, token);

    return std::move(resFuture)
        .then([](detail::RemoteCommandInternalResponse r) {
            // TODO SERVER-67661: Make IDL reply types have string representation for logging
            auto res =
                CommandType::Reply::parse(IDLParserContext("RemoteCommandRunner"), r.response);

            struct RemoteCommandRunnerResponse<typename CommandType::Reply> fullRes = {
                res, r.targetUsed
            };

            return fullRes;
        })
        .semi();
}
}  // namespace mongo::executor::remote_command_runner