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 java.util.stream.Collectors.toList; 021import static org.apache.hadoop.hbase.client.AsyncNonMetaRegionLocator.MAX_CONCURRENT_LOCATE_REQUEST_PER_TABLE; 022import static org.apache.hadoop.hbase.client.ConnectionUtils.isEmptyStartRow; 023import static org.apache.hadoop.hbase.client.ConnectionUtils.isEmptyStopRow; 024import static org.apache.hadoop.hbase.coprocessor.CoprocessorHost.REGION_COPROCESSOR_CONF_KEY; 025import static org.junit.Assert.assertEquals; 026import static org.junit.Assert.assertTrue; 027 028import java.io.IOException; 029import java.util.List; 030import java.util.Optional; 031import java.util.concurrent.CompletableFuture; 032import java.util.concurrent.ExecutionException; 033import java.util.concurrent.atomic.AtomicInteger; 034import java.util.stream.IntStream; 035import org.apache.commons.io.IOUtils; 036import org.apache.hadoop.conf.Configuration; 037import org.apache.hadoop.hbase.HBaseClassTestRule; 038import org.apache.hadoop.hbase.HBaseTestingUtility; 039import org.apache.hadoop.hbase.HRegionLocation; 040import org.apache.hadoop.hbase.TableName; 041import org.apache.hadoop.hbase.coprocessor.ObserverContext; 042import org.apache.hadoop.hbase.coprocessor.RegionCoprocessor; 043import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment; 044import org.apache.hadoop.hbase.coprocessor.RegionObserver; 045import org.apache.hadoop.hbase.regionserver.InternalScanner; 046import org.apache.hadoop.hbase.security.User; 047import org.apache.hadoop.hbase.testclassification.ClientTests; 048import org.apache.hadoop.hbase.testclassification.MediumTests; 049import org.apache.hadoop.hbase.util.Bytes; 050import org.apache.hadoop.hbase.util.Threads; 051import org.junit.AfterClass; 052import org.junit.BeforeClass; 053import org.junit.ClassRule; 054import org.junit.Test; 055import org.junit.experimental.categories.Category; 056 057@Category({ MediumTests.class, ClientTests.class }) 058public class TestAsyncNonMetaRegionLocatorConcurrenyLimit { 059 060 @ClassRule 061 public static final HBaseClassTestRule CLASS_RULE = 062 HBaseClassTestRule.forClass(TestAsyncNonMetaRegionLocatorConcurrenyLimit.class); 063 064 private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(); 065 066 private static TableName TABLE_NAME = TableName.valueOf("async"); 067 068 private static byte[] FAMILY = Bytes.toBytes("cf"); 069 070 private static AsyncConnectionImpl CONN; 071 072 private static AsyncNonMetaRegionLocator LOCATOR; 073 074 private static byte[][] SPLIT_KEYS; 075 076 private static int MAX_ALLOWED = 2; 077 078 private static AtomicInteger CONCURRENCY = new AtomicInteger(0); 079 080 private static AtomicInteger MAX_CONCURRENCY = new AtomicInteger(0); 081 082 public static final class CountingRegionObserver implements RegionCoprocessor, RegionObserver { 083 084 @Override 085 public Optional<RegionObserver> getRegionObserver() { 086 return Optional.of(this); 087 } 088 089 @Override 090 public boolean preScannerNext(ObserverContext<RegionCoprocessorEnvironment> c, 091 InternalScanner s, List<Result> result, int limit, boolean hasNext) throws IOException { 092 if (c.getEnvironment().getRegionInfo().isMetaRegion()) { 093 int concurrency = CONCURRENCY.incrementAndGet(); 094 for (;;) { 095 int max = MAX_CONCURRENCY.get(); 096 if (concurrency <= max) { 097 break; 098 } 099 if (MAX_CONCURRENCY.compareAndSet(max, concurrency)) { 100 break; 101 } 102 } 103 Threads.sleepWithoutInterrupt(10); 104 } 105 return hasNext; 106 } 107 108 @Override 109 public boolean postScannerNext(ObserverContext<RegionCoprocessorEnvironment> c, 110 InternalScanner s, List<Result> result, int limit, boolean hasNext) throws IOException { 111 if (c.getEnvironment().getRegionInfo().isMetaRegion()) { 112 CONCURRENCY.decrementAndGet(); 113 } 114 return hasNext; 115 } 116 } 117 118 @BeforeClass 119 public static void setUp() throws Exception { 120 Configuration conf = TEST_UTIL.getConfiguration(); 121 conf.set(REGION_COPROCESSOR_CONF_KEY, CountingRegionObserver.class.getName()); 122 conf.setInt(MAX_CONCURRENT_LOCATE_REQUEST_PER_TABLE, MAX_ALLOWED); 123 TEST_UTIL.startMiniCluster(3); 124 TEST_UTIL.getAdmin().balancerSwitch(false, true); 125 AsyncRegistry registry = AsyncRegistryFactory.getRegistry(TEST_UTIL.getConfiguration()); 126 CONN = new AsyncConnectionImpl(TEST_UTIL.getConfiguration(), registry, 127 registry.getClusterId().get(), User.getCurrent()); 128 LOCATOR = new AsyncNonMetaRegionLocator(CONN); 129 SPLIT_KEYS = IntStream.range(1, 256).mapToObj(i -> Bytes.toBytes(String.format("%02x", i))) 130 .toArray(byte[][]::new); 131 TEST_UTIL.createTable(TABLE_NAME, FAMILY, SPLIT_KEYS); 132 TEST_UTIL.waitTableAvailable(TABLE_NAME); 133 } 134 135 @AfterClass 136 public static void tearDown() throws Exception { 137 IOUtils.closeQuietly(CONN); 138 TEST_UTIL.shutdownMiniCluster(); 139 } 140 141 private void assertLocs(List<CompletableFuture<HRegionLocation>> futures) 142 throws InterruptedException, ExecutionException { 143 assertEquals(256, futures.size()); 144 for (int i = 0; i < futures.size(); i++) { 145 HRegionLocation loc = futures.get(i).get(); 146 if (i == 0) { 147 assertTrue(isEmptyStartRow(loc.getRegion().getStartKey())); 148 } else { 149 assertEquals(String.format("%02x", i), Bytes.toString(loc.getRegion().getStartKey())); 150 } 151 if (i == futures.size() - 1) { 152 assertTrue(isEmptyStopRow(loc.getRegion().getEndKey())); 153 } else { 154 assertEquals(String.format("%02x", i + 1), Bytes.toString(loc.getRegion().getEndKey())); 155 } 156 } 157 } 158 159 @Test 160 public void test() throws InterruptedException, ExecutionException { 161 List<CompletableFuture<HRegionLocation>> futures = 162 IntStream.range(0, 256).mapToObj(i -> Bytes.toBytes(String.format("%02x", i))) 163 .map(r -> LOCATOR.getRegionLocation(TABLE_NAME, r, RegionLocateType.CURRENT, false)) 164 .collect(toList()); 165 assertLocs(futures); 166 assertTrue("max allowed is " + MAX_ALLOWED + " but actual is " + MAX_CONCURRENCY.get(), 167 MAX_CONCURRENCY.get() <= MAX_ALLOWED); 168 } 169}