1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase;
20
21 import java.io.IOException;
22
23 import org.apache.hadoop.conf.Configured;
24 import org.apache.hadoop.hbase.classification.InterfaceAudience;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 @InterfaceAudience.Private
29 public class ZNodeClusterManager extends Configured implements ClusterManager {
30 private static final Logger LOG = LoggerFactory.getLogger(ZNodeClusterManager.class.getName());
31 private static final String SIGKILL = "SIGKILL";
32 private static final String SIGSTOP = "SIGSTOP";
33 private static final String SIGCONT = "SIGCONT";
34 public ZNodeClusterManager() {
35 }
36
37 private String getZKQuorumServersStringFromHbaseConfig() {
38 String port =
39 Integer.toString(getConf().getInt(HConstants.ZOOKEEPER_CLIENT_PORT, 2181));
40 String[] serverHosts = getConf().getStrings(HConstants.ZOOKEEPER_QUORUM, "localhost");
41 StringBuilder sb = new StringBuilder();
42 for (int i = 0; i < serverHosts.length; i++) {
43 serverHosts[i] = serverHosts[i] + ":" + port;
44 sb.append(serverHosts[i]);
45 if (i < serverHosts.length - 1) {
46 sb.append(",");
47 }
48 }
49 return sb.toString();
50 }
51
52 private String createZNode(String hostname, String cmd) throws IOException{
53 LOG.info("Zookeeper Mode enabled sending command to zookeeper + " +
54 cmd + "hostname:" + hostname);
55 ChaosZKClient chaosZKClient = new ChaosZKClient(getZKQuorumServersStringFromHbaseConfig());
56 return chaosZKClient.submitTask(new ChaosZKClient.TaskObject(cmd, hostname));
57 }
58
59 protected HBaseClusterManager.CommandProvider getCommandProvider(ServiceType service)
60 throws IOException {
61 switch (service) {
62 case HADOOP_DATANODE:
63 case HADOOP_NAMENODE:
64 return new HBaseClusterManager.HadoopShellCommandProvider(getConf());
65 case ZOOKEEPER_SERVER:
66 return new HBaseClusterManager.ZookeeperShellCommandProvider(getConf());
67 default:
68 return new HBaseClusterManager.HBaseShellCommandProvider(getConf());
69 }
70 }
71
72 public void signal(ServiceType service, String signal, String hostname) throws IOException {
73 createZNode(hostname, CmdType.exec.toString() +
74 getCommandProvider(service).signalCommand(service, signal));
75 }
76
77 private void createOpCommand(String hostname, ServiceType service,
78 HBaseClusterManager.CommandProvider.Operation op) throws IOException{
79 createZNode(hostname, CmdType.exec.toString() +
80 getCommandProvider(service).getCommand(service, op));
81 }
82
83 @Override
84 public void start(ServiceType service, String hostname, int port) throws IOException {
85 createOpCommand(hostname, service, HBaseClusterManager.CommandProvider.Operation.START);
86 }
87
88 @Override
89 public void stop(ServiceType service, String hostname, int port) throws IOException {
90 createOpCommand(hostname, service, HBaseClusterManager.CommandProvider.Operation.STOP);
91 }
92
93 @Override
94 public void restart(ServiceType service, String hostname, int port) throws IOException {
95 createOpCommand(hostname, service, HBaseClusterManager.CommandProvider.Operation.RESTART);
96 }
97
98 @Override
99 public void kill(ServiceType service, String hostname, int port) throws IOException {
100 signal(service, SIGKILL, hostname);
101 }
102
103 @Override
104 public void suspend(ServiceType service, String hostname, int port) throws IOException {
105 signal(service, SIGSTOP, hostname);
106 }
107
108 @Override
109 public void resume(ServiceType service, String hostname, int port) throws IOException {
110 signal(service, SIGCONT, hostname);
111 }
112
113 @Override
114 public boolean isRunning(ServiceType service, String hostname, int port) throws IOException {
115 return Boolean.parseBoolean(createZNode(hostname, CmdType.bool.toString() +
116 getCommandProvider(service).isRunningCommand(service)));
117 }
118
119 enum CmdType {
120 exec,
121 bool
122 }
123 }