libfoedus-core
FOEDUS Core Library
meta_log_buffer.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014-2015, Hewlett-Packard Development Company, LP.
3  * This program is free software; you can redistribute it and/or modify it
4  * under the terms of the GNU General Public License as published by the Free
5  * Software Foundation; either version 2 of the License, or (at your option)
6  * any later version.
7  *
8  * This program is distributed in the hope that it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11  * more details. You should have received a copy of the GNU General Public
12  * License along with this program; if not, write to the Free Software
13  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14  *
15  * HP designates this particular file as subject to the "Classpath" exception
16  * as provided by HP in the LICENSE.txt file that accompanied this code.
17  */
19 
20 #include <glog/logging.h>
21 
22 #include <cstring>
23 #include <ostream>
24 #include <thread>
25 
26 #include "foedus/engine.hpp"
29 
30 namespace foedus {
31 namespace log {
32 
33 void MetaLogBuffer::commit(BaseLogType* metalog, Epoch* commit_epoch) {
34  LOG(INFO) << "Writing a metadata log. " << metalog->header_ << "...";
35  {
36  soc::SharedMutexScope scope(&control_block_->mutex_);
37  // access to metadata buffer is mutex-protected
38  ASSERT_ND(control_block_->buffer_used_ == 0);
39 
40  // To avoid mixing with normal operations on the storage in this epoch, advance epoch.
41  // This happens within the mutex, so this is assured to be the only metadata log in the epoch.
43  *commit_epoch = engine_->get_xct_manager()->get_current_global_epoch();
44  LOG(INFO) << "Issued an epoch for the metadata log: " << *commit_epoch << "...";
45  metalog->header_.xct_id_.set_epoch(*commit_epoch);
46 
47  // copy to buffer_ first
49  || metalog->header_.get_kind() == kEngineLogs);
50  std::memcpy(control_block_->buffer_, metalog, metalog->header_.log_length_);
51 
52  // then write buffer_used_ after fence for the logger to safely read it
53  // Also, do it within mutex to avoid lost signal
54  {
55  // Wakeup the logger
56  control_block_->buffer_used_ = metalog->header_.log_length_;
57  control_block_->logger_wakeup_.signal();
58  }
59 
60  // Simply sleep for a while. Metadata logging is not so often, so we can simply spin
61  // with a short sleep.
62  while (control_block_->buffer_used_ > 0) {
63  std::this_thread::sleep_for(std::chrono::microseconds(100));
65  }
66  }
67  LOG(INFO) << "Wrote a metadata log. " << metalog->header_ << "...";
68 }
69 
70 std::ostream& operator<<(std::ostream& o, const MetaLogBuffer& v) {
71  o << "<MetaLogBuffer>"
72  << "<buffer_used_>" << v.control_block_->buffer_used_ << "</buffer_used_>"
73  << "<oldest_offset_>" << v.control_block_->oldest_offset_ << "</oldest_offset_>"
74  << "<durable_offset_>" << v.control_block_->durable_offset_ << "</durable_offset_>"
75  << "</MetaLogBuffer>";
76  return o;
77 }
78 
79 } // namespace log
80 } // namespace foedus
LogCodeKind get_kind() const __attribute__((always_inline))
Convenience method to get LogCodeKind.
void set_epoch(Epoch epoch) __attribute__((always_inline))
Definition: xct_id.hpp:965
Epoch get_current_global_epoch() const
Returns the current global epoch, the epoch a newly started transaction will be in.
Root package of FOEDUS (Fast Optimistic Engine for Data Unification Services).
Definition: assert_nd.hpp:44
Represents a time epoch.
Definition: epoch.hpp:61
Engine * engine_
Most attachable object stores an engine pointer (local engine), so we define it here.
Definition: attachable.hpp:107
Declares common log types used in all packages.
Base class for log type.
MetaLogControlBlock * control_block_
The shared data on shared memory that has been initialized in some SOC or master engine.
Definition: attachable.hpp:111
A single log buffer for metadata (eg create/drop storage).
xct::XctId xct_id_
Epoch and in-epoch ordinal of this log.
Auto-lock scope object for SharedMutex.
uint16_t log_length_
Byte size of this log entry including this header itself and everything.
uint64_t durable_offset_
Offset upto which log entries are fsynced.
engine targetted logs
Definition: log_type.hpp:107
std::ostream & operator<<(std::ostream &o, const LogHeader &v)
xct::XctManager * get_xct_manager() const
See Transaction Manager.
Definition: engine.cpp:61
void commit(BaseLogType *metalog, Epoch *commit_epoch)
Synchronously writes out the given log to metadata log file.
void advance_current_global_epoch()
Requests to advance the current global epoch as soon as possible and blocks until it actually does...
storage targetted logs
Definition: log_type.hpp:105
void memory_fence_acquire()
Equivalent to std::atomic_thread_fence(std::memory_order_acquire).
#define ASSERT_ND(x)
A warning-free wrapper macro of assert() that has no performance effect in release mode even when 'x'...
Definition: assert_nd.hpp:72
uint64_t oldest_offset_
Offset from which log entries are not gleaned yet.