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