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.AddCPULoadAction;
023import org.apache.hadoop.hbase.chaos.actions.CorruptPacketsCommandAction;
024import org.apache.hadoop.hbase.chaos.actions.DelayPacketsCommandAction;
025import org.apache.hadoop.hbase.chaos.actions.DumpClusterStatusAction;
026import org.apache.hadoop.hbase.chaos.actions.DuplicatePacketsCommandAction;
027import org.apache.hadoop.hbase.chaos.actions.FillDiskCommandAction;
028import org.apache.hadoop.hbase.chaos.actions.LosePacketsCommandAction;
029import org.apache.hadoop.hbase.chaos.actions.ReorderPacketsCommandAction;
030import org.apache.hadoop.hbase.chaos.monkies.ChaosMonkey;
031import org.apache.hadoop.hbase.chaos.monkies.PolicyBasedChaosMonkey;
032import org.apache.hadoop.hbase.chaos.policies.PeriodicRandomActionPolicy;
033
034/**
035 * A chaos monkey to create distributed cluster related issues, requires a user with
036 * passwordless ssh access to the cluster and sudo privileges.
037 */
038public class DistributedIssuesMonkeyFactory extends MonkeyFactory {
039
040  private long action1Period;
041  private long action2Period;
042
043  private long cpuLoadDuration;
044  private long cpuLoadProcesses;
045  private long networkIssueTimeout;
046  private long networkIssueDuration;
047  private float networkIssueRation;
048  private long networkIssueDelay;
049  private String networkIssueInterface;
050  private long fillDiskTimeout;
051  private String fillDiskPath;
052  private long fillDiskFileSize;
053  private long fillDiskIssueduration;
054
055  @Override public ChaosMonkey build() {
056    loadProperties();
057
058    Action[] actions1 = new Action[] {
059      new AddCPULoadAction(cpuLoadDuration, cpuLoadProcesses, networkIssueTimeout),
060      new CorruptPacketsCommandAction(networkIssueRation, networkIssueDuration,
061          networkIssueTimeout, networkIssueInterface),
062      new DuplicatePacketsCommandAction(networkIssueRation, networkIssueDuration,
063          networkIssueTimeout, networkIssueInterface),
064      new LosePacketsCommandAction(networkIssueRation, networkIssueDuration,
065          networkIssueTimeout, networkIssueInterface),
066      new DelayPacketsCommandAction(networkIssueDelay, networkIssueDuration,
067          networkIssueTimeout, networkIssueInterface),
068      new ReorderPacketsCommandAction(networkIssueRation, networkIssueDuration,
069          networkIssueDelay, networkIssueTimeout, networkIssueInterface),
070      new FillDiskCommandAction(fillDiskFileSize, fillDiskIssueduration, fillDiskPath,
071          fillDiskTimeout)};
072
073    // Action to log more info for debugging
074    Action[] actions2 = new Action[] {new DumpClusterStatusAction()};
075
076    return new PolicyBasedChaosMonkey(properties, util,
077      new PeriodicRandomActionPolicy(action1Period, actions1),
078      new PeriodicRandomActionPolicy(action2Period, actions2));
079  }
080
081  private void loadProperties() {
082    action1Period = Long.parseLong(this.properties
083        .getProperty(MonkeyConstants.PERIODIC_ACTION1_PERIOD,
084            MonkeyConstants.DEFAULT_PERIODIC_ACTION1_PERIOD + ""));
085    action2Period = Long.parseLong(this.properties
086        .getProperty(MonkeyConstants.PERIODIC_ACTION2_PERIOD,
087            MonkeyConstants.DEFAULT_PERIODIC_ACTION2_PERIOD + ""));
088    cpuLoadDuration = Long.parseLong(this.properties.getProperty(
089        MonkeyConstants.CPU_LOAD_DURATION,
090        MonkeyConstants.DEFAULT_CPU_LOAD_DURATION + ""));
091    cpuLoadProcesses = Long.parseLong(this.properties.getProperty(
092        MonkeyConstants.CPU_LOAD_PROCESSES,
093        MonkeyConstants.DEFAULT_CPU_LOAD_PROCESSES + ""));
094    networkIssueTimeout = Long.parseLong(this.properties
095        .getProperty(MonkeyConstants.NETWORK_ISSUE_COMMAND_TIMEOUT,
096            MonkeyConstants.DEFAULT_NETWORK_ISSUE_COMMAND_TIMEOUT + ""));
097    networkIssueDuration = Long.parseLong(this.properties
098        .getProperty(MonkeyConstants.NETWORK_ISSUE_DURATION,
099            MonkeyConstants.DEFAULT_NETWORK_ISSUE_DURATION + ""));
100    networkIssueRation = Float.parseFloat(this.properties
101        .getProperty(MonkeyConstants.NETWORK_ISSUE_RATIO,
102            MonkeyConstants.DEFAULT_NETWORK_ISSUE_RATIO + ""));
103    networkIssueDelay = Long.parseLong(this.properties
104        .getProperty(MonkeyConstants.NETWORK_ISSUE_DELAY,
105            MonkeyConstants.DEFAULT_NETWORK_ISSUE_DELAY + ""));
106    networkIssueInterface = this.properties
107        .getProperty(MonkeyConstants.NETWORK_ISSUE_INTERFACE,
108            MonkeyConstants.DEFAULT_NETWORK_ISSUE_INTERFACE + "");
109    fillDiskTimeout = Long.parseLong(this.properties
110        .getProperty(MonkeyConstants.FILL_DISK_COMMAND_TIMEOUT,
111            MonkeyConstants.DEFAULT_FILL_DISK_COMMAND_TIMEOUT + ""));
112    fillDiskPath = this.properties
113        .getProperty(MonkeyConstants.FILL_DISK_PATH,
114            MonkeyConstants.DEFAULT_FILL_DISK_PATH + "");
115    fillDiskFileSize = Long.parseLong(this.properties
116        .getProperty(MonkeyConstants.FILL_DISK_FILE_SIZE,
117            MonkeyConstants.DEFAULT_FILL_DISK_FILE_SIZE + ""));
118    fillDiskIssueduration = Long.parseLong(this.properties
119        .getProperty(MonkeyConstants.FILL_DISK_ISSUE_DURATION,
120            MonkeyConstants.DEFAULT_FILL_DISK_ISSUE_DURATION + ""));
121  }
122}