1
2 /**
3 * Copyright (C) 2018-present MongoDB, Inc.
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the Server Side Public License, version 1,
7 * as published by MongoDB, Inc.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * Server Side Public License for more details.
13 *
14 * You should have received a copy of the Server Side Public License
15 * along with this program. If not, see
16 * <http://www.mongodb.com/licensing/server-side-public-license>.
17 *
18 * As a special exception, the copyright holders give permission to link the
19 * code of portions of this program with the OpenSSL library under certain
20 * conditions as described in each individual source file and distribute
21 * linked combinations including the program with the OpenSSL library. You
22 * must comply with the Server Side Public License in all respects for
23 * all of the code used other than as permitted herein. If you modify file(s)
24 * with this exception, you may extend this exception to your version of the
25 * file(s), but you are not obligated to do so. If you do not wish to do so,
26 * delete this exception statement from your version. If you delete this
27 * exception statement from all source files in the program, then also delete
28 * it in the license file.
29 */
30
31 #include "mongo/platform/basic.h"
32
33 #include "mongo/db/sessions_collection_sharded.h"
34
35 #include "mongo/db/matcher/extensions_callback_noop.h"
36 #include "mongo/db/operation_context.h"
37 #include "mongo/db/query/canonical_query.h"
38 #include "mongo/db/query/query_request.h"
39 #include "mongo/db/sessions_collection_rs.h"
40 #include "mongo/rpc/get_status_from_command_result.h"
41 #include "mongo/s/catalog_cache.h"
42 #include "mongo/s/commands/cluster_write.h"
43 #include "mongo/s/grid.h"
44 #include "mongo/s/query/cluster_find.h"
45 #include "mongo/s/write_ops/batch_write_exec.h"
46 #include "mongo/s/write_ops/batched_command_request.h"
47 #include "mongo/s/write_ops/batched_command_response.h"
48 #include "mongo/util/net/op_msg.h"
49
50 namespace mongo {
51
52 namespace {
53
lsidQuery(const LogicalSessionId & lsid)54 BSONObj lsidQuery(const LogicalSessionId& lsid) {
55 return BSON(LogicalSessionRecord::kIdFieldName << lsid.toBSON());
56 }
57
58 } // namespace
59
_checkCacheForSessionsCollection(OperationContext * opCtx)60 Status SessionsCollectionSharded::_checkCacheForSessionsCollection(OperationContext* opCtx) {
61 // If the sharding state is not yet initialized, fail.
62 if (!Grid::get(opCtx)->isShardingInitialized()) {
63 return {ErrorCodes::ShardingStateNotInitialized, "sharding state is not yet initialized"};
64 }
65
66 // If the collection doesn't exist, fail. Only the config servers generate it.
67 auto res = Grid::get(opCtx)->catalogCache()->getShardedCollectionRoutingInfoWithRefresh(
68 opCtx, SessionsCollection::kSessionsNamespaceString);
69 if (!res.isOK()) {
70 return res.getStatus();
71 }
72
73 auto routingInfo = res.getValue();
74 if (routingInfo.cm()) {
75 return Status::OK();
76 }
77
78 return {ErrorCodes::NamespaceNotFound, "config.system.sessions does not exist"};
79 }
80
setupSessionsCollection(OperationContext * opCtx)81 Status SessionsCollectionSharded::setupSessionsCollection(OperationContext* opCtx) {
82 return checkSessionsCollectionExists(opCtx);
83 }
84
checkSessionsCollectionExists(OperationContext * opCtx)85 Status SessionsCollectionSharded::checkSessionsCollectionExists(OperationContext* opCtx) {
86 return _checkCacheForSessionsCollection(opCtx);
87 }
88
refreshSessions(OperationContext * opCtx,const LogicalSessionRecordSet & sessions)89 Status SessionsCollectionSharded::refreshSessions(OperationContext* opCtx,
90 const LogicalSessionRecordSet& sessions) {
91 auto send = [&](BSONObj toSend) {
92 auto opMsg =
93 OpMsgRequest::fromDBAndBody(SessionsCollection::kSessionsNamespaceString.db(), toSend);
94 auto request = BatchedCommandRequest::parseUpdate(opMsg);
95
96 BatchedCommandResponse response;
97 BatchWriteExecStats stats;
98
99 ClusterWriter::write(opCtx, request, &stats, &response);
100 if (response.getOk()) {
101 return Status::OK();
102 }
103
104 auto error = response.isErrCodeSet() ? ErrorCodes::Error(response.getErrCode())
105 : ErrorCodes::UnknownError;
106 return Status(error, response.getErrMessage());
107 };
108
109 return doRefresh(kSessionsNamespaceString, sessions, send);
110 }
111
removeRecords(OperationContext * opCtx,const LogicalSessionIdSet & sessions)112 Status SessionsCollectionSharded::removeRecords(OperationContext* opCtx,
113 const LogicalSessionIdSet& sessions) {
114 auto send = [&](BSONObj toSend) {
115 auto opMsg =
116 OpMsgRequest::fromDBAndBody(SessionsCollection::kSessionsNamespaceString.db(), toSend);
117 auto request = BatchedCommandRequest::parseDelete(opMsg);
118
119 BatchedCommandResponse response;
120 BatchWriteExecStats stats;
121
122 ClusterWriter::write(opCtx, request, &stats, &response);
123
124 if (response.getOk()) {
125 return Status::OK();
126 }
127
128 auto error = response.isErrCodeSet() ? ErrorCodes::Error(response.getErrCode())
129 : ErrorCodes::UnknownError;
130 return Status(error, response.getErrMessage());
131 };
132
133 return doRemove(kSessionsNamespaceString, sessions, send);
134 }
135
findRemovedSessions(OperationContext * opCtx,const LogicalSessionIdSet & sessions)136 StatusWith<LogicalSessionIdSet> SessionsCollectionSharded::findRemovedSessions(
137 OperationContext* opCtx, const LogicalSessionIdSet& sessions) {
138
139 auto send = [&](BSONObj toSend) -> StatusWith<BSONObj> {
140 auto qr = QueryRequest::makeFromFindCommand(
141 SessionsCollection::kSessionsNamespaceString, toSend, false);
142 if (!qr.isOK()) {
143 return qr.getStatus();
144 }
145
146 const boost::intrusive_ptr<ExpressionContext> expCtx;
147 auto cq = CanonicalQuery::canonicalize(opCtx,
148 std::move(qr.getValue()),
149 expCtx,
150 ExtensionsCallbackNoop(),
151 MatchExpressionParser::kBanAllSpecialFeatures);
152 if (!cq.isOK()) {
153 return cq.getStatus();
154 }
155
156 // Do the work to generate the first batch of results. This blocks waiting to get responses
157 // from the shard(s).
158 std::vector<BSONObj> batch;
159 BSONObj viewDefinition;
160 auto cursorId = ClusterFind::runQuery(
161 opCtx, *cq.getValue(), ReadPreferenceSetting::get(opCtx), &batch, &viewDefinition);
162
163 if (!cursorId.isOK()) {
164 return cursorId.getStatus();
165 }
166
167 BSONObjBuilder result;
168 CursorResponseBuilder firstBatch(/*firstBatch*/ true, &result);
169 for (const auto& obj : batch) {
170 firstBatch.append(obj);
171 }
172 firstBatch.done(cursorId.getValue(), SessionsCollection::kSessionsNamespaceString.ns());
173
174 return result.obj();
175 };
176
177 return doFetch(kSessionsNamespaceString, sessions, send);
178 }
179
removeTransactionRecords(OperationContext * opCtx,const LogicalSessionIdSet & sessions)180 Status SessionsCollectionSharded::removeTransactionRecords(OperationContext* opCtx,
181 const LogicalSessionIdSet& sessions) {
182 return SessionsCollectionRS::removeTransactionRecordsHelper(opCtx, sessions);
183 }
184
185 } // namespace mongo
186