0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
dumplog.cc
Go to the documentation of this file.
1 /*
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; either version 3
9  * of the 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 
22 #include <Common/Compat.h>
23 
28 
29 #include <FsBroker/Lib/Config.h>
30 #include <FsBroker/Lib/Client.h>
31 
32 #include <AsyncComm/Comm.h>
35 
36 #include <Common/Error.h>
37 #include <Common/InetAddr.h>
38 #include <Common/Logger.h>
39 #include <Common/Init.h>
40 #include <Common/Usage.h>
41 
42 #include <boost/algorithm/string/predicate.hpp>
43 
44 #include <cstdlib>
45 #include <iostream>
46 
47 using namespace Hypertable;
48 using namespace Config;
49 using namespace std;
50 
51 namespace {
52 
53 struct AppPolicy : Config::Policy {
54  static void init_options() {
55  cmdline_desc("Usage: %s [options] <log-dir>\n\n"
56  " This program dumps the given log's metadata.\n\nOptions")
57  .add_options()
58  ("block-summary", boo()->zero_tokens()->default_value(false), "Display commit log block information only")
59  ("display-values", boo()->zero_tokens()->default_value(false), "Display values (assumes they're printable)")
60  ("linked-logs", boo()->zero_tokens()->default_value(false), "Display valid (non-deleted) linked logs")
61  ;
62  cmdline_hidden_desc().add_options()("log-dir", str(), "dfs log dir");
63  cmdline_positional_desc().add("log-dir", -1);
64  }
65  static void init() {
66  if (!has("log-dir")) {
67  HT_ERROR_OUT <<"log-dir required\n"<< cmdline_desc() << HT_END;
68  exit(EXIT_FAILURE);
69  }
70  }
71 };
72 
73 typedef Meta::list<AppPolicy, FsClientPolicy, DefaultCommPolicy> Policies;
74 
75 void display_log(FsBroker::Lib::ClientPtr &dfs_client, const String &prefix,
76  CommitLogReader *log_reader, bool display_values);
77 void display_log_block_summary(FsBroker::Lib::ClientPtr &dfs_client,
78  const String &prefix, CommitLogReader *log_reader);
79 void display_log_valid_links(FsBroker::Lib::ClientPtr &dfs_client,
80  const String &prefix, CommitLogReader *log_reader);
81 
82 } // local namespace
83 
84 
85 int main(int argc, char **argv) {
86  try {
87  init_with_policies<Policies>(argc, argv);
88 
89  ConnectionManagerPtr conn_manager_ptr = make_shared<ConnectionManager>();
90 
91  String log_dir = get_str("log-dir");
92  String log_host = get("log-host", String());
93  int timeout = get_i32("dfs-timeout", 15000);
94  bool block_summary = get_bool("block-summary");
95  bool linked_logs = get_bool("linked-logs");
96 
100  FsBroker::Lib::ClientPtr dfs_client;
101 
102  if (log_host.length()) {
103  int log_port = get_i16("log-port");
104  InetAddr addr(log_host, log_port);
105 
106  dfs_client = std::make_shared<FsBroker::Lib::Client>(conn_manager_ptr, addr, timeout);
107  }
108  else {
109  dfs_client = std::make_shared<FsBroker::Lib::Client>(conn_manager_ptr, properties);
110  }
111 
112  if (!dfs_client->wait_for_connection(timeout)) {
113  HT_ERROR("Unable to connect to DFS Broker, exiting...");
114  exit(EXIT_FAILURE);
115  }
116 
117  FilesystemPtr fs = dfs_client;
118 
119  boost::trim_right_if(log_dir, boost::is_any_of("/"));
120 
121  CommitLogReaderPtr log_reader = make_shared<CommitLogReader>(fs, log_dir);
122 
123  if (block_summary) {
124  printf("LOG %s\n", log_dir.c_str());
125  display_log_block_summary(dfs_client, "", log_reader.get());
126  }
127  else if (linked_logs) {
128  display_log_valid_links(dfs_client, log_dir, log_reader.get());
129  }
130  else
131  display_log(dfs_client, "", log_reader.get(), has("display-values"));
132  }
133  catch (Exception &e) {
134  HT_ERROR_OUT << e << HT_END;
135  return 1;
136  }
137  return 0;
138 }
139 
140 
141 namespace {
142 
143  void
144  display_log(FsBroker::Lib::ClientPtr &dfs_client, const String &prefix,
145  CommitLogReader *log_reader, bool display_values) {
146  BlockHeaderCommitLog header;
147  const uint8_t *base;
148  size_t len;
149  const uint8_t *ptr, *end;
150  TableIdentifier table_id;
151  ByteString bs;
152  Key key;
153  String value;
154  uint32_t blockno=0;
155 
156  while (log_reader->next(&base, &len, &header)) {
157 
159 
160  ptr = base;
161  end = base + len;
162 
163  size_t len_saved = len;
164  try {
165  table_id.decode(&ptr, &len);
166  }
167  catch (Exception &e) {
168  if (e.code() == Error::PROTOCOL_ERROR) {
169  len = len_saved;
170  ptr = base;
171  legacy_decode(&ptr, &len, &table_id);
172  }
173  else
174  throw;
175  }
176 
177  while (ptr < end) {
178 
179  // extract the key
180  bs.ptr = ptr;
181  key.load(bs);
182  cout << key;
183  bs.next();
184 
185  if (display_values) {
186  const uint8_t *vptr;
187  size_t slen = bs.decode_length(&vptr);
188  cout << " value='" << std::string((char *)vptr, slen) << "'";
189  }
190 
191  //cout << " bno=" << blockno << endl;
192  cout << endl;
193 
194  // skip value
195  bs.next();
196  ptr = bs.ptr;
197 
198  if (ptr > end)
199  HT_THROW(Error::REQUEST_TRUNCATED, "Problem decoding value");
200  }
201  blockno++;
202  }
203  }
204 
205 
206 
207  void
208  display_log_block_summary(FsBroker::Lib::ClientPtr &dfs_client, const String &prefix,
209  CommitLogReader *log_reader) {
210  CommitLogBlockInfo binfo;
211  BlockHeaderCommitLog header;
212 
213  while (log_reader->next_raw_block(&binfo, &header)) {
214 
216 
217  printf("%s/%s\tcluster_id\t%llu\n",
218  binfo.log_dir, binfo.file_fragment, (Llu)header.get_cluster_id());
219  printf("%s/%s\trevision\t%llu\n",
220  binfo.log_dir, binfo.file_fragment, (Llu)header.get_revision());
221  printf("%s/%s\tstart-offset\t%llu\n",
222  binfo.log_dir, binfo.file_fragment, (Llu)binfo.start_offset);
223  printf("%s/%s\tend-offset\t%llu\n",
224  binfo.log_dir, binfo.file_fragment, (Llu)binfo.end_offset);
225 
226  if (binfo.error == Error::OK) {
227  printf("%s/%s\tlength\t%u\n",
228  binfo.log_dir, binfo.file_fragment,
229  header.get_data_length());
230  printf("%s/%s\tztype\t%s\n",
231  binfo.log_dir, binfo.file_fragment,
233  printf("%s/%s\tzlen\t%u\n",
234  binfo.log_dir, binfo.file_fragment,
235  header.get_data_zlength());
236  }
237  else
238  printf("%s/%s\terror\t%s\n",
239  binfo.log_dir, binfo.file_fragment,
240  Error::get_text(binfo.error));
241  }
242  }
243 
244  void
245  display_log_valid_links(FsBroker::Lib::ClientPtr &dfs_client, const String &prefix,
246  CommitLogReader *log_reader) {
247  BlockHeaderCommitLog header;
248  const uint8_t *base;
249  size_t len;
250  StringSet linked_logs;
251 
252  while (log_reader->next(&base, &len, &header))
253  ;
254 
255  log_reader->get_linked_logs(linked_logs);
256 
257  for (const auto &name : linked_logs)
258  std::cout << name << "\n";
259  std::cout << std::flush;
260 
261  }
262 
263 } // local namespace
std::set< String > StringSet
STL Set managing Strings.
Definition: StringExt.h:42
Interface and base of config policy.
Definition: Config.h:149
static const char MAGIC_DATA[10]
Definition: CommitLog.h:199
Holds information about an individual block.
PropertiesPtr properties
This singleton map stores all options.
Definition: Config.cc:47
std::string String
A String is simply a typedef to std::string.
Definition: String.h:44
bool check_magic(const char *magic)
Compares a given character sequence with the magic field.
Definition: BlockHeader.h:86
Helper class for printing usage banners on the command line.
Declarations for CommitLogReader.
void init(int argc, char *argv[], const Desc *desc=NULL)
Initialize with default policy.
Definition: Init.h:95
Declarations for TableIdentifier and TableIdentifierManaged.
uint16_t get_compression_type()
Gets the compression type field.
Definition: BlockHeader.h:128
long long unsigned int Llu
Shortcut for printf formats.
Definition: String.h:50
Po::typed_value< String > * str(String *v=0)
Definition: Properties.h:166
STL namespace.
bool next_raw_block(CommitLogBlockInfo *, BlockHeaderCommitLog *)
uint64_t get_cluster_id()
Gets the cluster ID field.
Desc & cmdline_desc(const char *usage)
A macro which definds global functions like get_bool(), get_str(), get_i16() etc. ...
Definition: Config.cc:72
bool has(const String &name)
Check existence of a configuration value.
Definition: Config.h:57
std::shared_ptr< CommitLogReader > CommitLogReaderPtr
Smart pointer to CommitLogReader.
A class managing one or more serializable ByteStrings.
Definition: ByteString.h:47
#define HT_ASSERT(_e_)
Definition: Logger.h:396
uint32_t get_data_length()
Gets the uncompressed data length field.
Definition: BlockHeader.h:96
int main(int argc, char **argv)
Definition: dumplog.cc:85
const char * get_text(int error)
Returns a descriptive error message.
Definition: Error.cc:330
Encapsulate an internet address.
Definition: InetAddr.h:66
std::shared_ptr< Client > ClientPtr
Smart pointer to Client.
Definition: Client.h:233
int error
Error (if any) encountered while reading block
Logging routines and macros.
Compatibility Macros for C/C++.
bool load(const SerializedKey &key)
Parses the opaque key and loads the components into the member variables.
Definition: Key.cc:158
Po::typed_value< bool > * boo(bool *v=0)
Definition: Properties.h:162
Initialization helper for applications.
#define HT_END
Definition: Logger.h:220
#define HT_ERROR_OUT
Definition: Logger.h:301
virtual void decode(const uint8_t **bufp, size_t *remainp)
Reads serialized representation of object from a buffer.
Definition: Serializable.cc:70
Provides sequential access to blocks in a commit log.
const uint8_t * ptr
The pointer to the serialized data.
Definition: ByteString.h:121
Hypertable definitions
static const char * get_compressor_name(uint16_t algo)
Returns string mnemonic for compression type.
void legacy_decode(const uint8_t **bufp, size_t *remainp, BalancePlan *plan)
#define HT_ERROR(msg)
Definition: Logger.h:299
Declarations for ConnectionManager.
std::shared_ptr< Filesystem > FilesystemPtr
Smart pointer to Filesystem.
Definition: Filesystem.h:572
Declarations for Comm.
size_t decode_length(const uint8_t **dptr) const
Retrieves the decoded length and returns a pointer to the string.
Definition: ByteString.h:83
Provides access to internal components of opaque key.
Definition: Key.h:40
Internet address wrapper classes and utility functions.
Meta::list< MyPolicy, DefaultPolicy > Policies
Declarations for ReactorFactory.
const char * log_dir
Log directory.
This is a generic exception class for Hypertable.
Definition: Error.h:314
bool next(const uint8_t **blockp, size_t *lenp, BlockHeaderCommitLog *)
uint64_t start_offset
Starting offset of block within fragment file.
Declarations for CommitLog.
uint32_t get_data_zlength()
Gets the compressed data length field.
Definition: BlockHeader.h:106
uint64_t end_offset
Ending offset of block within fragment file.
std::shared_ptr< ConnectionManager > ConnectionManagerPtr
Smart pointer to ConnectionManager.
void get_linked_logs(StringSet &linked_logs)
Desc & cmdline_hidden_desc()
Get the command line hidden options description (for positional options)
Definition: Config.cc:81
PositionalDesc & cmdline_positional_desc()
Get the command line positional options description.
Definition: Config.cc:90
Error codes, Exception handling, error logging.
int64_t get_revision()
Gets the revision number field.
#define HT_THROW(_code_, _msg_)
Definition: Error.h:478
Declarations for Client.
uint8_t * next()
Retrieves the next serialized String in the buffer.
Definition: ByteString.h:71
int code() const
Returns the error code.
Definition: Error.h:391
const char * file_fragment
File name of log fragment within log_dir.