1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.hbtop;
19
20 import java.util.ArrayList;
21 import java.util.List;
22 import java.util.Objects;
23
24 import org.apache.commons.cli.BasicParser;
25 import org.apache.commons.cli.CommandLine;
26 import org.apache.commons.cli.HelpFormatter;
27 import org.apache.commons.cli.Options;
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.apache.hadoop.conf.Configuration;
31 import org.apache.hadoop.conf.Configured;
32 import org.apache.hadoop.hbase.HBaseConfiguration;
33 import org.apache.hadoop.hbase.HBaseInterfaceAudience;
34 import org.apache.hadoop.hbase.classification.InterfaceAudience;
35 import org.apache.hadoop.hbase.hbtop.field.Field;
36 import org.apache.hadoop.hbase.hbtop.field.FieldInfo;
37 import org.apache.hadoop.hbase.hbtop.mode.Mode;
38 import org.apache.hadoop.hbase.hbtop.screen.Screen;
39 import org.apache.hadoop.util.Tool;
40 import org.apache.hadoop.util.ToolRunner;
41
42
43
44
45 @InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.TOOLS)
46 public class HBTop extends Configured implements Tool {
47
48 private static final Log LOG = LogFactory.getLog(HBTop.class);
49
50 public HBTop() {
51 this(HBaseConfiguration.create());
52 }
53
54 public HBTop(Configuration conf) {
55 super(Objects.requireNonNull(conf));
56 }
57
58 @Override
59 public int run(String[] args) throws Exception {
60 long initialRefreshDelay = 3 * 1000;
61 Mode initialMode = Mode.REGION;
62 List<Field> initialFields = null;
63 Field initialSortField = null;
64 Boolean initialAscendingSort = null;
65 List<RecordFilter> initialFilters = null;
66 long numberOfIterations = Long.MAX_VALUE;
67 boolean batchMode = false;
68 try {
69 Options opts = getOptions();
70 CommandLine commandLine = new BasicParser().parse(opts, args);
71
72 if (commandLine.hasOption("help")) {
73 printUsage(opts);
74 return 0;
75 }
76
77 if (commandLine.hasOption("mode")) {
78 String mode = commandLine.getOptionValue("mode");
79 switch (mode) {
80 case "n":
81 initialMode = Mode.NAMESPACE;
82 break;
83
84 case "t":
85 initialMode = Mode.TABLE;
86 break;
87
88 case "r":
89 initialMode = Mode.REGION;
90 break;
91
92 case "s":
93 initialMode = Mode.REGION_SERVER;
94 break;
95
96 default:
97 LOG.warn("Mode set invalid, using default");
98 break;
99 }
100 }
101
102 if (commandLine.hasOption("outputFieldNames")) {
103 for (FieldInfo fieldInfo : initialMode.getFieldInfos()) {
104 System.out.println(fieldInfo.getField().getHeader());
105 }
106 return 0;
107 }
108
109 if (commandLine.hasOption("delay")) {
110 int delay = 0;
111 try {
112 delay = Integer.parseInt(commandLine.getOptionValue("delay"));
113 } catch (NumberFormatException ignored) {
114 }
115
116 if (delay < 1) {
117 LOG.warn("Delay set too low or invalid, using default");
118 } else {
119 initialRefreshDelay = delay * 1000L;
120 }
121 }
122
123 if (commandLine.hasOption("numberOfIterations")) {
124 try {
125 numberOfIterations = Long.parseLong(commandLine.getOptionValue("numberOfIterations"));
126 } catch (NumberFormatException ignored) {
127 LOG.warn("The number of iterations set invalid, ignoring");
128 }
129 }
130
131 if (commandLine.hasOption("sortField")) {
132 String sortField = commandLine.getOptionValue("sortField");
133
134 String field;
135 boolean ascendingSort;
136 if (sortField.startsWith("+")) {
137 field = sortField.substring(1);
138 ascendingSort = false;
139 } else if (sortField.startsWith("-")) {
140 field = sortField.substring(1);
141 ascendingSort = true;
142 } else {
143 field = sortField;
144 ascendingSort = false;
145 }
146
147 FieldInfo fieldInfo = null;
148 for (FieldInfo info : initialMode.getFieldInfos()) {
149 if (info.getField().getHeader().equals(field)) {
150 fieldInfo = info;
151 break;
152 }
153 }
154 if (fieldInfo != null) {
155 initialSortField = fieldInfo.getField();
156 initialAscendingSort = ascendingSort;
157 } else {
158 LOG.warn("The specified sort field " + field + " is not found, using default");
159 }
160 }
161
162 if (commandLine.hasOption("fields")) {
163 String[] fields = commandLine.getOptionValue("fields").split(",");
164 initialFields = new ArrayList<>();
165 for (String field : fields) {
166 FieldInfo fieldInfo = null;
167 for (FieldInfo info : initialMode.getFieldInfos()) {
168 if (info.getField().getHeader().equals(field)) {
169 fieldInfo = info;
170 break;
171 }
172 }
173 if (fieldInfo != null) {
174 initialFields.add(fieldInfo.getField());
175 } else {
176 LOG.warn("The specified field " + field + " is not found, ignoring");
177 }
178 }
179 }
180
181 if (commandLine.hasOption("filters")) {
182 String[] filters = commandLine.getOptionValue("filters").split(",");
183
184 List<Field> fields = new ArrayList<>();
185 for (FieldInfo fieldInfo : initialMode.getFieldInfos()) {
186 fields.add(fieldInfo.getField());
187 }
188
189 for (String filter : filters) {
190 RecordFilter f = RecordFilter.parse(filter, fields, false);
191 if (f != null) {
192 if (initialFilters == null) {
193 initialFilters = new ArrayList<>();
194 }
195 initialFilters.add(f);
196 } else {
197 LOG.warn("The specified filter " + filter + " is invalid, ignoring");
198 }
199 }
200 }
201
202 if (commandLine.hasOption("batchMode")) {
203 batchMode = true;
204 }
205 } catch (Exception e) {
206 LOG.error("Unable to parse options", e);
207 return 1;
208 }
209
210 try (Screen screen = new Screen(getConf(), initialRefreshDelay, initialMode, initialFields,
211 initialSortField, initialAscendingSort, initialFilters, numberOfIterations, batchMode)) {
212 screen.run();
213 }
214
215 return 0;
216 }
217
218 private Options getOptions() {
219 Options opts = new Options();
220 opts.addOption("h", "help", false,
221 "Print usage; for help while the tool is running press 'h'");
222 opts.addOption("d", "delay", true,
223 "The refresh delay (in seconds); default is 3 seconds");
224 opts.addOption("m", "mode", true,
225 "The mode; n (Namespace)|t (Table)|r (Region)|s (RegionServer), default is r");
226 opts.addOption("n", "numberOfIterations", true,
227 "The number of iterations");
228 opts.addOption("s", "sortField", true,
229 "The initial sort field. You can prepend a `+' or `-' to the field name to also override"
230 + " the sort direction. A leading `+' will force sorting high to low, whereas a `-' will"
231 + " ensure a low to high ordering");
232 opts.addOption("O", "outputFieldNames", false,
233 "Print each of the available field names on a separate line, then quit");
234 opts.addOption("f", "fields", true,
235 "Show only the given fields. Specify comma separated fields to show multiple fields");
236 opts.addOption("i", "filters", true,
237 "The initial filters. Specify comma separated filters to set multiple filters");
238 opts.addOption("b", "batchMode", false,
239 "Starts hbtop in Batch mode, which could be useful for sending output from hbtop to other"
240 + " programs or to a file. In this mode, hbtop will not accept input and runs until the"
241 + " iterations limit you've set with the `-n' command-line option or until killed");
242 return opts;
243 }
244
245 private void printUsage(Options opts) {
246 new HelpFormatter().printHelp("hbase hbtop [opts] [-D<property=value>]*", opts);
247 System.out.println("");
248 System.out.println(" Note: -D properties will be applied to the conf used.");
249 System.out.println(" For example:");
250 System.out.println(" -Dhbase.client.zookeeper.quorum=<zookeeper quorum>");
251 System.out.println(" -Dzookeeper.znode.parent=<znode parent>");
252 System.out.println("");
253 }
254
255 public static void main(String[] args) throws Exception {
256 int res = ToolRunner.run(new HBTop(), args);
257 System.exit(res);
258 }
259 }