diff options
author | Max Hirschhorn <max.hirschhorn@mongodb.com> | 2016-09-14 20:49:17 -0400 |
---|---|---|
committer | Max Hirschhorn <max.hirschhorn@mongodb.com> | 2016-09-14 20:49:17 -0400 |
commit | 8302d0735b34a16cac000e5e345722487536e5bc (patch) | |
tree | c9ef60fe493eda60feacddfd67c0ec298939c4ef /src/mongo/db/catalog/collection_compact.cpp | |
parent | 46acb1b94944bc7aa68ff6a8b3cd2d340b272c6f (diff) | |
download | mongo-8302d0735b34a16cac000e5e345722487536e5bc.tar.gz |
SERVER-24033 Write full index spec in oplog entry for index creation.
This ensures that the index version (aka the "v" field) is always
present in the oplog entry when creating indexes on a 3.4 primary.
We can therefore assume that if the "v" field isn't present in the
corresponding oplog entry, then a v=1 index should be built.
Changes MultiBlockIndex::init() to return the index specifications
that were actually created.
The "repairDatabase", "compact", "copydb", and "cloneCollection"
commands no longer automatically upgrade the index version to the
current default version. Instead, the only command that does so is
the "reIndex" command.
Diffstat (limited to 'src/mongo/db/catalog/collection_compact.cpp')
-rw-r--r-- | src/mongo/db/catalog/collection_compact.cpp | 36 |
1 files changed, 22 insertions, 14 deletions
diff --git a/src/mongo/db/catalog/collection_compact.cpp b/src/mongo/db/catalog/collection_compact.cpp index afba5e94987..a89ce69fbd7 100644 --- a/src/mongo/db/catalog/collection_compact.cpp +++ b/src/mongo/db/catalog/collection_compact.cpp @@ -42,6 +42,7 @@ #include "mongo/db/commands/server_status.h" #include "mongo/db/curop.h" #include "mongo/db/index/index_access_method.h" +#include "mongo/db/index/index_descriptor.h" #include "mongo/db/operation_context.h" #include "mongo/util/log.h" #include "mongo/util/touch_pages.h" @@ -51,25 +52,32 @@ namespace mongo { using std::endl; using std::vector; +using IndexVersion = IndexDescriptor::IndexVersion; + namespace { BSONObj _compactAdjustIndexSpec(const BSONObj& oldSpec) { - BSONObjBuilder b; - BSONObj::iterator i(oldSpec); - while (i.more()) { - BSONElement e = i.next(); - if (str::equals(e.fieldName(), "v")) { - // Drop any preexisting index version spec. The default index version will - // be used instead for the new index. - continue; - } - if (str::equals(e.fieldName(), "background")) { + BSONObjBuilder bob; + + for (auto&& indexSpecElem : oldSpec) { + auto indexSpecElemFieldName = indexSpecElem.fieldNameStringData(); + if (IndexDescriptor::kIndexVersionFieldName == indexSpecElemFieldName) { + IndexVersion indexVersion = static_cast<IndexVersion>(indexSpecElem.numberInt()); + if (IndexVersion::kV0 == indexVersion) { + // We automatically upgrade v=0 indexes to v=1 indexes. + bob.append(IndexDescriptor::kIndexVersionFieldName, + static_cast<int>(IndexVersion::kV1)); + } else { + bob.append(IndexDescriptor::kIndexVersionFieldName, static_cast<int>(indexVersion)); + } + } else if (IndexDescriptor::kBackgroundFieldName == indexSpecElemFieldName) { // Create the new index in the foreground. continue; + } else { + bob.append(indexSpecElem); } - // Pass the element through to the new index spec. - b.append(e); } - return b.obj(); + + return bob.obj(); } class MyCompactAdaptor : public RecordStoreCompactAdaptor { @@ -182,7 +190,7 @@ StatusWith<CompactStats> Collection::compact(OperationContext* txn, indexer.allowInterruption(); indexer.ignoreUniqueConstraint(); // in compact we should be doing no checking - Status status = indexer.init(indexSpecs); + Status status = indexer.init(indexSpecs).getStatus(); if (!status.isOK()) return StatusWith<CompactStats>(status); |