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