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.test;
20  
21  import org.apache.hadoop.conf.Configuration;
22  import org.apache.hadoop.hbase.HBaseConfiguration;
23  import org.apache.hadoop.hbase.IntegrationTestingUtility;
24  import org.apache.hadoop.hbase.chaos.util.ChaosMonkeyRunner;
25  import org.apache.hadoop.hbase.chaos.util.Monkeys;
26  import org.apache.hadoop.hbase.testclassification.IntegrationTests;
27  import org.apache.hadoop.util.ToolRunner;
28  import org.junit.Test;
29  import org.junit.experimental.categories.Category;
30  
31  /**
32   * This is an integration test for showing a simple usage of how to use {@link Monkeys}
33   * to control {@link ChaosMonkeyRunner}.
34   */
35  @Category(IntegrationTests.class)
36  public class IntegrationTestMonkeys extends ChaosMonkeyRunner {
37    private static final int RUN_SECS = 15 * 1000;
38    private static final int WAIT_SECS = 10 * 1000;
39  
40    @Override
41    protected int doWork() throws Exception {
42      super.setUpCluster();
43      runMonkeys();
44      return 0;
45    }
46  
47    @Test
48    public void runMonkeys() throws Exception {
49      try (Monkeys monkeys = new Monkeys()) {
50        for (int i = 0; i < 2; i++) {
51          monkeys.startChaos();
52          Thread.sleep(RUN_SECS);
53          monkeys.stopChaos();
54          Thread.sleep(WAIT_SECS);
55        }
56      }
57    }
58  
59    public static void main(String[] args) throws Exception {
60      // Run chaos monkeys 15 seconds, then stop them.
61      // After 10 seconds, run chaos monkeys again.
62      Configuration conf = HBaseConfiguration.create();
63      IntegrationTestingUtility.setUseDistributedCluster(conf);
64      int exitCode = ToolRunner.run(conf, new IntegrationTestMonkeys(), args);
65      System.exit(exitCode);
66    }
67  }