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 *
030 * Fill the disk on a random regionserver.
031 */
032public class FillDiskCommandAction extends SudoCommandAction {
033  private static final Logger LOG = LoggerFactory.getLogger(FillDiskCommandAction.class);
034  private long size;
035  private long duration;
036  private String path;
037
038  /**
039   * Fill the disk on a random regionserver.
040   * Please note that the file will be created regardless of the set duration or timeout.
041   * So please use timeout and duration big enough to avoid complication caused by retries.
042   *
043   * @param size size of the generated file in MB or fill the disk if set to 0
044   * @param duration the time this issue persists in milliseconds
045   * @param path the path to the generated file
046   * @param timeout the timeout for executing required commands on the region server in milliseconds
047   */
048  public FillDiskCommandAction(long size, long duration, String path, long timeout) {
049    super(timeout);
050    this.size = size;
051    this.duration = duration;
052    this.path = path;
053  }
054
055  protected void localPerform() throws IOException {
056    LOG.info("Starting to execute FillDiskCommandAction");
057    ServerName server = PolicyBasedChaosMonkey.selectRandomItem(getCurrentServers());
058    String hostname = server.getHostname();
059
060    try {
061      clusterManager.execSudo(hostname, duration, getFillCommand());
062    } catch (IOException ex) {
063      LOG.info("Potential timeout. We try to stop the dd process on target machine");
064      clusterManager.execSudoWithRetries(hostname, timeout, getStopCommand());
065      throw ex;
066    } finally {
067      clusterManager.execSudoWithRetries(hostname, timeout, getClearCommand());
068      LOG.info("Finished to execute FillDiskCommandAction");
069    }
070  }
071
072  private String getFillCommand(){
073    if (size == 0){
074      return String.format("dd if=/dev/urandom of=%s/garbage bs=1M iflag=fullblock", path);
075    }
076    return String.format("dd if=/dev/urandom of=%s/garbage bs=1M count=%s iflag=fullblock",
077        path, size);
078  }
079
080  private String getClearCommand(){
081    return String.format("rm -f %s/garbage", path);
082  }
083
084  private String getStopCommand() {
085    return String.format("killall dd");
086  }
087}