YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/tools/fs_dump-tool.cc
Line
Count
Source (jump to first uncovered line)
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
//
18
// The following only applies to changes made to this file as part of YugaByte development.
19
//
20
// Portions Copyright (c) YugaByte, Inc.
21
//
22
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
23
// in compliance with the License.  You may obtain a copy of the License at
24
//
25
// http://www.apache.org/licenses/LICENSE-2.0
26
//
27
// Unless required by applicable law or agreed to in writing, software distributed under the License
28
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
29
// or implied.  See the License for the specific language governing permissions and limitations
30
// under the License.
31
//
32
// Tool to dump tablets
33
#include <iostream>
34
#include <memory>
35
#include <vector>
36
37
#include <gflags/gflags.h>
38
#include <glog/logging.h>
39
40
#include "yb/tools/fs_tool.h"
41
#include "yb/util/flags.h"
42
#include "yb/util/logging.h"
43
#include "yb/util/status.h"
44
#include "yb/util/status_log.h"
45
46
DEFINE_int32(nrows, 0, "Number of rows to dump");
47
DEFINE_bool(metadata_only, false, "Whether just to dump the block metadata, "
48
                                  "when printing blocks.");
49
50
/*
51
  TODO: support specifying start and end keys
52
53
  DEFINE_string(start_key, "", "Start key for rows to dump");
54
  DEFINE_string(end_key, "", "Start key for rows to dump");
55
*/
56
57
DEFINE_bool(headers_only, false, "Don't dump contents, dump headers only");
58
59
namespace yb {
60
namespace tools {
61
62
using std::string;
63
using std::vector;
64
using strings::Substitute;
65
66
namespace {
67
68
enum CommandType {
69
  DUMP_TABLET_DATA,
70
  PRINT_TABLET_META,
71
  PRINT_UUID,
72
};
73
74
struct CommandHandler {
75
  CommandType type_;
76
  string name_;
77
  string desc_;
78
79
  CommandHandler(CommandType type, string name, string desc)
80
0
      : type_(type), name_(std::move(name)), desc_(std::move(desc)) {}
81
};
82
83
const vector<CommandHandler> kCommandHandlers = {
84
    CommandHandler(DUMP_TABLET_DATA, "dump_tablet_data",
85
                   "Dump a tablet's data (requires a tablet id)"),
86
    CommandHandler(PRINT_TABLET_META, "print_meta",
87
                   "Print a tablet metadata (requires a tablet id)"),
88
    CommandHandler(PRINT_UUID, "print_uuid",
89
                   "Print the UUID (master or TS) to whom the data belongs") };
90
91
0
void PrintUsageToStream(const std::string& prog_name, std::ostream* out) {
92
0
  *out << "Usage: " << prog_name
93
0
       << " [-headers_only] [-nrows <num rows>] "
94
0
       << "-fs_wal_dirs <dirs> -fs_data_dirs <dirs> <command> <options> "
95
0
       << std::endl << std::endl;
96
0
  *out << "Commands: " << std::endl;
97
0
  for (const CommandHandler& handler : kCommandHandlers) {
98
0
    *out << handler.name_ << ": " << handler.desc_ << std::endl;
99
0
  }
100
0
}
101
0
void Usage(const string& prog_name, const string& msg) {
102
0
  std::cerr << "Error " << prog_name << ": " << msg << std::endl;
103
0
  PrintUsageToStream(prog_name, &std::cerr);
104
0
}
105
106
0
bool ValidateCommand(int argc, char** argv, CommandType* out) {
107
0
  if (argc < 2) {
108
0
    Usage(argv[0], "At least one command must be specified!");
109
0
    return false;
110
0
  }
111
0
  for (const CommandHandler& handler : kCommandHandlers) {
112
0
    if (argv[1] == handler.name_) {
113
0
      *out = handler.type_;
114
0
      return true;
115
0
    }
116
0
  }
117
0
  Usage("Invalid command specified: ", argv[1]);
118
0
  return false;
119
0
}
120
121
} // anonymous namespace
122
123
0
static int FsDumpToolMain(int argc, char** argv) {
124
0
  FLAGS_logtostderr = 1;
125
0
  std::stringstream usage_str;
126
0
  PrintUsageToStream(argv[0], &usage_str);
127
0
  google::SetUsageMessage(usage_str.str());
128
0
  ParseCommandLineFlags(&argc, &argv, true);
129
0
  InitGoogleLoggingSafe(argv[0]);
130
131
0
  CommandType cmd;
132
0
  if (!ValidateCommand(argc, argv, &cmd)) {
133
0
    return 2;
134
0
  }
135
136
0
  FsTool fs_tool(FLAGS_headers_only ? FsTool::HEADERS_ONLY : FsTool::MAXIMUM);
137
0
  CHECK_OK(fs_tool.Init());
138
139
0
  DumpOptions opts;
140
  // opts.start_key = FLAGS_start_key;
141
  // opts.end_key = FLAGS_end_key;
142
0
  opts.nrows = FLAGS_nrows;
143
0
  opts.metadata_only = FLAGS_metadata_only;
144
145
0
  switch (cmd) {
146
0
    case DUMP_TABLET_DATA:
147
0
    {
148
0
      if (argc < 3) {
149
0
        Usage(argv[0],
150
0
              Substitute("dump_tablet requires tablet id: $0 "
151
0
                         "dump_tablet <tablet_id>",
152
0
                         argv[0]));
153
0
        return 2;
154
0
      }
155
0
      CHECK_OK(fs_tool.DumpTabletData(argv[2]));
156
0
      break;
157
0
    }
158
159
0
    case PRINT_TABLET_META: {
160
0
      if (argc < 3) {
161
0
        Usage(argv[0], Substitute("print_meta requires a tablet id: $0"
162
0
                                  "print_meta <tablet_id>", argv[0]));
163
0
        return 2;
164
0
      }
165
0
      CHECK_OK(fs_tool.PrintTabletMeta(argv[2], 0));
166
0
      break;
167
0
    }
168
0
    case PRINT_UUID: {
169
0
      if (argc < 2) {
170
0
        Usage(argv[0], Substitute("$0 print_uuid", argv[0]));
171
0
        return 2;
172
0
      }
173
0
      CHECK_OK(fs_tool.PrintUUID(0));
174
0
      break;
175
0
    }
176
0
  }
177
178
0
  return 0;
179
0
}
180
181
} // namespace tools
182
} // namespace yb
183
184
13.2k
int main(int argc, char** argv) {
185
13.2k
  return yb::tools::FsDumpToolMain(argc, argv);
186
13.2k
}