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.factories;
020
021import java.util.Map;
022import java.util.Properties;
023import java.util.Set;
024
025import org.apache.hadoop.hbase.IntegrationTestingUtility;
026import org.apache.hadoop.hbase.TableName;
027import org.apache.hadoop.hbase.chaos.monkies.ChaosMonkey;
028
029import org.apache.hbase.thirdparty.com.google.common.collect.ImmutableMap;
030import org.apache.hadoop.hbase.util.ReflectionUtils;
031import org.slf4j.Logger;
032import org.slf4j.LoggerFactory;
033
034/**
035 * Base class of the factory that will create a ChaosMonkey.
036 */
037public abstract class MonkeyFactory {
038  private static final Logger LOG = LoggerFactory.getLogger(MonkeyFactory.class);
039
040  protected TableName tableName;
041  protected Set<String> columnFamilies;
042  protected IntegrationTestingUtility util;
043  protected Properties properties = new Properties();
044
045  public MonkeyFactory setTableName(TableName tableName) {
046    this.tableName = tableName;
047    return this;
048  }
049
050  public MonkeyFactory setColumnFamilies(Set<String> columnFamilies) {
051    this.columnFamilies = columnFamilies;
052    return this;
053  }
054
055  public MonkeyFactory setUtil(IntegrationTestingUtility util) {
056    this.util = util;
057    return this;
058  }
059
060  public MonkeyFactory setProperties(Properties props) {
061    if (props != null) {
062      this.properties = props;
063    }
064    return this;
065  }
066
067  public abstract ChaosMonkey build();
068
069  public static final String CALM = "calm";
070  // TODO: the name has become a misnomer since the default (not-slow) monkey has been removed
071  public static final String SLOW_DETERMINISTIC = "slowDeterministic";
072  public static final String UNBALANCE = "unbalance";
073  public static final String SERVER_KILLING = "serverKilling";
074  public static final String STRESS_AM = "stressAM";
075  public static final String NO_KILL = "noKill";
076  public static final String MASTER_KILLING = "masterKilling";
077  public static final String MOB_NO_KILL = "mobNoKill";
078  public static final String MOB_SLOW_DETERMINISTIC = "mobSlowDeterministic";
079  public static final String SERVER_AND_DEPENDENCIES_KILLING = "serverAndDependenciesKilling";
080  public static final String DISTRIBUTED_ISSUES = "distributedIssues";
081  public static final String DATA_ISSUES = "dataIssues";
082
083  public static Map<String, MonkeyFactory> FACTORIES = ImmutableMap.<String,MonkeyFactory>builder()
084    .put(CALM, new CalmMonkeyFactory())
085    .put(SLOW_DETERMINISTIC, new SlowDeterministicMonkeyFactory())
086    .put(UNBALANCE, new UnbalanceMonkeyFactory())
087    .put(SERVER_KILLING, new ServerKillingMonkeyFactory())
088    .put(STRESS_AM, new StressAssignmentManagerMonkeyFactory())
089    .put(NO_KILL, new NoKillMonkeyFactory())
090    .put(MASTER_KILLING, new MasterKillingMonkeyFactory())
091    .put(MOB_NO_KILL, new MobNoKillMonkeyFactory())
092    .put(MOB_SLOW_DETERMINISTIC, new MobNoKillMonkeyFactory())
093    .put(SERVER_AND_DEPENDENCIES_KILLING, new ServerAndDependenciesKillingMonkeyFactory())
094    .put(DISTRIBUTED_ISSUES, new DistributedIssuesMonkeyFactory())
095    .put(DATA_ISSUES, new DataIssuesMonkeyFactory())
096    .build();
097
098  public static MonkeyFactory getFactory(String factoryName) {
099    MonkeyFactory fact = FACTORIES.get(factoryName);
100    if (fact == null && factoryName != null && !factoryName.isEmpty()) {
101      Class klass = null;
102      try {
103        klass = Class.forName(factoryName);
104        if (klass != null) {
105          fact = (MonkeyFactory) ReflectionUtils.newInstance(klass);
106        }
107      } catch (Exception e) {
108        LOG.error("Error trying to create " + factoryName + " could not load it by class name");
109        return null;
110      }
111    }
112    return fact;
113  }
114}