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
|
/**
* 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::kIndex
#include "mongo/platform/basic.h"
#include "mongo/db/catalog/index_catalog_entry_impl.h"
#include <algorithm>
#include "mongo/base/init.h"
#include "mongo/db/catalog/collection_info_cache_impl.h"
#include "mongo/db/concurrency/d_concurrency.h"
#include "mongo/db/concurrency/write_conflict_exception.h"
#include "mongo/db/index/index_access_method.h"
#include "mongo/db/index/index_descriptor.h"
#include "mongo/db/logical_clock.h"
#include "mongo/db/matcher/expression.h"
#include "mongo/db/matcher/expression_parser.h"
#include "mongo/db/multi_key_path_tracker.h"
#include "mongo/db/operation_context.h"
#include "mongo/db/query/collation/collator_factory_interface.h"
#include "mongo/db/service_context.h"
#include "mongo/db/storage/durable_catalog.h"
#include "mongo/db/transaction_participant.h"
#include "mongo/stdx/memory.h"
#include "mongo/util/log.h"
#include "mongo/util/scopeguard.h"
namespace mongo {
using std::string;
IndexCatalogEntryImpl::IndexCatalogEntryImpl(OperationContext* const opCtx,
const NamespaceString& nss,
std::unique_ptr<IndexDescriptor> descriptor,
CollectionInfoCache* const infoCache)
: _ns(nss),
_descriptor(std::move(descriptor)),
_infoCache(infoCache),
_ordering(Ordering::make(_descriptor->keyPattern())),
_isReady(false),
_prefix(DurableCatalog::get(opCtx)->getIndexPrefix(opCtx, nss, _descriptor->indexName())) {
_descriptor->_cachedEntry = this;
_isReady = _catalogIsReady(opCtx);
{
stdx::lock_guard<Latch> lk(_indexMultikeyPathsMutex);
const bool isMultikey = _catalogIsMultikey(opCtx, &_indexMultikeyPaths);
_isMultikeyForRead.store(isMultikey);
_isMultikeyForWrite.store(isMultikey);
_indexTracksMultikeyPathsInCatalog = !_indexMultikeyPaths.empty();
}
const BSONObj& collation = _descriptor->collation();
if (!collation.isEmpty()) {
auto statusWithCollator =
CollatorFactoryInterface::get(opCtx->getServiceContext())->makeFromBSON(collation);
// Index spec should have already been validated.
invariant(statusWithCollator.getStatus());
_collator = std::move(statusWithCollator.getValue());
}
if (_descriptor->isPartial()) {
const BSONObj& filter = _descriptor->partialFilterExpression();
boost::intrusive_ptr<ExpressionContext> expCtx(
new ExpressionContext(opCtx, _collator.get()));
// Parsing the partial filter expression is not expected to fail here since the
// expression would have been successfully parsed upstream during index creation.
StatusWithMatchExpression statusWithMatcher =
MatchExpressionParser::parse(filter,
std::move(expCtx),
ExtensionsCallbackNoop(),
MatchExpressionParser::kBanAllSpecialFeatures);
invariant(statusWithMatcher.getStatus());
_filterExpression = std::move(statusWithMatcher.getValue());
LOG(2) << "have filter expression for " << _ns << " " << _descriptor->indexName() << " "
<< redact(filter);
}
}
IndexCatalogEntryImpl::~IndexCatalogEntryImpl() {
_descriptor->_cachedEntry = nullptr; // defensive
_descriptor.reset();
}
void IndexCatalogEntryImpl::init(std::unique_ptr<IndexAccessMethod> accessMethod) {
invariant(!_accessMethod);
_accessMethod = std::move(accessMethod);
}
bool IndexCatalogEntryImpl::isReady(OperationContext* opCtx) const {
// For multi-document transactions, we can open a snapshot prior to checking the
// minimumSnapshotVersion on a collection. This means we are unprotected from reading
// out-of-sync index catalog entries. To fix this, we uassert if we detect that the
// in-memory catalog is out-of-sync with the on-disk catalog.
if (opCtx->inMultiDocumentTransaction()) {
if (!_catalogIsPresent(opCtx) || _catalogIsReady(opCtx) != _isReady) {
uasserted(ErrorCodes::SnapshotUnavailable,
str::stream() << "Unable to read from a snapshot due to pending collection"
" catalog changes; please retry the operation.");
}
}
DEV invariant(_isReady == _catalogIsReady(opCtx));
return _isReady;
}
bool IndexCatalogEntryImpl::isMultikey(OperationContext* opCtx) const {
auto ret = _isMultikeyForRead.load();
if (ret) {
return true;
}
// Multikey updates are only persisted, to disk and in memory, when the transaction
// commits. In the case of multi-statement transactions, a client attempting to read their own
// transactions writes can return wrong results if their writes include multikey changes.
//
// To accomplish this, the write-path will persist multikey changes on the `Session` object
// and the read-path will query this state before determining there is no interesting multikey
// state. Note, it's always legal, though potentially wasteful, to return `true`.
auto txnParticipant = TransactionParticipant::get(opCtx);
if (!txnParticipant || !txnParticipant.transactionIsOpen()) {
return false;
}
invariant(txnParticipant);
for (const MultikeyPathInfo& path : txnParticipant.getUncommittedMultikeyPathInfos()) {
if (path.nss == NamespaceString(_ns) && path.indexName == _descriptor->indexName()) {
return true;
}
}
return false;
}
MultikeyPaths IndexCatalogEntryImpl::getMultikeyPaths(OperationContext* opCtx) const {
stdx::lock_guard<Latch> lk(_indexMultikeyPathsMutex);
auto txnParticipant = TransactionParticipant::get(opCtx);
if (!txnParticipant || !txnParticipant.transactionIsOpen()) {
return _indexMultikeyPaths;
}
invariant(txnParticipant);
MultikeyPaths ret = _indexMultikeyPaths;
for (const MultikeyPathInfo& path : txnParticipant.getUncommittedMultikeyPathInfos()) {
if (path.nss == NamespaceString(_ns) && path.indexName == _descriptor->indexName()) {
MultikeyPathTracker::mergeMultikeyPaths(&ret, path.multikeyPaths);
}
}
return ret;
}
// ---
void IndexCatalogEntryImpl::setMinimumVisibleSnapshot(Timestamp newMinimumVisibleSnapshot) {
if (!_minVisibleSnapshot || (newMinimumVisibleSnapshot > _minVisibleSnapshot.get())) {
_minVisibleSnapshot = newMinimumVisibleSnapshot;
}
}
void IndexCatalogEntryImpl::setIsReady(bool newIsReady) {
_isReady = newIsReady;
}
void IndexCatalogEntryImpl::setMultikey(OperationContext* opCtx,
const std::vector<BSONObj>& multikeyMetadataKeys,
const MultikeyPaths& multikeyPaths) {
// An index can either track path-level multikey information in the catalog or as metadata keys
// in the index itself, but not both.
invariant(!(_indexTracksMultikeyPathsInCatalog && multikeyMetadataKeys.size() > 0));
// If the index is already set as multikey and we don't have any path-level information to
// update, then there's nothing more for us to do.
bool hasNoPathLevelInfo = (!_indexTracksMultikeyPathsInCatalog && multikeyMetadataKeys.empty());
if (hasNoPathLevelInfo && _isMultikeyForWrite.load()) {
return;
}
if (_indexTracksMultikeyPathsInCatalog) {
stdx::lock_guard<Latch> lk(_indexMultikeyPathsMutex);
invariant(multikeyPaths.size() == _indexMultikeyPaths.size());
bool newPathIsMultikey = false;
for (size_t i = 0; i < multikeyPaths.size(); ++i) {
if (!std::includes(_indexMultikeyPaths[i].begin(),
_indexMultikeyPaths[i].end(),
multikeyPaths[i].begin(),
multikeyPaths[i].end())) {
// If 'multikeyPaths' contains a new path component that causes this index to be
// multikey, then we must update the index metadata in the CollectionCatalogEntry.
newPathIsMultikey = true;
break;
}
}
if (!newPathIsMultikey) {
// Otherwise, if all the path components in 'multikeyPaths' are already tracked in
// '_indexMultikeyPaths', then there's nothing more for us to do.
return;
}
}
MultikeyPaths paths = _indexTracksMultikeyPathsInCatalog ? multikeyPaths : MultikeyPaths{};
// On a primary, we can simply assign this write the same timestamp as the index creation,
// insert, or update that caused this index to become multikey. This is because if two
// operations concurrently try to change the index to be multikey, they will conflict and the
// loser will simply get a higher timestamp and go into the oplog second with a later optime.
//
// On a secondary, writes must get the timestamp of their oplog entry, and the multikey change
// must occur before the timestamp of the earliest write that makes the index multikey.
// Secondaries only serialize writes by document, not by collection. If two inserts that both
// make an index multikey are applied out of order, changing the index to multikey at the
// insert timestamps would change the index to multikey at the later timestamp, which would be
// wrong. To prevent this, rather than setting the index to be multikey here, we add the
// necessary information to the OperationContext and do the write at the timestamp of the
// beginning of the batch.
//
// One exception to this rule is for background indexes. Background indexes are built using
// a different OperationContext and thus this information would be ignored. Background index
// builds happen concurrently though and thus the multikey write can safely occur at the
// current clock time. Once a background index is committed, if a future write makes
// it multikey, that write will be marked as "isTrackingMultikeyPathInfo" on the applier's
// OperationContext and we can safely defer that write to the end of the batch.
if (MultikeyPathTracker::get(opCtx).isTrackingMultikeyPathInfo()) {
MultikeyPathTracker::get(opCtx).addMultikeyPathInfo(
{_ns, _descriptor->indexName(), multikeyMetadataKeys, std::move(paths)});
return;
}
// If multikeyMetadataKeys is non-empty, we must insert these keys into the index itself. We do
// not have to account for potential dupes, since all metadata keys are indexed against a single
// RecordId. An attempt to write a duplicate key will therefore be ignored.
if (!multikeyMetadataKeys.empty()) {
static const auto kMultikeyMetadataKeyId =
RecordId{RecordId::ReservedId::kWildcardMultikeyMetadataId};
uassertStatusOK(accessMethod()->insertKeys(
opCtx, multikeyMetadataKeys, kMultikeyMetadataKeyId, {}, {}, nullptr));
}
// In the absense of using the storage engine to read from the catalog, we must set multikey
// prior to the storage engine transaction committing.
//
// Moreover, there must not be an `onRollback` handler to reset this back to false. Given a long
// enough pause in processing `onRollback` handlers, a later writer that successfully flipped
// multikey can be undone. Alternatively, one could use a counter instead of a boolean to avoid
// that problem.
_isMultikeyForRead.store(true);
if (_indexTracksMultikeyPathsInCatalog) {
stdx::lock_guard<Latch> lk(_indexMultikeyPathsMutex);
for (size_t i = 0; i < multikeyPaths.size(); ++i) {
_indexMultikeyPaths[i].insert(multikeyPaths[i].begin(), multikeyPaths[i].end());
}
}
if (!opCtx->inMultiDocumentTransaction()) {
const bool indexMetadataHasChanged = DurableCatalog::get(opCtx)->setIndexIsMultikey(
opCtx, _ns, _descriptor->indexName(), paths);
if (indexMetadataHasChanged && _infoCache) {
LOG(1) << _ns << ": clearing plan cache - index " << _descriptor->keyPattern()
<< " set to multi key.";
_infoCache->clearQueryCache();
}
opCtx->recoveryUnit()->onCommit(
[this](boost::optional<Timestamp>) { _isMultikeyForWrite.store(true); });
return;
}
// If we are inside a multi-document transaction, we write the on-disk multikey update in a
// separate transaction so that it will not generate prepare conflicts with other operations
// that try to set the multikey flag. In general, it should always be safe to update the
// multikey flag earlier than necessary, and so we are not concerned with the atomicity of the
// multikey flag write and the parent transaction. We can do this write separately and commit it
// before the parent transaction commits.
auto txnParticipant = TransactionParticipant::get(opCtx);
TransactionParticipant::SideTransactionBlock sideTxn(opCtx);
writeConflictRetry(opCtx, "set index multikey", _ns.ns(), [&] {
WriteUnitOfWork wuow(opCtx);
// If we have a prepare optime for recovery, then we always use that. During recovery of
// prepared transactions, the logical clock may not yet be initialized, so we use the
// prepare timestamp of the transaction for this write. This is safe since the prepare
// timestamp is always <= the commit timestamp of a transaction, which satisfies the
// correctness requirement for multikey writes i.e. they must occur at or before the
// first write that set the multikey flag.
auto recoveryPrepareOpTime = txnParticipant.getPrepareOpTimeForRecovery();
Timestamp writeTs = recoveryPrepareOpTime.isNull()
? LogicalClock::get(opCtx)->getClusterTime().asTimestamp()
: recoveryPrepareOpTime.getTimestamp();
auto status = opCtx->recoveryUnit()->setTimestamp(writeTs);
if (status.code() == ErrorCodes::BadValue) {
log() << "Temporarily could not timestamp the multikey catalog write, retrying. "
<< status.reason();
throw WriteConflictException();
}
fassert(31164, status);
const bool indexMetadataHasChanged = DurableCatalog::get(opCtx)->setIndexIsMultikey(
opCtx, _ns, _descriptor->indexName(), paths);
if (indexMetadataHasChanged && _infoCache) {
LOG(1) << _ns << ": clearing plan cache - index " << _descriptor->keyPattern()
<< " set to multi key.";
_infoCache->clearQueryCache();
}
opCtx->recoveryUnit()->onCommit(
[this](boost::optional<Timestamp>) { _isMultikeyForWrite.store(true); });
wuow.commit();
// Within a multi-document transaction, reads should be able to see the effect of previous
// writes done within that transaction. If a previous write in a transaction has set the
// index to be multikey, then a subsequent read MUST know that fact in order to return
// correct results. This is true in general for multikey writes. Since we don't update the
// in-memory multikey flag until after the transaction commits, we track extra information
// here to let subsequent readers within the same transaction know if this index was set as
// multikey by a previous write in the transaction.
txnParticipant.addUncommittedMultikeyPathInfo(MultikeyPathInfo{
_ns, _descriptor->indexName(), multikeyMetadataKeys, std::move(paths)});
});
}
void IndexCatalogEntryImpl::setNs(NamespaceString ns) {
_ns = ns;
_descriptor->setNs(std::move(ns));
}
// ----
bool IndexCatalogEntryImpl::_catalogIsReady(OperationContext* opCtx) const {
return DurableCatalog::get(opCtx)->isIndexReady(opCtx, _ns, _descriptor->indexName());
}
bool IndexCatalogEntryImpl::_catalogIsPresent(OperationContext* opCtx) const {
return DurableCatalog::get(opCtx)->isIndexPresent(opCtx, _ns, _descriptor->indexName());
}
bool IndexCatalogEntryImpl::_catalogIsMultikey(OperationContext* opCtx,
MultikeyPaths* multikeyPaths) const {
if (_isMultikeyForRead.load()) {
return true;
}
return DurableCatalog::get(opCtx)->isIndexMultikey(
opCtx, _ns, _descriptor->indexName(), multikeyPaths);
}
KVPrefix IndexCatalogEntryImpl::_catalogGetPrefix(OperationContext* opCtx) const {
return DurableCatalog::get(opCtx)->getIndexPrefix(opCtx, _ns, _descriptor->indexName());
}
} // namespace mongo
|