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 org.apache.hadoop.hbase.chaos.actions.Action;
021import org.apache.hadoop.hbase.chaos.actions.DumpClusterStatusAction;
022import org.apache.hadoop.hbase.chaos.actions.ForceBalancerAction;
023import org.apache.hadoop.hbase.chaos.actions.GracefulRollingRestartRsAction;
024import org.apache.hadoop.hbase.chaos.actions.RestartActiveMasterAction;
025import org.apache.hadoop.hbase.chaos.actions.RestartRandomRsExceptMetaAction;
026import org.apache.hadoop.hbase.chaos.actions.RollingBatchRestartRsAction;
027import org.apache.hadoop.hbase.chaos.actions.RollingBatchSuspendResumeRsAction;
028import org.apache.hadoop.hbase.chaos.monkies.ChaosMonkey;
029import org.apache.hadoop.hbase.chaos.monkies.PolicyBasedChaosMonkey;
030import org.apache.hadoop.hbase.chaos.policies.CompositeSequentialPolicy;
031import org.apache.hadoop.hbase.chaos.policies.DoActionsOncePolicy;
032import org.apache.hadoop.hbase.chaos.policies.PeriodicRandomActionPolicy;
033
034/**
035 * Creates ChaosMonkeys for doing server restart actions, but not flush / compact / snapshot kind of
036 * actions.
037 */
038public class ServerKillingMonkeyFactory extends MonkeyFactory {
039
040  private long restartRandomRsExceptMetaSleepTime;
041  private long restartActiveMasterSleepTime;
042  private long rollingBatchRestartRSSleepTime;
043  private long gracefulRollingRestartTSSLeepTime;
044  private long rollingBatchSuspendRSSleepTime;
045  private float rollingBatchSuspendtRSRatio;
046  private long action1Period;
047
048  @Override
049  public ChaosMonkey build() {
050    loadProperties();
051
052    // Destructive actions to mess things around. Cannot run batch restart
053    // @formatter:off
054    Action[] actions1 = new Action[] {
055      new RestartRandomRsExceptMetaAction(restartRandomRsExceptMetaSleepTime),
056      new RestartActiveMasterAction(restartActiveMasterSleepTime),
057      // only allow 2 servers to be dead
058      new RollingBatchRestartRsAction(rollingBatchRestartRSSleepTime, 1.0f, 2, true),
059      new ForceBalancerAction(),
060      new GracefulRollingRestartRsAction(gracefulRollingRestartTSSLeepTime),
061      new RollingBatchSuspendResumeRsAction(rollingBatchSuspendRSSleepTime,
062          rollingBatchSuspendtRSRatio)
063    };
064    // @formatter:on
065
066    // Action to log more info for debugging
067    Action[] actions2 = new Action[] { new DumpClusterStatusAction() };
068
069    return new PolicyBasedChaosMonkey(properties, util,
070      new CompositeSequentialPolicy(new DoActionsOncePolicy(action1Period, actions1),
071        new PeriodicRandomActionPolicy(action1Period, actions1)),
072      new PeriodicRandomActionPolicy(action1Period, actions2));
073  }
074
075  private void loadProperties() {
076    restartRandomRsExceptMetaSleepTime = Long
077      .parseLong(this.properties.getProperty(MonkeyConstants.RESTART_RANDOM_RS_EXCEPTION_SLEEP_TIME,
078        MonkeyConstants.DEFAULT_RESTART_RANDOM_RS_EXCEPTION_SLEEP_TIME + ""));
079    restartActiveMasterSleepTime =
080      Long.parseLong(this.properties.getProperty(MonkeyConstants.RESTART_ACTIVE_MASTER_SLEEP_TIME,
081        MonkeyConstants.DEFAULT_RESTART_ACTIVE_MASTER_SLEEP_TIME + ""));
082    rollingBatchRestartRSSleepTime = Long
083      .parseLong(this.properties.getProperty(MonkeyConstants.ROLLING_BATCH_RESTART_RS_SLEEP_TIME,
084        MonkeyConstants.DEFAULT_ROLLING_BATCH_RESTART_RS_SLEEP_TIME + ""));
085    gracefulRollingRestartTSSLeepTime =
086      Long.parseLong(this.properties.getProperty(MonkeyConstants.GRACEFUL_RESTART_RS_SLEEP_TIME,
087        MonkeyConstants.DEFAULT_GRACEFUL_RESTART_RS_SLEEP_TIME + ""));
088    rollingBatchSuspendRSSleepTime = Long
089      .parseLong(this.properties.getProperty(MonkeyConstants.ROLLING_BATCH_SUSPEND_RS_SLEEP_TIME,
090        MonkeyConstants.DEFAULT_ROLLING_BATCH_SUSPEND_RS_SLEEP_TIME + ""));
091    rollingBatchSuspendtRSRatio =
092      Float.parseFloat(this.properties.getProperty(MonkeyConstants.ROLLING_BATCH_SUSPEND_RS_RATIO,
093        MonkeyConstants.DEFAULT_ROLLING_BATCH_SUSPEND_RS_RATIO + ""));
094    action1Period =
095      Long.parseLong(this.properties.getProperty(MonkeyConstants.PERIODIC_ACTION1_PERIOD,
096        MonkeyConstants.DEFAULT_PERIODIC_ACTION1_PERIOD + ""));
097  }
098}