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
|
/**
* Copyright (C) 2017 MongoDB Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* 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
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* 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 GNU Affero General 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 "mongo/platform/basic.h"
#include "mongo/db/sessions_collection.h"
#include <memory>
#include <vector>
#include "mongo/bson/bsonobjbuilder.h"
#include "mongo/client/dbclientinterface.h"
#include "mongo/db/create_indexes_gen.h"
#include "mongo/db/logical_session_id.h"
#include "mongo/db/ops/write_ops.h"
#include "mongo/db/refresh_sessions_gen.h"
#include "mongo/rpc/get_status_from_command_result.h"
#include "mongo/s/write_ops/batched_command_response.h"
#include "mongo/stdx/functional.h"
#include "mongo/stdx/memory.h"
namespace mongo {
namespace {
BSONObj lsidQuery(const LogicalSessionId& lsid) {
return BSON(LogicalSessionRecord::kIdFieldName << lsid.toBSON());
}
BSONObj lsidQuery(const LogicalSessionRecord& record) {
return lsidQuery(record.getId());
}
BSONObj updateQuery(const LogicalSessionRecord& record) {
// { $max : { lastUse : <time> }, $setOnInsert : { user : <user> } }
// Build our update doc.
BSONObjBuilder updateBuilder;
{
BSONObjBuilder maxBuilder(updateBuilder.subobjStart("$currentDate"));
maxBuilder.append(LogicalSessionRecord::kLastUseFieldName, true);
}
if (record.getUser()) {
BSONObjBuilder setBuilder(updateBuilder.subobjStart("$setOnInsert"));
setBuilder.append(LogicalSessionRecord::kUserFieldName, BSON("name" << *record.getUser()));
}
return updateBuilder.obj();
}
template <typename TFactory, typename AddLineFn, typename SendFn, typename Container>
Status runBulkGeneric(TFactory makeT, AddLineFn addLine, SendFn sendBatch, const Container& items) {
using T = decltype(makeT());
size_t i = 0;
boost::optional<T> thing;
auto setupBatch = [&] {
i = 0;
thing.emplace(makeT());
};
auto sendLocalBatch = [&] { return sendBatch(thing.value()); };
setupBatch();
for (const auto& item : items) {
addLine(*thing, item);
if (++i >= write_ops::kMaxWriteBatchSize) {
auto res = sendLocalBatch();
if (!res.isOK()) {
return res;
}
setupBatch();
}
}
if (i > 0) {
return sendLocalBatch();
} else {
return Status::OK();
}
}
template <typename InitBatchFn, typename AddLineFn, typename SendBatchFn, typename Container>
Status runBulkCmd(StringData label,
InitBatchFn&& initBatch,
AddLineFn&& addLine,
SendBatchFn&& sendBatch,
const Container& items) {
BufBuilder buf;
boost::optional<BSONObjBuilder> batchBuilder;
boost::optional<BSONArrayBuilder> entries;
auto makeBatch = [&] {
buf.reset();
batchBuilder.emplace(buf);
initBatch(&(batchBuilder.get()));
entries.emplace(batchBuilder->subarrayStart(label));
return &(entries.get());
};
auto sendLocalBatch = [&](BSONArrayBuilder*) {
entries->done();
return sendBatch(batchBuilder->done());
};
return runBulkGeneric(makeBatch, addLine, sendLocalBatch, items);
}
} // namespace
constexpr StringData SessionsCollection::kSessionsDb;
constexpr StringData SessionsCollection::kSessionsCollection;
constexpr StringData SessionsCollection::kSessionsFullNS;
const NamespaceString SessionsCollection::kSessionsNamespaceString =
NamespaceString{SessionsCollection::kSessionsFullNS};
SessionsCollection::~SessionsCollection() = default;
SessionsCollection::SendBatchFn SessionsCollection::makeSendFnForBatchWrite(
const NamespaceString& ns, DBClientBase* client) {
auto send = [client, ns](BSONObj batch) -> Status {
BSONObj res;
if (!client->runCommand(ns.db().toString(), batch, res)) {
return getStatusFromCommandResult(res);
}
BatchedCommandResponse response;
std::string errmsg;
if (!response.parseBSON(res, &errmsg)) {
return {ErrorCodes::FailedToParse, errmsg};
}
return response.toStatus();
};
return send;
}
SessionsCollection::SendBatchFn SessionsCollection::makeSendFnForCommand(const NamespaceString& ns,
DBClientBase* client) {
auto send = [client, ns](BSONObj cmd) -> Status {
BSONObj res;
if (!client->runCommand(ns.db().toString(), cmd, res)) {
return getStatusFromCommandResult(res);
}
return Status::OK();
};
return send;
}
SessionsCollection::FindBatchFn SessionsCollection::makeFindFnForCommand(const NamespaceString& ns,
DBClientBase* client) {
auto send = [client, ns](BSONObj cmd) -> StatusWith<BSONObj> {
BSONObj res;
if (!client->runCommand(ns.db().toString(), cmd, res)) {
return getStatusFromCommandResult(res);
}
return res;
};
return send;
}
Status SessionsCollection::doRefresh(const NamespaceString& ns,
const LogicalSessionRecordSet& sessions,
SendBatchFn send) {
auto init = [ns](BSONObjBuilder* batch) {
batch->append("update", ns.coll());
batch->append("ordered", false);
batch->append("allowImplicitCollectionCreation", false);
};
auto add = [](BSONArrayBuilder* entries, const LogicalSessionRecord& record) {
entries->append(
BSON("q" << lsidQuery(record) << "u" << updateQuery(record) << "upsert" << true));
};
return runBulkCmd("updates", init, add, send, sessions);
}
Status SessionsCollection::doRefreshExternal(const NamespaceString& ns,
const LogicalSessionRecordSet& sessions,
SendBatchFn send) {
auto makeT = [] { return std::vector<LogicalSessionRecord>{}; };
auto add = [](std::vector<LogicalSessionRecord>& batch, const LogicalSessionRecord& record) {
batch.push_back(record);
};
auto sendLocal = [&](std::vector<LogicalSessionRecord>& batch) {
RefreshSessionsCmdFromClusterMember idl;
idl.setRefreshSessionsInternal(batch);
return send(idl.toBSON());
};
return runBulkGeneric(makeT, add, sendLocal, sessions);
}
Status SessionsCollection::doRemove(const NamespaceString& ns,
const LogicalSessionIdSet& sessions,
SendBatchFn send) {
auto init = [ns](BSONObjBuilder* batch) {
batch->append("delete", ns.coll());
batch->append("ordered", false);
};
auto add = [](BSONArrayBuilder* builder, const LogicalSessionId& lsid) {
builder->append(BSON("q" << lsidQuery(lsid) << "limit" << 0));
};
return runBulkCmd("deletes", init, add, send, sessions);
}
Status SessionsCollection::doRemoveExternal(const NamespaceString& ns,
const LogicalSessionIdSet& sessions,
SendBatchFn send) {
// TODO SERVER-28335 Implement endSessions, with internal counterpart.
return Status::OK();
}
StatusWith<LogicalSessionIdSet> SessionsCollection::doFetch(const NamespaceString& ns,
const LogicalSessionIdSet& sessions,
FindBatchFn send) {
auto makeT = [] { return std::vector<LogicalSessionId>{}; };
auto add = [](std::vector<LogicalSessionId>& batch, const LogicalSessionId& record) {
batch.push_back(record);
};
LogicalSessionIdSet removed = sessions;
auto wrappedSend = [&](BSONObj batch) {
auto swBatchResult = send(batch);
if (!swBatchResult.isOK()) {
return swBatchResult.getStatus();
} else {
auto result = SessionsCollectionFetchResult::parse("SessionsCollectionFetchResult"_sd,
swBatchResult.getValue());
for (const auto& lsid : result.getCursor().getFirstBatch()) {
removed.erase(lsid.get_id());
}
return Status::OK();
}
};
auto sendLocal = [&](std::vector<LogicalSessionId>& batch) {
SessionsCollectionFetchRequest request;
request.setFind(ns.coll());
request.setFilter({});
request.getFilter().set_id({});
request.getFilter().get_id().setIn(batch);
request.setProjection({});
request.getProjection().set_id(1);
request.setBatchSize(batch.size());
request.setLimit(batch.size());
request.setAllowPartialResults(true);
request.setSingleBatch(true);
return wrappedSend(request.toBSON());
};
auto status = runBulkGeneric(makeT, add, sendLocal, sessions);
if (!status.isOK()) {
return status;
}
return removed;
}
BSONObj SessionsCollection::generateCreateIndexesCmd() {
NewIndexSpec index;
index.setKey(BSON("lastUse" << 1));
index.setName("lsidTTLIndex");
index.setExpireAfterSeconds(localLogicalSessionTimeoutMinutes * 60);
std::vector<NewIndexSpec> indexes;
indexes.push_back(std::move(index));
CreateIndexesCmd createIndexes;
createIndexes.setCreateIndexes(kSessionsCollection.toString());
createIndexes.setIndexes(std::move(indexes));
return createIndexes.toBSON();
}
} // namespace mongo
|