1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.client;
20
21 import static org.mockito.Matchers.anyObject;
22 import static org.mockito.Mockito.doReturn;
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.reset;
25 import static org.mockito.Mockito.times;
26 import static org.mockito.Mockito.verify;
27
28 import java.math.BigDecimal;
29 import java.util.List;
30 import java.util.NavigableMap;
31 import java.util.Random;
32
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35 import org.apache.hadoop.hbase.HBaseTestingUtility;
36 import org.apache.hadoop.hbase.HConstants;
37 import org.apache.hadoop.hbase.HRegionInfo;
38 import org.apache.hadoop.hbase.MetaTableAccessor;
39 import org.apache.hadoop.hbase.ServerName;
40 import org.apache.hadoop.hbase.TableName;
41 import org.apache.hadoop.hbase.testclassification.MediumTests;
42 import org.apache.hadoop.hbase.util.Bytes;
43 import org.apache.hadoop.hbase.util.StoppableImplementation;
44 import org.apache.hadoop.hbase.util.Threads;
45 import org.apache.hadoop.util.StringUtils;
46 import org.junit.After;
47 import org.junit.Assert;
48 import org.junit.Test;
49 import org.junit.experimental.categories.Category;
50
51 @Category(MediumTests.class)
52 public class TestMetaScanner {
53 final Log LOG = LogFactory.getLog(getClass());
54 private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
55 private Connection connection;
56
57 @SuppressWarnings("JUnit4SetUpNotRun")
58 public void setUp() throws Exception {
59 TEST_UTIL.startMiniCluster(1);
60 this.connection = TEST_UTIL.getConnection();
61 }
62
63 @After
64 public void tearDown() throws Exception {
65 TEST_UTIL.shutdownMiniCluster();
66 }
67
68 @Test
69 public void testMetaScanner() throws Exception {
70 LOG.info("Starting testMetaScanner");
71
72 setUp();
73 final TableName TABLENAME = TableName.valueOf("testMetaScanner");
74 final byte[] FAMILY = Bytes.toBytes("family");
75 final byte[][] SPLIT_KEYS =
76 new byte[][] { Bytes.toBytes("region_a"), Bytes.toBytes("region_b") };
77
78 TEST_UTIL.createTable(TABLENAME, FAMILY, SPLIT_KEYS);
79 HTable table = (HTable) connection.getTable(TABLENAME);
80
81 TEST_UTIL.countRows(table);
82
83 MetaScanner.MetaScannerVisitor visitor =
84 mock(MetaScanner.MetaScannerVisitor.class);
85 doReturn(true).when(visitor).processRow((Result)anyObject());
86
87
88 MetaScanner.metaScan(connection, visitor, TABLENAME);
89 verify(visitor, times(3)).processRow((Result)anyObject());
90
91
92
93 reset(visitor);
94 doReturn(true).when(visitor).processRow((Result)anyObject());
95 MetaScanner.metaScan(connection, visitor, TABLENAME, HConstants.EMPTY_BYTE_ARRAY, 1000);
96 verify(visitor, times(3)).processRow((Result)anyObject());
97
98
99
100 reset(visitor);
101 doReturn(true).when(visitor).processRow((Result)anyObject());
102 MetaScanner.metaScan(connection, visitor, TABLENAME, Bytes.toBytes("region_ac"), 1000);
103 verify(visitor, times(2)).processRow((Result)anyObject());
104
105
106 reset(visitor);
107 doReturn(true).when(visitor).processRow((Result) anyObject());
108 MetaScanner.metaScan(connection, visitor, TABLENAME, Bytes.toBytes("region_ac"), 1);
109 verify(visitor, times(1)).processRow((Result) anyObject());
110 table.close();
111 }
112
113 @Test
114 public void testConcurrentMetaScannerAndCatalogJanitor() throws Throwable {
115
116
117
118
119 TEST_UTIL.getConfiguration().setLong("hbase.catalogjanitor.interval", 500);
120 setUp();
121
122 final long runtime = 30 * 1000;
123 LOG.info("Starting testConcurrentMetaScannerAndCatalogJanitor");
124 final TableName TABLENAME =
125 TableName.valueOf("testConcurrentMetaScannerAndCatalogJanitor");
126 final byte[] FAMILY = Bytes.toBytes("family");
127 TEST_UTIL.createTable(TABLENAME, FAMILY);
128
129 class RegionMetaSplitter extends StoppableImplementation implements Runnable {
130 Random random = new Random();
131 Throwable ex = null;
132 @Override
133 public void run() {
134 while (!isStopped()) {
135 try {
136 List<HRegionInfo> regions = MetaScanner.listAllRegions(TEST_UTIL.getConfiguration(),
137 connection, false);
138
139
140 HRegionInfo parent = regions.get(random.nextInt(regions.size()));
141 if (parent == null || !TABLENAME.equals(parent.getTable())) {
142 continue;
143 }
144
145 long startKey = 0, endKey = Long.MAX_VALUE;
146 byte[] start = parent.getStartKey();
147 byte[] end = parent.getEndKey();
148 if (!Bytes.equals(HConstants.EMPTY_START_ROW, parent.getStartKey())) {
149 startKey = Bytes.toLong(parent.getStartKey());
150 }
151 if (!Bytes.equals(HConstants.EMPTY_END_ROW, parent.getEndKey())) {
152 endKey = Bytes.toLong(parent.getEndKey());
153 }
154 if (startKey == endKey) {
155 continue;
156 }
157
158 long midKey = BigDecimal.valueOf(startKey).add(BigDecimal.valueOf(endKey))
159 .divideToIntegralValue(BigDecimal.valueOf(2)).longValue();
160
161 HRegionInfo splita = new HRegionInfo(TABLENAME,
162 start,
163 Bytes.toBytes(midKey));
164 HRegionInfo splitb = new HRegionInfo(TABLENAME,
165 Bytes.toBytes(midKey),
166 end);
167
168 MetaTableAccessor.splitRegion(connection,
169 parent, splita, splitb, ServerName.valueOf("fooserver", 1, 0), 1);
170
171 Threads.sleep(random.nextInt(200));
172 } catch (Throwable e) {
173 ex = e;
174 Assert.fail(StringUtils.stringifyException(e));
175 }
176 }
177 }
178 void rethrowExceptionIfAny() throws Throwable {
179 if (ex != null) { throw ex; }
180 }
181 }
182
183 class MetaScannerVerifier extends StoppableImplementation implements Runnable {
184 Random random = new Random();
185 Throwable ex = null;
186 @Override
187 public void run() {
188 while(!isStopped()) {
189 try {
190 NavigableMap<HRegionInfo, ServerName> regions =
191 MetaScanner.allTableRegions(connection, TABLENAME);
192
193 LOG.info("-------");
194 byte[] lastEndKey = HConstants.EMPTY_START_ROW;
195 for (HRegionInfo hri: regions.navigableKeySet()) {
196 long startKey = 0, endKey = Long.MAX_VALUE;
197 if (!Bytes.equals(HConstants.EMPTY_START_ROW, hri.getStartKey())) {
198 startKey = Bytes.toLong(hri.getStartKey());
199 }
200 if (!Bytes.equals(HConstants.EMPTY_END_ROW, hri.getEndKey())) {
201 endKey = Bytes.toLong(hri.getEndKey());
202 }
203 LOG.info("start:" + startKey + " end:" + endKey + " hri:" + hri);
204 Assert.assertTrue("lastEndKey=" + Bytes.toString(lastEndKey) + ", startKey=" +
205 Bytes.toString(hri.getStartKey()), Bytes.equals(lastEndKey, hri.getStartKey()));
206 lastEndKey = hri.getEndKey();
207 }
208 Assert.assertTrue(Bytes.equals(lastEndKey, HConstants.EMPTY_END_ROW));
209 LOG.info("-------");
210 Threads.sleep(10 + random.nextInt(50));
211 } catch (Throwable e) {
212 ex = e;
213 Assert.fail(StringUtils.stringifyException(e));
214 }
215 }
216 }
217 void rethrowExceptionIfAny() throws Throwable {
218 if (ex != null) { throw ex; }
219 }
220 }
221
222 RegionMetaSplitter regionMetaSplitter = new RegionMetaSplitter();
223 MetaScannerVerifier metaScannerVerifier = new MetaScannerVerifier();
224
225 Thread regionMetaSplitterThread = new Thread(regionMetaSplitter);
226 Thread metaScannerVerifierThread = new Thread(metaScannerVerifier);
227
228 regionMetaSplitterThread.start();
229 metaScannerVerifierThread.start();
230
231 Threads.sleep(runtime);
232
233 regionMetaSplitter.stop("test finished");
234 metaScannerVerifier.stop("test finished");
235
236 regionMetaSplitterThread.join();
237 metaScannerVerifierThread.join();
238
239 regionMetaSplitter.rethrowExceptionIfAny();
240 metaScannerVerifier.rethrowExceptionIfAny();
241 }
242
243 }