summaryrefslogtreecommitdiff
path: root/src/mongo/db/catalog/collection_catalog.h
diff options
context:
space:
mode:
authorHenrik Edin <henrik.edin@mongodb.com>2020-08-31 15:39:53 -0400
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2020-09-15 14:28:38 +0000
commitaccc7e7cd7e7a984347361d03ee76514c4a54163 (patch)
treed01b140af8d8609d19ed1b5f5126511bc06377e3 /src/mongo/db/catalog/collection_catalog.h
parentb3f2165c7cf7d2e4d098f86eaf8bfa3b87b683a1 (diff)
downloadmongo-accc7e7cd7e7a984347361d03ee76514c4a54163.tar.gz
SERVER-50349 Getting a writable collection now requires the caller to be inside a WUOW by default
There are three modes when accessing a writable Collection: * Managed in WUOW (default) * Unmanaged (users need to commit/rollback) * Inplace that provides direct access to the Collection in the catalog. (Users need to ensure there's no concurrent operations going on) Added a helper RAII type CollectionWriter that abstract the three modes above and also provides abstraction on different methods of accessing Collections (AutoGetCollection or manual lookups). Writable Collection is aquired lazily when needed (usually inside a WUOW).
Diffstat (limited to 'src/mongo/db/catalog/collection_catalog.h')
-rw-r--r--src/mongo/db/catalog/collection_catalog.h32
1 files changed, 31 insertions, 1 deletions
diff --git a/src/mongo/db/catalog/collection_catalog.h b/src/mongo/db/catalog/collection_catalog.h
index f9ad25938f9..32afe461f04 100644
--- a/src/mongo/db/catalog/collection_catalog.h
+++ b/src/mongo/db/catalog/collection_catalog.h
@@ -57,9 +57,23 @@ class CollectionCatalog {
public:
using CollectionInfoFn = std::function<bool(const Collection* collection)>;
+ enum class LifetimeMode {
+ // Lifetime of writable Collection is managed by an active write unit of work. The writable
+ // collection is installed in the catalog during commit.
+ kManagedInWriteUnitOfWork,
+
+ // Unmanaged writable Collection usable outside of write unit of work. Users need to commit
+ // the Collection to the catalog.
+ kUnmanagedClone,
+
+ // Inplace writable access to the Collection currently installed in the catalog. This is
+ // only safe when the server is in a state where there can be no concurrent readers.
+ kInplace
+ };
+
class iterator {
public:
- using value_type = Collection*;
+ using value_type = const Collection*;
iterator(StringData dbName, uint64_t genNum, const CollectionCatalog& catalog);
iterator(std::map<std::pair<std::string, CollectionUUID>,
@@ -69,6 +83,8 @@ public:
iterator operator++(int);
boost::optional<CollectionUUID> uuid();
+ Collection* getWritableCollection(OperationContext* opCtx, LifetimeMode mode);
+
/*
* Equality operators == and != do not attempt to reposition the iterators being compared.
* The behavior for comparing invalid iterators is undefined.
@@ -164,6 +180,7 @@ public:
* Returns nullptr if the 'uuid' is not known.
*/
Collection* lookupCollectionByUUIDForMetadataWrite(OperationContext* opCtx,
+ LifetimeMode mode,
CollectionUUID uuid);
const Collection* lookupCollectionByUUID(OperationContext* opCtx, CollectionUUID uuid) const;
std::shared_ptr<const Collection> lookupCollectionByUUIDForRead(OperationContext* opCtx,
@@ -186,6 +203,7 @@ public:
* Returns nullptr if the namespace is unknown.
*/
Collection* lookupCollectionByNamespaceForMetadataWrite(OperationContext* opCtx,
+ LifetimeMode mode,
const NamespaceString& nss);
const Collection* lookupCollectionByNamespace(OperationContext* opCtx,
const NamespaceString& nss) const;
@@ -326,6 +344,18 @@ public:
*/
void addResource(const ResourceId& rid, const std::string& entry);
+ /**
+ * Commit unmanaged Collection that was acquired by lookupCollectionBy***ForMetadataWrite and
+ * lifetime mode kUnmanagedClone.
+ */
+ void commitUnmanagedClone(Collection* collection);
+
+ /**
+ * Discard unmanaged Collection that was acquired by lookupCollectionBy***ForMetadataWrite and
+ * lifetime mode kUnmanagedClone.
+ */
+ void discardUnmanagedClone(Collection* collection);
+
private:
friend class CollectionCatalog::iterator;