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.client; 019 020import static org.apache.hadoop.hbase.HConstants.EMPTY_START_ROW; 021import static org.apache.hadoop.hbase.HConstants.HBASE_CLIENT_META_OPERATION_TIMEOUT; 022import static org.apache.hadoop.hbase.coprocessor.CoprocessorHost.REGION_COPROCESSOR_CONF_KEY; 023import static org.hamcrest.CoreMatchers.instanceOf; 024import static org.junit.Assert.assertEquals; 025import static org.junit.Assert.assertThat; 026import static org.junit.Assert.assertTrue; 027import static org.junit.Assert.fail; 028 029import java.io.IOException; 030import java.util.Optional; 031import java.util.concurrent.ExecutionException; 032import java.util.concurrent.TimeUnit; 033import org.apache.commons.io.IOUtils; 034import org.apache.hadoop.conf.Configuration; 035import org.apache.hadoop.hbase.HBaseClassTestRule; 036import org.apache.hadoop.hbase.HBaseTestingUtility; 037import org.apache.hadoop.hbase.HRegionLocation; 038import org.apache.hadoop.hbase.TableName; 039import org.apache.hadoop.hbase.coprocessor.ObserverContext; 040import org.apache.hadoop.hbase.coprocessor.RegionCoprocessor; 041import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment; 042import org.apache.hadoop.hbase.coprocessor.RegionObserver; 043import org.apache.hadoop.hbase.exceptions.TimeoutIOException; 044import org.apache.hadoop.hbase.security.User; 045import org.apache.hadoop.hbase.testclassification.ClientTests; 046import org.apache.hadoop.hbase.testclassification.MediumTests; 047import org.apache.hadoop.hbase.util.Bytes; 048import org.apache.hadoop.hbase.util.Threads; 049import org.junit.AfterClass; 050import org.junit.BeforeClass; 051import org.junit.ClassRule; 052import org.junit.Test; 053import org.junit.experimental.categories.Category; 054 055@Category({ MediumTests.class, ClientTests.class }) 056public class TestAsyncRegionLocatorTimeout { 057 058 @ClassRule 059 public static final HBaseClassTestRule CLASS_RULE = 060 HBaseClassTestRule.forClass(TestAsyncRegionLocatorTimeout.class); 061 062 private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(); 063 064 private static TableName TABLE_NAME = TableName.valueOf("async"); 065 066 private static byte[] FAMILY = Bytes.toBytes("cf"); 067 068 private static AsyncConnectionImpl CONN; 069 070 private static AsyncRegionLocator LOCATOR; 071 072 private static volatile long SLEEP_MS = 0L; 073 074 public static class SleepRegionObserver implements RegionCoprocessor, RegionObserver { 075 @Override 076 public Optional<RegionObserver> getRegionObserver() { 077 return Optional.of(this); 078 } 079 080 @Override 081 public void preScannerOpen(ObserverContext<RegionCoprocessorEnvironment> e, Scan scan) 082 throws IOException { 083 if (SLEEP_MS > 0) { 084 Threads.sleepWithoutInterrupt(SLEEP_MS); 085 } 086 } 087 } 088 089 @BeforeClass 090 public static void setUp() throws Exception { 091 Configuration conf = TEST_UTIL.getConfiguration(); 092 conf.set(REGION_COPROCESSOR_CONF_KEY, SleepRegionObserver.class.getName()); 093 conf.setLong(HBASE_CLIENT_META_OPERATION_TIMEOUT, 2000); 094 TEST_UTIL.startMiniCluster(1); 095 TEST_UTIL.createTable(TABLE_NAME, FAMILY); 096 TEST_UTIL.waitTableAvailable(TABLE_NAME); 097 AsyncRegistry registry = AsyncRegistryFactory.getRegistry(TEST_UTIL.getConfiguration()); 098 CONN = new AsyncConnectionImpl(TEST_UTIL.getConfiguration(), registry, 099 registry.getClusterId().get(), User.getCurrent()); 100 LOCATOR = CONN.getLocator(); 101 } 102 103 @AfterClass 104 public static void tearDown() throws Exception { 105 IOUtils.closeQuietly(CONN); 106 TEST_UTIL.shutdownMiniCluster(); 107 } 108 109 @Test 110 public void test() throws InterruptedException, ExecutionException { 111 SLEEP_MS = 1000; 112 long startNs = System.nanoTime(); 113 try { 114 LOCATOR.getRegionLocation(TABLE_NAME, EMPTY_START_ROW, RegionLocateType.CURRENT, 115 TimeUnit.MILLISECONDS.toNanos(500)).get(); 116 fail(); 117 } catch (ExecutionException e) { 118 e.printStackTrace(); 119 assertThat(e.getCause(), instanceOf(TimeoutIOException.class)); 120 } 121 long costMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNs); 122 assertTrue(costMs >= 500); 123 assertTrue(costMs < 1000); 124 // wait for the background task finish 125 Thread.sleep(2000); 126 // Now the location should be in cache, so we will not visit meta again. 127 HRegionLocation loc = LOCATOR.getRegionLocation(TABLE_NAME, EMPTY_START_ROW, 128 RegionLocateType.CURRENT, TimeUnit.MILLISECONDS.toNanos(500)).get(); 129 assertEquals(loc.getServerName(), 130 TEST_UTIL.getHBaseCluster().getRegionServer(0).getServerName()); 131 } 132}