-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
ServerCommandLine.java
175 lines (160 loc) · 5.67 KB
/
ServerCommandLine.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/**
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.hadoop.hbase.util;
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Locale;
import java.util.Map.Entry;
import java.util.Set;
import org.apache.hadoop.hbase.HConstants;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
import org.apache.yetus.audience.InterfaceAudience;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static org.apache.hadoop.hbase.util.Threads.isNonDaemonThreadRunning;
/**
* Base class for command lines that start up various HBase daemons.
*/
@InterfaceAudience.Private
public abstract class ServerCommandLine extends Configured implements Tool {
private static final Logger LOG = LoggerFactory.getLogger(ServerCommandLine.class);
@SuppressWarnings("serial")
private static final Set<String> DEFAULT_SKIP_WORDS = new HashSet<String>() {
{
add("secret");
add("passwd");
add("password");
add("credential");
}
};
/**
* Implementing subclasses should return a usage string to print out.
*/
protected abstract String getUsage();
/**
* Print usage information for this command line.
*
* @param message if not null, print this message before the usage info.
*/
protected void usage(String message) {
if (message != null) {
System.err.println(message);
System.err.println("");
}
System.err.println(getUsage());
}
/**
* Log information about the currently running JVM.
*/
public static void logJVMInfo() {
// Print out vm stats before starting up.
RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
if (runtime != null) {
LOG.info("vmName=" + runtime.getVmName() + ", vmVendor=" +
runtime.getVmVendor() + ", vmVersion=" + runtime.getVmVersion());
LOG.info("vmInputArguments=" + runtime.getInputArguments());
}
}
/**
* Print into log some of the important hbase attributes.
*/
private static void logHBaseConfigs(Configuration conf) {
final String [] keys = new String [] {
// Expand this list as you see fit.
"hbase.tmp.dir",
HConstants.HBASE_DIR,
HConstants.CLUSTER_DISTRIBUTED,
HConstants.ZOOKEEPER_QUORUM,
};
for (String key: keys) {
LOG.info(key + ": " + conf.get(key));
}
}
/**
* Logs information about the currently running JVM process including
* the environment variables. Logging of env vars can be disabled by
* setting {@code "hbase.envvars.logging.disabled"} to {@code "true"}.
* <p>If enabled, you can also exclude environment variables containing
* certain substrings by setting {@code "hbase.envvars.logging.skipwords"}
* to comma separated list of such substrings.
*/
public static void logProcessInfo(Configuration conf) {
logHBaseConfigs(conf);
// log environment variables unless asked not to
if (conf == null || !conf.getBoolean("hbase.envvars.logging.disabled", false)) {
Set<String> skipWords = new HashSet<>(DEFAULT_SKIP_WORDS);
if (conf != null) {
String[] confSkipWords = conf.getStrings("hbase.envvars.logging.skipwords");
if (confSkipWords != null) {
skipWords.addAll(Arrays.asList(confSkipWords));
}
}
nextEnv:
for (Entry<String, String> entry : System.getenv().entrySet()) {
String key = entry.getKey().toLowerCase(Locale.ROOT);
String value = entry.getValue().toLowerCase(Locale.ROOT);
// exclude variables which may contain skip words
for(String skipWord : skipWords) {
if (key.contains(skipWord) || value.contains(skipWord))
continue nextEnv;
}
LOG.info("env:"+entry);
}
}
// and JVM info
logJVMInfo();
}
/**
* Parse and run the given command line. This will exit the JVM with
* the exit code returned from <code>run()</code>.
* If return code is 0, wait for atmost 30 seconds for all non-daemon threads to quit,
* otherwise exit the jvm
*/
public void doMain(String args[]) {
try {
int ret = ToolRunner.run(HBaseConfiguration.create(), this, args);
if (ret != 0) {
System.exit(ret);
}
// Return code is 0 here.
boolean forceStop = false;
long startTime = EnvironmentEdgeManager.currentTime();
while (isNonDaemonThreadRunning()) {
if (EnvironmentEdgeManager.currentTime() - startTime > 30 * 1000) {
forceStop = true;
break;
}
Thread.sleep(1000);
}
if (forceStop) {
LOG.error("Failed to stop all non-daemon threads, so terminating JVM");
System.exit(-1);
}
} catch (Exception e) {
LOG.error("Failed to run", e);
System.exit(-1);
}
}
}