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.actions;
020
021import java.io.IOException;
022import org.apache.hadoop.hbase.ServerName;
023import org.apache.hadoop.hbase.chaos.monkies.PolicyBasedChaosMonkey;
024import org.slf4j.Logger;
025import org.slf4j.LoggerFactory;
026
027/**
028 * Action that adds high cpu load to a random regionserver for a given duration
029 */
030public class AddCPULoadAction extends SudoCommandAction {
031  private static final Logger LOG = LoggerFactory.getLogger(AddCPULoadAction.class);
032  private static final String CPU_LOAD_COMMAND =
033      "seq 1 %s | xargs -I{} -n 1 -P %s timeout %s dd if=/dev/urandom of=/dev/null bs=1M " +
034          "iflag=fullblock";
035
036  private final long duration;
037  private final long processes;
038
039  /**
040   * Add high load to cpu
041   *
042   * @param duration  Duration that this thread should generate the load for in milliseconds
043   * @param processes The number of parallel processes, should be equal to cpu threads for max load
044   */
045  public AddCPULoadAction(long duration, long processes, long timeout) {
046    super(timeout);
047    this.duration = duration;
048    this.processes = processes;
049  }
050
051  @Override protected Logger getLogger() {
052    return LOG;
053  }
054
055  protected void localPerform() throws IOException {
056    getLogger().info("Starting to execute AddCPULoadAction");
057    ServerName server = PolicyBasedChaosMonkey.selectRandomItem(getCurrentServers());
058    String hostname = server.getHostname();
059
060    try {
061      clusterManager.execSudo(hostname, timeout, getCommand());
062    } catch (IOException ex){
063      //This will always happen. We use timeout to kill a continuously running process
064      //after the duration expires
065    }
066    getLogger().info("Finished to execute AddCPULoadAction");
067  }
068
069  private String getCommand(){
070    return String.format(CPU_LOAD_COMMAND, processes, processes, duration/1000f);
071  }
072}