libfoedus-core
FOEDUS Core Library
logger_impl.hpp
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  */
18 #ifndef FOEDUS_LOG_LOGGER_IMPL_HPP_
19 #define FOEDUS_LOG_LOGGER_IMPL_HPP_
20 #include <stdint.h>
21 
22 #include <atomic>
23 #include <iosfwd>
24 #include <mutex>
25 #include <string>
26 #include <thread>
27 #include <vector>
28 
29 #include "foedus/attachable.hpp"
30 #include "foedus/epoch.hpp"
31 #include "foedus/fwd.hpp"
32 #include "foedus/initializable.hpp"
33 #include "foedus/fs/fwd.hpp"
34 #include "foedus/fs/path.hpp"
36 #include "foedus/log/fwd.hpp"
37 #include "foedus/log/log_id.hpp"
40 #include "foedus/savepoint/fwd.hpp"
44 #include "foedus/thread/fwd.hpp"
46 
47 namespace foedus {
48 namespace log {
49 
52  enum Constants {
57  kMaxEpochHistory = 1 << 16,
58  };
59 
60  // this is backed by shared memory. not instantiation. just reinterpret_cast.
61  LoggerControlBlock() = delete;
62  ~LoggerControlBlock() = delete;
63 
64  void initialize() {
67  stop_requested_ = false;
70  }
71  void uninitialize() {
73  }
74 
75 
76  bool is_epoch_history_empty() const { return epoch_history_count_ == 0; }
77  uint32_t get_tail_epoch_history() const {
79  }
80 
81  static uint32_t wrap_epoch_history_index(uint32_t index) { return index % kMaxEpochHistory; }
82 
94  std::atomic< Epoch::EpochInteger > durable_epoch_;
103 
117 
118  // the followings are also shared just for savepoint.
119 
124  std::atomic< LogFileOrdinal > oldest_ordinal_;
129  std::atomic< uint64_t > oldest_file_offset_begin_;
133  std::atomic< LogFileOrdinal > current_ordinal_;
134 
140  std::atomic< uint64_t > current_file_durable_offset_;
141 
143  std::atomic<bool> stop_requested_;
144 
147 
152 
159 };
160 
168 class Logger final : public DefaultInitializable, public LoggerRef {
169  public:
171  Engine* engine,
172  LoggerControlBlock* control_block,
173  LoggerId id,
174  thread::ThreadGroupId numa_node,
175  uint8_t in_node_ordinal,
176  const fs::Path &log_folder,
177  const std::vector< thread::ThreadId > &assigned_thread_ids)
178  : LoggerRef(engine, control_block, id, numa_node, in_node_ordinal),
179  log_folder_(log_folder),
180  assigned_thread_ids_(assigned_thread_ids) {}
181  ErrorStack initialize_once() override;
182  ErrorStack uninitialize_once() override;
183 
184  Logger() = delete;
185  Logger(const Logger &other) = delete;
186  Logger& operator=(const Logger &other) = delete;
187 
188  LogFileOrdinal get_current_ordinal() const { return control_block_->current_ordinal_; }
189  bool is_stop_requested() const { return control_block_->stop_requested_; }
190 
191  std::string to_string() const;
192  friend std::ostream& operator<<(std::ostream& o, const Logger& v);
193 
194  private:
202  void handle_logger();
203 
208  ErrorStack update_durable_epoch(Epoch new_durable_epoch, bool had_any_log);
213  Epoch calculate_min_durable_epoch();
214 
218  ErrorStack switch_file_if_required();
219 
225  ErrorStack check_lagging_threads(Epoch new_epoch);
226 
231  ErrorStack log_epoch_switch(Epoch new_epoch);
232 
237  ErrorStack write_dummy_epoch_mark();
238 
245  ErrorStack write_one_epoch(Epoch write_epoch);
252  ErrorStack write_one_epoch_piece(
253  const ThreadLogBuffer& buffer,
254  Epoch write_epoch,
255  uint64_t from_offset,
256  uint64_t upto_offset);
257 
259  void assert_consistent();
261  void assert_written_logs(Epoch write_epoch, const char* logs, uint64_t bytes) const;
262 
263  const fs::Path log_folder_;
264  const std::vector< thread::ThreadId > assigned_thread_ids_;
265 
266  std::thread logger_thread_;
267 
281  memory::AlignedMemory fill_buffer_;
282 
286  fs::DirectIoFile* current_file_;
290  fs::Path current_file_path_;
291 
292  std::vector< thread::Thread* > assigned_threads_;
293 
295  std::mutex epoch_switch_mutex_;
296 };
297 static_assert(
298  sizeof(LoggerControlBlock) <= soc::NodeMemoryAnchors::kLoggerMemorySize,
299  "LoggerControlBlock is too large.");
300 } // namespace log
301 } // namespace foedus
302 #endif // FOEDUS_LOG_LOGGER_IMPL_HPP_
std::atomic< LogFileOrdinal > current_ordinal_
Ordinal of the log file this logger is currently appending to.
std::atomic< uint64_t > oldest_file_offset_begin_
Inclusive beginning of active region in the oldest log file.
std::atomic< Epoch::EpochInteger > durable_epoch_
Upto what epoch the logger flushed logs in all buffers assigned to it.
Definition: logger_impl.hpp:94
Forward declarations of classes in filesystem package.
ErrorStack initialize_once() override
Definition: logger_impl.cpp:74
Epoch marked_epoch_
Upto what epoch the logger has put epoch marker in the log file.
A thread-local log buffer.
soc::SharedPolling wakeup_cond_
The logger sleeps on this conditional.
Logger(Engine *engine, LoggerControlBlock *control_block, LoggerId id, thread::ThreadGroupId numa_node, uint8_t in_node_ordinal, const fs::Path &log_folder, const std::vector< thread::ThreadId > &assigned_thread_ids)
Forward declarations of classes in log manager package.
void initialize(bool recursive=false)
Root package of FOEDUS (Fast Optimistic Engine for Data Unification Services).
Definition: assert_nd.hpp:44
Typedefs of ID types used in thread package.
LogFileOrdinal get_current_ordinal() const
Forward declarations of classes in root package.
Brings error stacktrace information as return value of functions.
Definition: error_stack.hpp:81
std::atomic< LogFileOrdinal > oldest_ordinal_
Ordinal of the oldest active log file of this logger.
Represents a time epoch.
Definition: epoch.hpp:61
uint32_t get_tail_epoch_history() const
Definition: logger_impl.hpp:77
A polling-wait mechanism that can be placed in shared memory and used from multiple processes...
Typical implementation of Initializable as a skeleton base class.
std::atomic< uint64_t > current_file_durable_offset_
We called fsync on current file up to this offset.
std::atomic< bool > stop_requested_
Whether this logger should terminate.
Typedefs of ID types used in log package.
bool is_stop_requested() const
A mutex that can be placed in shared memory and used from multiple processes.
A view of Logger object for other SOCs and master engine.
Definition: logger_ref.hpp:35
soc::SharedMutex epoch_history_mutex_
the followings are covered this mutex
Analogue of boost::filesystem::path.
Definition: path.hpp:37
uint32_t LogFileOrdinal
Ordinal of log files (eg "log.0", "log.1").
Definition: log_id.hpp:46
LoggerControlBlock * control_block_
The shared data on shared memory that has been initialized in some SOC or master engine.
Definition: attachable.hpp:111
A log writer that writes out buffered logs to stable storages.
Database engine object that holds all resources and provides APIs.
Definition: engine.hpp:109
EpochHistory epoch_histories_[kMaxEpochHistory]
Remembers all epoch switching in this logger.
friend std::ostream & operator<<(std::ostream &o, const Logger &v)
uint32_t epoch_history_head_
index of the oldest history in epoch_histories_
Shared data of Logger.
Definition: logger_impl.hpp:51
ErrorStack uninitialize_once() override
Forward declarations of classes in savepoint manager package.
Logger & operator=(const Logger &other)=delete
Represents an I/O stream on one file without filesystem caching.
Represents one memory block aligned to actual OS/hardware pages.
std::string to_string() const
Max number of active epoch histories.
Definition: logger_impl.hpp:57
Forward declarations of classes in thread package.
uint16_t LoggerId
Typedef for an ID of Logger.
Definition: log_id.hpp:36
uint8_t ThreadGroupId
Typedef for an ID of ThreadGroup (NUMA node).
Definition: thread_id.hpp:38
static uint32_t wrap_epoch_history_index(uint32_t index)
Definition: logger_impl.hpp:81
uint32_t epoch_history_count_
number of active entries in epoch_histories_ .
Represents an event where a logger switched its epoch.