From 1e7e63b6b84b5dc2767300822b470db0401786bd Mon Sep 17 00:00:00 2001 From: Reid Chan Date: Mon, 4 Sep 2017 18:23:34 +0800 Subject: [PATCH] HBASE-18651 Let ChaosMonkeyRunner expose the chaos monkey runner it creates --- .../apache/hadoop/hbase/chaos/util/Monkeys.java | 87 ++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 hbase-it/src/test/java/org/apache/hadoop/hbase/chaos/util/Monkeys.java diff --git a/hbase-it/src/test/java/org/apache/hadoop/hbase/chaos/util/Monkeys.java b/hbase-it/src/test/java/org/apache/hadoop/hbase/chaos/util/Monkeys.java new file mode 100644 index 0000000000..b82d60aa86 --- /dev/null +++ b/hbase-it/src/test/java/org/apache/hadoop/hbase/chaos/util/Monkeys.java @@ -0,0 +1,87 @@ +/** + * 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.chaos.util; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.hbase.HBaseConfiguration; +import org.apache.hadoop.hbase.IntegrationTestingUtility; +import org.apache.hadoop.util.ToolRunner; + +import com.google.common.base.Preconditions; + +/** + * This class can be used to control chaos monkeys life cycle. + */ +public class Monkeys { + private static final Log LOG = LogFactory.getLog(Monkeys.class); + + private final Configuration conf; + private ChaosMonkeyRunner monkeyRunner; + private Thread runnerThread; + + public Monkeys() { + this(HBaseConfiguration.create()); + } + + public Monkeys(Configuration conf) { + Preconditions.checkArgument(conf != null, "Should specify a configuration"); + this.conf = conf; + this.monkeyRunner = new ChaosMonkeyRunner(); + this.runnerThread = new Thread(new Runnable() { + @Override + public void run() { + try { + ToolRunner.run(conf, monkeyRunner, null); + } catch (Exception e) { + LOG.error("Exception occured when running chaos monkeys: ", e); + } + } + }); + IntegrationTestingUtility.setUseDistributedCluster(conf); + } + + public void addResource(Configuration otherConf) { + conf.addResource(otherConf); + monkeyRunner.setConf(conf); + } + + public void addResource(String otherConf) { + conf.addResource(otherConf); + monkeyRunner.setConf(conf); + } + + public void startChaos() { + runnerThread.start(); + LOG.info("Chaos monkeys are running."); + } + + public void stopChaos() { + monkeyRunner.stopRunner(); + while (runnerThread.isAlive()) { + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + LOG.warn("Interruption occured while stopping chaos monkeys " + e); + } + } + LOG.info("Chaos monkeys are stopped."); + } +} -- 2.11.0 (Apple Git-81)