Storage Engine API
dur_commitjob.h
Go to the documentation of this file.
1 
29 #pragma once
30 
31 
33 #include "mongo/util/concurrency/mutex.h"
34 
35 namespace mongo {
36 namespace dur {
37 
38 typedef std::vector<std::shared_ptr<DurOp>> DurOpsVector;
39 
45 struct WriteIntent {
46  WriteIntent() : p(0) {}
47  WriteIntent(void* a, unsigned b) : p((char*)a + b), len(b) {}
48 
49  void* start() const {
50  return (char*)p - len;
51  }
52  void* end() const {
53  return p;
54  }
55  unsigned length() const {
56  return len;
57  }
58  bool operator<(const WriteIntent& rhs) const {
59  return end() < rhs.end();
60  }
61 
62  bool overlaps(const WriteIntent& rhs) const {
63  return (start() <= rhs.end() && end() >= rhs.start());
64  }
65 
66  bool contains(const WriteIntent& rhs) const {
67  return (start() <= rhs.start() && end() >= rhs.end());
68  }
69 
70  // merge into me:
71  void absorb(const WriteIntent& other);
72 
73  friend std::ostream& operator<<(std::ostream& out, const WriteIntent& wi) {
74  return (out << "p: " << wi.p << " end: " << wi.end() << " len: " << wi.len);
75  }
76 
77 private:
78  void* p; // intent to write up to p
79  unsigned len; // up to this len
80 };
81 
82 typedef std::vector<WriteIntent> WriteIntentsVector;
83 
84 
89 template <int Prime>
90 class Already {
91  MONGO_DISALLOW_COPYING(Already);
92 
93 public:
94  Already() {
95  clear();
96  }
97 
98  void clear() {
99  memset(this, 0, sizeof(*this));
100  }
101 
108  bool checkAndSet(void* p, int len) {
109  const unsigned x = hashPointer(p);
110  std::pair<void*, int>& nd = nodes[x % Prime];
111 
112  if (nd.first == p) {
113  if (nd.second < len) {
114  nd.second = len;
115  return false; // haven't indicated this len yet
116  }
117  return true; // already indicated
118  }
119 
120  nd.first = p;
121  nd.second = len;
122  return false; // a new set
123  }
124 
125 private:
126  static unsigned hashPointer(void* v) {
127  unsigned x = 0;
128  unsigned char* p = (unsigned char*)&v;
129  for (unsigned i = 0; i < sizeof(void*); i++) {
130  x = x * 131 + p[i];
131  }
132  return x;
133  }
134 
135  std::pair<void*, int> nodes[Prime];
136 };
137 
138 
142 class CommitJob {
143  MONGO_DISALLOW_COPYING(CommitJob);
144 
145 public:
146  CommitJob();
147  ~CommitJob();
148 
152  void noteOp(std::shared_ptr<DurOp> p);
153 
159  void note(void* p, int len);
160 
164  bool hasWritten() const {
165  return _hasWritten;
166  }
167 
172  void committingReset();
173 
177  size_t bytes() const {
178  return _bytes;
179  }
180 
186  const WriteIntentsVector& getIntentsSorted() {
187  sort(_intents.begin(), _intents.end());
188  return _intents;
189  }
190 
191  const DurOpsVector& ops() const {
192  return _durOps;
193  }
194 
195  SimpleMutex groupCommitMutex;
196 
197 private:
198  void _insertWriteIntent(void* p, int len) {
199  _intents.push_back(WriteIntent(p, len));
200  }
201 
202 
203  // Whether we put write intents or durops
205 
206  // Write intents along with a bitmask for whether we have already noted them
208  WriteIntentsVector _intents;
209 
210  // All the ops other than basic writes
211  DurOpsVector _durOps;
212 
213  // Used to count the private map used bytes. Note that _lastNotedPos doesn't reset with
214  // each commit, but that is ok we aren't being that precise.
216  size_t _bytes;
217 
218  // Warning logging for large commits
219  uint64_t _lastComplainMs;
220  unsigned _complains;
221 };
222 
223 } // namespace "dur"
224 } // namespace "mongo"
bool hasWritten() const
When this value is false we don&#39;t have to do any group commit.
Definition: dur_commitjob.h:164
static unsigned hashPointer(void *v)
Definition: dur_commitjob.h:126
size_t _bytes
Definition: dur_commitjob.h:216
size_t bytes() const
We check how much written and if it is getting to be a lot, we commit sooner.
Definition: dur_commitjob.h:177
friend std::ostream & operator<<(std::ostream &out, const WriteIntent &wi)
Definition: dur_commitjob.h:73
Tracks all write operations on the private view so they can be journaled.
Definition: dur_commitjob.h:142
unsigned len
Definition: dur_commitjob.h:79
void absorb(const WriteIntent &other)
Definition: dur_commitjob.cpp:54
uint64_t _lastComplainMs
Definition: dur_commitjob.h:219
Copyright (C) 2014 MongoDB Inc.
Definition: bson_collection_catalog_entry.cpp:38
SimpleMutex groupCommitMutex
Definition: dur_commitjob.h:195
void _insertWriteIntent(void *p, int len)
Definition: dur_commitjob.h:198
unsigned _complains
Definition: dur_commitjob.h:220
DurOpsVector _durOps
Definition: dur_commitjob.h:211
bool overlaps(const WriteIntent &rhs) const
Definition: dur_commitjob.h:62
Already()
Definition: dur_commitjob.h:94
bool checkAndSet(void *p, int len)
Checks if we have Already recorded/indicated our write intent for this region of memory and automatic...
Definition: dur_commitjob.h:108
Declaration of an intent to write to a region of a memory mapped view.
Definition: dur_commitjob.h:45
void clear()
Definition: dur_commitjob.h:98
const WriteIntentsVector & getIntentsSorted()
Sorts the internal list of write intents so that overlapping and duplicate items can be merged...
Definition: dur_commitjob.h:186
Already< 127 > _alreadyNoted
Definition: dur_commitjob.h:207
WriteIntentsVector _intents
Definition: dur_commitjob.h:208
std::vector< WriteIntent > WriteIntentsVector
Definition: dur_commitjob.h:82
size_t _lastNotedPos
Definition: dur_commitjob.h:215
Bitmap to remember things we have already marked for journaling.
Definition: dur_commitjob.h:90
void * start() const
Definition: dur_commitjob.h:49
const DurOpsVector & ops() const
Definition: dur_commitjob.h:191
bool contains(const WriteIntent &rhs) const
Definition: dur_commitjob.h:66
WriteIntent(void *a, unsigned b)
Definition: dur_commitjob.h:47
void * p
Definition: dur_commitjob.h:78
bool _hasWritten
Definition: dur_commitjob.h:204
unsigned length() const
Definition: dur_commitjob.h:55
void * end() const
Definition: dur_commitjob.h:52
bool operator<(const WriteIntent &rhs) const
Definition: dur_commitjob.h:58
WriteIntent()
Definition: dur_commitjob.h:46
std::vector< std::shared_ptr< DurOp > > DurOpsVector
Definition: dur_commitjob.h:38