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 org.apache.hadoop.hbase.chaos.actions.Action;
022import org.apache.hadoop.hbase.chaos.actions.DumpClusterStatusAction;
023import org.apache.hadoop.hbase.chaos.actions.RestartActiveMasterAction;
024import org.apache.hadoop.hbase.chaos.monkies.ChaosMonkey;
025import org.apache.hadoop.hbase.chaos.monkies.PolicyBasedChaosMonkey;
026import org.apache.hadoop.hbase.chaos.policies.PeriodicRandomActionPolicy;
027
028/**
029 * A chaos monkey to kill the active master periodically. Can be run in single master
030 * or multi master setup.
031 */
032public class MasterKillingMonkeyFactory extends MonkeyFactory {
033
034  private long action1Period;
035  private long action2Period;
036
037  private long restartActiveMasterSleepTime;
038
039  @Override
040  public ChaosMonkey build() {
041    loadProperties();
042
043    // Destructive actions to mess things around.
044    Action[] actions1 = new Action[] {
045        new RestartActiveMasterAction(restartActiveMasterSleepTime),
046    };
047
048    // Action to log more info for debugging
049    Action[] actions2 = new Action[] {
050        new DumpClusterStatusAction()
051    };
052
053    return new PolicyBasedChaosMonkey(util,
054        new PeriodicRandomActionPolicy(action1Period, actions1),
055        new PeriodicRandomActionPolicy(action2Period, actions2));
056  }
057
058  private void loadProperties() {
059
060      action1Period = Long.parseLong(this.properties.getProperty(
061        MonkeyConstants.PERIODIC_ACTION1_PERIOD,
062        MonkeyConstants.DEFAULT_PERIODIC_ACTION1_PERIOD + ""));
063      action2Period = Long.parseLong(this.properties.getProperty(
064        MonkeyConstants.PERIODIC_ACTION2_PERIOD,
065        MonkeyConstants.DEFAULT_PERIODIC_ACTION2_PERIOD + ""));
066      restartActiveMasterSleepTime = Long.parseLong(this.properties.getProperty(
067        MonkeyConstants.RESTART_ACTIVE_MASTER_SLEEP_TIME,
068        MonkeyConstants.DEFAULT_RESTART_ACTIVE_MASTER_SLEEP_TIME + ""));
069  }
070
071}