summaryrefslogtreecommitdiff
path: root/src/mongo/db
diff options
context:
space:
mode:
authorDavid Storch <david.storch@10gen.com>2018-12-07 17:12:07 -0500
committerDavid Storch <david.storch@10gen.com>2018-12-10 10:52:38 -0500
commit35efc48897270a83097008f9343ae8c56a9e77cc (patch)
tree406883d218e510433424f8d3bff29e0ab28d97f3 /src/mongo/db
parent7b1e64f12e855d979232ef7b185002d5c8103353 (diff)
downloadmongo-35efc48897270a83097008f9343ae8c56a9e77cc.tar.gz
SERVER-37449 Add RequiresAllIndicesStage and use for subplanning and multiplanning.
These stages hold the collection's list of indices during plan selection, and expect all indices in this list to remain valid until plan selection completes.
Diffstat (limited to 'src/mongo/db')
-rw-r--r--src/mongo/db/SConscript1
-rw-r--r--src/mongo/db/catalog/index_catalog.h6
-rw-r--r--src/mongo/db/catalog/index_catalog_entry.h4
-rw-r--r--src/mongo/db/catalog/index_catalog_impl.cpp5
-rw-r--r--src/mongo/db/catalog/index_catalog_impl.h2
-rw-r--r--src/mongo/db/catalog/index_catalog_noop.h5
-rw-r--r--src/mongo/db/exec/cached_plan.cpp10
-rw-r--r--src/mongo/db/exec/cached_plan.h20
-rw-r--r--src/mongo/db/exec/requires_all_indices_stage.cpp46
-rw-r--r--src/mongo/db/exec/requires_all_indices_stage.h81
-rw-r--r--src/mongo/db/exec/requires_collection_stage.cpp11
-rw-r--r--src/mongo/db/exec/requires_collection_stage.h7
-rw-r--r--src/mongo/db/exec/requires_index_stage.cpp3
-rw-r--r--src/mongo/db/exec/subplan.cpp11
-rw-r--r--src/mongo/db/exec/subplan.h9
15 files changed, 197 insertions, 24 deletions
diff --git a/src/mongo/db/SConscript b/src/mongo/db/SConscript
index 2bc852aa9a0..8c76d6ee9c4 100644
--- a/src/mongo/db/SConscript
+++ b/src/mongo/db/SConscript
@@ -1100,6 +1100,7 @@ env.Library(
'exec/projection_exec.cpp',
'exec/queued_data_stage.cpp',
'exec/record_store_fast_count.cpp',
+ 'exec/requires_all_indices_stage.cpp',
'exec/requires_collection_stage.cpp',
'exec/requires_index_stage.cpp',
'exec/shard_filter.cpp',
diff --git a/src/mongo/db/catalog/index_catalog.h b/src/mongo/db/catalog/index_catalog.h
index 4e8f70d6d66..68ab42b8c92 100644
--- a/src/mongo/db/catalog/index_catalog.h
+++ b/src/mongo/db/catalog/index_catalog.h
@@ -292,6 +292,12 @@ public:
virtual std::shared_ptr<const IndexCatalogEntry> getEntryShared(
const IndexDescriptor*) const = 0;
+ /**
+ * Returns a vector of shared pointers to all index entries. Excludes unfinished indexes.
+ */
+ virtual std::vector<std::shared_ptr<const IndexCatalogEntry>> getAllReadyEntriesShared()
+ const = 0;
+
virtual IndexAccessMethod* getIndex(const IndexDescriptor* const desc) = 0;
virtual const IndexAccessMethod* getIndex(const IndexDescriptor* const desc) const = 0;
diff --git a/src/mongo/db/catalog/index_catalog_entry.h b/src/mongo/db/catalog/index_catalog_entry.h
index 192788e3948..1e52a6ac447 100644
--- a/src/mongo/db/catalog/index_catalog_entry.h
+++ b/src/mongo/db/catalog/index_catalog_entry.h
@@ -207,6 +207,10 @@ public:
_entries.push_back(std::move(entry));
}
+ std::vector<std::shared_ptr<const IndexCatalogEntry>> getAllEntries() const {
+ return {_entries.begin(), _entries.end()};
+ }
+
private:
std::vector<std::shared_ptr<IndexCatalogEntry>> _entries;
};
diff --git a/src/mongo/db/catalog/index_catalog_impl.cpp b/src/mongo/db/catalog/index_catalog_impl.cpp
index 5d0c9542dd7..88805d030fd 100644
--- a/src/mongo/db/catalog/index_catalog_impl.cpp
+++ b/src/mongo/db/catalog/index_catalog_impl.cpp
@@ -1109,6 +1109,11 @@ std::shared_ptr<const IndexCatalogEntry> IndexCatalogImpl::getEntryShared(
return _buildingIndexes.findShared(indexDescriptor);
}
+std::vector<std::shared_ptr<const IndexCatalogEntry>> IndexCatalogImpl::getAllReadyEntriesShared()
+ const {
+ return _readyIndexes.getAllEntries();
+}
+
const IndexDescriptor* IndexCatalogImpl::refreshEntry(OperationContext* opCtx,
const IndexDescriptor* oldDesc) {
invariant(opCtx->lockState()->isCollectionLockedForMode(_collection->ns().ns(), MODE_X));
diff --git a/src/mongo/db/catalog/index_catalog_impl.h b/src/mongo/db/catalog/index_catalog_impl.h
index 121bcf69bc5..ead5634dbbb 100644
--- a/src/mongo/db/catalog/index_catalog_impl.h
+++ b/src/mongo/db/catalog/index_catalog_impl.h
@@ -166,6 +166,8 @@ public:
std::shared_ptr<const IndexCatalogEntry> getEntryShared(const IndexDescriptor*) const override;
+ std::vector<std::shared_ptr<const IndexCatalogEntry>> getAllReadyEntriesShared() const override;
+
IndexAccessMethod* getIndex(const IndexDescriptor* desc) override;
const IndexAccessMethod* getIndex(const IndexDescriptor* desc) const override;
diff --git a/src/mongo/db/catalog/index_catalog_noop.h b/src/mongo/db/catalog/index_catalog_noop.h
index 34ea94afee2..9ef0f7d1fc4 100644
--- a/src/mongo/db/catalog/index_catalog_noop.h
+++ b/src/mongo/db/catalog/index_catalog_noop.h
@@ -119,6 +119,11 @@ public:
return nullptr;
}
+ std::vector<std::shared_ptr<const IndexCatalogEntry>> getAllReadyEntriesShared()
+ const override {
+ return {};
+ }
+
IndexAccessMethod* getIndex(const IndexDescriptor* const desc) override {
return nullptr;
}
diff --git a/src/mongo/db/exec/cached_plan.cpp b/src/mongo/db/exec/cached_plan.cpp
index 2955132451e..5f348ae3533 100644
--- a/src/mongo/db/exec/cached_plan.cpp
+++ b/src/mongo/db/exec/cached_plan.cpp
@@ -63,7 +63,7 @@ CachedPlanStage::CachedPlanStage(OperationContext* opCtx,
const QueryPlannerParams& params,
size_t decisionWorks,
PlanStage* root)
- : RequiresCollectionStage(kStageType, opCtx, collection),
+ : RequiresAllIndicesStage(kStageType, opCtx, collection),
_ws(ws),
_canonicalQuery(cq),
_plannerParams(params),
@@ -77,6 +77,14 @@ Status CachedPlanStage::pickBestPlan(PlanYieldPolicy* yieldPolicy) {
// make sense.
ScopedTimer timer(getClock(), &_commonStats.executionTimeMillis);
+ // During plan selection, the list of indices we are using to plan must remain stable, so the
+ // query will die during yield recovery if any index has been dropped. However, once plan
+ // selection completes successfully, we no longer need all indices to stick around. The selected
+ // plan should safely die on yield recovery if it is using the dropped index.
+ //
+ // Dismiss the requirement that no indices can be dropped when this method returns.
+ ON_BLOCK_EXIT([this] { releaseAllIndicesRequirement(); });
+
// If we work this many times during the trial period, then we will replan the
// query from scratch.
size_t maxWorksBeforeReplan =
diff --git a/src/mongo/db/exec/cached_plan.h b/src/mongo/db/exec/cached_plan.h
index d1c6fcaaf8f..2ea991517cd 100644
--- a/src/mongo/db/exec/cached_plan.h
+++ b/src/mongo/db/exec/cached_plan.h
@@ -33,7 +33,7 @@
#include <memory>
#include <queue>
-#include "mongo/db/exec/requires_collection_stage.h"
+#include "mongo/db/exec/requires_all_indices_stage.h"
#include "mongo/db/exec/working_set.h"
#include "mongo/db/jsobj.h"
#include "mongo/db/query/canonical_query.h"
@@ -46,13 +46,16 @@ namespace mongo {
class PlanYieldPolicy;
/**
- * This stage outputs its mainChild, and possibly its backup child
- * and also updates the cache.
- *
- * Preconditions: Valid RecordId.
+ * Runs a trial period in order to evaluate the cost of a cached plan. If the cost is unexpectedly
+ * high, the plan cache entry is deactivated and we use multi-planning to select an entirely new
+ * winning plan. This process is called "replanning".
*
+ * This stage requires all indices to stay intact during the trial period so that replanning can
+ * occur with the set of indices in 'params'. As a future improvement, we could instead refresh the
+ * list of indices in 'params' prior to replanning, and thus avoid inheriting from
+ * RequiresAllIndicesStage.
*/
-class CachedPlanStage final : public RequiresCollectionStage {
+class CachedPlanStage final : public RequiresAllIndicesStage {
public:
CachedPlanStage(OperationContext* opCtx,
Collection* collection,
@@ -86,11 +89,6 @@ public:
*/
Status pickBestPlan(PlanYieldPolicy* yieldPolicy);
-protected:
- void doSaveStateRequiresCollection() final {}
-
- void doRestoreStateRequiresCollection() final {}
-
private:
/**
* Passes stats from the trial period run of the cached plan to the plan cache.
diff --git a/src/mongo/db/exec/requires_all_indices_stage.cpp b/src/mongo/db/exec/requires_all_indices_stage.cpp
new file mode 100644
index 00000000000..be26a5a0402
--- /dev/null
+++ b/src/mongo/db/exec/requires_all_indices_stage.cpp
@@ -0,0 +1,46 @@
+/**
+ * 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.
+ */
+
+#include "mongo/platform/basic.h"
+
+#include "mongo/db/exec/requires_all_indices_stage.h"
+
+namespace mongo {
+
+void RequiresAllIndicesStage::doRestoreStateRequiresCollection() {
+ size_t i = 0;
+ for (auto&& index : _indexCatalogEntries) {
+ uassert(ErrorCodes::QueryPlanKilled,
+ str::stream() << "query plan killed :: index '" << _indexNames[i] << "' dropped",
+ index.lock());
+ ++i;
+ }
+}
+
+} // namespace mongo
diff --git a/src/mongo/db/exec/requires_all_indices_stage.h b/src/mongo/db/exec/requires_all_indices_stage.h
new file mode 100644
index 00000000000..f10492d160b
--- /dev/null
+++ b/src/mongo/db/exec/requires_all_indices_stage.h
@@ -0,0 +1,81 @@
+/**
+ * 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.
+ */
+
+#pragma once
+
+#include "mongo/db/exec/requires_collection_stage.h"
+
+namespace mongo {
+
+/**
+ * A base class for plan stages which require access to _all_ indices of a collection, and should
+ * cause the query to die on yield recovery if any index is dropped. Plan stages which depend on a
+ * single index, such as IXSCAN, should instead use RequiresIndexStage.
+ */
+class RequiresAllIndicesStage : public RequiresCollectionStage {
+public:
+ RequiresAllIndicesStage(const char* stageType, OperationContext* opCtx, const Collection* coll)
+ : RequiresCollectionStage(stageType, opCtx, coll) {
+ auto allEntriesShared = coll->getIndexCatalog()->getAllReadyEntriesShared();
+ _indexCatalogEntries.reserve(allEntriesShared.size());
+ _indexNames.reserve(allEntriesShared.size());
+ for (auto&& index : allEntriesShared) {
+ _indexCatalogEntries.emplace_back(index);
+ _indexNames.push_back(index->descriptor()->indexName());
+ }
+ }
+
+ virtual ~RequiresAllIndicesStage() = default;
+
+protected:
+ void doSaveStateRequiresCollection() override final {}
+
+ void doRestoreStateRequiresCollection() override final;
+
+ /**
+ * Subclasses may call this to indicate that they no longer require all indices on the
+ * collection to survive. After calling this, yield recovery will never fail.
+ */
+ void releaseAllIndicesRequirement() {
+ _indexCatalogEntries.clear();
+ _indexNames.clear();
+ }
+
+private:
+ // This stage holds weak pointers to all of the index catalog entries known at the time of
+ // construction. During yield recovery, we attempt to lock each weak pointer in order to
+ // determine whether an index we rely on has been destroyed. If any index has been destroyed,
+ // then we throw a query-fatal exception during restore.
+ std::vector<std::weak_ptr<const IndexCatalogEntry>> _indexCatalogEntries;
+
+ // The names of the indices above. Used for error reporting.
+ std::vector<std::string> _indexNames;
+};
+
+} // namespace mongo
diff --git a/src/mongo/db/exec/requires_collection_stage.cpp b/src/mongo/db/exec/requires_collection_stage.cpp
index 5825485dea6..c4794c3bfd2 100644
--- a/src/mongo/db/exec/requires_collection_stage.cpp
+++ b/src/mongo/db/exec/requires_collection_stage.cpp
@@ -50,9 +50,18 @@ void RequiresCollectionStageBase<CollectionT>::doRestoreState() {
const UUIDCatalog& catalog = UUIDCatalog::get(getOpCtx());
_collection = catalog.lookupCollectionByUUID(_collectionUUID);
uassert(ErrorCodes::QueryPlanKilled,
- str::stream() << "UUID " << _collectionUUID << " no longer exists.",
+ str::stream() << "Collection dropped. UUID " << _collectionUUID << " no longer exists.",
_collection);
+ // TODO SERVER-31695: Allow queries to survive collection rename, rather than throwing here when
+ // a rename has happened during yield.
+ uassert(ErrorCodes::QueryPlanKilled,
+ str::stream() << "Collection with UUID " << _collectionUUID << " was renamed from '"
+ << _nss.ns()
+ << "' to '"
+ << _collection->ns().ns(),
+ _nss == _collection->ns());
+
doRestoreStateRequiresCollection();
}
diff --git a/src/mongo/db/exec/requires_collection_stage.h b/src/mongo/db/exec/requires_collection_stage.h
index 9ebd96f84f0..f57e2bf8987 100644
--- a/src/mongo/db/exec/requires_collection_stage.h
+++ b/src/mongo/db/exec/requires_collection_stage.h
@@ -56,7 +56,8 @@ public:
RequiresCollectionStageBase(const char* stageType, OperationContext* opCtx, CollectionT coll)
: PlanStage(stageType, opCtx),
_collection(coll),
- _collectionUUID(_collection->uuid().get()) {
+ _collectionUUID(_collection->uuid().get()),
+ _nss(_collection->ns()) {
invariant(_collection);
}
@@ -88,6 +89,10 @@ protected:
private:
CollectionT _collection;
const UUID _collectionUUID;
+
+ // TODO SERVER-31695: The namespace will no longer be needed once queries can survive collection
+ // renames.
+ const NamespaceString _nss;
};
// Type alias for use by PlanStages that read a Collection.
diff --git a/src/mongo/db/exec/requires_index_stage.cpp b/src/mongo/db/exec/requires_index_stage.cpp
index 4c7150e1920..8718f98d1f4 100644
--- a/src/mongo/db/exec/requires_index_stage.cpp
+++ b/src/mongo/db/exec/requires_index_stage.cpp
@@ -51,8 +51,7 @@ void RequiresIndexStage::doRestoreStateRequiresCollection() {
// our index is no longer valid, and the query should die.
_indexCatalogEntry = _weakIndexCatalogEntry.lock();
uassert(ErrorCodes::QueryPlanKilled,
- str::stream() << "query plan killed :: index named '" << _indexName
- << "' is no longer valid",
+ str::stream() << "query plan killed :: index '" << _indexName << "' dropped",
_indexCatalogEntry);
_indexDescriptor = _indexCatalogEntry->descriptor();
diff --git a/src/mongo/db/exec/subplan.cpp b/src/mongo/db/exec/subplan.cpp
index 2ac55e6870c..200caa778b7 100644
--- a/src/mongo/db/exec/subplan.cpp
+++ b/src/mongo/db/exec/subplan.cpp
@@ -40,6 +40,7 @@
#include "mongo/db/exec/multi_plan.h"
#include "mongo/db/exec/scoped_timer.h"
#include "mongo/db/matcher/extensions_callback_real.h"
+#include "mongo/db/query/get_executor.h"
#include "mongo/db/query/plan_executor.h"
#include "mongo/db/query/planner_access.h"
#include "mongo/db/query/planner_analysis.h"
@@ -65,7 +66,7 @@ SubplanStage::SubplanStage(OperationContext* opCtx,
WorkingSet* ws,
const QueryPlannerParams& params,
CanonicalQuery* cq)
- : RequiresCollectionStage(kStageType, opCtx, collection),
+ : RequiresAllIndicesStage(kStageType, opCtx, collection),
_ws(ws),
_plannerParams(params),
_query(cq) {
@@ -435,6 +436,14 @@ Status SubplanStage::pickBestPlan(PlanYieldPolicy* yieldPolicy) {
// work that happens here, so this is needed for the time accounting to make sense.
ScopedTimer timer(getClock(), &_commonStats.executionTimeMillis);
+ // During plan selection, the list of indices we are using to plan must remain stable, so the
+ // query will die during yield recovery if any index has been dropped. However, once plan
+ // selection completes successfully, we no longer need all indices to stick around. The selected
+ // plan should safely die on yield recovery if it is using the dropped index.
+ //
+ // Dismiss the requirement that no indices can be dropped when this method returns.
+ ON_BLOCK_EXIT([this] { releaseAllIndicesRequirement(); });
+
// Plan each branch of the $or.
Status subplanningStatus = planSubqueries();
if (!subplanningStatus.isOK()) {
diff --git a/src/mongo/db/exec/subplan.h b/src/mongo/db/exec/subplan.h
index 3f589a2b8ef..a56c47532e2 100644
--- a/src/mongo/db/exec/subplan.h
+++ b/src/mongo/db/exec/subplan.h
@@ -37,7 +37,7 @@
#include "mongo/base/owned_pointer_vector.h"
#include "mongo/base/status.h"
#include "mongo/base/string_data.h"
-#include "mongo/db/exec/requires_collection_stage.h"
+#include "mongo/db/exec/requires_all_indices_stage.h"
#include "mongo/db/query/canonical_query.h"
#include "mongo/db/query/plan_cache.h"
#include "mongo/db/query/plan_yield_policy.h"
@@ -69,7 +69,7 @@ class OperationContext;
*
* --Plans for entire rooted $or queries are neither written to nor read from the plan cache.
*/
-class SubplanStage final : public RequiresCollectionStage {
+class SubplanStage final : public RequiresAllIndicesStage {
public:
SubplanStage(OperationContext* opCtx,
const Collection* collection,
@@ -127,11 +127,6 @@ public:
return _compositeSolution.get();
}
-protected:
- void doSaveStateRequiresCollection() final {}
-
- void doRestoreStateRequiresCollection() final {}
-
private:
/**
* A class used internally in order to keep track of the results of planning