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