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