summaryrefslogtreecommitdiff
path: root/src/mongo/s/query/cluster_client_cursor_params.h
blob: d8bb0ae8da006caeffb3b24585edd05235cc364c (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
/**
 *    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.
 */

#pragma once

#include <boost/optional.hpp>
#include <functional>
#include <memory>
#include <vector>

#include "mongo/bson/bsonobj.h"
#include "mongo/client/read_preference.h"
#include "mongo/db/auth/privilege.h"
#include "mongo/db/auth/user_name.h"
#include "mongo/db/cursor_id.h"
#include "mongo/db/initialize_api_parameters.h"
#include "mongo/db/namespace_string.h"
#include "mongo/db/pipeline/pipeline.h"
#include "mongo/db/query/cursor_response.h"
#include "mongo/db/query/tailable_mode.h"
#include "mongo/db/repl/read_concern_args.h"
#include "mongo/s/client/shard.h"
#include "mongo/s/query/async_results_merger_params_gen.h"
#include "mongo/util/net/hostandport.h"

namespace mongo {
namespace executor {
class TaskExecutor;
}

class OperationContext;
class RouterExecStage;

using repl::ReadConcernArgs;

/**
 * The resulting ClusterClientCursor will take ownership of the existing remote cursor, generating
 * results based on the cursor's current state.
 *
 * Note that any results already generated from this cursor will not be returned by the resulting
 * ClusterClientCursor. The caller is responsible for ensuring that results previously generated by
 * this cursor have been processed.
 */
struct ClusterClientCursorParams {
    ClusterClientCursorParams(NamespaceString nss,
                              APIParameters apiParameters,
                              boost::optional<ReadPreferenceSetting> readPref = boost::none,
                              boost::optional<ReadConcernArgs> readConcernArgs = boost::none)
        : nsString(std::move(nss)), apiParameters(std::move(apiParameters)) {
        if (readPref) {
            readPreference = std::move(readPref.get());
        }
        if (readConcernArgs) {
            readConcern = std::move(readConcernArgs.get());
        }
    }

    /**
     * Extracts the subset of fields here needed by the AsyncResultsMerger. The returned
     * AsyncResultsMergerParams will assume ownership of 'remotes'.
     */
    AsyncResultsMergerParams extractARMParams() {
        AsyncResultsMergerParams armParams;
        if (!sortToApplyOnRouter.isEmpty()) {
            armParams.setSort(sortToApplyOnRouter);
        }
        armParams.setCompareWholeSortKey(compareWholeSortKeyOnRouter);
        armParams.setRemotes(std::move(remotes));
        armParams.setTailableMode(tailableMode);
        armParams.setBatchSize(batchSize);
        armParams.setNss(nsString);
        armParams.setAllowPartialResults(isAllowPartialResults);

        OperationSessionInfoFromClient sessionInfo;
        boost::optional<LogicalSessionFromClient> lsidFromClient;

        if (lsid) {
            lsidFromClient.emplace(lsid->getId());
            lsidFromClient->setUid(lsid->getUid());
        }

        sessionInfo.setSessionId(lsidFromClient);
        sessionInfo.setTxnNumber(txnNumber);
        sessionInfo.setAutocommit(isAutoCommit);
        armParams.setOperationSessionInfo(sessionInfo);

        return armParams;
    }

    // Namespace against which the cursors exist.
    NamespaceString nsString;

    // The original command object which generated this cursor. Must either be empty or owned.
    BSONObj originatingCommandObj;

    // The privileges required for the originatingCommand.
    PrivilegeVector originatingPrivileges;

    // Per-remote node data.
    std::vector<RemoteCursor> remotes;

    // The sort specification to be applied on router. Leave empty if there is no sort.
    BSONObj sortToApplyOnRouter;

    // When 'compareWholeSortKeyOnRouter' is true, $sortKey is a scalar value, rather than an
    // object. We extract the sort key {$sortKey: <value>}. The sort key pattern is verified to be
    // {$sortKey: 1}.
    bool compareWholeSortKeyOnRouter = false;

    // The number of results to skip on the router. Optional. Should not be forwarded to the remote
    // hosts in 'cmdObj'.
    boost::optional<long long> skipToApplyOnRouter;

    // The number of results per batch. Optional. If specified, will be specified as the batch for
    // each getMore.
    boost::optional<std::int64_t> batchSize;

    // Limits the number of results returned by the ClusterClientCursor to this many. Optional.
    // Should be forwarded to the remote hosts in 'cmdObj'.
    boost::optional<long long> limit;

    // Whether this cursor is tailing a capped collection, and whether it has the awaitData option
    // set.
    TailableModeEnum tailableMode = TailableModeEnum::kNormal;

    // The API parameters associated with the cursor.
    APIParameters apiParameters;

    // Set if a readPreference must be respected throughout the lifetime of the cursor.
    boost::optional<ReadPreferenceSetting> readPreference;

    // Set if a readConcern must be respected throughout the lifetime of the cursor.
    boost::optional<ReadConcernArgs> readConcern;

    // Whether the client indicated that it is willing to receive partial results in the case of an
    // unreachable host.
    bool isAllowPartialResults = false;

    // The logical session id of the command that created the cursor.
    boost::optional<LogicalSessionId> lsid;

    // The transaction number of the command that created the cursor.
    boost::optional<TxnNumber> txnNumber;

    // Set to false for multi statement transactions.
    boost::optional<bool> isAutoCommit;
};

}  // namespace mongo