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