0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
MetaLogWriter.h
Go to the documentation of this file.
1 /* -*- c++ -*-
2  * Copyright (C) 2007-2015 Hypertable, Inc.
3  *
4  * This file is part of Hypertable.
5  *
6  * Hypertable is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; version 3 of the
9  * License, or any later version.
10  *
11  * Hypertable is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301, USA.
20  */
21 
28 #ifndef Hypertable_Lib_MetaLogWriter_h
29 #define Hypertable_Lib_MetaLogWriter_h
30 
31 #include "MetaLogDefinition.h"
32 #include "MetaLogEntity.h"
33 
34 #include <Common/Filesystem.h>
35 
36 #include <AsyncComm/Comm.h>
38 
39 #include <condition_variable>
40 #include <deque>
41 #include <memory>
42 #include <mutex>
43 #include <utility>
44 #include <vector>
45 
46 namespace Hypertable {
47 
48  namespace MetaLog {
49 
52 
66  class Writer {
67  public:
68 
98  Writer(FilesystemPtr &fs, DefinitionPtr &definition, const std::string &path,
99  std::vector<EntityPtr> &initial_entities);
100 
104  ~Writer();
105 
109  void close();
110 
121  void record_state(EntityPtr entity);
122 
129  void record_state(std::vector<EntityPtr> &entities);
130 
142  void record_removal(EntityPtr entity);
143 
150  void record_removal(std::vector<EntityPtr> &entities);
151 
152  void signal_write_ready();
153 
155  static bool skip_recover_entry;
156 
157  private:
158 
161  public:
162 
164  WriteScheduler(Writer *writer);
165 
166  virtual ~WriteScheduler();
167 
168  void schedule();
169 
170  void handle(EventPtr &event) override;
171 
172  private:
176  std::condition_variable m_cond;
182  int32_t m_interval {};
184  bool m_scheduled {};
185  };
186 
187  typedef std::shared_ptr<WriteScheduler> WriteSchedulerPtr;
188 
198  void write_header();
199 
208  void purge_old_log_files();
209 
210  void roll();
211 
212  void service_write_queue();
213 
216 
218  condition_variable m_cond;
219 
222 
225 
227  std::string m_path;
228 
230  std::string m_filename;
231 
233  int m_fd {-1};
234 
236  std::string m_backup_path;
237 
239  std::string m_backup_filename;
240 
243 
245  int32_t m_offset {};
246 
248  std::deque<int32_t> m_file_ids;
249 
251  int64_t m_max_file_size {};
252 
254  size_t m_history_size {};
255 
257  int32_t m_replication {};
258 
261 
262  // Serialized entity (length and smart pointer to buffer)
263  typedef std::pair<size_t, std::shared_ptr<uint8_t>> SerializedEntityT;
264 
266  std::map<int64_t, SerializedEntityT> m_entity_map;
267 
269  bool m_write_ready {};
270 
272  std::vector<StaticBufferPtr> m_write_queue;
273 
275  WriteSchedulerPtr m_write_scheduler;
276 
277  };
278 
280  typedef std::shared_ptr<Writer> WriterPtr;
281 
283  }
284 
285 }
286 
287 #endif // Hypertable_Lib_MetaLogWriter_h
static std::mutex mutex
Definition: Logger.cc:43
std::string m_backup_filename
Pathname of local log backup file.
std::shared_ptr< WriteScheduler > WriteSchedulerPtr
int m_backup_fd
File descriptor of backup MetaLog file in local filesystem.
Filesystem::Flags m_flush_method
Log flush method (FLUSH or SYNC)
void record_removal(EntityPtr entity)
Records the removal of an entity.
Abstract base class for a filesystem.
std::shared_ptr< Entity > EntityPtr
Smart pointer to Entity.
bool m_scheduled
Flag indicating that write has been scheduled.
Abstract base class for application dispatch handlers registered with AsyncComm.
Flags
Enumeration type for append flags.
Definition: Filesystem.h:76
int32_t m_offset
Current write offset of MetaLog file.
void purge_old_log_files()
Purges old MetaLog files.
Periodically flushes deferred writes to disk.
Declarations for MetaLog::Definition.
Writer(FilesystemPtr &fs, DefinitionPtr &definition, const std::string &path, std::vector< EntityPtr > &initial_entities)
Constructor.
std::shared_ptr< Event > EventPtr
Smart pointer to Event.
Definition: Event.h:228
static bool skip_recover_entry
Global flag to force writer to skip writing EntityRecover (testing)
WriteSchedulerPtr m_write_scheduler
Write scheduler.
DefinitionPtr m_definition
Smart pointer to MetaLog Definition.
void write_header()
Writes MetaLog file header.
bool m_write_ready
Flag indicating that.
Declarations for DispatchHandler.
std::condition_variable m_cond
Condition variable to signal when timer has stopped.
int64_t m_max_file_size
Maximum file size.
Writes a MetaLog.
Definition: MetaLogWriter.h:66
Declarations for MetaLog::Entity.
condition_variable m_cond
Condition variable to signal completion of deferred writes.
std::string m_backup_path
Pathname of local log backup directory.
Comm * m_comm
Pointer to Comm layer.
std::string m_path
Path name of MetaLog directory.
Hypertable definitions
int32_t m_replication
Replication factor.
Entry point to AsyncComm service.
Definition: Comm.h:61
std::pair< size_t, std::shared_ptr< uint8_t > > SerializedEntityT
std::shared_ptr< Writer > WriterPtr
Smart pointer to Writer.
std::mutex m_mutex
Mutex for serializing access to members
std::shared_ptr< Filesystem > FilesystemPtr
Smart pointer to Filesystem.
Definition: Filesystem.h:572
WriteScheduler(Writer *writer)
Constructor.
void close()
Closes open file descriptors.
size_t m_history_size
Number of old MetaLog files to retain for historical purposes.
Declarations for Comm.
std::mutex m_mutex
Mutex for serializing access to members
int m_fd
File descriptor of MetaLog file in FS.
std::map< int64_t, SerializedEntityT > m_entity_map
Map of current serialized entity data.
std::deque< int32_t > m_file_ids
Deque of existing file name IDs.
FilesystemPtr m_fs
Smart pointer to Filesystem object.
void record_state(EntityPtr entity)
Persists an Entity to the log.
std::vector< StaticBufferPtr > m_write_queue
Vector of pending writes.
Writer * m_writer
Pointer to MetaLogWriter.
void handle(EventPtr &event) override
Callback method.
std::string m_filename
Full pathname of MetaLog file open for writing.
std::shared_ptr< Definition > DefinitionPtr
Smart pointer to Definition.