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 @SuppressWarnings("InlineFormatString") 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 * @param duration Duration that this thread should generate the load for in milliseconds 042 * @param processes The number of parallel processes, should be equal to cpu threads for max load 043 */ 044 public AddCPULoadAction(long duration, long processes, long timeout) { 045 super(timeout); 046 this.duration = duration; 047 this.processes = processes; 048 } 049 050 @Override 051 protected Logger getLogger() { 052 return LOG; 053 } 054 055 @Override 056 protected void localPerform() throws IOException { 057 getLogger().info("Starting to execute AddCPULoadAction"); 058 ServerName server = PolicyBasedChaosMonkey.selectRandomItem(getCurrentServers()); 059 String hostname = server.getHostname(); 060 061 try { 062 clusterManager.execSudo(hostname, timeout, getCommand()); 063 } catch (IOException ex) { 064 // This will always happen. We use timeout to kill a continuously running process 065 // after the duration expires 066 } 067 getLogger().info("Finished to execute AddCPULoadAction"); 068 } 069 070 private String getCommand() { 071 return String.format(CPU_LOAD_COMMAND, processes, processes, duration / 1000f); 072 } 073}