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.factories;
20  
21  import java.util.Map;
22  import java.util.Properties;
23  import java.util.Set;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.hadoop.hbase.IntegrationTestingUtility;
28  import org.apache.hadoop.hbase.TableName;
29  import org.apache.hadoop.hbase.chaos.monkies.ChaosMonkey;
30  
31  import com.google.common.collect.ImmutableMap;
32  import org.apache.hadoop.hbase.util.ReflectionUtils;
33  
34  /**
35   * Base class of the factory that will create a ChaosMonkey.
36   */
37  public abstract class MonkeyFactory {
38    private static final Log LOG = LogFactory.getLog(MonkeyFactory.class);
39  
40    protected TableName tableName;
41    protected Set<String> columnFamilies;
42    protected IntegrationTestingUtility util;
43    protected Properties properties = new Properties();
44  
45    public MonkeyFactory setTableName(TableName tableName) {
46      this.tableName = tableName;
47      return this;
48    }
49  
50    public MonkeyFactory setColumnFamilies(Set<String> columnFamilies) {
51      this.columnFamilies = columnFamilies;
52      return this;
53    }
54  
55    public MonkeyFactory setUtil(IntegrationTestingUtility util) {
56      this.util = util;
57      return this;
58    }
59  
60    public MonkeyFactory setProperties(Properties props) {
61      if (props != null) {
62        this.properties = props;
63      }
64      return this;
65    }
66  
67    public abstract ChaosMonkey build();
68  
69    public static final String CALM = "calm";
70    // TODO: the name has become a misnomer since the default (not-slow) monkey has been removed
71    public static final String SLOW_DETERMINISTIC = "slowDeterministic";
72    public static final String UNBALANCE = "unbalance";
73    public static final String SERVER_KILLING = "serverKilling";
74    public static final String STRESS_AM = "stressAM";
75    public static final String NO_KILL = "noKill";
76    public static final String MASTER_KILLING = "masterKilling";
77    public static final String SERVER_AND_DEPENDENCIES_KILLING = "serverAndDependenciesKilling";
78  
79    public static Map<String, MonkeyFactory> FACTORIES = ImmutableMap.<String,MonkeyFactory>builder()
80      .put(CALM, new CalmMonkeyFactory())
81      .put(SLOW_DETERMINISTIC, new SlowDeterministicMonkeyFactory())
82      .put(UNBALANCE, new UnbalanceMonkeyFactory())
83      .put(SERVER_KILLING, new ServerKillingMonkeyFactory())
84      .put(SERVER_AND_DEPENDENCIES_KILLING, new ServerAndDependenciesKillingMonkeyFactory())
85      .put(STRESS_AM, new StressAssignmentManagerMonkeyFactory())
86      .put(NO_KILL, new NoKillMonkeyFactory())
87      .put(MASTER_KILLING, new MasterKillingMonkeyFactory())
88      .build();
89  
90    public static MonkeyFactory getFactory(String factoryName) {
91      MonkeyFactory fact = FACTORIES.get(factoryName);
92      if (fact == null && factoryName != null && !factoryName.isEmpty()) {
93        Class klass = null;
94        try {
95          klass = Class.forName(factoryName);
96          if (klass != null) {
97            fact = (MonkeyFactory) ReflectionUtils.newInstance(klass);
98          }
99        } catch (Exception e) {
100         LOG.error("Error trying to create " + factoryName + " could not load it by class name");
101         return null;
102       }
103     }
104     return fact;
105   }
106 }