View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.hbase.chaos;
20  
21  import java.net.UnknownHostException;
22  import java.util.Arrays;
23  import java.util.Collection;
24  
25  import org.apache.commons.cli.CommandLine;
26  import org.apache.commons.cli.GnuParser;
27  import org.apache.commons.cli.Option;
28  import org.apache.commons.cli.Options;
29  import org.apache.hadoop.conf.Configuration;
30  import org.apache.hadoop.hbase.AuthUtil;
31  import org.apache.hadoop.hbase.ChoreService;
32  import org.apache.hadoop.hbase.HBaseConfiguration;
33  import org.apache.hadoop.hbase.ScheduledChore;
34  import org.apache.hadoop.hbase.classification.InterfaceAudience;
35  import org.apache.hadoop.util.GenericOptionsParser;
36  import org.slf4j.Logger;
37  import org.slf4j.LoggerFactory;
38  
39  
40  /**
41   * Class used to start/stop Chaos related services (currently chaosagent)
42   */
43  @InterfaceAudience.Private
44  public class ChaosService {
45  
46    private static final Logger LOG = LoggerFactory.getLogger(ChaosService.class.getName());
47  
48    public static void execute(String[] args, Configuration conf) {
49      LOG.info("arguments : " + Arrays.toString(args));
50  
51      try {
52        CommandLine cmdline = new GnuParser().parse(getOptions(), args);
53        if (cmdline.hasOption(ChaosServiceName.CHAOSAGENT.toString().toLowerCase())) {
54          String actionStr = cmdline.getOptionValue(ChaosServiceName.CHAOSAGENT.toString().toLowerCase());
55          try {
56            ExecutorAction action = ExecutorAction.valueOf(actionStr.toUpperCase());
57            if (action == ExecutorAction.START) {
58              ChaosServiceStart(conf, ChaosServiceName.CHAOSAGENT);
59            } else if (action == ExecutorAction.STOP) {
60              ChaosServiceStop();
61            }
62          } catch (IllegalArgumentException e) {
63            LOG.error("action passed: {} Unexpected action. Please provide only start/stop.",
64              actionStr, e);
65            throw new RuntimeException(e);
66          }
67        } else {
68          LOG.error("Invalid Options");
69        }
70      } catch (Exception e) {
71        LOG.error("Error while starting ChaosService : ", e);
72      }
73    }
74  
75    private static void ChaosServiceStart(Configuration conf, ChaosServiceName serviceName) {
76      switch (serviceName) {
77        case CHAOSAGENT:
78          ChaosAgent.stopChaosAgent.set(false);
79          try {
80            Thread t = new Thread(new ChaosAgent(conf,
81              ChaosUtils.getZKQuorum(conf), ChaosUtils.getHostName()));
82            t.start();
83            t.join();
84          } catch (InterruptedException | UnknownHostException e) {
85            LOG.error("Failed while executing next task execution of ChaosAgent on : {}",
86              serviceName, e);
87          }
88          break;
89        default:
90          LOG.error("Service Name not known : " + serviceName.toString());
91      }
92    }
93  
94    private static void ChaosServiceStop() {
95      ChaosAgent.stopChaosAgent.set(true);
96    }
97  
98    private static Options getOptions() {
99      Options options = new Options();
100     options.addOption(new Option("c", ChaosServiceName.CHAOSAGENT.toString().toLowerCase(),
101       true, "expecting a start/stop argument"));
102     options.addOption(new Option("D", ChaosServiceName.GENERIC.toString(),
103       true, "generic D param"));
104     LOG.info(Arrays.toString(new Collection[] { options.getOptions() }));
105     return options;
106   }
107 
108   public static void main(String[] args) throws Exception {
109     Configuration conf = HBaseConfiguration.create();
110     new GenericOptionsParser(conf, args);
111 
112     ChoreService choreChaosService = null;
113     ScheduledChore authChore = AuthUtil.getAuthChore(conf);
114 
115     try {
116       if (authChore != null) {
117         choreChaosService = new ChoreService(ChaosConstants.CHORE_SERVICE_PREFIX);
118         choreChaosService.scheduleChore(authChore);
119       }
120 
121       execute(args, conf);
122     } finally {
123       if (authChore != null)
124         choreChaosService.shutdown();
125     }
126   }
127 
128   enum ChaosServiceName {
129     CHAOSAGENT,
130     GENERIC
131   }
132 
133 
134   enum ExecutorAction {
135     START,
136     STOP
137   }
138 }