Storage Engine API
dur_recovery_unit.h
Go to the documentation of this file.
1 
29 #include <set>
30 #include <string>
31 #include <utility>
32 #include <vector>
33 
34 #include "mongo/base/owned_pointer_vector.h"
36 #include "mongo/platform/compiler.h"
37 
38 #pragma once
39 
40 namespace mongo {
41 
45 class DurRecoveryUnit : public RecoveryUnit {
46 public:
48 
49  void beginUnitOfWork(OperationContext* opCtx) final;
50  void commitUnitOfWork() final;
51  void abortUnitOfWork() final;
52 
53  virtual bool waitUntilDurable();
54 
55  virtual void abandonSnapshot();
56 
57  // The recovery unit takes ownership of change.
58  virtual void registerChange(Change* change);
59 
60  virtual void* writingPtr(void* addr, size_t len);
61 
62  virtual void setRollbackWritesDisabled();
63 
64  virtual SnapshotId getSnapshotId() const {
65  return SnapshotId();
66  }
67 
68  virtual void setOrderedCommit(bool orderedCommit) {}
69 
70 private:
77  void commitChanges();
78 
84 
89  void rollbackChanges();
90 
91 
96  void mergingWritingPtr(char* data, size_t len);
97 
101  void resetChanges();
102 
103  // Changes are ordered from oldest to newest.
104  typedef OwnedPointerVector<Change> Changes;
105  Changes _changes;
106 
107 
108  // Number of pending uncommitted writes. Incremented even if new write is fully covered by
109  // existing writes.
110  size_t _writeCount;
111  // Total size of the pending uncommitted writes.
112  size_t _writeBytes;
113 
118  struct Write {
119  Write(char* addr, int len, int offset) : addr(addr), len(len), offset(offset) {}
120  Write(const Write& rhs) : addr(rhs.addr), len(rhs.len), offset(rhs.offset) {}
121  Write() : addr(0), len(0), offset(0) {}
122  bool operator<(const Write& rhs) const {
123  return addr < rhs.addr;
124  }
125 
126  struct compareEnd {
127  bool operator()(const Write& lhs, const Write& rhs) const {
128  return lhs.addr + lhs.len < rhs.addr + rhs.len;
129  }
130  };
131 
132  char* end() const {
133  return addr + len;
134  }
135 
136  char* addr;
137  int len;
138  int offset; // index into _preimageBuffer
139  };
140 
148  typedef std::set<Write, Write::compareEnd> MergedWrites;
149  MergedWrites _mergedWrites;
150 
151  // Generally it's more efficient to just store pre-images unconditionally and then
152  // sort/eliminate duplicates at commit time. However, this can lead to excessive memory
153  // use in cases involving large indexes arrays, where the same memory is written many
154  // times. To keep the speed for the general case and bound memory use, the first few MB of
155  // pre-images are stored unconditionally, but once the threshold has been exceeded, the
156  // remainder is stored in a more space-efficient datastructure.
157  typedef std::vector<Write> InitialWrites;
158  InitialWrites _initialWrites;
159 
160  std::string _preimageBuffer;
161 
163 
164 
165  // Default is false.
166  // If true, no preimages are tracked. If rollback is subsequently attempted, the process
167  // will abort.
169 };
170 
171 } // namespace mongo
size_t _writeBytes
Definition: dur_recovery_unit.h:112
void commitUnitOfWork() final
Marks the end of a unit of work and commits all changes registered by calls to onCommit or registerCh...
Definition: dur_recovery_unit.cpp:56
int len
Definition: dur_recovery_unit.h:137
virtual SnapshotId getSnapshotId() const
Gets the local SnapshotId.
Definition: dur_recovery_unit.h:64
char * addr
Definition: dur_recovery_unit.h:136
std::set< Write, Write::compareEnd > MergedWrites
Writes are ordered by ending address, so MergedWrites::upper_bound() can find the first overlapping w...
Definition: dur_recovery_unit.h:148
Copyright (C) 2014 MongoDB Inc.
Definition: bson_collection_catalog_entry.cpp:38
std::string _preimageBuffer
Definition: dur_recovery_unit.h:160
size_t _writeCount
Definition: dur_recovery_unit.h:110
InitialWrites _initialWrites
Definition: dur_recovery_unit.h:158
Write(char *addr, int len, int offset)
Definition: dur_recovery_unit.h:119
void commitChanges()
Marks writes for journaling, if enabled, and then commits all other Changes in order.
Definition: dur_recovery_unit.cpp:79
virtual void abandonSnapshot()
If there is an open transaction, it is closed.
Definition: dur_recovery_unit.cpp:74
Definition: dur_recovery_unit.h:126
void mergingWritingPtr(char *data, size_t len)
Version of writingPtr that checks existing writes for overlap and only stores those changes not yet c...
Definition: dur_recovery_unit.cpp:204
bool operator()(const Write &lhs, const Write &rhs) const
Definition: dur_recovery_unit.h:127
std::vector< Write > InitialWrites
Definition: dur_recovery_unit.h:157
Write(const Write &rhs)
Definition: dur_recovery_unit.h:120
std::shared_ptr< void > data
Definition: ephemeral_for_test_record_store_test.cpp:74
DurRecoveryUnit()
Definition: dur_recovery_unit.cpp:48
virtual void setRollbackWritesDisabled()
Sets a flag that declares this RecoveryUnit will skip rolling back writes, for the duration of the cu...
Definition: dur_recovery_unit.cpp:306
char * end() const
Definition: dur_recovery_unit.h:132
OwnedPointerVector< Change > Changes
Definition: dur_recovery_unit.h:104
These are memory writes inside the mmapv1 mmap-ed files.
Definition: dur_recovery_unit.h:118
A RecoveryUnit is responsible for ensuring that data is persisted.
Definition: recovery_unit.h:51
virtual void registerChange(Change *change)
The RecoveryUnit takes ownership of the change.
Definition: dur_recovery_unit.cpp:311
virtual bool waitUntilDurable()
Waits until all commits that happened before this call are durable in the journal.
Definition: dur_recovery_unit.cpp:199
void markWritesForJournaling()
Creates a list of write intents to be journaled, and hands it of to the active DurabilityInterface.
Definition: dur_recovery_unit.cpp:92
void rollbackChanges()
Restores state by rolling back all writes using the saved pre-images, and then rolling back all other...
Definition: dur_recovery_unit.cpp:164
Just pass through to getDur().
Definition: dur_recovery_unit.h:45
void resetChanges()
Reset to a clean state without any uncommitted changes or write.
Definition: dur_recovery_unit.cpp:153
virtual void setOrderedCommit(bool orderedCommit)
Definition: dur_recovery_unit.h:68
A Change is an action that is registerChange()&#39;d while a WriteUnitOfWork exists.
Definition: recovery_unit.h:281
void abortUnitOfWork() final
Marks the end of a unit of work and rolls back all changes registered by calls to onRollback or regis...
Definition: dur_recovery_unit.cpp:67
Definition: snapshot.h:37
Collection *const OperationContext *const opCtx
Definition: collection_impl.cpp:80
bool _rollbackWritesDisabled
Definition: dur_recovery_unit.h:168
void beginUnitOfWork(OperationContext *opCtx) final
Marks the beginning of a unit of work.
Definition: dur_recovery_unit.cpp:51
virtual void * writingPtr(void *addr, size_t len)
Declare that the data at [x, x + len) is being written.
Definition: dur_recovery_unit.cpp:260
Write()
Definition: dur_recovery_unit.h:121
bool operator<(const Write &rhs) const
Definition: dur_recovery_unit.h:122
int offset
Definition: dur_recovery_unit.h:138
bool _inUnitOfWork
Definition: dur_recovery_unit.h:162
Changes _changes
Definition: dur_recovery_unit.h:105
MergedWrites _mergedWrites
Definition: dur_recovery_unit.h:149