diff options
Diffstat (limited to 'src/mongo/db/storage/mmap_v1/dur_journal_writer.h')
-rw-r--r-- | src/mongo/db/storage/mmap_v1/dur_journal_writer.h | 264 |
1 files changed, 136 insertions, 128 deletions
diff --git a/src/mongo/db/storage/mmap_v1/dur_journal_writer.h b/src/mongo/db/storage/mmap_v1/dur_journal_writer.h index 6ac91de6532..2f738cbb380 100644 --- a/src/mongo/db/storage/mmap_v1/dur_journal_writer.h +++ b/src/mongo/db/storage/mmap_v1/dur_journal_writer.h @@ -38,150 +38,158 @@ namespace mongo { namespace dur { +/** + * Manages the thread and queues used for writing the journal to disk and notify parties with + * are waiting on the write concern. + * + * NOTE: Not thread-safe and must not be used from more than one thread. + */ +class JournalWriter { + MONGO_DISALLOW_COPYING(JournalWriter); + +public: /** - * Manages the thread and queues used for writing the journal to disk and notify parties with - * are waiting on the write concern. - * - * NOTE: Not thread-safe and must not be used from more than one thread. + * Stores the memory and the header for a complete journal buffer which is pending to be + * written by the journal writer thread. */ - class JournalWriter { - MONGO_DISALLOW_COPYING(JournalWriter); + class Buffer { public: + Buffer(size_t initialSize); + ~Buffer(); + + JSectHeader& getHeader() { + return _header; + } + AlignedBuilder& getBuilder() { + return _builder; + } - /** - * Stores the memory and the header for a complete journal buffer which is pending to be - * written by the journal writer thread. - */ - class Buffer { - public: - Buffer(size_t initialSize); - ~Buffer(); - - JSectHeader& getHeader() { return _header; } - AlignedBuilder& getBuilder() { return _builder; } - - void setNoop() { _isNoop = true; } - - private: - friend class BufferGuard; - friend class JournalWriter; - - - void _assertEmpty(); - void _reset(); - void _setShutdown() { _isShutdown = true; } - - // Specifies the commit number which flushing this buffer would notify. This value is - // zero, if there is no data to be flushed or if the buffer is noop/shutdown. - NotifyAll::When _commitNumber; - - // Special buffer that's posted when there is nothing to be written to the journal, - // but we want to order a notification so it happens after all other writes have - // completed. - bool _isNoop; - - // Special buffer that's posted when the receiving thread must terminate. This should - // be the last entry posted to the queue and the commit number should be zero. - bool _isShutdown; - - JSectHeader _header; - AlignedBuilder _builder; - }; - - - /** - * Initializes the journal writer. - * - * @param commitNotify Notification object to be called after journal entries have been - * written to disk. The caller retains ownership and the notify object must outlive - * the journal writer object. - * @param applyToDataFilesNotify Notification object to be called after journal entries - * have been applied to the shared view. This means that if the shared view were to be - * flushed at this point, the journal files before this point are not necessary. The - * caller retains ownership and the notify object must outlive the journal writer - * object. - * @param numBuffers How many buffers to create to hold outstanding writes. If there are - * more than this number of journal writes that have not completed, the write calls - * will block. - */ - JournalWriter(NotifyAll* commitNotify, NotifyAll* applyToDataFilesNotify, size_t numBuffers); - ~JournalWriter(); - - /** - * Allocates buffer memory and starts the journal writer thread. - */ - void start(); - - /** - * Terminates the journal writer thread and frees memory for the buffers. Must not be - * called if there are any pending journal writes. - */ - void shutdown(); - - /** - * Asserts that there are no pending journal writes. - */ - void assertIdle(); - - /** - * Obtains a new empty buffer into which a journal entry should be written. - * - * This method may block if there are no free buffers. - * - * The caller does not own the buffer and needs to "return" it to the writer by calling - * writeBuffer. Buffers with data on them should never be discarded until they are written. - */ - Buffer* newBuffer(); - - /** - * Requests that the specified buffer be written asynchronously. - * - * This method may block if there are too many outstanding unwritten buffers. - * - * @param buffer Buffer entry to be written. The buffer object must not be used anymore - * after it has been given to this function. - * @param commitNumber What commit number to be notified once the buffer has been written - * to disk. - */ - void writeBuffer(Buffer* buffer, NotifyAll::When commitNumber); - - /** - * Ensures that all previously submitted write requests complete. This call is blocking. - */ - void flush(); + void setNoop() { + _isNoop = true; + } private: friend class BufferGuard; + friend class JournalWriter; - typedef BlockingQueue<Buffer*> BufferQueue; - // Start all buffers with 4MB of size - enum { InitialBufferSizeBytes = 4 * 1024 * 1024 }; + void _assertEmpty(); + void _reset(); + void _setShutdown() { + _isShutdown = true; + } + // Specifies the commit number which flushing this buffer would notify. This value is + // zero, if there is no data to be flushed or if the buffer is noop/shutdown. + NotifyAll::When _commitNumber; - void _journalWriterThread(); + // Special buffer that's posted when there is nothing to be written to the journal, + // but we want to order a notification so it happens after all other writes have + // completed. + bool _isNoop; + // Special buffer that's posted when the receiving thread must terminate. This should + // be the last entry posted to the queue and the commit number should be zero. + bool _isShutdown; - // This gets notified as journal buffers are written. It is not owned and needs to outlive - // the journal writer object. - NotifyAll* const _commitNotify; + JSectHeader _header; + AlignedBuilder _builder; + }; - // This gets notified as journal buffers are done being applied to the shared view - NotifyAll* const _applyToDataFilesNotify; - // Wraps and controls the journal writer thread - stdx::thread _journalWriterThreadHandle; + /** + * Initializes the journal writer. + * + * @param commitNotify Notification object to be called after journal entries have been + * written to disk. The caller retains ownership and the notify object must outlive + * the journal writer object. + * @param applyToDataFilesNotify Notification object to be called after journal entries + * have been applied to the shared view. This means that if the shared view were to be + * flushed at this point, the journal files before this point are not necessary. The + * caller retains ownership and the notify object must outlive the journal writer + * object. + * @param numBuffers How many buffers to create to hold outstanding writes. If there are + * more than this number of journal writes that have not completed, the write calls + * will block. + */ + JournalWriter(NotifyAll* commitNotify, NotifyAll* applyToDataFilesNotify, size_t numBuffers); + ~JournalWriter(); - // Indicates that shutdown has been requested. Used for idempotency of the shutdown call. - bool _shutdownRequested; + /** + * Allocates buffer memory and starts the journal writer thread. + */ + void start(); - // Queue of buffers, which need to be written by the journal writer thread - BufferQueue _journalQueue; - NotifyAll::When _lastCommitNumber; + /** + * Terminates the journal writer thread and frees memory for the buffers. Must not be + * called if there are any pending journal writes. + */ + void shutdown(); - // Queue of buffers, whose write has been completed by the journal writer thread. - BufferQueue _readyQueue; - }; + /** + * Asserts that there are no pending journal writes. + */ + void assertIdle(); + + /** + * Obtains a new empty buffer into which a journal entry should be written. + * + * This method may block if there are no free buffers. + * + * The caller does not own the buffer and needs to "return" it to the writer by calling + * writeBuffer. Buffers with data on them should never be discarded until they are written. + */ + Buffer* newBuffer(); + + /** + * Requests that the specified buffer be written asynchronously. + * + * This method may block if there are too many outstanding unwritten buffers. + * + * @param buffer Buffer entry to be written. The buffer object must not be used anymore + * after it has been given to this function. + * @param commitNumber What commit number to be notified once the buffer has been written + * to disk. + */ + void writeBuffer(Buffer* buffer, NotifyAll::When commitNumber); + + /** + * Ensures that all previously submitted write requests complete. This call is blocking. + */ + void flush(); + +private: + friend class BufferGuard; + + typedef BlockingQueue<Buffer*> BufferQueue; + + // Start all buffers with 4MB of size + enum { InitialBufferSizeBytes = 4 * 1024 * 1024 }; + + + void _journalWriterThread(); + + + // This gets notified as journal buffers are written. It is not owned and needs to outlive + // the journal writer object. + NotifyAll* const _commitNotify; + + // This gets notified as journal buffers are done being applied to the shared view + NotifyAll* const _applyToDataFilesNotify; + + // Wraps and controls the journal writer thread + stdx::thread _journalWriterThreadHandle; + + // Indicates that shutdown has been requested. Used for idempotency of the shutdown call. + bool _shutdownRequested; + + // Queue of buffers, which need to be written by the journal writer thread + BufferQueue _journalQueue; + NotifyAll::When _lastCommitNumber; + + // Queue of buffers, whose write has been completed by the journal writer thread. + BufferQueue _readyQueue; +}; -} // namespace dur -} // namespace mongo +} // namespace dur +} // namespace mongo |