Storage Engine API
recovery_unit.h
Go to the documentation of this file.
1 
29 #pragma once
30 
31 #include <cstdint>
32 #include <stdlib.h>
33 #include <string>
34 
35 #include "mongo/base/disallow_copying.h"
36 #include "mongo/base/status.h"
37 #include "mongo/bson/timestamp.h"
38 #include "mongo/db/repl/read_concern_level.h"
39 #include "mongo/db/repl/replication_coordinator.h"
41 
42 namespace mongo {
43 
44 class BSONObjBuilder;
45 class OperationContext;
46 
51 class RecoveryUnit {
53 
54 public:
55  virtual ~RecoveryUnit() {}
56 
63  virtual void beginUnitOfWork(OperationContext* opCtx) = 0;
64 
71  virtual void commitUnitOfWork() = 0;
72 
80  virtual void abortUnitOfWork() = 0;
81 
91  virtual void prepareUnitOfWork() {
92  uasserted(ErrorCodes::CommandNotSupported,
93  "This storage engine does not support prepared transactions");
94  }
95 
96 
101  virtual void setIgnorePrepared(bool ignore) {}
102 
111  virtual bool waitUntilDurable() = 0;
112 
125  return waitUntilDurable();
126  }
127 
132  virtual void abandonSnapshot() = 0;
133 
140  virtual void preallocateSnapshot() {}
141 
157  return {ErrorCodes::CommandNotSupported,
158  "Current storage engine does not support majority readConcerns"};
159  }
160 
173  virtual boost::optional<Timestamp> getPointInTimeReadTimestamp() const {
174  return boost::none;
175  }
176 
184  virtual SnapshotId getSnapshotId() const = 0;
185 
199  virtual Status setTimestamp(Timestamp timestamp) {
200  return Status::OK();
201  }
202 
210  virtual void setCommitTimestamp(Timestamp timestamp) {}
211 
212  virtual void clearCommitTimestamp() {}
213 
214  virtual Timestamp getCommitTimestamp() {
215  return {};
216  }
217 
226  virtual void setPrepareTimestamp(Timestamp timestamp) {
227  uasserted(ErrorCodes::CommandNotSupported,
228  "This storage engine does not support prepared transactions");
229  }
230 
235  enum ReadSource {
262  };
263 
273  virtual void setTimestampReadSource(ReadSource source,
274  boost::optional<Timestamp> provided = boost::none) {}
275 
277  return ReadSource::kUnset;
278  };
279 
295  class Change {
296  public:
297  virtual ~Change() {}
298 
299  virtual void rollback() = 0;
300  virtual void commit(boost::optional<Timestamp> commitTime) = 0;
301  };
302 
312  virtual void registerChange(Change* change) = 0;
313 
319  template <typename Callback>
320  void onRollback(Callback callback) {
321  class OnRollbackChange final : public Change {
322  public:
323  OnRollbackChange(Callback&& callback) : _callback(std::move(callback)) {}
324  void rollback() final {
325  _callback();
326  }
327  void commit(boost::optional<Timestamp>) final {}
328 
329  private:
330  Callback _callback;
331  };
332 
333  registerChange(new OnRollbackChange(std::move(callback)));
334  }
335 
341  template <typename Callback>
342  void onCommit(Callback callback) {
343  class OnCommitChange final : public Change {
344  public:
345  OnCommitChange(Callback&& callback) : _callback(std::move(callback)) {}
346  void rollback() final {}
347  void commit(boost::optional<Timestamp> commitTime) final {
348  _callback(commitTime);
349  }
350 
351  private:
352  Callback _callback;
353  };
354 
355  registerChange(new OnCommitChange(std::move(callback)));
356  }
357 
358  //
359  // The remaining methods probably belong on DurRecoveryUnit rather than on the interface.
360  //
361 
365  virtual void* writingPtr(void* data, size_t len) = 0;
366 
367  //
368  // Syntactic sugar
369  //
370 
374  inline int& writingInt(int& d) {
375  return *writing(&d);
376  }
377 
381  template <typename T>
382  inline T* writing(T* x) {
383  writingPtr(x, sizeof(T));
384  return x;
385  }
386 
400  virtual void setRollbackWritesDisabled() = 0;
401 
402  virtual void setOrderedCommit(bool orderedCommit) = 0;
403 
404 protected:
406 };
407 
408 } // namespace mongo
void onRollback(Callback callback)
Registers a callback to be called if the current WriteUnitOfWork rolls back.
Definition: recovery_unit.h:320
virtual void rollback()=0
T * writing(T *x)
A templated helper for writingPtr.
Definition: recovery_unit.h:382
virtual void setRollbackWritesDisabled()=0
Sets a flag that declares this RecoveryUnit will skip rolling back writes, for the duration of the cu...
Copyright (C) 2014 MongoDB Inc.
Definition: bson_collection_catalog_entry.cpp:38
virtual void abandonSnapshot()=0
If there is an open transaction, it is closed.
virtual bool waitUntilUnjournaledWritesDurable()
Unlike waitUntilDurable, this method takes a stable checkpoint, making durable any writes on unjourna...
Definition: recovery_unit.h:124
ReadSource
The ReadSource indicates which exteral or provided timestamp to read from for future transactions.
Definition: recovery_unit.h:235
virtual ~Change()
Definition: recovery_unit.h:297
Read from the majority all-commmitted timestamp.
Definition: recovery_unit.h:247
virtual void beginUnitOfWork(OperationContext *opCtx)=0
Marks the beginning of a unit of work.
RecoveryUnit()
Definition: recovery_unit.h:405
virtual ReadSource getTimestampReadSource() const
Definition: recovery_unit.h:276
Do not read from a timestamp.
Definition: recovery_unit.h:239
int & writingInt(int &d)
Declare write intent for an int.
Definition: recovery_unit.h:374
virtual Timestamp getCommitTimestamp()
Definition: recovery_unit.h:214
virtual Status obtainMajorityCommittedSnapshot()
Obtains a majority committed snapshot.
Definition: recovery_unit.h:156
virtual Status setTimestamp(Timestamp timestamp)
Sets a timestamp to assign to future writes in a transaction.
Definition: recovery_unit.h:199
std::shared_ptr< void > data
Definition: ephemeral_for_test_record_store_test.cpp:74
OperationContext Database StringData CollectionOptions bool const BSONObj &idIndex Status
Definition: database_impl.cpp:955
virtual SnapshotId getSnapshotId() const =0
Gets the local SnapshotId.
Read from the last applied timestamp.
Definition: recovery_unit.h:252
virtual void commit(boost::optional< Timestamp > commitTime)=0
A RecoveryUnit is responsible for ensuring that data is persisted.
Definition: recovery_unit.h:51
void onCommit(Callback callback)
Registers a callback to be called if the current WriteUnitOfWork commits.
Definition: recovery_unit.h:342
virtual void abortUnitOfWork()=0
Marks the end of a unit of work and rolls back all changes registered by calls to onRollback or regis...
virtual void commitUnitOfWork()=0
Marks the end of a unit of work and commits all changes registered by calls to onCommit or registerCh...
virtual void prepareUnitOfWork()
Transitions the active unit of work to the "prepared" state.
Definition: recovery_unit.h:91
Read from the last applied timestamp.
Definition: recovery_unit.h:257
Read from the timestamp provided to setTimestampReadSource.
Definition: recovery_unit.h:261
virtual void setIgnorePrepared(bool ignore)
Sets whether or not to ignore prepared transactions if supported by this storage engine.
Definition: recovery_unit.h:101
A Change is an action that is registerChange()'d while a WriteUnitOfWork exists.
Definition: recovery_unit.h:295
virtual void * writingPtr(void *data, size_t len)=0
Declare that the data at [x, x + len) is being written.
virtual ~RecoveryUnit()
Definition: recovery_unit.h:55
virtual void clearCommitTimestamp()
Definition: recovery_unit.h:212
virtual void setPrepareTimestamp(Timestamp timestamp)
Sets a prepare timestamp for the current transaction.
Definition: recovery_unit.h:226
virtual void setCommitTimestamp(Timestamp timestamp)
Sets a timestamp that will be assigned to all future writes on this RecoveryUnit until clearCommitTim...
Definition: recovery_unit.h:210
Definition: snapshot.h:37
virtual boost::optional< Timestamp > getPointInTimeReadTimestamp() const
Returns the Timestamp being used by this recovery unit or boost::none if not reading from a point in ...
Definition: recovery_unit.h:173
virtual void preallocateSnapshot()
Informs the RecoveryUnit that a snapshot will be needed soon, if one was not already established.
Definition: recovery_unit.h:140
Collection *const OperationContext *const opCtx
Definition: collection_impl.cpp:80
virtual bool waitUntilDurable()=0
Waits until all commits that happened before this call are durable in the journal.
Read without a timestamp explicitly.
Definition: recovery_unit.h:243
virtual void registerChange(Change *change)=0
The RecoveryUnit takes ownership of the change.
virtual void setOrderedCommit(bool orderedCommit)=0
MONGO_DISALLOW_COPYING(RecoveryUnit)
virtual void setTimestampReadSource(ReadSource source, boost::optional< Timestamp > provided=boost::none)
Sets which timestamp to use for read transactions.
Definition: recovery_unit.h:273