0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
CommitLog.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 
26 
27 #ifndef Hypertable_Lib_CommitLog_h
28 #define Hypertable_Lib_CommitLog_h
29 
33 
34 #include <Common/DynamicBuffer.h>
35 #include <Common/String.h>
36 #include <Common/Properties.h>
37 #include <Common/Filesystem.h>
38 
39 #include <deque>
40 #include <map>
41 #include <memory>
42 #include <stack>
43 
44 namespace Hypertable {
45 
48 
63  class CommitLog : public CommitLogBase {
64 
65  public:
66 
68  uint32_t distance;
69  int64_t size;
70  int64_t cumulative_size;
71  uint32_t fragno;
72  };
73 
74  typedef std::map<int64_t, CumulativeFragmentData> CumulativeSizeMap;
75 
85  CommitLog(FilesystemPtr &fs, const std::string &log_dir,
86  PropertiesPtr &props, CommitLogBase *init_log = 0,
87  bool is_meta=true)
88  : CommitLogBase(log_dir), m_fs(fs) {
89  initialize(log_dir, props, init_log, is_meta);
90  }
91 
99  CommitLog(FilesystemPtr &fs, const std::string &log_dir, bool is_meta=true);
100 
101  virtual ~CommitLog();
102 
108  int64_t get_timestamp();
109 
118  int write(uint64_t cluster_id, DynamicBuffer &buffer, int64_t revision, Filesystem::Flags flags);
119 
124  int flush();
125 
130  int sync();
131 
138  int link_log(uint64_t cluster_id, CommitLogBase *log_base);
139 
144  int close();
145 
154  int purge(int64_t revision, StringSet &remove_ok_logs,
155  StringSet &removed_logs, std::string *trace);
156 
166  void load_cumulative_size_map(CumulativeSizeMap &cumulative_size_map);
167 
172 
180  void get_stats(const std::string &prefix, std::string &result);
181 
185  int64_t size() {
186  std::lock_guard<std::mutex>lock(m_mutex);
187  int64_t total = 0;
188  for (LogFragmentQueue::iterator iter = m_fragment_queue.begin();
189  iter != m_fragment_queue.end(); iter++)
190  total += (*iter)->size;
191  return total;
192  }
193 
194  const std::string& get_current_fragment_file() {
195  std::lock_guard<std::mutex>lock(m_mutex);
196  return m_cur_fragment_fname;
197  }
198 
199  static const char MAGIC_DATA[10];
200  static const char MAGIC_LINK[10];
201 
202  private:
203  void initialize(const std::string &log_dir,
204  PropertiesPtr &, CommitLogBase *init_log, bool is_meta);
205  int roll(CommitLogFileInfo **clfip=0);
206  int compress_and_write(DynamicBuffer &input, BlockHeader *header,
207  int64_t revision, Filesystem::Flags flags);
208  void remove_file_info(CommitLogFileInfo *fi, StringSet &removed_logs);
209 
211  std::set<CommitLogFileInfo *> m_reap_set;
212  std::unique_ptr<BlockCompressionCodec> m_compressor;
213  std::string m_cur_fragment_fname;
217  int32_t m_fd;
218  int32_t m_replication;
220  };
221 
223  typedef std::shared_ptr<CommitLog> CommitLogPtr;
224 
226 
227 }
228 
229 #endif // Hypertable_Lib_CommitLog_h
std::set< String > StringSet
STL Set managing Strings.
Definition: StringExt.h:42
static const char MAGIC_DATA[10]
Definition: CommitLog.h:199
std::map< int64_t, CumulativeFragmentData > CumulativeSizeMap
Definition: CommitLog.h:74
int write(uint64_t cluster_id, DynamicBuffer &buffer, int64_t revision, Filesystem::Flags flags)
Writes a block of updates to the commit log.
Definition: CommitLog.cc:181
void initialize(const std::string &log_dir, PropertiesPtr &, CommitLogBase *init_log, bool is_meta)
Definition: CommitLog.cc:70
Abstract base class for a filesystem.
Flags
Enumeration type for append flags.
Definition: Filesystem.h:76
Program options handling.
const std::string & get_current_fragment_file()
Definition: CommitLog.h:194
int64_t get_max_fragment_size()
Returns the maximum size of each log fragment file.
Definition: CommitLog.h:171
Declarations for BlockCompressionCodec.
A dynamic, resizable and reference counted memory buffer.
Definition: DynamicBuffer.h:42
void load_cumulative_size_map(CumulativeSizeMap &cumulative_size_map)
Fills up a map of cumulative fragment size data.
Definition: CommitLog.cc:510
int64_t size()
Returns total size of commit log.
Definition: CommitLog.h:185
Declarations for CommitLogBlockStream.
void get_stats(const std::string &prefix, std::string &result)
Returns the stats on all commit log fragments.
Definition: CommitLog.cc:544
A dynamic, resizable memory buffer.
int sync()
Sync previous updates written to commit log.
Definition: CommitLog.cc:161
int link_log(uint64_t cluster_id, CommitLogBase *log_base)
Links an external log into this log.
Definition: CommitLog.cc:211
static const char MAGIC_LINK[10]
Definition: CommitLog.h:200
std::shared_ptr< Properties > PropertiesPtr
Definition: Properties.h:447
virtual ~CommitLog()
Definition: CommitLog.cc:65
int close()
Closes the log.
Definition: CommitLog.cc:290
int64_t m_max_fragment_size
Definition: CommitLog.h:215
void remove_file_info(CommitLogFileInfo *fi, StringSet &removed_logs)
Definition: CommitLog.cc:366
int64_t m_cur_fragment_length
Definition: CommitLog.h:214
Commit log for persisting range updates.
Definition: CommitLog.h:63
Hypertable definitions
std::shared_ptr< Filesystem > FilesystemPtr
Smart pointer to Filesystem.
Definition: Filesystem.h:572
std::shared_ptr< CommitLog > CommitLogPtr
Smart pointer to CommitLog.
Definition: CommitLog.h:223
std::string m_cur_fragment_fname
Definition: CommitLog.h:213
int flush()
Flushes previous updates written to commit log.
Definition: CommitLog.cc:143
A String class based on std::string.
LogFragmentQueue m_fragment_queue
std::set< CommitLogFileInfo * > m_reap_set
Definition: CommitLog.h:211
uint32_t m_cur_fragment_num
Definition: CommitLog.h:216
int64_t get_timestamp()
Atomically obtains a timestamp.
Definition: CommitLog.cc:139
int roll(CommitLogFileInfo **clfip=0)
Definition: CommitLog.cc:406
Base class for block headers.
Definition: BlockHeader.h:48
std::unique_ptr< BlockCompressionCodec > m_compressor
Definition: CommitLog.h:212
int purge(int64_t revision, StringSet &remove_ok_logs, StringSet &removed_logs, std::string *trace)
Purges the log.
Definition: CommitLog.cc:310
int compress_and_write(DynamicBuffer &input, BlockHeader *header, int64_t revision, Filesystem::Flags flags)
Definition: CommitLog.cc:477
FilesystemPtr m_fs
Definition: CommitLog.h:210
CommitLog(FilesystemPtr &fs, const std::string &log_dir, PropertiesPtr &props, CommitLogBase *init_log=0, bool is_meta=true)
Constructs a CommitLog object using supplied properties.
Definition: CommitLog.h:85