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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
|
/**
* Copyright (C) 2020-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.
*/
#include "mongo/db/s/resharding/resharding_oplog_application.h"
#include "mongo/db/catalog/collection_write_path.h"
#include "mongo/db/concurrency/exception_util.h"
#include "mongo/db/dbhelpers.h"
#include "mongo/db/index/index_access_method.h"
#include "mongo/db/logical_session_cache.h"
#include "mongo/db/namespace_string.h"
#include "mongo/db/ops/delete.h"
#include "mongo/db/ops/delete_request_gen.h"
#include "mongo/db/ops/parsed_delete.h"
#include "mongo/db/ops/update.h"
#include "mongo/db/query/get_executor.h"
#include "mongo/db/query/plan_executor.h"
#include "mongo/db/repl/oplog_applier_utils.h"
#include "mongo/db/s/operation_sharding_state.h"
#include "mongo/db/s/resharding/resharding_server_parameters_gen.h"
#include "mongo/db/session_catalog_mongod.h"
#include "mongo/db/stats/counters.h"
#include "mongo/db/transaction/transaction_participant.h"
#include "mongo/logv2/log.h"
#include "mongo/s/sharding_feature_flags_gen.h"
#define MONGO_LOGV2_DEFAULT_COMPONENT ::mongo::logv2::LogComponent::kResharding
namespace mongo {
namespace {
Date_t getDeadline(OperationContext* opCtx) {
return opCtx->getServiceContext()->getPreciseClockSource()->now() +
Milliseconds(resharding::gReshardingOplogApplierMaxLockRequestTimeoutMillis.load());
}
void runWithTransaction(OperationContext* opCtx,
const NamespaceString& nss,
unique_function<void(OperationContext*)> func) {
AlternativeSessionRegion asr(opCtx);
auto* const client = asr.opCtx()->getClient();
{
stdx::lock_guard<Client> lk(*client);
client->setSystemOperationKillableByStepdown(lk);
}
asr.opCtx()->setAlwaysInterruptAtStepDownOrUp_UNSAFE();
AuthorizationSession::get(client)->grantInternalAuthorization(client);
TxnNumber txnNumber = 0;
asr.opCtx()->setTxnNumber(txnNumber);
asr.opCtx()->setInMultiDocumentTransaction();
// ReshardingOpObserver depends on the collection metadata being known when processing writes to
// the temporary resharding collection. We attach shard version IGNORED to the write operations
// and leave it to ReshardingOplogBatchApplier::applyBatch() to retry on a StaleConfig exception
// to allow the collection metadata information to be recovered.
ScopedSetShardRole scopedSetShardRole(asr.opCtx(),
nss,
ShardVersion::IGNORED() /* shardVersion */,
boost::none /* databaseVersion */);
MongoDOperationContextSession ocs(asr.opCtx());
auto txnParticipant = TransactionParticipant::get(asr.opCtx());
ScopeGuard guard([opCtx = asr.opCtx(), &txnParticipant] {
try {
txnParticipant.abortTransaction(opCtx);
} catch (DBException& e) {
LOGV2_WARNING(4990200,
"Failed to abort transaction in AlternativeSessionRegion",
"error"_attr = redact(e));
}
});
txnParticipant.beginOrContinue(
asr.opCtx(), {txnNumber}, false /* autocommit */, true /* startTransaction */);
txnParticipant.unstashTransactionResources(asr.opCtx(), "reshardingOplogApplication");
func(asr.opCtx());
if (txnParticipant.retrieveCompletedTransactionOperations(asr.opCtx()).size() > 0) {
// Similar to the `isTimestamped` check in `applyOperation`, we only want to commit the
// transaction if we're doing replicated writes.
txnParticipant.commitUnpreparedTransaction(asr.opCtx());
} else {
txnParticipant.abortTransaction(asr.opCtx());
}
txnParticipant.stashTransactionResources(asr.opCtx());
guard.dismiss();
}
} // namespace
ReshardingOplogApplicationRules::ReshardingOplogApplicationRules(
NamespaceString outputNss,
std::vector<NamespaceString> allStashNss,
size_t myStashIdx,
ShardId donorShardId,
ChunkManager sourceChunkMgr,
ReshardingOplogApplierMetrics* applierMetrics)
: _outputNss(std::move(outputNss)),
_allStashNss(std::move(allStashNss)),
_myStashIdx(myStashIdx),
_myStashNss(_allStashNss.at(_myStashIdx)),
_donorShardId(std::move(donorShardId)),
_sourceChunkMgr(std::move(sourceChunkMgr)),
_applierMetrics(applierMetrics) {}
Status ReshardingOplogApplicationRules::applyOperation(OperationContext* opCtx,
const repl::OplogEntry& op) const {
LOGV2_DEBUG(49901, 3, "Applying op for resharding", "op"_attr = redact(op.toBSONForLogging()));
invariant(!opCtx->lockState()->inAWriteUnitOfWork());
invariant(opCtx->writesAreReplicated());
return writeConflictRetry(opCtx, "applyOplogEntryCRUDOpResharding", op.getNss().ns(), [&] {
try {
WriteUnitOfWork wuow(opCtx);
AutoGetCollection autoCollOutput(opCtx,
_outputNss,
MODE_IX,
AutoGetCollectionViewMode::kViewsForbidden,
getDeadline(opCtx));
uassert(
ErrorCodes::NamespaceNotFound,
str::stream() << "Failed to apply op during resharding due to missing collection "
<< _outputNss.ns(),
autoCollOutput);
AutoGetCollection autoCollStash(opCtx,
_myStashNss,
MODE_IX,
AutoGetCollectionViewMode::kViewsForbidden,
getDeadline(opCtx));
uassert(
ErrorCodes::NamespaceNotFound,
str::stream() << "Failed to apply op during resharding due to missing collection "
<< _myStashNss.ns(),
autoCollStash);
auto opType = op.getOpType();
switch (opType) {
case repl::OpTypeEnum::kInsert:
_applyInsert_inlock(
opCtx, autoCollOutput.getDb(), *autoCollOutput, *autoCollStash, op);
_applierMetrics->onInsertApplied();
break;
case repl::OpTypeEnum::kUpdate:
_applyUpdate_inlock(
opCtx, autoCollOutput.getDb(), *autoCollOutput, *autoCollStash, op);
_applierMetrics->onUpdateApplied();
break;
case repl::OpTypeEnum::kDelete:
_applyDelete_inlock(
opCtx, autoCollOutput.getDb(), *autoCollOutput, *autoCollStash, op);
_applierMetrics->onDeleteApplied();
break;
default:
MONGO_UNREACHABLE;
}
if (opCtx->recoveryUnit()->isTimestamped()) {
// Resharding oplog application does two kinds of writes:
//
// 1) The (obvious) write for applying oplog entries to documents being resharded.
// 2) An unreplicated no-op write that on a document in the output collection to
// ensure serialization of concurrent transactions.
//
// Some of the code paths can end up where only the second kind of write is made. In
// that case, there is no timestamp associated with the write. This results in a
// mixed-mode update chain within WT that is problematic with durable history. We
// roll back those transactions by only committing the `WriteUnitOfWork` when there
// is a timestamp set.
wuow.commit();
}
return Status::OK();
} catch (const DBException& ex) {
if (ex.code() == ErrorCodes::WriteConflict || ex.code() == ErrorCodes::LockTimeout) {
throwWriteConflictException();
}
return ex.toStatus();
}
});
}
void ReshardingOplogApplicationRules::_applyInsert_inlock(OperationContext* opCtx,
Database* db,
const CollectionPtr& outputColl,
const CollectionPtr& stashColl,
const repl::OplogEntry& op) const {
/**
* The rules to apply ordinary insert operations are as follows:
*
* Note that [op _id] refers to the value of op["o"]["_id"].
*
* 1. If there exists a document with _id == [op _id] in the conflict stash collection, replace
* the contents of the doc in the conflict stash collection for this donor shard with the
* contents of 'op'.
* 2. If there does NOT exist a document with _id == [op _id] in the output collection, insert
* the contents of 'op' into the output collection.
* 3. If there exists a document with _id == [op _id] in the output collection and it is owned
* by this donor shard, replace the contents of the doc in the output collection with the
* contents of 'op'.
* 4. If there exists a document with _id == [op _id] in the output collection and it is NOT
* owned by this donor shard, insert the contents of 'op' into the conflict stash collection.
*/
BSONObj oField = op.getObject();
// If the 'o' field does not have an _id, the oplog entry is corrupted.
auto idField = oField["_id"];
uassert(ErrorCodes::NoSuchKey,
str::stream() << "Failed to apply insert due to missing _id: "
<< redact(op.toBSONForLogging()),
!idField.eoo());
BSONObj idQuery = idField.wrap();
auto updateMod = write_ops::UpdateModification::parseFromClassicUpdate(oField);
// First, query the conflict stash collection using [op _id] as the query. If a doc exists,
// apply rule #1 and run a replacement update on the stash collection.
auto stashCollDoc = _queryStashCollById(opCtx, stashColl, idQuery);
if (!stashCollDoc.isEmpty()) {
auto request = UpdateRequest();
request.setNamespaceString(_myStashNss);
request.setQuery(idQuery);
request.setUpdateModification(updateMod);
request.setUpsert(false);
request.setFromOplogApplication(true);
UpdateResult ur = update(opCtx, db, request);
invariant(ur.numMatched != 0);
_applierMetrics->onWriteToStashCollections();
return;
}
// Query the output collection for a doc with _id == [op _id]. If a doc does not exist, apply
// rule #2 and insert this doc into the output collection.
BSONObj outputCollDoc;
auto foundDoc = Helpers::findByIdAndNoopUpdate(opCtx, outputColl, idQuery, outputCollDoc);
if (!foundDoc) {
uassertStatusOK(collection_internal::insertDocument(opCtx,
outputColl,
InsertStatement(oField),
nullptr /* OpDebug */,
false /* fromMigrate */));
return;
}
invariant(!outputCollDoc.isEmpty());
// A doc with [op _id] already exists in the output collection. Check whether this doc belongs
// to '_donorShardId' under the original shard key. If it does, apply rule #3 and run a
// replacement update on the output collection.
if (_sourceChunkMgr.keyBelongsToShard(
_sourceChunkMgr.getShardKeyPattern().extractShardKeyFromDoc(outputCollDoc),
_donorShardId)) {
auto request = UpdateRequest();
request.setNamespaceString(_outputNss);
request.setQuery(idQuery);
request.setUpdateModification(updateMod);
request.setUpsert(false);
request.setFromOplogApplication(true);
UpdateResult ur = update(opCtx, db, request);
invariant(ur.numMatched != 0);
return;
}
// The doc does not belong to '_donorShardId' under the original shard key, so apply rule #4
// and insert the contents of 'op' to the stash collection.
uassertStatusOK(collection_internal::insertDocument(
opCtx, stashColl, InsertStatement(oField), nullptr /* OpDebug */, false /* fromMigrate */));
_applierMetrics->onWriteToStashCollections();
}
void ReshardingOplogApplicationRules::_applyUpdate_inlock(OperationContext* opCtx,
Database* db,
const CollectionPtr& outputColl,
const CollectionPtr& stashColl,
const repl::OplogEntry& op) const {
/**
* The rules to apply ordinary update operations are as follows:
*
* Note that [op _id] refers to the value of op["o"]["_id"].
*
* 1. If there exists a document with _id == [op _id] in the conflict stash collection, update
* the document from this collection.
* 2. If there does NOT exist a document with _id == [op _id] in the output collection, do
* nothing.
* 3. If there exists a document with _id == [op _id] in the output collection but it is NOT
* owned by this donor shard, do nothing.
* 4. If there exists a document with _id == [op _id] in the output collection and it is owned
* by this donor shard, update the document from this collection.
*/
BSONObj oField = op.getObject();
BSONObj o2Field;
if (op.getObject2())
o2Field = op.getObject2().value();
// If the 'o2' field does not have an _id, the oplog entry is corrupted.
auto idField = o2Field["_id"];
uassert(ErrorCodes::NoSuchKey,
str::stream() << "Failed to apply update due to missing _id: "
<< redact(op.toBSONForLogging()),
!idField.eoo());
BSONObj idQuery = idField.wrap();
auto updateMod = write_ops::UpdateModification::parseFromOplogEntry(
oField, write_ops::UpdateModification::DiffOptions{});
// First, query the conflict stash collection using [op _id] as the query. If a doc exists,
// apply rule #1 and update the doc from the stash collection.
auto stashCollDoc = _queryStashCollById(opCtx, stashColl, idQuery);
if (!stashCollDoc.isEmpty()) {
auto request = UpdateRequest();
request.setNamespaceString(_myStashNss);
request.setQuery(idQuery);
request.setUpdateModification(std::move(updateMod));
request.setUpsert(false);
request.setFromOplogApplication(true);
UpdateResult ur = update(opCtx, db, request);
invariant(ur.numMatched != 0);
_applierMetrics->onWriteToStashCollections();
return;
}
// Query the output collection for a doc with _id == [op _id].
BSONObj outputCollDoc;
auto foundDoc = Helpers::findByIdAndNoopUpdate(opCtx, outputColl, idQuery, outputCollDoc);
if (!foundDoc ||
!_sourceChunkMgr.keyBelongsToShard(
_sourceChunkMgr.getShardKeyPattern().extractShardKeyFromDoc(outputCollDoc),
_donorShardId)) {
// Either a doc with _id == [op _id] does not exist in the output collection (rule
// #2) or a doc does exist, but it does not belong to '_donorShardId' under the
// original shard key (rule #3). In either case, do nothing.
return;
}
invariant(!outputCollDoc.isEmpty());
// A doc with _id == [op _id] exists and is owned by '_donorShardId'. Apply rule #4 and update
// the doc in the ouput collection.
auto request = UpdateRequest();
request.setNamespaceString(_outputNss);
request.setQuery(idQuery);
request.setUpdateModification(std::move(updateMod));
request.setUpsert(false);
request.setFromOplogApplication(true);
UpdateResult ur = update(opCtx, db, request);
invariant(ur.numMatched != 0);
}
void ReshardingOplogApplicationRules::_applyDelete_inlock(OperationContext* opCtx,
Database* db,
const CollectionPtr& outputColl,
const CollectionPtr& stashColl,
const repl::OplogEntry& op) const {
/**
* The rules to apply ordinary delete operations are as follows:
*
* Note that [op _id] refers to the value of op["o"]["_id"].
*
* 1. If there exists a document with _id == [op _id] in the conflict stash collection, delete
* the document from this collection.
* 2. If there does NOT exist a document with _id == [op _id] in the output collection, do
* nothing.
* 3. If there exists a document with _id == [op _id] in the output collection but it is NOT
* owned by this donor shard, do nothing.
* 4. If there exists a document with _id == [op _id] in the output collection and it is owned
* by this donor shard, atomically delete the doc from the output collection, choose a doc with
* _id == [op _id] arbitrarily from among all resharding conflict stash collections to delete
* from that resharding conflict stash collection and insert into the output collection.
*/
BSONObj oField = op.getObject();
// If the 'o' field does not have an _id, the oplog entry is corrupted.
auto idField = oField["_id"];
uassert(ErrorCodes::NoSuchKey,
str::stream() << "Failed to apply delete due to missing _id: "
<< redact(op.toBSONForLogging()),
!idField.eoo());
BSONObj idQuery = idField.wrap();
const NamespaceString outputNss = op.getNss();
// First, query the conflict stash collection using [op _id] as the query. If a doc exists,
// apply rule #1 and delete the doc from the stash collection.
auto stashCollDoc = _queryStashCollById(opCtx, stashColl, idQuery);
if (!stashCollDoc.isEmpty()) {
auto nDeleted = deleteObjects(opCtx, stashColl, _myStashNss, idQuery, true /* justOne */);
invariant(nDeleted != 0);
_applierMetrics->onWriteToStashCollections();
return;
}
// Now run 'findByIdAndNoopUpdate' to figure out which of rules #2, #3, and #4 we must apply.
// We must run 'findByIdAndNoopUpdate' in the same storage transaction as the ops run in the
// single replica set transaction that is executed if we apply rule #4, so we therefore must run
// 'findByIdAndNoopUpdate' as a part of the single replica set transaction.
runWithTransaction(opCtx, _outputNss, [this, idQuery](OperationContext* opCtx) {
AutoGetCollection autoCollOutput(opCtx,
_outputNss,
MODE_IX,
AutoGetCollectionViewMode::kViewsForbidden,
getDeadline(opCtx));
uassert(ErrorCodes::NamespaceNotFound,
str::stream() << "Failed to apply op during resharding due to missing collection "
<< _outputNss.ns(),
autoCollOutput);
// Query the output collection for a doc with _id == [op _id].
BSONObj outputCollDoc;
auto foundDoc =
Helpers::findByIdAndNoopUpdate(opCtx, *autoCollOutput, idQuery, outputCollDoc);
if (!foundDoc ||
!_sourceChunkMgr.keyBelongsToShard(
_sourceChunkMgr.getShardKeyPattern().extractShardKeyFromDoc(outputCollDoc),
_donorShardId)) {
// Either a doc with _id == [op _id] does not exist in the output collection (rule
// #2) or a doc does exist, but it does not belong to '_donorShardId' under the
// original shard key (rule #3). In either case, do nothing.
return;
}
invariant(!outputCollDoc.isEmpty());
// A doc with _id == [op _id] exists and is owned by '_donorShardId'. Apply rule #4 and
// atomically:
// 1. Delete the doc from '_outputNss'
// 2. Choose a document with _id == [op _id] arbitrarily from among all resharding conflict
// stash collections to delete from that resharding conflict stash collection
// 3. Insert the doc just deleted into the output collection
// Delete from the output collection
auto nDeleted =
deleteObjects(opCtx, *autoCollOutput, _outputNss, idQuery, true /* justOne */);
invariant(nDeleted != 0);
// Attempt to delete a doc from one of the stash collections. Once we've matched a doc in
// one collection, we'll break.
BSONObj doc;
size_t i = 0;
for (const auto& coll : _allStashNss) {
if (i == _myStashIdx) {
++i;
continue;
}
AutoGetCollection autoCollStash(opCtx,
coll,
MODE_IX,
AutoGetCollectionViewMode::kViewsForbidden,
getDeadline(opCtx));
uassert(
ErrorCodes::NamespaceNotFound,
str::stream() << "Failed to apply op during resharding due to missing collection "
<< coll.ns(),
autoCollStash);
auto request = DeleteRequest{};
request.setNsString(coll);
request.setQuery(idQuery);
request.setMulti(false);
request.setReturnDeleted(true);
ParsedDelete parsedDelete(opCtx, &request);
uassertStatusOK(parsedDelete.parseRequest());
auto exec = uassertStatusOK(getExecutorDelete(&CurOp::get(opCtx)->debug(),
&(*autoCollStash),
&parsedDelete,
boost::none /* verbosity */));
BSONObj res;
auto state = exec->getNext(&res, nullptr);
_applierMetrics->onWriteToStashCollections();
if (PlanExecutor::ADVANCED == state) {
// We matched a document and deleted it, so break.
doc = std::move(res);
break;
}
invariant(state == PlanExecutor::IS_EOF);
++i;
}
// Insert the doc we just deleted from one of the stash collections into the output
// collection.
if (!doc.isEmpty()) {
uassertStatusOK(collection_internal::insertDocument(opCtx,
*autoCollOutput,
InsertStatement(doc),
nullptr /* OpDebug */,
false /* fromMigrate */));
}
});
}
BSONObj ReshardingOplogApplicationRules::_queryStashCollById(OperationContext* opCtx,
const CollectionPtr& coll,
const BSONObj& idQuery) const {
const IndexCatalog* indexCatalog = coll->getIndexCatalog();
uassert(4990100,
str::stream() << "Missing _id index for collection " << _myStashNss.ns(),
indexCatalog->haveIdIndex(opCtx));
BSONObj result;
Helpers::findById(opCtx, _myStashNss.ns(), idQuery, result);
return result;
}
} // namespace mongo
|