Storage Engine API
dur_journal_writer.h
Go to the documentation of this file.
1 
29 #pragma once
30 
31 #include "mongo/base/disallow_copying.h"
36 #include "mongo/stdx/thread.h"
37 #include "mongo/util/queue.h"
38 
39 namespace mongo {
40 namespace dur {
41 
50 
51 public:
56  class Buffer {
57  public:
58  Buffer(size_t initialSize);
59  ~Buffer();
60 
62  return _header;
63  }
65  return _builder;
66  }
67 
68  void setNoop() {
69  _isNoop = true;
70  }
71 
73 
74  private:
75  friend class BufferGuard;
76  friend class JournalWriter;
77 
78 
79  void _assertEmpty();
80  void _reset();
81  void _setShutdown() {
82  _isShutdown = true;
83  }
84 
85  // Specifies the commit number which flushing this buffer would notify. This value is
86  // zero, if there is no data to be flushed or if the buffer is noop/shutdown.
88 
89  // Special buffer that's posted when there is nothing to be written to the journal,
90  // but we want to order a notification so it happens after all other writes have
91  // completed.
92  bool _isNoop;
93 
94  // Special buffer that's posted when the receiving thread must terminate. This should
95  // be the last entry posted to the queue and the commit number should be zero.
97 
100  };
101 
102 
118  JournalWriter(CommitNotifier* commitNotify,
119  CommitNotifier* applyToDataFilesNotify,
120  size_t numBuffers);
121  ~JournalWriter();
122 
126  void start();
127 
132  void shutdown();
133 
137  void assertIdle();
138 
147  Buffer* newBuffer();
148 
159  void writeBuffer(Buffer* buffer, CommitNotifier::When commitNumber);
160 
164  void flush();
165 
166 private:
167  friend class BufferGuard;
168 
169  typedef BlockingQueue<Buffer*> BufferQueue;
170 
171  // Start all buffers with 4MB of size
172  enum { InitialBufferSizeBytes = 4 * 1024 * 1024 };
173 
174 
175  void _journalWriterThread();
176 
177 
178  // This gets notified as journal buffers are written. It is not owned and needs to outlive
179  // the journal writer object.
181 
182  // This gets notified as journal buffers are done being applied to the shared view
184 
185  // Wraps and controls the journal writer thread
187 
188  // Indicates that shutdown has been requested. Used for idempotency of the shutdown call.
190 
191  // Queue of buffers, which need to be written by the journal writer thread
192  BufferQueue _journalQueue;
194 
195  // Queue of buffers, whose write has been completed by the journal writer thread.
196  BufferQueue _readyQueue;
197 };
198 
199 } // namespace dur
200 } // namespace mongo
MONGO_DISALLOW_COPYING(JournalWriter)
CommitNotifier *const _commitNotify
Definition: dur_journal_writer.h:180
void shutdown()
Terminates the journal writer thread and frees memory for the buffers.
Definition: dur_journal_writer.cpp:147
Stores the memory and the header for a complete journal buffer which is pending to be written by the ...
Definition: dur_journal_writer.h:56
JSectHeader _header
Definition: dur_journal_writer.h:98
Establishes a synchronization point between threads.
Definition: commit_notifier.h:40
void _reset()
Definition: dur_journal_writer.cpp:300
Manages the thread and queues used for writing the journal to disk and notify parties with are waitin...
Definition: dur_journal_writer.h:48
Copyright (C) 2014 MongoDB Inc.
Definition: bson_collection_catalog_entry.cpp:38
bool _shutdownRequested
Definition: dur_journal_writer.h:189
~Buffer()
Definition: dur_journal_writer.cpp:291
Used inside the journal writer thread to ensure that used buffers are cleaned up properly.
Definition: dur_journal_writer.cpp:83
friend class JournalWriter
Definition: dur_journal_writer.h:76
"Section" header.
Definition: dur_journalformat.h:91
void setNoop()
Definition: dur_journal_writer.h:68
void writeBuffer(Buffer *buffer, CommitNotifier::When commitNumber)
Requests that the specified buffer be written asynchronously.
Definition: dur_journal_writer.cpp:186
BlockingQueue< Buffer * > BufferQueue
Definition: dur_journal_writer.h:169
JournalListener::Token journalListenerToken
Definition: dur_journal_writer.h:72
a page-aligned BufBuilder.
Definition: aligned_builder.h:37
BufferQueue _readyQueue
Definition: dur_journal_writer.h:196
void _journalWriterThread()
Definition: dur_journal_writer.cpp:210
Buffer(size_t initialSize)
Definition: dur_journal_writer.cpp:288
CommitNotifier *const _applyToDataFilesNotify
Definition: dur_journal_writer.h:183
bool _isNoop
Definition: dur_journal_writer.h:92
CommitNotifier::When _lastCommitNumber
Definition: dur_journal_writer.h:193
void assertIdle()
Asserts that there are no pending journal writes.
Definition: dur_journal_writer.cpp:173
~JournalWriter()
Definition: dur_journal_writer.cpp:127
void _assertEmpty()
Definition: dur_journal_writer.cpp:295
bool _isShutdown
Definition: dur_journal_writer.h:96
void flush()
Ensures that all previously submitted write requests complete.
Definition: dur_journal_writer.cpp:195
BufferQueue _journalQueue
Definition: dur_journal_writer.h:192
Buffer * newBuffer()
Obtains a new empty buffer into which a journal entry should be written.
Definition: dur_journal_writer.cpp:179
AlignedBuilder _builder
Definition: dur_journal_writer.h:99
void start()
Allocates buffer memory and starts the journal writer thread.
Definition: dur_journal_writer.cpp:133
unsigned long long When
Definition: commit_notifier.h:44
repl::OpTime Token
Definition: journal_listener.h:50
AlignedBuilder & getBuilder()
Definition: dur_journal_writer.h:64
void _setShutdown()
Definition: dur_journal_writer.h:81
stdx::thread _journalWriterThreadHandle
Definition: dur_journal_writer.h:186
Definition: dur_journal_writer.h:172
CommitNotifier::When _commitNumber
Definition: dur_journal_writer.h:87
JSectHeader & getHeader()
Definition: dur_journal_writer.h:61