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