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.HBASE_CLIENT_META_OPERATION_TIMEOUT; 021import static org.apache.hadoop.hbase.master.LoadBalancer.TABLES_ON_MASTER; 022import static org.junit.Assert.assertEquals; 023 024import java.io.IOException; 025import java.util.ArrayList; 026import java.util.Arrays; 027import java.util.Collections; 028import java.util.List; 029import java.util.Random; 030import java.util.concurrent.ExecutionException; 031import java.util.concurrent.ExecutorService; 032import java.util.concurrent.Executors; 033import java.util.concurrent.Future; 034import java.util.concurrent.TimeUnit; 035import java.util.concurrent.atomic.AtomicBoolean; 036import java.util.stream.Collectors; 037import java.util.stream.IntStream; 038import org.apache.commons.io.IOUtils; 039import org.apache.hadoop.hbase.HBaseClassTestRule; 040import org.apache.hadoop.hbase.HBaseTestingUtility; 041import org.apache.hadoop.hbase.MemoryCompactionPolicy; 042import org.apache.hadoop.hbase.ServerName; 043import org.apache.hadoop.hbase.TableName; 044import org.apache.hadoop.hbase.io.ByteBufferPool; 045import org.apache.hadoop.hbase.regionserver.CompactingMemStore; 046import org.apache.hadoop.hbase.regionserver.HRegion; 047import org.apache.hadoop.hbase.testclassification.ClientTests; 048import org.apache.hadoop.hbase.testclassification.LargeTests; 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/** 058 * Will split the table, and move region randomly when testing. 059 */ 060@Category({ LargeTests.class, ClientTests.class }) 061public class TestAsyncTableGetMultiThreaded { 062 063 @ClassRule 064 public static final HBaseClassTestRule CLASS_RULE = 065 HBaseClassTestRule.forClass(TestAsyncTableGetMultiThreaded.class); 066 067 private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(); 068 069 private static TableName TABLE_NAME = TableName.valueOf("async"); 070 071 private static byte[] FAMILY = Bytes.toBytes("cf"); 072 073 private static byte[] QUALIFIER = Bytes.toBytes("cq"); 074 075 private static int COUNT = 1000; 076 077 private static AsyncConnection CONN; 078 079 private static AsyncTable<?> TABLE; 080 081 private static byte[][] SPLIT_KEYS; 082 083 @BeforeClass 084 public static void setUp() throws Exception { 085 setUp(MemoryCompactionPolicy.NONE); 086 } 087 088 protected static void setUp(MemoryCompactionPolicy memoryCompaction) throws Exception { 089 TEST_UTIL.getConfiguration().set(TABLES_ON_MASTER, "none"); 090 TEST_UTIL.getConfiguration().setLong(HBASE_CLIENT_META_OPERATION_TIMEOUT, 60000L); 091 TEST_UTIL.getConfiguration().setInt(ByteBufferPool.MAX_POOL_SIZE_KEY, 100); 092 TEST_UTIL.getConfiguration().set(CompactingMemStore.COMPACTING_MEMSTORE_TYPE_KEY, 093 String.valueOf(memoryCompaction)); 094 095 TEST_UTIL.startMiniCluster(5); 096 SPLIT_KEYS = new byte[8][]; 097 for (int i = 111; i < 999; i += 111) { 098 SPLIT_KEYS[i / 111 - 1] = Bytes.toBytes(String.format("%03d", i)); 099 } 100 TEST_UTIL.createTable(TABLE_NAME, FAMILY); 101 TEST_UTIL.waitTableAvailable(TABLE_NAME); 102 CONN = ConnectionFactory.createAsyncConnection(TEST_UTIL.getConfiguration()).get(); 103 TABLE = CONN.getTableBuilder(TABLE_NAME).setReadRpcTimeout(1, TimeUnit.SECONDS) 104 .setMaxRetries(1000).build(); 105 TABLE.putAll( 106 IntStream.range(0, COUNT).mapToObj(i -> new Put(Bytes.toBytes(String.format("%03d", i))) 107 .addColumn(FAMILY, QUALIFIER, Bytes.toBytes(i))).collect(Collectors.toList())) 108 .get(); 109 } 110 111 @AfterClass 112 public static void tearDown() throws Exception { 113 IOUtils.closeQuietly(CONN); 114 TEST_UTIL.shutdownMiniCluster(); 115 } 116 117 private void run(AtomicBoolean stop) throws InterruptedException, ExecutionException { 118 while (!stop.get()) { 119 for (int i = 0; i < COUNT; i++) { 120 assertEquals(i, Bytes.toInt(TABLE.get(new Get(Bytes.toBytes(String.format("%03d", i)))) 121 .get().getValue(FAMILY, QUALIFIER))); 122 } 123 } 124 } 125 126 @Test 127 public void test() throws IOException, InterruptedException, ExecutionException { 128 int numThreads = 20; 129 AtomicBoolean stop = new AtomicBoolean(false); 130 ExecutorService executor = 131 Executors.newFixedThreadPool(numThreads, Threads.newDaemonThreadFactory("TestAsyncGet-")); 132 List<Future<?>> futures = new ArrayList<>(); 133 IntStream.range(0, numThreads).forEach(i -> futures.add(executor.submit(() -> { 134 run(stop); 135 return null; 136 }))); 137 Collections.shuffle(Arrays.asList(SPLIT_KEYS), new Random(123)); 138 Admin admin = TEST_UTIL.getAdmin(); 139 for (byte[] splitPoint : SPLIT_KEYS) { 140 admin.split(TABLE_NAME, splitPoint); 141 for (HRegion region : TEST_UTIL.getHBaseCluster().getRegions(TABLE_NAME)) { 142 region.compact(true); 143 } 144 Thread.sleep(5000); 145 admin.balance(true); 146 Thread.sleep(5000); 147 ServerName metaServer = TEST_UTIL.getHBaseCluster().getServerHoldingMeta(); 148 ServerName newMetaServer = TEST_UTIL.getHBaseCluster().getRegionServerThreads().stream() 149 .map(t -> t.getRegionServer().getServerName()).filter(s -> !s.equals(metaServer)) 150 .findAny().get(); 151 admin.move(RegionInfoBuilder.FIRST_META_REGIONINFO.getEncodedNameAsBytes(), 152 Bytes.toBytes(newMetaServer.getServerName())); 153 Thread.sleep(5000); 154 } 155 stop.set(true); 156 executor.shutdown(); 157 for (Future<?> future : futures) { 158 future.get(); 159 } 160 } 161}