libfoedus-core
FOEDUS Core Library
foedus::log::Logger Class Referencefinal

A log writer that writes out buffered logs to stable storages. More...

Detailed Description

A log writer that writes out buffered logs to stable storages.

This is a private implementation-details of Log Manager, thus file name ends with _impl. Do not include this header from a client program unless you know what you are doing.

Definition at line 168 of file logger_impl.hpp.

#include <logger_impl.hpp>

Inheritance diagram for foedus::log::Logger:
Collaboration diagram for foedus::log::Logger:

Public Member Functions

 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)
 
ErrorStack initialize_once () override
 
ErrorStack uninitialize_once () override
 
 Logger ()=delete
 
 Logger (const Logger &other)=delete
 
Loggeroperator= (const Logger &other)=delete
 
LogFileOrdinal get_current_ordinal () const
 
bool is_stop_requested () const
 
std::string to_string () const
 
- Public Member Functions inherited from foedus::DefaultInitializable
 DefaultInitializable ()
 
virtual ~DefaultInitializable ()
 
 DefaultInitializable (const DefaultInitializable &)=delete
 
DefaultInitializableoperator= (const DefaultInitializable &)=delete
 
ErrorStack initialize () override final
 Typical implementation of Initializable::initialize() that provides initialize-once semantics. More...
 
ErrorStack uninitialize () override final
 Typical implementation of Initializable::uninitialize() that provides uninitialize-once semantics. More...
 
bool is_initialized () const override final
 Returns whether the object has been already initialized or not. More...
 
- Public Member Functions inherited from foedus::Initializable
virtual ~Initializable ()
 
- Public Member Functions inherited from foedus::log::LoggerRef
 LoggerRef ()
 
 LoggerRef (Engine *engine, LoggerControlBlock *block, LoggerId id, uint16_t numa_node, uint16_t in_node_ordinal)
 
Epoch get_durable_epoch () const
 Returns this logger's durable epoch. More...
 
void wakeup ()
 Wakes up this logger if it is sleeping. More...
 
void wakeup_for_durable_epoch (Epoch desired_durable_epoch)
 Wakes up this logger if its durable_epoch has not reached the given epoch yet. More...
 
void copy_logger_state (savepoint::Savepoint *new_savepoint) const
 Called from log manager's copy_logger_states. More...
 
void add_epoch_history (const EpochMarkerLogType &epoch_marker)
 Append a new epoch history. More...
 
LogRange get_log_range (Epoch prev_epoch, Epoch until_epoch)
 Constructs the range of log entries that represent the given epoch ranges. More...
 
- Public Member Functions inherited from foedus::Attachable< LoggerControlBlock >
 Attachable ()
 
 Attachable (Engine *engine)
 
 Attachable (Engine *engine, LoggerControlBlock *control_block)
 
 Attachable (LoggerControlBlock *control_block)
 
 Attachable (const Attachable &other)
 
virtual ~Attachable ()
 
Attachableoperator= (const Attachable &other)
 
virtual void attach (LoggerControlBlock *control_block)
 Attaches to the given shared memory. More...
 
bool is_attached () const
 Returns whether the object has been already attached to some shared memory. More...
 
LoggerControlBlock * get_control_block () const
 
Engineget_engine () const
 
void set_engine (Engine *engine)
 

Friends

std::ostream & operator<< (std::ostream &o, const Logger &v)
 

Additional Inherited Members

- Protected Attributes inherited from foedus::log::LoggerRef
LoggerId id_
 
uint16_t numa_node_
 
uint16_t in_node_ordinal_
 
- Protected Attributes inherited from foedus::Attachable< LoggerControlBlock >
Engineengine_
 Most attachable object stores an engine pointer (local engine), so we define it here. More...
 
LoggerControlBlock * control_block_
 The shared data on shared memory that has been initialized in some SOC or master engine. More...
 

Constructor & Destructor Documentation

foedus::log::Logger::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 
)
inline

Definition at line 170 of file logger_impl.hpp.

