libfoedus-core
FOEDUS Core Library
logger_ref.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 
25 
26 namespace foedus {
27 namespace log {
30  Engine* engine,
31  LoggerControlBlock* block,
32  LoggerId id,
33  uint16_t numa_node,
34  uint16_t in_node_ordinal)
35  : Attachable<LoggerControlBlock>(engine, block) {
36  id_ = id;
37  numa_node_ = numa_node;
38  in_node_ordinal_ = in_node_ordinal;
39 }
40 
42  return Epoch(control_block_->durable_epoch_);
43 }
44 
45 void LoggerRef::wakeup_for_durable_epoch(Epoch desired_durable_epoch) {
47  if (get_durable_epoch() < desired_durable_epoch) {
48  wakeup();
49  }
50 }
51 
53  control_block_->wakeup_cond_.signal();
54 }
55 
57  new_savepoint->oldest_log_files_.push_back(control_block_->oldest_ordinal_);
58  new_savepoint->oldest_log_files_offset_begin_.push_back(
59  control_block_->oldest_file_offset_begin_);
60  new_savepoint->current_log_files_.push_back(control_block_->current_ordinal_);
61  new_savepoint->current_log_files_offset_durable_.push_back(
62  control_block_->current_file_durable_offset_);
63 }
64 
66  soc::SharedMutexScope scope(&control_block_->epoch_history_mutex_);
67  uint32_t tail_index = control_block_->get_tail_epoch_history();
68  ASSERT_ND(control_block_->epoch_history_count_ == 0
69  || control_block_->epoch_histories_[tail_index].new_epoch_ == epoch_marker.old_epoch_);
70  // the first epoch marker is allowed only if it's a dummy marker.
71  // this simplifies the detection of first epoch marker
72  if (!control_block_->is_epoch_history_empty()
73  && epoch_marker.old_epoch_ == epoch_marker.new_epoch_) {
74  LOG(INFO) << "Ignored a dummy epoch marker while replaying epoch marker log on Logger-"
75  << id_ << ". marker=" << epoch_marker;
76  } else {
77  if (control_block_->epoch_history_count_ >= LoggerControlBlock::kMaxEpochHistory) {
78  // TASK(Hideaki) To avoid this, we should maintain sparse history, like one history per
79  // tens of epochs. So far kMaxEpochHistory is enough big, so it won't happen.
80  LOG(FATAL) << "Exceeded kMaxEpochHistory. Unexpected.";
81  }
82  ++control_block_->epoch_history_count_;
83  tail_index = control_block_->get_tail_epoch_history();
84  control_block_->epoch_histories_[tail_index] = EpochHistory(epoch_marker);
85  ASSERT_ND(control_block_->epoch_histories_[tail_index].new_epoch_.is_valid());
86  ASSERT_ND(control_block_->epoch_histories_[tail_index].old_epoch_.is_valid());
87  }
88 }
89 
90 
91 LogRange LoggerRef::get_log_range(Epoch prev_epoch, Epoch until_epoch) {
92  // TASK(Hideaki) binary search. we assume there are not many epoch histories, so not urgent.
93  ASSERT_ND(until_epoch <= get_durable_epoch());
94  LogRange result;
95 
96  // Epoch mark is not written to empty epoch. Thus, we might holes at the beginning, in the
97  // middle, and at the end. Be careful.
98  soc::SharedMutexScope scope(&control_block_->epoch_history_mutex_);
99  const uint32_t head = control_block_->epoch_history_head_;
100  const uint32_t count = control_block_->epoch_history_count_;
101  uint32_t pos = 0; // RELATIVE position from head
102  result.begin_file_ordinal = 0;
103  result.begin_offset = 0;
104  if (prev_epoch.is_valid()) {
105  // first, locate the prev_epoch
106  for (; pos < count; ++pos) {
107  uint32_t abs_pos = control_block_->wrap_epoch_history_index(head + pos);
108  const EpochHistory& cur = control_block_->epoch_histories_[abs_pos];
109  if (cur.new_epoch_ > prev_epoch) {
111  result.begin_offset = cur.log_file_offset_;
112  break;
113  }
114  }
115  if (pos == count) {
116  LOG(INFO) << "No epoch mark found for " << prev_epoch << " in logger-" << id_
117  << " This usually happens when there was no transactional log before system shutdown";
118  result.end_file_ordinal = 0;
119  result.end_offset = 0;
120  return result;
121  }
122  }
123 
124  // next, locate until_epoch. we might not find it if the logger was idle for a while.
125  // in that case, the last mark tells the file/ordinal.
126  for (; pos < count; ++pos) {
127  uint32_t abs_pos = control_block_->wrap_epoch_history_index(head + pos);
128  const EpochHistory& cur = control_block_->epoch_histories_[abs_pos];
129  // first mark that is after the until_epoch tells how much we have to read.
130  // note that we might have multiple marks of the same epoch because of beginning-of-file marker.
131  // we can't stop at the first mark with new_epoch==until.
132  if (cur.new_epoch_ > until_epoch) {
133  result.end_file_ordinal = cur.log_file_ordinal_;
134  result.end_offset = cur.log_file_offset_;
135  break;
136  }
137  }
138 
139  if (pos == count) {
140  if (count == 0) {
141  ASSERT_ND(result.begin_file_ordinal == 0);
142  ASSERT_ND(result.begin_offset == 0);
143  result.end_file_ordinal = 0;
144  result.end_offset = 0;
145  } else {
146  // in this case, we read everything.
147  result.end_file_ordinal = control_block_->current_ordinal_;
148  result.end_offset = control_block_->current_file_durable_offset_;
149  }
150  }
151 
154  || result.begin_offset <= result.end_offset);
155  return result;
156 }
157 
158 } // namespace log
159 } // namespace foedus
std::vector< uint64_t > oldest_log_files_offset_begin_
Indicates the inclusive beginning of active region in the oldest log file.
Definition: savepoint.hpp:104
void copy_logger_state(savepoint::Savepoint *new_savepoint) const
Called from log manager's copy_logger_states.
Definition: logger_ref.cpp:56
Root package of FOEDUS (Fast Optimistic Engine for Data Unification Services).
Definition: assert_nd.hpp:44
a contiguous range of log entries that might span multiple files.
Definition: log_id.hpp:52
std::vector< uint64_t > current_log_files_offset_durable_
Indicates the exclusive end of durable region in the current log file.
Definition: savepoint.hpp:114
uint64_t end_offset
Definition: log_id.hpp:56
Represents a time epoch.
Definition: epoch.hpp:61
The information we maintain in savepoint manager and externalize to a file.
Definition: savepoint.hpp:40
Epoch old_epoch_
Epoch before this switch.
LogRange get_log_range(Epoch prev_epoch, Epoch until_epoch)
Constructs the range of log entries that represent the given epoch ranges.
Definition: logger_ref.cpp:91
LogFileOrdinal log_file_ordinal_
LogFileOrdinal begin_file_ordinal
Definition: log_id.hpp:53
uint64_t begin_offset
Definition: log_id.hpp:55
LoggerControlBlock * control_block_
The shared data on shared memory that has been initialized in some SOC or master engine.
Definition: attachable.hpp:111
Database engine object that holds all resources and provides APIs.
Definition: engine.hpp:109
A log type to declare a switch of epoch in a logger or the engine.
Epoch get_durable_epoch() const
Returns this logger's durable epoch.
Definition: logger_ref.cpp:41
void wakeup_for_durable_epoch(Epoch desired_durable_epoch)
Wakes up this logger if its durable_epoch has not reached the given epoch yet.
Definition: logger_ref.cpp:45
Auto-lock scope object for SharedMutex.
void wakeup()
Wakes up this logger if it is sleeping.
Definition: logger_ref.cpp:52
Attachable Resources on Shared Memory.
Definition: attachable.hpp:58
Shared data of Logger.
Definition: logger_impl.hpp:51
bool is_valid() const
Definition: epoch.hpp:96
Epoch new_epoch_
Epoch after this switch.
std::vector< log::LogFileOrdinal > oldest_log_files_
Ordinal of the oldest active log file in each logger.
Definition: savepoint.hpp:101
Max number of active epoch histories.
Definition: logger_impl.hpp:57
Atomic fence methods and load/store with fences that work for both C++11/non-C++11 code...
void memory_fence_acquire()
Equivalent to std::atomic_thread_fence(std::memory_order_acquire).
LogFileOrdinal end_file_ordinal
Definition: log_id.hpp:54
void add_epoch_history(const EpochMarkerLogType &epoch_marker)
Append a new epoch history.
Definition: logger_ref.cpp:65
#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
uint16_t LoggerId
Typedef for an ID of Logger.
Definition: log_id.hpp:36
std::vector< log::LogFileOrdinal > current_log_files_
Indicates the log file each logger is currently appending to.
Definition: savepoint.hpp:107
Represents an event where a logger switched its epoch.