Storage Engine API
wiredtiger_recovery_unit.h
Go to the documentation of this file.
1 
29 #pragma once
30 
31 #include <wiredtiger.h>
32 
33 #include <boost/optional.hpp>
34 #include <cstdint>
35 #include <memory>
36 #include <vector>
37 
38 #include "mongo/base/checked_cast.h"
39 #include "mongo/bson/timestamp.h"
40 #include "mongo/db/operation_context.h"
41 #include "mongo/db/record_id.h"
42 #include "mongo/db/repl/read_concern_level.h"
46 #include "mongo/util/timer.h"
47 
48 namespace mongo {
49 
50 class BSONObjBuilder;
51 
52 class WiredTigerRecoveryUnit final : public RecoveryUnit {
53 public:
55 
65 
66  void beginUnitOfWork(OperationContext* opCtx) override;
67  void prepareUnitOfWork() override;
68  void commitUnitOfWork() override;
69  void abortUnitOfWork() override;
70 
71  bool waitUntilDurable() override;
72 
73  bool waitUntilUnjournaledWritesDurable() override;
74 
75  void registerChange(Change* change) override;
76 
77  void abandonSnapshot() override;
78  void preallocateSnapshot() override;
79 
81 
82  boost::optional<Timestamp> getPointInTimeReadTimestamp() const override;
83 
84  SnapshotId getSnapshotId() const override;
85 
86  Status setTimestamp(Timestamp timestamp) override;
87 
88  void setCommitTimestamp(Timestamp timestamp) override;
89 
90  void clearCommitTimestamp() override;
91 
92  Timestamp getCommitTimestamp() override;
93 
94  void setPrepareTimestamp(Timestamp timestamp) override;
95 
96  void setIgnorePrepared(bool ignore) override;
97 
99  boost::optional<Timestamp> provided = boost::none) override;
100 
101  ReadSource getTimestampReadSource() const override;
102 
103  void* writingPtr(void* data, size_t len) override;
104 
105  void setRollbackWritesDisabled() override {}
106 
107  virtual void setOrderedCommit(bool orderedCommit) override {
108  _orderedCommit = orderedCommit;
109  }
110 
111  // ---- WT STUFF
112 
115  _isOplogReader = true;
116  }
117 
122  void beginIdle();
123 
130 
132  return _sessionCache;
133  }
134  bool inActiveTxn() const {
135  return _active;
136  }
137  void assertInActiveTxn() const;
138 
139  static WiredTigerRecoveryUnit* get(OperationContext* opCtx) {
140  return checked_cast<WiredTigerRecoveryUnit*>(opCtx->recoveryUnit());
141  }
142 
143  static void appendGlobalStats(BSONObjBuilder& b);
144 
145 private:
146  void _abort();
147  void _commit();
148 
149  void _ensureSession();
150  void _txnClose(bool commit);
151  void _txnOpen();
152 
158  bool _active;
159  bool _isTimestamped = false;
160 
161  // Specifies which external source to use when setting read timestamps on transactions.
162  ReadSource _timestampReadSource = ReadSource::kNone;
163 
164  // Commits are assumed ordered. Unordered commits are assumed to always need to reserve a
165  // new optime, and thus always call oplogDiskLocRegister() on the record store.
166  bool _orderedCommit = true;
167 
168  // Ignoring prepared transactions will not return prepare conflicts and allow seeing prepared,
169  // but uncommitted data.
172  Timestamp _commitTimestamp;
173  Timestamp _prepareTimestamp;
174  boost::optional<Timestamp> _lastTimestampSet;
175  uint64_t _mySnapshotId;
177  Timestamp _readAtTimestamp;
178  std::unique_ptr<Timer> _timer;
179  bool _isOplogReader = false;
180  typedef std::vector<std::unique_ptr<Change>> Changes;
181  Changes _changes;
182 };
183 
188 public:
189  WiredTigerCursor(const std::string& uri,
190  uint64_t tableID,
191  bool forRecordStore,
192  OperationContext* opCtx);
193 
194  ~WiredTigerCursor();
195 
196 
197  WT_CURSOR* get() const {
198  // TODO(SERVER-16816): assertInActiveTxn();
199  return _cursor;
200  }
201 
202  WT_CURSOR* operator->() const {
203  return get();
204  }
205 
207  return _session;
208  }
209 
210  void reset();
211 
212  void assertInActiveTxn() const {
213  _ru->assertInActiveTxn();
214  }
215 
216 private:
217  uint64_t _tableID;
220  WT_CURSOR* _cursor; // owned, but pulled
221 };
222 }
WiredTigerSession * getSessionNoTxn()
Returns a session without starting a new WT txn on the session.
Definition: wiredtiger_recovery_unit.cpp:215
ReadSource getTimestampReadSource() const override
Definition: wiredtiger_recovery_unit.cpp:486
uint64_t _mySnapshotId
Definition: wiredtiger_recovery_unit.h:175
void beginIdle()
Enter a period of wait or computation during which there are no WT calls.
Definition: wiredtiger_recovery_unit.cpp:490
WiredTigerOplogManager * _oplogManager
Definition: wiredtiger_recovery_unit.h:154
SnapshotId getSnapshotId() const override
Gets the local SnapshotId.
Definition: wiredtiger_recovery_unit.cpp:301
bool waitUntilDurable() override
Waits until all commits that happened before this call are durable in the journal.
Definition: wiredtiger_recovery_unit.cpp:180
void registerChange(Change *change) override
The RecoveryUnit takes ownership of the change.
Definition: wiredtiger_recovery_unit.cpp:199
void _txnOpen()
Definition: wiredtiger_recovery_unit.cpp:336
~WiredTigerRecoveryUnit()
Definition: wiredtiger_recovery_unit.cpp:75
std::vector< std::unique_ptr< Change > > Changes
Definition: wiredtiger_recovery_unit.h:180
virtual void setOrderedCommit(bool orderedCommit) override
Definition: wiredtiger_recovery_unit.h:107
IgnorePrepared
Definition: wiredtiger_begin_transaction_block.h:48
WiredTigerRecoveryUnit(WiredTigerSessionCache *sc)
Definition: wiredtiger_recovery_unit.cpp:64
Timestamp _commitTimestamp
Definition: wiredtiger_recovery_unit.h:172
void setIsOplogReader()
Definition: wiredtiger_recovery_unit.h:114
Copyright (C) 2014 MongoDB Inc.
Definition: bson_collection_catalog_entry.cpp:38
void setRollbackWritesDisabled() override
Sets a flag that declares this RecoveryUnit will skip rolling back writes, for the duration of the cu...
Definition: wiredtiger_recovery_unit.h:105
WT_CURSOR * _cursor
Definition: wiredtiger_recovery_unit.h:220
boost::optional< WiredTigerCursor > _cursor
Definition: wiredtiger_index.cpp:1124
WiredTigerSessionCache * _sessionCache
Definition: wiredtiger_recovery_unit.h:153
WiredTigerSession * getSession()
Definition: wiredtiger_recovery_unit.cpp:208
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
void setIgnorePrepared(bool ignore) override
Sets whether or not to ignore prepared transactions if supported by this storage engine.
Definition: wiredtiger_recovery_unit.cpp:467
void assertInActiveTxn() const
Definition: wiredtiger_recovery_unit.cpp:204
This is a structure that caches 1 cursor for each uri.
Definition: wiredtiger_session_cache.h:65
void prepareUnitOfWork() override
Transitions the active unit of work to the "prepared" state.
Definition: wiredtiger_recovery_unit.cpp:147
Timestamp _majorityCommittedSnapshot
Definition: wiredtiger_recovery_unit.h:176
void setCommitTimestamp(Timestamp timestamp) override
Sets a timestamp that will be assigned to all future writes on this RecoveryUnit until clearCommitTim...
Definition: wiredtiger_recovery_unit.cpp:429
bool _areWriteUnitOfWorksBanned
Definition: wiredtiger_recovery_unit.h:156
std::shared_ptr< void > data
Definition: ephemeral_for_test_record_store_test.cpp:74
void abortUnitOfWork() override
Marks the end of a unit of work and rolls back all changes registered by calls to onRollback or regis...
Definition: wiredtiger_recovery_unit.cpp:168
void preallocateSnapshot() override
Informs the RecoveryUnit that a snapshot will be needed soon, if one was not already established...
Definition: wiredtiger_recovery_unit.cpp:234
bool _isTimestamped
Definition: wiredtiger_recovery_unit.h:159
WT_CURSOR * operator->() const
Definition: wiredtiger_recovery_unit.h:202
void _abort()
Definition: wiredtiger_recovery_unit.cpp:111
Status obtainMajorityCommittedSnapshot() override
Obtains a majority committed snapshot.
Definition: wiredtiger_recovery_unit.cpp:306
This is a smart pointer that wraps a WT_CURSOR and knows how to obtain and get from pool...
Definition: wiredtiger_recovery_unit.h:187
Timestamp _prepareTimestamp
Definition: wiredtiger_recovery_unit.h:173
void setPrepareTimestamp(Timestamp timestamp) override
Sets a prepare timestamp for the current transaction.
Definition: wiredtiger_recovery_unit.cpp:459
WiredTigerSession * getSession()
Definition: wiredtiger_recovery_unit.h:206
void beginUnitOfWork(OperationContext *opCtx) override
Marks the beginning of a unit of work.
Definition: wiredtiger_recovery_unit.cpp:141
WiredTigerSessionCache * getSessionCache()
Definition: wiredtiger_recovery_unit.h:131
bool _orderedCommit
Definition: wiredtiger_recovery_unit.h:166
Definition: wiredtiger_oplog_manager.h:47
Timestamp getCommitTimestamp() override
Definition: wiredtiger_recovery_unit.cpp:444
A RecoveryUnit is responsible for ensuring that data is persisted.
Definition: recovery_unit.h:51
bool waitUntilUnjournaledWritesDurable() override
Unlike waitUntilDurable, this method takes a stable checkpoint, making durable any writes on unjourna...
Definition: wiredtiger_recovery_unit.cpp:188
Changes _changes
Definition: wiredtiger_recovery_unit.h:181
UniqueWiredTigerSession _session
Definition: wiredtiger_recovery_unit.h:155
boost::optional< Timestamp > _lastTimestampSet
Definition: wiredtiger_recovery_unit.h:174
void assertInActiveTxn() const
Definition: wiredtiger_recovery_unit.h:212
WiredTigerBeginTxnBlock::IgnorePrepared _ignorePrepared
Definition: wiredtiger_recovery_unit.h:170
void _commit()
Definition: wiredtiger_recovery_unit.cpp:80
void * writingPtr(void *data, size_t len) override
Declare that the data at [x, x + len) is being written.
Definition: wiredtiger_recovery_unit.cpp:239
boost::optional< Timestamp > getPointInTimeReadTimestamp() const override
Returns the Timestamp being used by this recovery unit or boost::none if not reading from a point in ...
Definition: wiredtiger_recovery_unit.cpp:317
void abandonSnapshot() override
If there is an open transaction, it is closed.
Definition: wiredtiger_recovery_unit.cpp:225
ReadSource _timestampReadSource
Definition: wiredtiger_recovery_unit.h:162
bool inActiveTxn() const
Definition: wiredtiger_recovery_unit.h:134
static void appendGlobalStats(BSONObjBuilder &b)
void setTimestampReadSource(ReadSource source, boost::optional< Timestamp > provided=boost::none) override
Sets which timestamp to use for read transactions.
Definition: wiredtiger_recovery_unit.cpp:472
A Change is an action that is registerChange()&#39;d while a WriteUnitOfWork exists.
Definition: recovery_unit.h:281
void _txnClose(bool commit)
Definition: wiredtiger_recovery_unit.cpp:244
bool _inUnitOfWork
Definition: wiredtiger_recovery_unit.h:157
WiredTigerSession * _session
Definition: wiredtiger_recovery_unit.h:219
Status setTimestamp(Timestamp timestamp) override
Sets a timestamp to assign to future writes in a transaction.
Definition: wiredtiger_recovery_unit.cpp:405
bool _isOplogReader
Definition: wiredtiger_recovery_unit.h:179
bool _active
Definition: wiredtiger_recovery_unit.h:158
Timestamp _readAtTimestamp
Definition: wiredtiger_recovery_unit.h:177
Definition: snapshot.h:37
This cache implements a shared pool of WiredTiger sessions with the goal to amortize the cost of sess...
Definition: wiredtiger_session_cache.h:156
Collection *const OperationContext *const opCtx
Definition: collection_impl.cpp:80
uint64_t _tableID
Definition: wiredtiger_recovery_unit.h:217
std::unique_ptr< WiredTigerSession, typename WiredTigerSessionCache::WiredTigerSessionDeleter > UniqueWiredTigerSession
A unique handle type for WiredTigerSession pointers obtained from a WiredTigerSessionCache.
Definition: wiredtiger_session_cache.h:305
void clearCommitTimestamp() override
Definition: wiredtiger_recovery_unit.cpp:448
void commitUnitOfWork() override
Marks the end of a unit of work and commits all changes registered by calls to onCommit or registerCh...
Definition: wiredtiger_recovery_unit.cpp:162
std::unique_ptr< Timer > _timer
Definition: wiredtiger_recovery_unit.h:178
Definition: wiredtiger_recovery_unit.h:52
std::string uri
Definition: wiredtiger_standard_record_store_test.cpp:367
void _ensureSession()
Definition: wiredtiger_recovery_unit.cpp:174
WiredTigerRecoveryUnit * _ru
Definition: wiredtiger_recovery_unit.h:218