178  : LoggerRef(engine, control_block, id, numa_node, in_node_ordinal),
179  log_folder_(log_folder),
180  assigned_thread_ids_(assigned_thread_ids) {}
foedus::log::Logger::Logger ( )
delete
foedus::log::Logger::Logger ( const Logger other)
delete

Member Function Documentation

LogFileOrdinal foedus::log::Logger::get_current_ordinal ( ) const
inline

Definition at line 188 of file logger_impl.hpp.

References foedus::Attachable< LoggerControlBlock >::control_block_.

188 { return control_block_->current_ordinal_; }
LoggerControlBlock * control_block_
The shared data on shared memory that has been initialized in some SOC or master engine.
Definition: attachable.hpp:111
ErrorStack foedus::log::Logger::initialize_once ( )
overridevirtual

Implements foedus::DefaultInitializable.

Definition at line 74 of file logger_impl.cpp.

References foedus::memory::NumaNodeMemory::allocate_numa_memory(), ASSERT_ND, CHECK_ERROR, foedus::log::LogOptions::construct_suffixed_log_path(), foedus::Attachable< LoggerControlBlock >::control_block_, foedus::thread::decompose_numa_local_ordinal(), foedus::log::LogOptions::emulation_, foedus::Attachable< LoggerControlBlock >::engine_, foedus::memory::AlignedMemory::get_alignment(), foedus::fs::DirectIoFile::get_current_offset(), foedus::savepoint::SavepointManager::get_initial_durable_epoch(), foedus::thread::ThreadPoolPimpl::get_local_group(), foedus::memory::EngineMemory::get_local_memory(), foedus::savepoint::SavepointManager::get_logger_savepoint(), foedus::Engine::get_memory_manager(), foedus::Engine::get_options(), foedus::thread::ThreadPool::get_pimpl(), foedus::Engine::get_savepoint_manager(), foedus::memory::AlignedMemory::get_size(), foedus::thread::ThreadGroup::get_thread(), foedus::Engine::get_thread_pool(), foedus::log::LoggerRef::id_, foedus::memory::AlignedMemory::is_null(), foedus::log::FillerLogType::kLogWriteUnitSize, foedus::kRetOk, foedus::EngineOptions::log_, foedus::log::LoggerRef::numa_node_, foedus::fs::DirectIoFile::open(), foedus::fs::DirectIoFile::truncate(), foedus::Epoch::value(), and WRAP_ERROR_CODE.

