0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
metalog_dump.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 
25 
28 
29 #include <Hypertable/Lib/MetaLog.h>
31 
32 #include <FsBroker/Lib/Client.h>
33 #include <FsBroker/Lib/Config.h>
34 
35 #include <AsyncComm/Comm.h>
37 
38 #include <Common/Error.h>
39 #include <Common/FileUtils.h>
40 #include <Common/InetAddr.h>
41 #include <Common/Init.h>
42 #include <Common/Logger.h>
43 #include <Common/Usage.h>
44 
45 #include <boost/algorithm/string.hpp>
46 
47 #include <cctype>
48 #include <cstdlib>
49 #include <cstring>
50 #include <iostream>
51 #include <unordered_map>
52 
53 using namespace Hypertable;
54 using namespace Config;
55 using namespace std;
56 
57 namespace {
58 
59  struct AppPolicy : Config::Policy {
60  static void init_options() {
61  cmdline_desc("Usage: %s [options] <log-dir>\n\n"
62  " This program dumps a textual representation of\n"
63  " the given metalog. If the last path component\n"
64  " is a number, then the it is assumed to be an\n"
65  " individual log file, otherwise the path is assumed\n"
66  " to be the metalog directory\n\nOptions")
67  .add_options()
68  ("all", "Display all entities in log (not just latest state)")
69  ("metadata-tsv", "For each Range, dump StartRow and Location .tsv lines")
70  ("location", str()->default_value(""),
71  "Used with --metadata-tsv to specify location proxy")
72  ("print-logs", boo()->zero_tokens()->default_value(false), "Print log files, one per line")
73  ("show-version", "Display log version number and exit")
74  ;
75  cmdline_hidden_desc().add_options()("log-path", str(), "dfs log path");
76  cmdline_positional_desc().add("log-path", -1);
77  }
78  static void init() {
79  if (!has("log-path")) {
80  HT_ERROR_OUT <<"log-path required\n"<< cmdline_desc() << HT_END;
81  exit(EXIT_FAILURE);
82  }
83  }
84  };
85 
86  typedef Meta::list<FsClientPolicy, DefaultCommPolicy, AppPolicy> Policies;
87 
88  void determine_log_type(FilesystemPtr &fs, String path, String &name, bool *is_file) {
89  bool check_file=false;
90  boost::trim_right_if(path, boost::is_any_of("/"));
91 
92  *is_file = false;
93 
94  const char *base, *ptr;
95  if ((base = strrchr(path.c_str(), '/')) != (const char *)0) {
96  for (ptr=base+1; *ptr && isdigit(*ptr); ptr++)
97  ;
98  if (*ptr == 0)
99  *is_file = true;
100  }
101 
102  if (!*is_file) {
103  std::vector<Filesystem::Dirent> listing;
104  fs->readdir(path, listing);
105  if (!listing.empty()) {
106  path += String("/") + listing[0].name;
107  check_file = true;
108  }
109  }
110 
111  if (*is_file || check_file) {
112  int fd = fs->open(path, Filesystem::OPEN_FLAG_VERIFY_CHECKSUM);
113  MetaLog::Header header;
114  uint8_t buf[MetaLog::Header::LENGTH];
115  const uint8_t *ptr = buf;
116  size_t remaining = MetaLog::Header::LENGTH;
117 
118  size_t nread = fs->read(fd, buf, MetaLog::Header::LENGTH);
119 
120  if (nread != MetaLog::Header::LENGTH)
122  "Short read of header for '%s' (expected %d, got %d)",
123  path.c_str(), (int)MetaLog::Header::LENGTH, (int)nread);
124 
125  header.decode(&ptr, &remaining);
126  name = header.name;
127  fs->close(fd);
128  }
129  else
130  name = String(base+1);
131  }
132 
133 
134 } // local namespace
135 
136 
137 int main(int argc, char **argv) {
138  try {
139  init_with_policies<Policies>(argc, argv);
140 
141  ConnectionManagerPtr conn_manager_ptr = make_shared<ConnectionManager>();
142 
143  String log_path = get_str("log-path");
144  String log_host = get("log-host", String());
145  int timeout = has("dfs-timeout") ? get_i32("dfs-timeout") : 10000;
146  bool dump_all = has("all");
147  bool show_version = has("show-version");
148  bool metadata_tsv = has("metadata-tsv");
149  bool print_logs = get_bool("print-logs");
150  String location;
151 
152  if (metadata_tsv) {
153  if (has("location") && get_str("location").size()>0)
154  location = get_str("location");
155  else
156  location = FileUtils::file_to_string(System::install_dir + (String)"/run/location");
157  }
158 
162  FsBroker::Lib::ClientPtr dfs_client;
163 
164  if (log_host.length()) {
165  int log_port = get_i16("log-port");
166  InetAddr addr(log_host, log_port);
167 
168  dfs_client = std::make_shared<FsBroker::Lib::Client>(conn_manager_ptr, addr, timeout);
169  }
170  else {
171  dfs_client = std::make_shared<FsBroker::Lib::Client>(conn_manager_ptr, properties);
172  }
173 
174  if (!dfs_client->wait_for_connection(timeout)) {
175  HT_ERROR("Unable to connect to DFS Broker, exiting...");
176  exit(EXIT_FAILURE);
177  }
178 
179  // Population Defintion map
180  std::unordered_map<String, MetaLog::DefinitionPtr> defmap;
181  MetaLog::DefinitionPtr def = make_shared<MetaLog::DefinitionRangeServer>("");
182  defmap[def->name()] = def;
183  def = make_shared<MetaLog::DefinitionMaster>("");
184  defmap[def->name()] = def;
185 
186  FilesystemPtr fs = dfs_client;
187  MetaLog::ReaderPtr rsml_reader;
188  String name;
189  bool is_file;
190 
191  determine_log_type(fs, log_path, name, &is_file);
192 
193  auto iter = defmap.find(name);
194  if (iter == defmap.end()) {
195  cerr << "No definition for log type '" << name << "'" << endl;
196  exit(EXIT_FAILURE);
197  }
198  def = iter->second;
199 
200  int reader_flags = dump_all ? MetaLog::Reader::LOAD_ALL_ENTITIES : 0;
201  if (is_file) {
202  rsml_reader = make_shared<MetaLog::Reader>(fs, def, reader_flags);
203  rsml_reader->load_file(log_path);
204  }
205  else
206  rsml_reader = make_shared<MetaLog::Reader>(fs, def, log_path, reader_flags);
207 
208  if (!metadata_tsv && !print_logs)
209  cout << "log version: " << rsml_reader->version() << "\n";
210 
211  if (show_version)
212  quick_exit(EXIT_SUCCESS);
213 
214  std::vector<MetaLog::EntityPtr> entities;
215 
216  if (dump_all)
217  rsml_reader->get_all_entities(entities);
218  else
219  rsml_reader->get_entities(entities);
220 
221  MetaLogEntityRange *entity_range;
222  if (metadata_tsv) {
223  for (size_t i=0; i<entities.size(); i++) {
224  entity_range = dynamic_cast<MetaLogEntityRange *>(entities[i].get());
225  if (entity_range) {
226  TableIdentifier table;
227  String start_row, end_row;
228  entity_range->get_table_identifier(table);
229  entity_range->get_boundary_rows(start_row, end_row);
230  cout << table.id << ":" << end_row << "\tStartRow\t" << start_row << "\n";
231  cout << table.id << ":" << end_row << "\tLocation\t" << location << "\n";
232  }
233  }
234  }
235  else if (print_logs) {
236  for (auto &entity : entities) {
237  entity_range = dynamic_cast<MetaLogEntityRange *>(entity.get());
238  if (entity_range) {
239  String log = entity_range->get_transfer_log();
240  if (!log.empty())
241  std::cout << log << "\n";
242  }
243  }
244  std::cout << std::flush;
245  }
246  else {
247  for (size_t i=0; i<entities.size(); i++)
248  cout << *entities[i] << "\n";
249  }
250 
251  cout << flush;
252 
253  }
254  catch (Exception &e) {
255  HT_ERROR_OUT << e << HT_END;
256  return 1;
257  }
258  quick_exit(EXIT_SUCCESS);
259 }
void get_table_identifier(TableIdentifier &table)
Copies table identifier.
Interface and base of config policy.
Definition: Config.h:149
Declarations for MetaLog::Reader.
char name[14]
MetaLog definition name (e.g. "mml" or "rsml")
Definition: MetaLog.h:88
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
Declarations for MetaLogEntityRange.
Helper class for printing usage banners on the command line.
void init(int argc, char *argv[], const Desc *desc=NULL)
Initialize with default policy.
Definition: Init.h:95
Po::typed_value< String > * str(String *v=0)
Definition: Properties.h:166
STL namespace.
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
File system utility functions.
void decode(const uint8_t **bufp, size_t *remainp)
Decodes serialized header.
Definition: MetaLog.cc:46
MetaLog entity for range state persisted in RSML.
Encapsulate an internet address.
Definition: InetAddr.h:66
std::shared_ptr< Client > ClientPtr
Smart pointer to Client.
Definition: Client.h:233
String get_transfer_log()
Gets transfer log.
void get_boundary_rows(String &start, String &end)
Gets boundary rows (start and end rows)
Logging routines and macros.
Compatibility Macros for C/C++.
Po::typed_value< bool > * boo(bool *v=0)
Definition: Properties.h:162
Initialization helper for applications.
#define HT_END
Definition: Logger.h:220
Static header length.
Definition: MetaLog.h:81
#define HT_ERROR_OUT
Definition: Logger.h:301
Hypertable definitions
std::shared_ptr< Reader > ReaderPtr
Smart pointer to Reader.
#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.
#define HT_THROWF(_code_, _fmt_,...)
Definition: Error.h:490
Internet address wrapper classes and utility functions.
Meta::list< MyPolicy, DefaultPolicy > Policies
static String install_dir
The installation directory.
Definition: System.h:114
This is a generic exception class for Hypertable.
Definition: Error.h:314
Declarations for MetaLog.
int main(int argc, char **argv)
std::shared_ptr< ConnectionManager > ConnectionManagerPtr
Smart pointer to ConnectionManager.
Desc & cmdline_hidden_desc()
Get the command line hidden options description (for positional options)
Definition: Config.cc:81
Metalog file header.
Definition: MetaLog.h:57
PositionalDesc & cmdline_positional_desc()
Get the command line positional options description.
Definition: Config.cc:90
Error codes, Exception handling, error logging.
static String file_to_string(const String &fname)
Reads a full file into a String.
Definition: FileUtils.cc:333
Declarations for Client.
std::shared_ptr< Definition > DefinitionPtr
Smart pointer to Definition.