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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
|
/**
* 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.
*/
#define MONGO_LOG_DEFAULT_COMPONENT ::mongo::logger::LogComponent::kExecutor
#include "mongo/platform/basic.h"
#include "mongo/client/fetcher.h"
#include <ostream>
#include <utility>
#include "mongo/db/jsobj.h"
#include "mongo/db/namespace_string.h"
#include "mongo/rpc/get_status_from_command_result.h"
#include "mongo/util/assert_util.h"
#include "mongo/util/destructor_guard.h"
#include "mongo/util/log.h"
#include "mongo/util/mongoutils/str.h"
#include "mongo/util/scopeguard.h"
namespace mongo {
namespace {
using executor::RemoteCommandRequest;
using executor::RemoteCommandResponse;
using RemoteCommandCallbackArgs = executor::TaskExecutor::RemoteCommandCallbackArgs;
const char* kCursorFieldName = "cursor";
const char* kCursorIdFieldName = "id";
const char* kNamespaceFieldName = "ns";
const char* kFirstBatchFieldName = "firstBatch";
const char* kNextBatchFieldName = "nextBatch";
/**
* Parses cursor response in command result for cursor ID, namespace and documents.
* 'batchFieldName' will be 'firstBatch' for the initial remote command invocation and
* 'nextBatch' for getMore.
*/
Status parseCursorResponse(const BSONObj& obj,
const std::string& batchFieldName,
Fetcher::QueryResponse* batchData) {
invariant(obj.isOwned());
invariant(batchFieldName == kFirstBatchFieldName || batchFieldName == kNextBatchFieldName);
invariant(batchData);
BSONElement cursorElement = obj.getField(kCursorFieldName);
if (cursorElement.eoo()) {
return Status(ErrorCodes::FailedToParse,
str::stream() << "cursor response must contain '" << kCursorFieldName
<< "' field: "
<< obj);
}
if (!cursorElement.isABSONObj()) {
return Status(
ErrorCodes::FailedToParse,
str::stream() << "'" << kCursorFieldName << "' field must be an object: " << obj);
}
BSONObj cursorObj = cursorElement.Obj();
BSONElement cursorIdElement = cursorObj.getField(kCursorIdFieldName);
if (cursorIdElement.eoo()) {
return Status(ErrorCodes::FailedToParse,
str::stream() << "cursor response must contain '" << kCursorFieldName << "."
<< kCursorIdFieldName
<< "' field: "
<< obj);
}
if (cursorIdElement.type() != mongo::NumberLong) {
return Status(ErrorCodes::FailedToParse,
str::stream() << "'" << kCursorFieldName << "." << kCursorIdFieldName
<< "' field must be a 'long' but was a '"
<< typeName(cursorIdElement.type())
<< "': "
<< obj);
}
batchData->cursorId = cursorIdElement.numberLong();
BSONElement namespaceElement = cursorObj.getField(kNamespaceFieldName);
if (namespaceElement.eoo()) {
return Status(ErrorCodes::FailedToParse,
str::stream() << "cursor response must contain "
<< "'"
<< kCursorFieldName
<< "."
<< kNamespaceFieldName
<< "' field: "
<< obj);
}
if (namespaceElement.type() != mongo::String) {
return Status(ErrorCodes::FailedToParse,
str::stream() << "'" << kCursorFieldName << "." << kNamespaceFieldName
<< "' field must be a string: "
<< obj);
}
const NamespaceString tempNss(namespaceElement.valueStringData());
if (!tempNss.isValid()) {
return Status(ErrorCodes::BadValue,
str::stream() << "'" << kCursorFieldName << "." << kNamespaceFieldName
<< "' contains an invalid namespace: "
<< obj);
}
batchData->nss = tempNss;
BSONElement batchElement = cursorObj.getField(batchFieldName);
if (batchElement.eoo()) {
return Status(ErrorCodes::FailedToParse,
str::stream() << "cursor response must contain '" << kCursorFieldName << "."
<< batchFieldName
<< "' field: "
<< obj);
}
if (!batchElement.isABSONObj()) {
return Status(ErrorCodes::FailedToParse,
str::stream() << "'" << kCursorFieldName << "." << batchFieldName
<< "' field must be an array: "
<< obj);
}
BSONObj batchObj = batchElement.Obj();
for (auto itemElement : batchObj) {
if (!itemElement.isABSONObj()) {
return Status(ErrorCodes::FailedToParse,
str::stream() << "found non-object " << itemElement << " in "
<< "'"
<< kCursorFieldName
<< "."
<< batchFieldName
<< "' field: "
<< obj);
}
batchData->documents.push_back(itemElement.Obj());
}
for (auto& doc : batchData->documents) {
doc.shareOwnershipWith(obj);
}
return Status::OK();
}
} // namespace
Fetcher::Fetcher(executor::TaskExecutor* executor,
const HostAndPort& source,
const std::string& dbname,
const BSONObj& findCmdObj,
const CallbackFn& work,
const BSONObj& metadata,
Milliseconds findNetworkTimeout,
Milliseconds getMoreNetworkTimeout,
std::unique_ptr<RemoteCommandRetryScheduler::RetryPolicy> firstCommandRetryPolicy)
: _executor(executor),
_source(source),
_dbname(dbname),
_cmdObj(findCmdObj.getOwned()),
_metadata(metadata.getOwned()),
_work(work),
_findNetworkTimeout(findNetworkTimeout),
_getMoreNetworkTimeout(getMoreNetworkTimeout),
_firstRemoteCommandScheduler(
_executor,
RemoteCommandRequest(_source, _dbname, _cmdObj, _metadata, nullptr, _findNetworkTimeout),
[this](const auto& x) { return this->_callback(x, kFirstBatchFieldName); },
std::move(firstCommandRetryPolicy)) {
uassert(ErrorCodes::BadValue, "callback function cannot be null", work);
}
Fetcher::~Fetcher() {
DESTRUCTOR_GUARD(shutdown(); join(););
}
HostAndPort Fetcher::getSource() const {
return _source;
}
BSONObj Fetcher::getCommandObject() const {
return _cmdObj;
}
BSONObj Fetcher::getMetadataObject() const {
return _metadata;
}
std::string Fetcher::toString() const {
return getDiagnosticString();
}
std::string Fetcher::getDiagnosticString() const {
stdx::lock_guard<stdx::mutex> lk(_mutex);
str::stream output;
output << "Fetcher";
output << " source: " << _source.toString();
output << " database: " << _dbname;
output << " query: " << _cmdObj;
output << " query metadata: " << _metadata;
output << " active: " << _isActive_inlock();
output << " findNetworkTimeout: " << _findNetworkTimeout;
output << " getMoreNetworkTimeout: " << _getMoreNetworkTimeout;
output << " shutting down?: " << _isShuttingDown_inlock();
output << " first: " << _first;
output << " firstCommandScheduler: " << _firstRemoteCommandScheduler.toString();
if (_getMoreCallbackHandle.isValid()) {
output << " getMoreHandle.valid: " << _getMoreCallbackHandle.isValid();
output << " getMoreHandle.cancelled: " << _getMoreCallbackHandle.isCanceled();
}
return output;
}
bool Fetcher::isActive() const {
stdx::lock_guard<stdx::mutex> lk(_mutex);
return _isActive_inlock();
}
bool Fetcher::_isActive_inlock() const {
return State::kRunning == _state || State::kShuttingDown == _state;
}
Status Fetcher::schedule() {
stdx::lock_guard<stdx::mutex> lock(_mutex);
switch (_state) {
case State::kPreStart:
_state = State::kRunning;
break;
case State::kRunning:
return Status(ErrorCodes::InternalError, "fetcher already started");
case State::kShuttingDown:
return Status(ErrorCodes::ShutdownInProgress, "fetcher shutting down");
case State::kComplete:
return Status(ErrorCodes::ShutdownInProgress, "fetcher completed");
}
auto status = _firstRemoteCommandScheduler.startup();
if (!status.isOK()) {
_state = State::kComplete;
return status;
}
return Status::OK();
}
void Fetcher::shutdown() {
stdx::lock_guard<stdx::mutex> lock(_mutex);
switch (_state) {
case State::kPreStart:
// Transition directly from PreStart to Complete if not started yet.
_state = State::kComplete;
return;
case State::kRunning:
_state = State::kShuttingDown;
break;
case State::kShuttingDown:
case State::kComplete:
// Nothing to do if we are already in ShuttingDown or Complete state.
return;
}
_firstRemoteCommandScheduler.shutdown();
if (_getMoreCallbackHandle) {
_executor->cancel(_getMoreCallbackHandle);
}
}
void Fetcher::join() {
stdx::unique_lock<stdx::mutex> lk(_mutex);
_condition.wait(lk, [this]() { return !_isActive_inlock(); });
}
Fetcher::State Fetcher::getState_forTest() const {
stdx::lock_guard<stdx::mutex> lk(_mutex);
return _state;
}
bool Fetcher::_isShuttingDown() const {
stdx::lock_guard<stdx::mutex> lk(_mutex);
return _isShuttingDown_inlock();
}
bool Fetcher::_isShuttingDown_inlock() const {
return State::kShuttingDown == _state;
}
Status Fetcher::_scheduleGetMore(const BSONObj& cmdObj) {
stdx::lock_guard<stdx::mutex> lk(_mutex);
if (_isShuttingDown_inlock()) {
return Status(ErrorCodes::CallbackCanceled,
"fetcher was shut down after previous batch was processed");
}
StatusWith<executor::TaskExecutor::CallbackHandle> scheduleResult =
_executor->scheduleRemoteCommand(
RemoteCommandRequest(
_source, _dbname, cmdObj, _metadata, nullptr, _getMoreNetworkTimeout),
[this](const auto& x) { return this->_callback(x, kNextBatchFieldName); });
if (!scheduleResult.isOK()) {
return scheduleResult.getStatus();
}
_getMoreCallbackHandle = scheduleResult.getValue();
return Status::OK();
}
void Fetcher::_callback(const RemoteCommandCallbackArgs& rcbd, const char* batchFieldName) {
QueryResponse batchData;
auto finishCallbackGuard = MakeGuard([this, &batchData] {
if (batchData.cursorId && !batchData.nss.isEmpty()) {
_sendKillCursors(batchData.cursorId, batchData.nss);
}
_finishCallback();
});
if (!rcbd.response.isOK()) {
_work(StatusWith<Fetcher::QueryResponse>(rcbd.response.status), nullptr, nullptr);
return;
}
if (_isShuttingDown()) {
_work(Status(ErrorCodes::CallbackCanceled, "fetcher shutting down"), nullptr, nullptr);
return;
}
const BSONObj& queryResponseObj = rcbd.response.data;
Status status = getStatusFromCommandResult(queryResponseObj);
if (!status.isOK()) {
_work(StatusWith<Fetcher::QueryResponse>(status), nullptr, nullptr);
return;
}
status = parseCursorResponse(queryResponseObj, batchFieldName, &batchData);
if (!status.isOK()) {
_work(StatusWith<Fetcher::QueryResponse>(status), nullptr, nullptr);
return;
}
batchData.otherFields.metadata = std::move(rcbd.response.data);
batchData.elapsedMillis = rcbd.response.elapsedMillis.value_or(Milliseconds{0});
{
stdx::lock_guard<stdx::mutex> lk(_mutex);
batchData.first = _first;
_first = false;
}
NextAction nextAction = NextAction::kNoAction;
if (!batchData.cursorId) {
_work(StatusWith<QueryResponse>(batchData), &nextAction, nullptr);
return;
}
nextAction = NextAction::kGetMore;
BSONObjBuilder bob;
_work(StatusWith<QueryResponse>(batchData), &nextAction, &bob);
// Callback function _work may modify nextAction to request the fetcher
// not to schedule a getMore command.
if (nextAction != NextAction::kGetMore) {
return;
}
// Callback function may also disable the fetching of additional data by not filling in the
// BSONObjBuilder for the getMore command.
auto cmdObj = bob.obj();
if (cmdObj.isEmpty()) {
return;
}
status = _scheduleGetMore(cmdObj);
if (!status.isOK()) {
nextAction = NextAction::kNoAction;
_work(StatusWith<Fetcher::QueryResponse>(status), nullptr, nullptr);
return;
}
finishCallbackGuard.Dismiss();
}
void Fetcher::_sendKillCursors(const CursorId id, const NamespaceString& nss) {
if (id) {
auto logKillCursorsResult = [](const RemoteCommandCallbackArgs& args) {
if (!args.response.isOK()) {
warning() << "killCursors command task failed: " << redact(args.response.status);
return;
}
auto status = getStatusFromCommandResult(args.response.data);
if (!status.isOK()) {
warning() << "killCursors command failed: " << redact(status);
}
};
auto cmdObj = BSON("killCursors" << nss.coll() << "cursors" << BSON_ARRAY(id));
auto scheduleResult = _executor->scheduleRemoteCommand(
RemoteCommandRequest(_source, _dbname, cmdObj, nullptr), logKillCursorsResult);
if (!scheduleResult.isOK()) {
warning() << "failed to schedule killCursors command: "
<< redact(scheduleResult.getStatus());
}
}
}
void Fetcher::_finishCallback() {
// After running callback function, clear '_work' to release any resources that might be held by
// this function object.
// '_work' must be moved to a temporary copy and destroyed outside the lock in case there is any
// logic that's invoked at the function object's destruction that might call into this Fetcher.
// 'tempWork' must be declared before lock guard 'lk' so that it is destroyed outside the lock.
Fetcher::CallbackFn tempWork;
stdx::lock_guard<stdx::mutex> lk(_mutex);
invariant(State::kComplete != _state);
_state = State::kComplete;
_first = false;
_condition.notify_all();
invariant(_work);
std::swap(_work, tempWork);
}
std::ostream& operator<<(std::ostream& os, const Fetcher::State& state) {
switch (state) {
case Fetcher::State::kPreStart:
return os << "PreStart";
case Fetcher::State::kRunning:
return os << "Running";
case Fetcher::State::kShuttingDown:
return os << "ShuttingDown";
case Fetcher::State::kComplete:
return os << "Complete";
}
MONGO_UNREACHABLE;
}
} // namespace mongo
|