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.master.assignment; 019 020import static org.junit.Assert.assertEquals; 021 022import java.util.List; 023import java.util.concurrent.Callable; 024import java.util.concurrent.ExecutorCompletionService; 025import java.util.concurrent.Future; 026import java.util.concurrent.ThreadPoolExecutor; 027import java.util.concurrent.TimeUnit; 028import org.apache.hadoop.hbase.HBaseClassTestRule; 029import org.apache.hadoop.hbase.HBaseTestingUtil; 030import org.apache.hadoop.hbase.TableName; 031import org.apache.hadoop.hbase.client.RegionInfo; 032import org.apache.hadoop.hbase.client.RegionInfoBuilder; 033import org.apache.hadoop.hbase.procedure2.util.StringUtils; 034import org.apache.hadoop.hbase.testclassification.MasterTests; 035import org.apache.hadoop.hbase.testclassification.MediumTests; 036import org.apache.hadoop.hbase.util.Bytes; 037import org.apache.hadoop.hbase.util.EnvironmentEdgeManager; 038import org.apache.hadoop.hbase.util.Threads; 039import org.junit.After; 040import org.junit.AfterClass; 041import org.junit.Before; 042import org.junit.BeforeClass; 043import org.junit.ClassRule; 044import org.junit.Test; 045import org.junit.experimental.categories.Category; 046import org.slf4j.Logger; 047import org.slf4j.LoggerFactory; 048 049import org.apache.hbase.thirdparty.com.google.common.util.concurrent.ThreadFactoryBuilder; 050 051@Category({ MasterTests.class, MediumTests.class }) 052public class TestRegionStates { 053 054 @ClassRule 055 public static final HBaseClassTestRule CLASS_RULE = 056 HBaseClassTestRule.forClass(TestRegionStates.class); 057 058 private static final Logger LOG = LoggerFactory.getLogger(TestRegionStates.class); 059 060 protected static final HBaseTestingUtil UTIL = new HBaseTestingUtil(); 061 062 private static ThreadPoolExecutor threadPool; 063 private static ExecutorCompletionService<Object> executorService; 064 065 @BeforeClass 066 public static void setUp() throws Exception { 067 threadPool = Threads.getBoundedCachedThreadPool(32, 60L, TimeUnit.SECONDS, 068 new ThreadFactoryBuilder().setNameFormat("ProcedureDispatcher-pool-%d").setDaemon(true) 069 .setUncaughtExceptionHandler((t, e) -> LOG.warn("Failed thread " + t.getName(), e)) 070 .build()); 071 executorService = new ExecutorCompletionService<>(threadPool); 072 } 073 074 @AfterClass 075 public static void tearDown() throws Exception { 076 threadPool.shutdown(); 077 } 078 079 @Before 080 public void testSetup() { 081 } 082 083 @After 084 public void testTearDown() throws Exception { 085 while (true) { 086 Future<Object> f = executorService.poll(); 087 if (f == null) break; 088 f.get(); 089 } 090 } 091 092 private static void waitExecutorService(final int count) throws Exception { 093 for (int i = 0; i < count; ++i) { 094 executorService.take().get(); 095 } 096 } 097 098 // ========================================================================== 099 // Regions related 100 // ========================================================================== 101 102 @Test 103 public void testRegionDoubleCreation() throws Exception { 104 // NOTE: RegionInfo sort by table first, so we are relying on that 105 final TableName TABLE_NAME_A = TableName.valueOf("testOrderedByTableA"); 106 final TableName TABLE_NAME_B = TableName.valueOf("testOrderedByTableB"); 107 final TableName TABLE_NAME_C = TableName.valueOf("testOrderedByTableC"); 108 final RegionStates stateMap = new RegionStates(); 109 final int NRUNS = 1000; 110 final int NSMALL_RUNS = 3; 111 112 // add some regions for table B 113 for (int i = 0; i < NRUNS; ++i) { 114 addRegionNode(stateMap, TABLE_NAME_B, i); 115 } 116 // re-add the regions for table B 117 for (int i = 0; i < NRUNS; ++i) { 118 addRegionNode(stateMap, TABLE_NAME_B, i); 119 } 120 waitExecutorService(NRUNS * 2); 121 122 // add two other tables A and C that will be placed before and after table B (sort order) 123 for (int i = 0; i < NSMALL_RUNS; ++i) { 124 addRegionNode(stateMap, TABLE_NAME_A, i); 125 addRegionNode(stateMap, TABLE_NAME_C, i); 126 } 127 waitExecutorService(NSMALL_RUNS * 2); 128 // check for the list of regions of the 3 tables 129 checkTableRegions(stateMap, TABLE_NAME_A, NSMALL_RUNS); 130 checkTableRegions(stateMap, TABLE_NAME_B, NRUNS); 131 checkTableRegions(stateMap, TABLE_NAME_C, NSMALL_RUNS); 132 } 133 134 private void checkTableRegions(final RegionStates stateMap, final TableName tableName, 135 final int nregions) { 136 List<RegionStateNode> rns = stateMap.getTableRegionStateNodes(tableName); 137 assertEquals(nregions, rns.size()); 138 for (int i = 1; i < rns.size(); ++i) { 139 long a = Bytes.toLong(rns.get(i - 1).getRegionInfo().getStartKey()); 140 long b = Bytes.toLong(rns.get(i + 0).getRegionInfo().getStartKey()); 141 assertEquals(b, a + 1); 142 } 143 } 144 145 private void addRegionNode(final RegionStates stateMap, final TableName tableName, 146 final long regionId) { 147 executorService.submit(new Callable<Object>() { 148 @Override 149 public Object call() { 150 return stateMap.getOrCreateRegionStateNode( 151 RegionInfoBuilder.newBuilder(tableName).setStartKey(Bytes.toBytes(regionId)) 152 .setEndKey(Bytes.toBytes(regionId + 1)).setSplit(false).setRegionId(0).build()); 153 } 154 }); 155 } 156 157 private RegionInfo createRegionInfo(final TableName tableName, final long regionId) { 158 return RegionInfoBuilder.newBuilder(tableName).setStartKey(Bytes.toBytes(regionId)) 159 .setEndKey(Bytes.toBytes(regionId + 1)).setSplit(false).setRegionId(0).build(); 160 } 161 162 @Test 163 public void testPerf() throws Exception { 164 final TableName TABLE_NAME = TableName.valueOf("testPerf"); 165 final int NRUNS = 1000000; // 1M 166 final RegionStates stateMap = new RegionStates(); 167 168 long st = EnvironmentEdgeManager.currentTime(); 169 for (int i = 0; i < NRUNS; ++i) { 170 final int regionId = i; 171 executorService.submit(new Callable<Object>() { 172 @Override 173 public Object call() { 174 RegionInfo hri = createRegionInfo(TABLE_NAME, regionId); 175 return stateMap.getOrCreateRegionStateNode(hri); 176 } 177 }); 178 } 179 waitExecutorService(NRUNS); 180 long et = EnvironmentEdgeManager.currentTime(); 181 LOG.info(String.format("PERF STATEMAP INSERT: %s %s/sec", StringUtils.humanTimeDiff(et - st), 182 StringUtils.humanSize(NRUNS / ((et - st) / 1000.0f)))); 183 184 st = EnvironmentEdgeManager.currentTime(); 185 for (int i = 0; i < NRUNS; ++i) { 186 final int regionId = i; 187 executorService.submit(new Callable<Object>() { 188 @Override 189 public Object call() { 190 RegionInfo hri = createRegionInfo(TABLE_NAME, regionId); 191 return stateMap.getRegionState(hri); 192 } 193 }); 194 } 195 196 waitExecutorService(NRUNS); 197 et = EnvironmentEdgeManager.currentTime(); 198 LOG.info(String.format("PERF STATEMAP GET: %s %s/sec", StringUtils.humanTimeDiff(et - st), 199 StringUtils.humanSize(NRUNS / ((et - st) / 1000.0f)))); 200 } 201 202 @Test 203 public void testPerfSingleThread() { 204 final TableName TABLE_NAME = TableName.valueOf("testPerf"); 205 final int NRUNS = 1 * 1000000; // 1M 206 207 final RegionStates stateMap = new RegionStates(); 208 long st = EnvironmentEdgeManager.currentTime(); 209 for (int i = 0; i < NRUNS; ++i) { 210 stateMap.createRegionStateNode(createRegionInfo(TABLE_NAME, i)); 211 } 212 long et = EnvironmentEdgeManager.currentTime(); 213 LOG.info(String.format("PERF SingleThread: %s %s/sec", StringUtils.humanTimeDiff(et - st), 214 StringUtils.humanSize(NRUNS / ((et - st) / 1000.0f)))); 215 } 216}