001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.apache.hadoop.hbase.chaos.util;
020
021import java.io.Closeable;
022import java.io.IOException;
023import java.util.concurrent.ExecutorService;
024import java.util.concurrent.Executors;
025import java.util.concurrent.TimeUnit;
026
027import org.apache.hadoop.conf.Configuration;
028import org.apache.hadoop.hbase.HBaseConfiguration;
029import org.apache.hadoop.hbase.IntegrationTestingUtility;
030import org.slf4j.Logger;
031import org.slf4j.LoggerFactory;
032
033import org.apache.hbase.thirdparty.com.google.common.base.Preconditions;
034import org.apache.hbase.thirdparty.com.google.common.util.concurrent.ThreadFactoryBuilder;
035
036/**
037 * This class can be used to control chaos monkeys life cycle.
038 */
039public class Monkeys implements Closeable {
040  private static final Logger LOG = LoggerFactory.getLogger(Monkeys.class);
041
042  private final Configuration conf;
043  private final ChaosMonkeyRunner monkeyRunner;
044  private final Runnable runner;
045  private final ExecutorService executor;
046
047  public Monkeys() {
048    this(HBaseConfiguration.create());
049  }
050
051  public Monkeys(Configuration conf) {
052    this.conf = Preconditions.checkNotNull(conf, "Should specify a configuration");
053    this.monkeyRunner = new ChaosMonkeyRunner();
054    this.runner = () -> {
055      try {
056        monkeyRunner.getAndStartMonkey();
057      } catch (Exception e) {
058        LOG.error("Exception occured when running chaos monkeys: ", e);
059      }
060    };
061    this.executor = Executors.newSingleThreadExecutor(new ThreadFactoryBuilder()
062        .setDaemon(true).setNameFormat("ChaosMonkey").build());
063    IntegrationTestingUtility.setUseDistributedCluster(this.conf);
064  }
065
066  public void addResource(Configuration otherConf) {
067    conf.addResource(otherConf);
068    monkeyRunner.setConf(conf);
069  }
070
071  public void addResource(String otherConf) {
072    conf.addResource(otherConf);
073    monkeyRunner.setConf(conf);
074  }
075
076  public void startChaos() {
077    executor.execute(runner);
078    LOG.info("Chaos monkeys are running.");
079  }
080
081  public void stopChaos() {
082    monkeyRunner.stopRunner();
083    LOG.info("Chaos monkeys are stopped.");
084  }
085
086  @Override
087  public void close() throws IOException {
088    executor.shutdown();
089    try {
090      // wait 10 seconds.
091      executor.awaitTermination(10, TimeUnit.SECONDS);
092    } catch (InterruptedException e) {
093      Thread.currentThread().interrupt();
094      LOG.warn("Interruption occured while stopping chaos monkeys " + e);
095    }
096  }
097}