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.io.asyncfs;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertTrue;
022
023import org.apache.hadoop.conf.Configuration;
024import org.apache.hadoop.hbase.HBaseClassTestRule;
025import org.apache.hadoop.hbase.HBaseConfiguration;
026import org.apache.hadoop.hbase.io.asyncfs.monitor.ExcludeDatanodeManager;
027import org.apache.hadoop.hbase.io.asyncfs.monitor.StreamSlowMonitor;
028import org.apache.hadoop.hbase.testclassification.SmallTests;
029import org.apache.hadoop.hdfs.protocol.DatanodeInfo;
030import org.junit.ClassRule;
031import org.junit.Test;
032import org.junit.experimental.categories.Category;
033
034@Category({ SmallTests.class })
035public class TestExcludeDatanodeManager {
036
037  @ClassRule
038  public static final HBaseClassTestRule CLASS_RULE =
039    HBaseClassTestRule.forClass(TestExcludeDatanodeManager.class);
040
041  @Test
042  public void testExcludeSlowDNBySpeed() {
043    Configuration conf = HBaseConfiguration.create();
044    ExcludeDatanodeManager excludeDatanodeManager = new ExcludeDatanodeManager(conf);
045    StreamSlowMonitor streamSlowDNsMonitor =
046      excludeDatanodeManager.getStreamSlowMonitor("testMonitor");
047    assertEquals(0, excludeDatanodeManager.getExcludeDNs().size());
048    DatanodeInfo datanodeInfo = new DatanodeInfo.DatanodeInfoBuilder().setIpAddr("0.0.0.0")
049      .setHostName("hostname1").setDatanodeUuid("uuid1").setXferPort(111).setInfoPort(222)
050      .setInfoSecurePort(333).setIpcPort(444).setNetworkLocation("location1").build();
051    streamSlowDNsMonitor.checkProcessTimeAndSpeed(datanodeInfo, 100000, 5100,
052      System.currentTimeMillis() - 5100, 0);
053    streamSlowDNsMonitor.checkProcessTimeAndSpeed(datanodeInfo, 100000, 5100,
054      System.currentTimeMillis() - 5100, 0);
055    streamSlowDNsMonitor.checkProcessTimeAndSpeed(datanodeInfo, 100000, 5100,
056      System.currentTimeMillis() - 5100, 0);
057    assertEquals(1, excludeDatanodeManager.getExcludeDNs().size());
058    assertTrue(excludeDatanodeManager.getExcludeDNs().containsKey(datanodeInfo));
059  }
060
061  @Test
062  public void testExcludeSlowDNByProcessTime() {
063    Configuration conf = HBaseConfiguration.create();
064    ExcludeDatanodeManager excludeDatanodeManager = new ExcludeDatanodeManager(conf);
065    StreamSlowMonitor streamSlowDNsMonitor =
066      excludeDatanodeManager.getStreamSlowMonitor("testMonitor");
067    assertEquals(0, excludeDatanodeManager.getExcludeDNs().size());
068    DatanodeInfo datanodeInfo = new DatanodeInfo.DatanodeInfoBuilder().setIpAddr("0.0.0.0")
069      .setHostName("hostname1").setDatanodeUuid("uuid1").setXferPort(111).setInfoPort(222)
070      .setInfoSecurePort(333).setIpcPort(444).setNetworkLocation("location1").build();
071    streamSlowDNsMonitor.checkProcessTimeAndSpeed(datanodeInfo, 5000, 7000,
072      System.currentTimeMillis() - 7000, 0);
073    streamSlowDNsMonitor.checkProcessTimeAndSpeed(datanodeInfo, 5000, 7000,
074      System.currentTimeMillis() - 7000, 0);
075    streamSlowDNsMonitor.checkProcessTimeAndSpeed(datanodeInfo, 5000, 7000,
076      System.currentTimeMillis() - 7000, 0);
077    assertEquals(1, excludeDatanodeManager.getExcludeDNs().size());
078    assertTrue(excludeDatanodeManager.getExcludeDNs().containsKey(datanodeInfo));
079  }
080}