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; 019 020import static org.apache.hadoop.hbase.regionserver.HRegion.warmupHRegion; 021import static org.junit.jupiter.api.Assertions.assertTrue; 022 023import java.io.IOException; 024import org.apache.hadoop.hbase.HBaseTestingUtil; 025import org.apache.hadoop.hbase.TableName; 026import org.apache.hadoop.hbase.Waiter; 027import org.apache.hadoop.hbase.client.CompactionState; 028import org.apache.hadoop.hbase.client.Put; 029import org.apache.hadoop.hbase.client.RegionInfo; 030import org.apache.hadoop.hbase.client.Table; 031import org.apache.hadoop.hbase.client.TableDescriptor; 032import org.apache.hadoop.hbase.regionserver.HRegion; 033import org.apache.hadoop.hbase.regionserver.HRegionServer; 034import org.apache.hadoop.hbase.testclassification.LargeTests; 035import org.apache.hadoop.hbase.testclassification.MasterTests; 036import org.apache.hadoop.hbase.util.Bytes; 037import org.apache.hadoop.hbase.util.EnvironmentEdgeManager; 038import org.junit.jupiter.api.AfterAll; 039import org.junit.jupiter.api.AfterEach; 040import org.junit.jupiter.api.BeforeAll; 041import org.junit.jupiter.api.BeforeEach; 042import org.junit.jupiter.api.Tag; 043import org.junit.jupiter.api.Test; 044import org.slf4j.Logger; 045import org.slf4j.LoggerFactory; 046 047/** 048 * Run tests that use the HBase clients; {@link org.apache.hadoop.hbase.client.TableBuilder}. Sets 049 * up the HBase mini cluster once at start and runs through all client tests. Each creates a table 050 * named for the method and does its stuff against that. 051 */ 052@Tag(MasterTests.TAG) 053@Tag(LargeTests.TAG) 054public class TestWarmupRegion { 055 056 private static final Logger LOG = LoggerFactory.getLogger(TestWarmupRegion.class); 057 protected TableName TABLENAME = TableName.valueOf("testPurgeFutureDeletes"); 058 protected final static HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil(); 059 private static byte[] ROW = Bytes.toBytes("testRow"); 060 private static byte[] FAMILY = Bytes.toBytes("testFamily"); 061 private static byte[] VALUE = Bytes.toBytes("testValue"); 062 private static byte[] COLUMN = Bytes.toBytes("column"); 063 private static int numRows = 10000; 064 protected static int SLAVES = 3; 065 private static Table table; 066 067 /** 068 * @throws java.lang.Exception 069 */ 070 @BeforeAll 071 public static void setUpBeforeClass() throws Exception { 072 TEST_UTIL.startMiniCluster(SLAVES); 073 } 074 075 /** 076 * @throws java.lang.Exception 077 */ 078 @AfterAll 079 public static void tearDownAfterClass() throws Exception { 080 TEST_UTIL.shutdownMiniCluster(); 081 } 082 083 /** 084 * @throws java.lang.Exception 085 */ 086 @BeforeEach 087 public void setUp() throws Exception { 088 table = TEST_UTIL.createTable(TABLENAME, FAMILY); 089 090 // future timestamp 091 for (int i = 0; i < numRows; i++) { 092 long ts = EnvironmentEdgeManager.currentTime() * 2; 093 Put put = new Put(ROW, ts); 094 put.addColumn(FAMILY, COLUMN, VALUE); 095 table.put(put); 096 } 097 098 // major compaction, purged future deletes 099 TEST_UTIL.getAdmin().flush(TABLENAME); 100 TEST_UTIL.getAdmin().majorCompact(TABLENAME); 101 102 // waiting for the major compaction to complete 103 TEST_UTIL.waitFor(6000, new Waiter.Predicate<IOException>() { 104 @Override 105 public boolean evaluate() throws IOException { 106 return TEST_UTIL.getAdmin().getCompactionState(TABLENAME) == CompactionState.NONE; 107 } 108 }); 109 110 table.close(); 111 } 112 113 /** 114 * @throws java.lang.Exception 115 */ 116 @AfterEach 117 public void tearDown() throws Exception { 118 TEST_UTIL.deleteTable(TABLENAME); 119 } 120 121 protected void runwarmup() throws InterruptedException { 122 Thread thread = new Thread(new Runnable() { 123 @Override 124 public void run() { 125 HRegionServer rs = TEST_UTIL.getMiniHBaseCluster().getRegionServer(0); 126 HRegion region = TEST_UTIL.getMiniHBaseCluster().getRegions(TABLENAME).get(0); 127 RegionInfo info = region.getRegionInfo(); 128 129 try { 130 TableDescriptor htd = table.getDescriptor(); 131 for (int i = 0; i < 10; i++) { 132 warmupHRegion(info, htd, rs.getWAL(info), rs.getConfiguration(), rs, null); 133 } 134 } catch (IOException ie) { 135 LOG.error("Failed warming up region " + info.getRegionNameAsString(), ie); 136 } 137 } 138 }); 139 thread.start(); 140 thread.join(); 141 } 142 143 /** 144 * Basic client side validation of HBASE-4536 145 */ 146 @Test 147 public void testWarmup() throws Exception { 148 int serverid = 0; 149 HRegion region = TEST_UTIL.getMiniHBaseCluster().getRegions(TABLENAME).get(0); 150 RegionInfo info = region.getRegionInfo(); 151 runwarmup(); 152 for (int i = 0; i < 10; i++) { 153 HRegionServer rs = TEST_UTIL.getMiniHBaseCluster().getRegionServer(serverid); 154 byte[] destName = Bytes.toBytes(rs.getServerName().toString()); 155 assertTrue(destName != null); 156 LOG.info("i=" + i); 157 TEST_UTIL.getMiniHBaseCluster().getMaster().move(info.getEncodedNameAsBytes(), destName); 158 serverid = (serverid + 1) % 2; 159 } 160 } 161 162 @Test 163 public void testWarmupAndClose() throws IOException { 164 HRegionServer rs = TEST_UTIL.getMiniHBaseCluster().getRegionServer(0); 165 HRegion region = TEST_UTIL.getMiniHBaseCluster().getRegions(TABLENAME).get(0); 166 RegionInfo info = region.getRegionInfo(); 167 168 TableDescriptor htd = table.getDescriptor(); 169 HRegion warmedUpRegion = 170 warmupHRegion(info, htd, rs.getWAL(info), rs.getConfiguration(), rs, null); 171 assertTrue(warmedUpRegion.isClosed()); 172 } 173}