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
081  public static Map<String, MonkeyFactory> FACTORIES = ImmutableMap.<String,MonkeyFactory>builder()
082    .put(CALM, new CalmMonkeyFactory())
083    .put(SLOW_DETERMINISTIC, new SlowDeterministicMonkeyFactory())
084    .put(UNBALANCE, new UnbalanceMonkeyFactory())
085    .put(SERVER_KILLING, new ServerKillingMonkeyFactory())
086    .put(STRESS_AM, new StressAssignmentManagerMonkeyFactory())
087    .put(NO_KILL, new NoKillMonkeyFactory())
088    .put(MASTER_KILLING, new MasterKillingMonkeyFactory())
089    .put(MOB_NO_KILL, new MobNoKillMonkeyFactory())
090    .put(MOB_SLOW_DETERMINISTIC, new MobNoKillMonkeyFactory())
091    .put(SERVER_AND_DEPENDENCIES_KILLING, new ServerAndDependenciesKillingMonkeyFactory())
092    .build();
093
094  public static MonkeyFactory getFactory(String factoryName) {
095    MonkeyFactory fact = FACTORIES.get(factoryName);
096    if (fact == null && factoryName != null && !factoryName.isEmpty()) {
097      Class klass = null;
098      try {
099        klass = Class.forName(factoryName);
100        if (klass != null) {
101          fact = (MonkeyFactory) ReflectionUtils.newInstance(klass);
102        }
103      } catch (Exception e) {
104        LOG.error("Error trying to create " + factoryName + " could not load it by class name");
105        return null;
106      }
107    }
108    return fact;
109  }
110}