1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.master;
20
21 import static org.apache.hadoop.hbase.regionserver.HRegion.warmupHRegion;
22 import java.io.IOException;
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.hadoop.conf.Configuration;
26 import org.apache.hadoop.hbase.HBaseTestingUtility;
27 import org.apache.hadoop.hbase.HRegionInfo;
28 import org.apache.hadoop.hbase.HTableDescriptor;
29 import org.apache.hadoop.hbase.MiniHBaseCluster;
30 import org.apache.hadoop.hbase.TableName;
31 import org.apache.hadoop.hbase.Waiter;
32 import org.apache.hadoop.hbase.client.Put;
33 import org.apache.hadoop.hbase.client.Table;
34 import org.apache.hadoop.hbase.protobuf.generated.AdminProtos;
35 import org.apache.hadoop.hbase.regionserver.HRegion;
36 import org.apache.hadoop.hbase.regionserver.HRegionServer;
37 import org.apache.hadoop.hbase.testclassification.LargeTests;
38 import org.apache.hadoop.hbase.util.Bytes;
39 import org.junit.experimental.categories.Category;
40 import org.junit.BeforeClass;
41 import org.junit.AfterClass;
42 import org.junit.Before;
43 import org.junit.After;
44 import org.junit.Test;
45
46
47
48
49
50
51 @Category(LargeTests.class)
52 @SuppressWarnings ("deprecation")
53 public class TestWarmupRegion {
54 private static final Log LOG = LogFactory.getLog(TestWarmupRegion.class);
55 protected TableName TABLENAME = TableName.valueOf("testPurgeFutureDeletes");
56 protected final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
57 private static byte [] ROW = Bytes.toBytes("testRow");
58 private static byte [] FAMILY = Bytes.toBytes("testFamily");
59 private static byte [] QUALIFIER = Bytes.toBytes("testQualifier");
60 private static byte [] VALUE = Bytes.toBytes("testValue");
61 private static byte[] COLUMN = Bytes.toBytes("column");
62 private static int numRows = 10000;
63 protected static int SLAVES = 3;
64 private static MiniHBaseCluster myCluster;
65 private static Table table;
66
67
68
69
70 @BeforeClass
71 public static void setUpBeforeClass() throws Exception {
72 Configuration conf = TEST_UTIL.getConfiguration();
73 TEST_UTIL.startMiniCluster(SLAVES);
74 }
75
76
77
78
79 @AfterClass
80 public static void tearDownAfterClass() throws Exception {
81 TEST_UTIL.shutdownMiniCluster();
82 }
83
84
85
86
87 @Before
88 public void setUp() throws Exception {
89 table = TEST_UTIL.createTable(TABLENAME, FAMILY);
90
91
92 for (int i = 0; i < numRows; i++) {
93 long ts = System.currentTimeMillis() * 2;
94 Put put = new Put(ROW, ts);
95 put.add(FAMILY, COLUMN, VALUE);
96 table.put(put);
97 }
98
99
100 TEST_UTIL.getHBaseAdmin().flush(TABLENAME);
101 TEST_UTIL.getHBaseAdmin().majorCompact(TABLENAME);
102
103
104 TEST_UTIL.waitFor(6000, new Waiter.Predicate<IOException>() {
105 @Override
106 public boolean evaluate() throws IOException {
107 return TEST_UTIL.getHBaseAdmin().getCompactionState(TABLENAME) ==
108 AdminProtos.GetRegionInfoResponse.CompactionState.NONE;
109 }
110 });
111
112 table.close();
113 }
114
115
116
117
118
119 @After
120 public void tearDown() throws Exception {
121
122 }
123
124 protected void runwarmup() throws InterruptedException{
125 Thread thread = new Thread(new Runnable() {
126 @Override
127 public void run() {
128 HRegionServer rs = TEST_UTIL.getMiniHBaseCluster().getRegionServer(0);
129 HRegion region = TEST_UTIL.getMiniHBaseCluster().getRegions(TABLENAME).get(0);
130 HRegionInfo info = region.getRegionInfo();
131
132 try {
133 HTableDescriptor htd = table.getTableDescriptor();
134 for (int i = 0; i < 10; i++) {
135 warmupHRegion(info, htd, rs.getWAL(info), rs.getConfiguration(), rs, null);
136 }
137
138 } catch (IOException ie) {
139 LOG.error("Failed warming up region " + info.getRegionNameAsString(), ie);
140 }
141 }
142 });
143 thread.start();
144 thread.join();
145 }
146
147
148
149
150 @Test
151 public void testWarmup() throws Exception {
152 int serverid = 0;
153 HRegion region = TEST_UTIL.getMiniHBaseCluster().getRegions(TABLENAME).get(0);
154 HRegionInfo info = region.getRegionInfo();
155 runwarmup();
156 for (int i = 0; i < 10; i++) {
157 HRegionServer rs = TEST_UTIL.getMiniHBaseCluster().getRegionServer(serverid);
158 byte [] destName = Bytes.toBytes(rs.getServerName().toString());
159 TEST_UTIL.getMiniHBaseCluster().getMaster().move(info.getEncodedNameAsBytes(), destName);
160 serverid = (serverid + 1) % 2;
161 }
162 }
163 }