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 {
248  };
249 
259  virtual void setTimestampReadSource(ReadSource source,
260  boost::optional<Timestamp> provided = boost::none) {}
261 
263  return ReadSource::kNone;
264  };
265 
281  class Change {
282  public:
283  virtual ~Change() {}
284 
285  virtual void rollback() = 0;
286  virtual void commit(boost::optional<Timestamp> commitTime) = 0;
287  };
288 
298  virtual void registerChange(Change* change) = 0;
299 
305  template <typename Callback>
306  void onRollback(Callback callback) {
307  class OnRollbackChange final : public Change {
308  public:
309  OnRollbackChange(Callback&& callback) : _callback(std::move(callback)) {}
310  void rollback() final {
311  _callback();
312  }
313  void commit(boost::optional<Timestamp>) final {}
314 
315  private:
316  Callback _callback;
317  };
318 
319  registerChange(new OnRollbackChange(std::move(callback)));
320  }
321 
327  template <typename Callback>
328  void onCommit(Callback callback) {
329  class OnCommitChange final : public Change {
330  public:
331  OnCommitChange(Callback&& callback) : _callback(std::move(callback)) {}
332  void rollback() final {}
333  void commit(boost::optional<Timestamp> commitTime) final {
334  _callback(commitTime);
335  }
336 
337  private:
338  Callback _callback;
339  };
340 
341  registerChange(new OnCommitChange(std::move(callback)));
342  }
343 
344  //
345  // The remaining methods probably belong on DurRecoveryUnit rather than on the interface.
346  //
347 
351  virtual void* writingPtr(void* data, size_t len) = 0;
352 
353  //
354  // Syntactic sugar
355  //
356 
360  inline int& writingInt(int& d) {
361  return *writing(&d);
362  }
363 
367  template <typename T>
368  inline T* writing(T* x) {
369  writingPtr(x, sizeof(T));
370  return x;
371  }
372 
386  virtual void setRollbackWritesDisabled() = 0;
387 
388  virtual void setOrderedCommit(bool orderedCommit) = 0;
389 
390 protected:
392 };
393 
394 } // namespace mongo
void onRollback(Callback callback)
Registers a callback to be called if the current WriteUnitOfWork rolls back.
Definition: recovery_unit.h:306
virtual void rollback()=0
T * writing(T *x)
A templated helper for writingPtr.
Definition: recovery_unit.h:368
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
OperationContext Database StringData BSONObj CollectionOptions::ParseKind bool const BSONObj &idIndex Status
Definition: database_impl.cpp:956
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:283
Read from the majority all-commmitted timestamp.
Definition: recovery_unit.h:239
virtual void beginUnitOfWork(OperationContext *opCtx)=0
Marks the beginning of a unit of work.
RecoveryUnit()
Definition: recovery_unit.h:391
virtual ReadSource getTimestampReadSource() const
Definition: recovery_unit.h:262
int & writingInt(int &d)
Declare write intent for an int.
Definition: recovery_unit.h:360
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
virtual SnapshotId getSnapshotId() const =0
Gets the local SnapshotId.
Read from the last applied timestamp.
Definition: recovery_unit.h:242
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:328
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:245
Read from the timestamp provided to setTimestampReadSource.
Definition: recovery_unit.h:247
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()&#39;d while a WriteUnitOfWork exists.
Definition: recovery_unit.h:281
virtual void * writingPtr(void *data, size_t len)=0
Declare that the data at [x, x + len) is being written.
Do not read from a timestamp.
Definition: recovery_unit.h:237
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.
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:259