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 * Fill the disk on a random regionserver.
028 */
029public class FillDiskCommandAction extends SudoCommandAction {
030  private static final Logger LOG = LoggerFactory.getLogger(FillDiskCommandAction.class);
031  private final long size;
032  private final long duration;
033  private final String path;
034
035  /**
036   * Fill the disk on a random regionserver. Please note that the file will be created regardless of
037   * the set duration or timeout. So please use timeout and duration big enough to avoid
038   * complication caused by retries.
039   * @param size     size of the generated file in MB or fill the disk if set to 0
040   * @param duration the time this issue persists in milliseconds
041   * @param path     the path to the generated file
042   * @param timeout  the timeout for executing required commands on the region server in
043   *                 milliseconds
044   */
045  public FillDiskCommandAction(long size, long duration, String path, long timeout) {
046    super(timeout);
047    this.size = size;
048    this.duration = duration;
049    this.path = path;
050  }
051
052  @Override
053  protected Logger getLogger() {
054    return LOG;
055  }
056
057  @Override
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", path,
080      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}