74  {
75  control_block_->initialize();
76  // clear all variables
77  current_file_ = nullptr;
78  LOG(INFO) << "Initializing Logger-" << id_ << ". assigned " << assigned_thread_ids_.size()
79  << " threads, starting from " << assigned_thread_ids_[0] << ", numa_node_="
80  << static_cast<int>(numa_node_);
81 
82  // Initialize the values from the latest savepoint.
83  savepoint::LoggerSavepointInfo info = engine_->get_savepoint_manager()->get_logger_savepoint(id_);
84  // durable epoch from initial savepoint
85  control_block_->durable_epoch_
87  control_block_->marked_epoch_ = Epoch(control_block_->durable_epoch_);
88  control_block_->oldest_ordinal_ = info.oldest_log_file_; // ordinal/length too
89  control_block_->current_ordinal_ = info.current_log_file_;
90  control_block_->current_file_durable_offset_ = info.current_log_file_offset_durable_;
91  control_block_->oldest_file_offset_begin_ = info.oldest_log_file_offset_begin_;
92  current_file_path_ = engine_->get_options().log_.construct_suffixed_log_path(
93  numa_node_,
94  id_,
95  control_block_->current_ordinal_);
96  // open the log file
97  current_file_ = new fs::DirectIoFile(current_file_path_,
99  WRAP_ERROR_CODE(current_file_->open(true, true, true, true));
100  if (control_block_->current_file_durable_offset_ < current_file_->get_current_offset()) {
101  // there are non-durable regions as an incomplete remnant of previous execution.
102  // probably there was a crash. in this case, we discard the non-durable regions.
103  LOG(ERROR) << "Logger-" << id_ << "'s log file has a non-durable region. Probably there"
104  << " was a crash. Will truncate it to " << control_block_->current_file_durable_offset_
105  << " from " << current_file_->get_current_offset();
106  WRAP_ERROR_CODE(current_file_->truncate(
107  control_block_->current_file_durable_offset_,
108  true)); // sync right now
109  }
110  ASSERT_ND(control_block_->current_file_durable_offset_ == current_file_->get_current_offset());
111  LOG(INFO) << "Initialized logger: " << *this;
112 
113  // which threads are assigned to me?
114  for (auto thread_id : assigned_thread_ids_) {
115  assigned_threads_.push_back(
118  }
119 
120  // grab a buffer to pad incomplete blocks for direct file I/O
122  FillerLogType::kLogWriteUnitSize, &fill_buffer_));
123  ASSERT_ND(!fill_buffer_.is_null());
126  LOG(INFO) << "Logger-" << id_ << " grabbed a padding buffer. size=" << fill_buffer_.get_size();
127  CHECK_ERROR(write_dummy_epoch_mark());
128 
129  // log file and buffer prepared. let's launch the logger thread
130  logger_thread_ = std::move(std::thread(&Logger::handle_logger, this));
131 
132  assert_consistent();
133  return kRetOk;
134 }
ThreadPoolPimpl * get_pimpl() const
Returns the pimpl of this object.
ErrorCode truncate(uint64_t new_length, bool sync=false)
Discard the content of the file after the given offset.
ErrorStack allocate_numa_memory(uint64_t size, AlignedMemory *out) const
std::string construct_suffixed_log_path(int node, int logger, LogFileOrdinal ordinal) const
construct full path of individual log file (log_folder/LOGGERID_ORDINAL.log)
Definition: log_options.cpp:41
uint64_t get_alignment() const
Returns the alignment of the memory block.
ThreadGroup * get_local_group() const
ThreadLocalOrdinal decompose_numa_local_ordinal(ThreadId global_id)
Extracts local ordinal from the given globally unique ID of Thread (core).
Definition: thread_id.hpp:139
ErrorCode open(bool read, bool write, bool append, bool create)
Tries to open the file for the specified volume.
Engine * engine_
Most attachable object stores an engine pointer (local engine), so we define it here.
Definition: attachable.hpp:107
uint64_t get_current_offset() const
const EngineOptions & get_options() const
Definition: engine.cpp:39
savepoint::SavepointManager * get_savepoint_manager() const
See Savepoint Manager.
Definition: engine.cpp:53
LoggerControlBlock * control_block_
The shared data on shared memory that has been initialized in some SOC or master engine.
Definition: attachable.hpp:111
Thread * get_thread(ThreadLocalOrdinal ordinal) const
Returns Thread object for the given ordinal in this group.
uint64_t get_size() const
Returns the byte size of the memory block.
#define CHECK_ERROR(x)
This macro calls x and checks its returned value.
NumaNodeMemory * get_local_memory() const
foedus::fs::DeviceEmulationOptions emulation_
Settings to emulate slower logging device.
const ErrorStack kRetOk
Normal return value for no-error case.
#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
#define WRAP_ERROR_CODE(x)
Same as CHECK_ERROR(x) except it receives only an error code, thus more efficient.
memory::EngineMemory * get_memory_manager() const
See Memory Manager.
Definition: engine.cpp:50
thread::ThreadPool * get_thread_pool() const
See Thread and Thread-Group.
Definition: engine.cpp:52
We always write to file in a multiply of this value, filling up the rest if needed.
LoggerSavepointInfo get_logger_savepoint(log::LoggerId logger_id)
Returns the saved information of the given logger in latest savepoint.
EpochInteger value() const
Returns the raw integer representation.
Definition: epoch.hpp:102
bool is_null() const
Returns if this object doesn't hold a valid memory block.

Here is the call graph for this function:

bool foedus::log::Logger::is_stop_requested ( ) const
inline

Definition at line 189 of file logger_impl.hpp.

References foedus::Attachable< LoggerControlBlock >::control_block_.

