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