Storage Engine API
storage_engine.h
Go to the documentation of this file.
1 // storage_engine.h
2 
31 #pragma once
32 
33 #include <string>
34 #include <vector>
35 
36 #include "mongo/base/status.h"
37 #include "mongo/bson/bsonobj.h"
38 #include "mongo/bson/timestamp.h"
39 #include "mongo/util/mongoutils/str.h"
40 
41 namespace mongo {
42 
43 class DatabaseCatalogEntry;
44 class JournalListener;
45 class OperationContext;
46 class RecoveryUnit;
47 class SnapshotManager;
48 struct StorageGlobalParams;
49 class StorageEngineLockFile;
50 class StorageEngineMetadata;
51 
58 public:
66  class Factory {
67  public:
68  virtual ~Factory() {}
69 
74  virtual StorageEngine* create(const StorageGlobalParams& params,
75  const StorageEngineLockFile* lockFile) const = 0;
76 
83  virtual StringData getCanonicalName() const = 0;
84 
91  virtual Status validateCollectionStorageOptions(const BSONObj& options) const {
92  if (options.isEmpty())
93  return Status::OK();
94  return Status(ErrorCodes::InvalidOptions,
95  str::stream() << "storage engine " << getCanonicalName()
96  << " does not support any collection storage options");
97  }
98 
105  virtual Status validateIndexStorageOptions(const BSONObj& options) const {
106  if (options.isEmpty())
107  return Status::OK();
108  return Status(ErrorCodes::InvalidOptions,
109  str::stream() << "storage engine " << getCanonicalName()
110  << " does not support any index storage options");
111  }
112 
119  virtual Status validateMetadata(const StorageEngineMetadata& metadata,
120  const StorageGlobalParams& params) const = 0;
121 
127  virtual BSONObj createMetadataOptions(const StorageGlobalParams& params) const = 0;
128 
136  virtual bool supportsReadOnly() const {
137  return false;
138  }
139  };
140 
144  virtual ~StorageEngine() {}
145 
151  virtual void finishInit() {}
152 
159  virtual RecoveryUnit* newRecoveryUnit() = 0;
160 
166  virtual void listDatabases(std::vector<std::string>* out) const = 0;
167 
174  virtual DatabaseCatalogEntry* getDatabaseCatalogEntry(OperationContext* opCtx,
175  StringData db) = 0;
176 
184  virtual bool supportsDocLocking() const = 0;
185 
189  virtual bool supportsDBLocking() const {
190  return true;
191  }
192 
196  virtual bool supportsCappedCollections() const {
197  return true;
198  }
199 
203  virtual bool isDurable() const = 0;
204 
208  virtual bool isEphemeral() const = 0;
209 
213  virtual bool isMmapV1() const {
214  return false;
215  }
216 
223  virtual void loadCatalog(OperationContext* opCtx) {}
224  virtual void closeCatalog(OperationContext* opCtx) {}
225 
229  virtual Status closeDatabase(OperationContext* opCtx, StringData db) = 0;
230 
234  virtual Status dropDatabase(OperationContext* opCtx, StringData db) = 0;
235 
239  virtual int flushAllFiles(OperationContext* opCtx, bool sync) = 0;
240 
260  virtual Status beginBackup(OperationContext* opCtx) {
261  return Status(ErrorCodes::CommandNotSupported,
262  "The current storage engine doesn't support backup mode");
263  }
264 
272  virtual void endBackup(OperationContext* opCtx) {
273  return;
274  }
275 
285  virtual Status repairRecordStore(OperationContext* opCtx, const std::string& ns) = 0;
286 
295  virtual void cleanShutdown() = 0;
296 
303  return nullptr;
304  }
305 
310  virtual void setJournalListener(JournalListener* jl) = 0;
311 
318  virtual bool supportsRecoverToStableTimestamp() const {
319  return false;
320  }
321 
325  virtual bool supportsReadConcernSnapshot() const {
326  return false;
327  }
328 
345  virtual StatusWith<Timestamp> recoverToStableTimestamp(OperationContext* opCtx) {
346  fassertFailed(40547);
347  }
348 
354  virtual boost::optional<Timestamp> getRecoveryTimestamp() const {
355  MONGO_UNREACHABLE;
356  }
357 
364  virtual boost::optional<Timestamp> getLastStableCheckpointTimestamp() const {
365  MONGO_UNREACHABLE;
366  }
367 
372  virtual void setStableTimestamp(Timestamp timestamp) {}
373 
378  virtual void setInitialDataTimestamp(Timestamp timestamp) {}
379 
391 
396  virtual void setOldestTimestamp(Timestamp timestamp) {}
397 
405  virtual bool isCacheUnderPressure(OperationContext* opCtx) const {
406  return false;
407  }
408 
413  virtual void setCachePressureForTest(int pressure) {}
414 
422  virtual void replicationBatchIsComplete() const {};
423 
424  // (CollectionName, IndexName)
425  typedef std::pair<std::string, std::string> CollectionIndexNamePair;
426 
432  OperationContext* opCtx) {
433  return std::vector<CollectionIndexNamePair>();
434  };
435 
442  virtual Timestamp getAllCommittedTimestamp() const = 0;
443 };
444 
445 } // namespace mongo
virtual ~Factory()
Definition: storage_engine.h:68
virtual StorageEngine * create(const StorageGlobalParams &params, const StorageEngineLockFile *lockFile) const =0
Return a new instance of the StorageEngine.
virtual void replicationBatchIsComplete() const
Notifies the storage engine that a replication batch has completed.
Definition: storage_engine.h:422
virtual void finishInit()
Called after the globalStorageEngine pointer has been set up, before any other methods are called...
Definition: storage_engine.h:151
virtual void closeCatalog(OperationContext *opCtx)
Definition: storage_engine.h:224
virtual int flushAllFiles(OperationContext *opCtx, bool sync)=0
Collection *const const NamespaceString & ns
Definition: collection_info_cache_impl.cpp:53
virtual Status beginBackup(OperationContext *opCtx)
Transitions the storage engine into backup mode.
Definition: storage_engine.h:260
virtual void loadCatalog(OperationContext *opCtx)
Populates and tears down in-memory data structures, respectively.
Definition: storage_engine.h:223
Copyright (C) 2014 MongoDB Inc.
Definition: bson_collection_catalog_entry.cpp:38
virtual void setOldestTimestamp(Timestamp timestamp)
Sets the oldest timestamp for which the storage engine must maintain snapshot history through...
Definition: storage_engine.h:396
virtual ~StorageEngine()
The destructor should only be called if we are tearing down but not exiting the process.
Definition: storage_engine.h:144
OperationContext Database StringData BSONObj CollectionOptions::ParseKind bool const BSONObj &idIndex Status
Definition: database_impl.cpp:956
virtual Status repairRecordStore(OperationContext *opCtx, const std::string &ns)=0
Recover as much data as possible from a potentially corrupt RecordStore.
virtual bool isCacheUnderPressure(OperationContext *opCtx) const
Indicates whether the storage engine cache is under pressure.
Definition: storage_engine.h:405
virtual bool isDurable() const =0
Returns whether the engine supports a journalling concept or not.
Definition: storage_options.h:46
virtual Status dropDatabase(OperationContext *opCtx, StringData db)=0
Deletes all data and metadata for a database.
virtual void setInitialDataTimestamp(Timestamp timestamp)
Tells the storage engine the timestamp of the data at startup.
Definition: storage_engine.h:378
virtual bool isEphemeral() const =0
Returns true if the engine does not persist data to disk; false otherwise.
virtual bool supportsReadConcernSnapshot() const
Returns true if the storage engine supports the readConcern level "snapshot".
Definition: storage_engine.h:325
virtual StatusWith< std::vector< CollectionIndexNamePair > > reconcileCatalogAndIdents(OperationContext *opCtx)
Drop abandoned idents.
Definition: storage_engine.h:431
virtual void listDatabases(std::vector< std::string > *out) const =0
List the databases stored in this storage engine.
This class allows for the storageEngine to alert the rest of the system about journaled write progres...
Definition: journal_listener.h:48
virtual BSONObj createMetadataOptions(const StorageGlobalParams &params) const =0
Returns a new document suitable for storing in the data directory metadata.
virtual void setJournalListener(JournalListener *jl)=0
Sets a new JournalListener, which is used by the storage engine to alert the rest of the system about...
This reads and write the storage engine metadata file &#39;storage.bson&#39; in the data directory (See –dbp...
Definition: storage_engine_metadata.h:47
virtual boost::optional< Timestamp > getRecoveryTimestamp() const
Returns the stable timestamp that the storage engine recovered to on startup.
Definition: storage_engine.h:354
virtual void setOldestTimestampFromStable()
Uses the current stable timestamp to set the oldest timestamp for which the storage engine must maint...
Definition: storage_engine.h:390
Definition: index_key_validate.h:40
A RecoveryUnit is responsible for ensuring that data is persisted.
Definition: recovery_unit.h:51
virtual StatusWith< Timestamp > recoverToStableTimestamp(OperationContext *opCtx)
Recovers the storage engine state to the last stable timestamp.
Definition: storage_engine.h:345
virtual bool isMmapV1() const
Only MMAPv1 should override this and return true to trigger MMAPv1-specific behavior.
Definition: storage_engine.h:213
virtual Timestamp getAllCommittedTimestamp() const =0
Returns the all committed timestamp.
virtual bool supportsDBLocking() const
Returns whether the storage engine supports locking at a database level.
Definition: storage_engine.h:189
virtual void endBackup(OperationContext *opCtx)
Transitions the storage engine out of backup mode.
Definition: storage_engine.h:272
Definition: database_catalog_entry.h:50
virtual bool supportsCappedCollections() const
Returns whether the storage engine supports capped collections.
Definition: storage_engine.h:196
virtual boost::optional< Timestamp > getLastStableCheckpointTimestamp() const
Returns a timestamp that is guaranteed to be persisted to disk in a checkpoint.
Definition: storage_engine.h:364
std::pair< std::string, std::string > CollectionIndexNamePair
Definition: storage_engine.h:422
OperationContext Database * db
Definition: database_impl.cpp:949
virtual Status closeDatabase(OperationContext *opCtx, StringData db)=0
Closes all file handles associated with a database.
OperationContext Database StringData BSONObj options
Definition: database_impl.cpp:949
The interface for creating new instances of storage engines.
Definition: storage_engine.h:66
virtual bool supportsReadOnly() const
Returns whether the engine supports read-only mode.
Definition: storage_engine.h:136
virtual RecoveryUnit * newRecoveryUnit()=0
Returns a new interface to the storage engine&#39;s recovery unit.
Manages snapshots that can be read from at a later time.
Definition: snapshot_manager.h:47
virtual Status validateCollectionStorageOptions(const BSONObj &options) const
Validates creation options for a collection in the StorageEngine.
Definition: storage_engine.h:91
virtual Status validateMetadata(const StorageEngineMetadata &metadata, const StorageGlobalParams &params) const =0
Validates existing metadata in the data directory against startup options.
virtual Status validateIndexStorageOptions(const BSONObj &options) const
Validates creation options for an index in the StorageEngine.
Definition: storage_engine.h:105
Definition: storage_engine_lock_file.h:41
Collection *const OperationContext *const opCtx
Definition: collection_impl.cpp:80
virtual void setStableTimestamp(Timestamp timestamp)
Sets the highest timestamp at which the storage engine is allowed to take a checkpoint.
Definition: storage_engine.h:372
virtual void setCachePressureForTest(int pressure)
For unit tests only.
Definition: storage_engine.h:413
virtual bool supportsRecoverToStableTimestamp() const
Returns whether the storage engine supports "recover to stable timestamp".
Definition: storage_engine.h:318
The StorageEngine class is the top level interface for creating a new storage engine.
Definition: storage_engine.h:57
virtual DatabaseCatalogEntry * getDatabaseCatalogEntry(OperationContext *opCtx, StringData db)=0
Return the DatabaseCatalogEntry that describes the database indicated by &#39;db&#39;.
virtual SnapshotManager * getSnapshotManager() const
Returns the SnapshotManager for this StorageEngine or NULL if not supported.
Definition: storage_engine.h:302
virtual StringData getCanonicalName() const =0
Returns the name of the storage engine.
virtual bool supportsDocLocking() const =0
Returns whether the storage engine supports its own locking locking below the collection level...
virtual void cleanShutdown()=0
This method will be called before there is a clean shutdown.