189 { return control_block_->stop_requested_; }
LoggerControlBlock * control_block_
The shared data on shared memory that has been initialized in some SOC or master engine.
Definition: attachable.hpp:111
Logger& foedus::log::Logger::operator= ( const Logger other)
delete
std::string foedus::log::Logger::to_string ( ) const

Definition at line 528 of file logger_impl.cpp.

528  {
529  std::stringstream stream;
530  stream << *this;
531  return stream.str();
532 }
ErrorStack foedus::log::Logger::uninitialize_once ( )
overridevirtual

Implements foedus::DefaultInitializable.

Definition at line 136 of file logger_impl.cpp.

References foedus::fs::DirectIoFile::close(), foedus::Attachable< LoggerControlBlock >::control_block_, foedus::log::LoggerRef::id_, foedus::memory::AlignedMemory::release_block(), and SUMMARIZE_ERROR_BATCH.

136  {
137  LOG(INFO) << "Uninitializing Logger-" << id_ << ": " << *this;
138  ErrorStackBatch batch;
139  if (logger_thread_.joinable()) {
140  {
141  control_block_->stop_requested_ = true;
142  control_block_->wakeup_cond_.signal();
143  }
144  logger_thread_.join();
145  }
146  if (current_file_) {
147  current_file_->close();
148  delete current_file_;
149  current_file_ = nullptr;
150  }
151  fill_buffer_.release_block();
152  control_block_->uninitialize();
153  return SUMMARIZE_ERROR_BATCH(batch);
154 }
void release_block()
Releases the memory block.
bool close()
Close the file if not yet closed.
LoggerControlBlock * control_block_
The shared data on shared memory that has been initialized in some SOC or master engine.
Definition: attachable.hpp:111
#define SUMMARIZE_ERROR_BATCH(x)
This macro calls ErrorStackBatch::summarize() with automatically provided parameters.

Here is the call graph for this function:

Friends And Related Function Documentation

std::ostream& operator<< ( std::ostream &  o,
const Logger v 
)
friend

Definition at line 533 of file logger_impl.cpp.

533  {
534  o << "<Logger>"
535  << "<id_>" << v.id_ << "</id_>"
536  << "<numa_node_>" << static_cast<int>(v.numa_node_) << "</numa_node_>"
537  << "<in_node_ordinal_>" << static_cast<int>(v.in_node_ordinal_) << "</in_node_ordinal_>"
538  << "<log_folder_>" << v.log_folder_ << "</log_folder_>";
539  o << "<assigned_thread_ids_>";
540  for (auto thread_id : v.assigned_thread_ids_) {
541  o << "<thread_id>" << thread_id << "</thread_id>";
542  }
543  o << "</assigned_thread_ids_>";
544  o << "<durable_epoch_>" << v.get_durable_epoch() << "</durable_epoch_>"
545  << "<marked_epoch_>" << v.control_block_->marked_epoch_ << "</marked_epoch_>"
546  << "<oldest_ordinal_>" << v.control_block_->oldest_ordinal_ << "</oldest_ordinal_>"
547  << "<oldest_file_offset_begin_>" << v.control_block_->oldest_file_offset_begin_
548  << "</oldest_file_offset_begin_>"
549  << "<current_ordinal_>" << v.control_block_->current_ordinal_ << "</current_ordinal_>";
550 
551  o << "<current_file_>";
552  if (v.current_file_) {
553  o << *v.current_file_;
554  } else {
555  o << "nullptr";
556  }
557  o << "</current_file_>";
558 
559  o << "<current_file_path_>" << v.current_file_path_ << "</current_file_path_>";
560 
561  o << "<current_file_length_>";
562  if (v.current_file_) {
563  o << v.current_file_->get_current_offset();
564  } else {
565  o << "nullptr";
566  }
567  o << "</current_file_length_>";
568 
569  o << "<epoch_history_head>"
570  << v.control_block_->epoch_history_head_ << "</epoch_history_head>";
571  o << "<epoch_history_count>"
572  << v.control_block_->epoch_history_count_ << "</epoch_history_count>";
573  o << "</Logger>";
574  return o;
575 }

The documentation for this class was generated from the following files: