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