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 * Reorder network packets on a random regionserver.
031 */
032public class ReorderPacketsCommandAction extends TCCommandAction {
033  private static final Logger LOG = LoggerFactory.getLogger(ReorderPacketsCommandAction.class);
034  private float ratio;
035  private long duration;
036  private long delay;
037
038  /**
039   * Reorder network packets on a random regionserver.
040   *
041   * @param ratio the ratio of packets reordered
042   * @param duration the time this issue persists in milliseconds
043   * @param delay the delay between reordered and non-reordered packets in milliseconds
044   * @param timeout the timeout for executing required commands on the region server in milliseconds
045   * @param network network interface the regionserver uses for communication
046   */
047  public ReorderPacketsCommandAction(float ratio, long duration, long delay, long timeout,
048      String network) {
049    super(timeout, network);
050    this.ratio = ratio;
051    this.duration = duration;
052    this.delay = delay;
053  }
054
055  protected void localPerform() throws IOException {
056    LOG.info("Starting to execute ReorderPacketsCommandAction");
057    ServerName server = PolicyBasedChaosMonkey.selectRandomItem(getCurrentServers());
058    String hostname = server.getHostname();
059
060    try {
061      clusterManager.execSudoWithRetries(hostname, timeout, getCommand(ADD));
062      Thread.sleep(duration);
063    } catch (InterruptedException e) {
064      LOG.debug("Failed to run the command for the full duration", e);
065    } finally {
066      clusterManager.execSudoWithRetries(hostname, timeout, getCommand(DELETE));
067    }
068
069    LOG.info("Finished to execute ReorderPacketsCommandAction");
070  }
071
072  private String getCommand(String operation){
073    return String.format("tc qdisc %s dev %s root netem delay %sms reorder %s%% 50%%",
074        operation, network, delay, ratio * 100);
075  }
076}