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