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
|
// kv_collection_catalog_entry.cpp
/**
* Copyright (C) 2014 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 <memory>
#include "mongo/db/storage/kv/kv_collection_catalog_entry.h"
#include "mongo/db/catalog/uuid_catalog.h"
#include "mongo/db/index/index_descriptor.h"
#include "mongo/db/storage/kv/kv_catalog.h"
#include "mongo/db/storage/kv/kv_catalog_feature_tracker.h"
#include "mongo/db/storage/kv/kv_engine.h"
namespace mongo {
using std::string;
namespace {
bool indexTypeSupportsPathLevelMultikeyTracking(StringData accessMethod) {
return accessMethod == IndexNames::BTREE || accessMethod == IndexNames::GEO_2DSPHERE;
}
} // namespace
class KVCollectionCatalogEntry::AddIndexChange : public RecoveryUnit::Change {
public:
AddIndexChange(OperationContext* opCtx, KVCollectionCatalogEntry* cce, StringData ident)
: _opCtx(opCtx), _cce(cce), _ident(ident.toString()) {}
virtual void commit() {}
virtual void rollback() {
// Intentionally ignoring failure.
_cce->_engine->dropIdent(_opCtx, _ident).transitional_ignore();
}
OperationContext* const _opCtx;
KVCollectionCatalogEntry* const _cce;
const std::string _ident;
};
class KVCollectionCatalogEntry::RemoveIndexChange : public RecoveryUnit::Change {
public:
RemoveIndexChange(OperationContext* opCtx, KVCollectionCatalogEntry* cce, StringData ident)
: _opCtx(opCtx), _cce(cce), _ident(ident.toString()) {}
virtual void rollback() {}
virtual void commit() {
// Intentionally ignoring failure here. Since we've removed the metadata pointing to the
// index, we should never see it again anyway.
_cce->_engine->dropIdent(_opCtx, _ident).transitional_ignore();
}
OperationContext* const _opCtx;
KVCollectionCatalogEntry* const _cce;
const std::string _ident;
};
KVCollectionCatalogEntry::KVCollectionCatalogEntry(KVEngine* engine,
KVCatalog* catalog,
StringData ns,
StringData ident,
std::unique_ptr<RecordStore> rs)
: BSONCollectionCatalogEntry(ns),
_engine(engine),
_catalog(catalog),
_ident(ident.toString()),
_recordStore(std::move(rs)) {}
KVCollectionCatalogEntry::~KVCollectionCatalogEntry() {}
bool KVCollectionCatalogEntry::setIndexIsMultikey(OperationContext* opCtx,
StringData indexName,
const MultikeyPaths& multikeyPaths) {
MetaData md = _getMetaData(opCtx);
int offset = md.findIndexOffset(indexName);
invariant(offset >= 0);
const bool tracksPathLevelMultikeyInfo = !md.indexes[offset].multikeyPaths.empty();
if (tracksPathLevelMultikeyInfo) {
invariant(!multikeyPaths.empty());
invariant(multikeyPaths.size() == md.indexes[offset].multikeyPaths.size());
} else {
invariant(multikeyPaths.empty());
if (md.indexes[offset].multikey) {
// The index is already set as multikey and we aren't tracking path-level multikey
// information for it. We return false to indicate that the index metadata is unchanged.
return false;
}
}
md.indexes[offset].multikey = true;
if (tracksPathLevelMultikeyInfo) {
bool newPathIsMultikey = false;
bool somePathIsMultikey = false;
// Store new path components that cause this index to be multikey in catalog's index
// metadata.
for (size_t i = 0; i < multikeyPaths.size(); ++i) {
std::set<size_t>& indexMultikeyComponents = md.indexes[offset].multikeyPaths[i];
for (const auto multikeyComponent : multikeyPaths[i]) {
auto result = indexMultikeyComponents.insert(multikeyComponent);
newPathIsMultikey = newPathIsMultikey || result.second;
somePathIsMultikey = true;
}
}
// If all of the sets in the multikey paths vector were empty, then no component of any
// indexed field caused the index to be multikey. setIndexIsMultikey() therefore shouldn't
// have been called.
invariant(somePathIsMultikey);
if (!newPathIsMultikey) {
// We return false to indicate that the index metadata is unchanged.
return false;
}
}
_catalog->putMetaData(opCtx, ns().toString(), md);
return true;
}
void KVCollectionCatalogEntry::setIndexHead(OperationContext* opCtx,
StringData indexName,
const RecordId& newHead) {
MetaData md = _getMetaData(opCtx);
int offset = md.findIndexOffset(indexName);
invariant(offset >= 0);
md.indexes[offset].head = newHead;
_catalog->putMetaData(opCtx, ns().toString(), md);
}
Status KVCollectionCatalogEntry::removeIndex(OperationContext* opCtx, StringData indexName) {
MetaData md = _getMetaData(opCtx);
if (md.findIndexOffset(indexName) < 0)
return Status::OK(); // never had the index so nothing to do.
const string ident = _catalog->getIndexIdent(opCtx, ns().ns(), indexName);
md.eraseIndex(indexName);
_catalog->putMetaData(opCtx, ns().toString(), md);
// Lazily remove to isolate underlying engine from rollback.
opCtx->recoveryUnit()->registerChange(new RemoveIndexChange(opCtx, this, ident));
return Status::OK();
}
Status KVCollectionCatalogEntry::prepareForIndexBuild(OperationContext* opCtx,
const IndexDescriptor* spec) {
MetaData md = _getMetaData(opCtx);
KVPrefix prefix = KVPrefix::getNextPrefix(ns());
IndexMetaData imd(spec->infoObj(), false, RecordId(), false, prefix);
if (indexTypeSupportsPathLevelMultikeyTracking(spec->getAccessMethodName())) {
const auto feature =
KVCatalog::FeatureTracker::RepairableFeature::kPathLevelMultikeyTracking;
if (!_catalog->getFeatureTracker()->isRepairableFeatureInUse(opCtx, feature)) {
_catalog->getFeatureTracker()->markRepairableFeatureAsInUse(opCtx, feature);
}
imd.multikeyPaths = MultikeyPaths{static_cast<size_t>(spec->keyPattern().nFields())};
}
// Mark collation feature as in use if the index has a non-simple collation.
if (imd.spec["collation"]) {
const auto feature = KVCatalog::FeatureTracker::NonRepairableFeature::kCollation;
if (!_catalog->getFeatureTracker()->isNonRepairableFeatureInUse(opCtx, feature)) {
_catalog->getFeatureTracker()->markNonRepairableFeatureAsInUse(opCtx, feature);
}
}
md.indexes.push_back(imd);
_catalog->putMetaData(opCtx, ns().toString(), md);
string ident = _catalog->getIndexIdent(opCtx, ns().ns(), spec->indexName());
const Status status = _engine->createGroupedSortedDataInterface(opCtx, ident, spec, prefix);
if (status.isOK()) {
opCtx->recoveryUnit()->registerChange(new AddIndexChange(opCtx, this, ident));
}
return status;
}
void KVCollectionCatalogEntry::indexBuildSuccess(OperationContext* opCtx, StringData indexName) {
MetaData md = _getMetaData(opCtx);
int offset = md.findIndexOffset(indexName);
invariant(offset >= 0);
md.indexes[offset].ready = true;
_catalog->putMetaData(opCtx, ns().toString(), md);
}
void KVCollectionCatalogEntry::updateTTLSetting(OperationContext* opCtx,
StringData idxName,
long long newExpireSeconds) {
MetaData md = _getMetaData(opCtx);
int offset = md.findIndexOffset(idxName);
invariant(offset >= 0);
md.indexes[offset].updateTTLSetting(newExpireSeconds);
_catalog->putMetaData(opCtx, ns().toString(), md);
}
void KVCollectionCatalogEntry::addUUID(OperationContext* opCtx,
CollectionUUID uuid,
Collection* coll) {
// Add a UUID to CollectionOptions if a UUID does not yet exist.
MetaData md = _getMetaData(opCtx);
if (!md.options.uuid) {
md.options.uuid = uuid;
_catalog->putMetaData(opCtx, ns().toString(), md);
UUIDCatalog& catalog = UUIDCatalog::get(opCtx->getServiceContext());
catalog.onCreateCollection(opCtx, coll, uuid);
} else {
fassert(40564, md.options.uuid.get() == uuid);
}
}
void KVCollectionCatalogEntry::removeUUID(OperationContext* opCtx) {
// Remove the UUID from CollectionOptions if a UUID exists.
MetaData md = _getMetaData(opCtx);
if (md.options.uuid) {
CollectionUUID uuid = md.options.uuid.get();
md.options.uuid = boost::none;
_catalog->putMetaData(opCtx, ns().toString(), md);
UUIDCatalog& catalog = UUIDCatalog::get(opCtx->getServiceContext());
Collection* coll = catalog.lookupCollectionByUUID(uuid);
if (coll) {
catalog.onDropCollection(opCtx, uuid);
}
}
}
bool KVCollectionCatalogEntry::isEqualToMetadataUUID(OperationContext* opCtx,
OptionalCollectionUUID uuid) {
MetaData md = _getMetaData(opCtx);
if (uuid) {
return md.options.uuid && md.options.uuid.get() == uuid.get();
} else {
return !md.options.uuid;
}
}
void KVCollectionCatalogEntry::updateFlags(OperationContext* opCtx, int newValue) {
MetaData md = _getMetaData(opCtx);
md.options.flags = newValue;
md.options.flagsSet = true;
_catalog->putMetaData(opCtx, ns().toString(), md);
}
void KVCollectionCatalogEntry::updateValidator(OperationContext* opCtx,
const BSONObj& validator,
StringData validationLevel,
StringData validationAction) {
MetaData md = _getMetaData(opCtx);
md.options.validator = validator;
md.options.validationLevel = validationLevel.toString();
md.options.validationAction = validationAction.toString();
_catalog->putMetaData(opCtx, ns().toString(), md);
}
void KVCollectionCatalogEntry::setIsTemp(OperationContext* opCtx, bool isTemp) {
MetaData md = _getMetaData(opCtx);
md.options.temp = isTemp;
_catalog->putMetaData(opCtx, ns().toString(), md);
}
void KVCollectionCatalogEntry::updateCappedSize(OperationContext* opCtx, long long size) {
MetaData md = _getMetaData(opCtx);
md.options.cappedSize = size;
_catalog->putMetaData(opCtx, ns().toString(), md);
}
BSONCollectionCatalogEntry::MetaData KVCollectionCatalogEntry::_getMetaData(
OperationContext* opCtx) const {
return _catalog->getMetaData(opCtx, ns().toString());
}
}
|