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