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