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