1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase.client;
21
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertNull;
24 import static org.junit.Assert.assertTrue;
25 import static org.junit.Assert.fail;
26
27 import java.util.ArrayList;
28 import java.util.Arrays;
29 import java.util.List;
30 import java.util.Random;
31
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34 import org.apache.hadoop.hbase.TableName;
35 import org.apache.hadoop.hbase.HBaseTestingUtility;
36 import org.apache.hadoop.hbase.HColumnDescriptor;
37 import org.apache.hadoop.hbase.HRegionLocation;
38 import org.apache.hadoop.hbase.HTableDescriptor;
39 import org.apache.hadoop.hbase.LargeTests;
40 import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
41 import org.apache.hadoop.hbase.protobuf.generated.AdminProtos;
42 import org.apache.hadoop.hbase.util.Bytes;
43 import org.apache.hadoop.hbase.util.Pair;
44 import org.junit.After;
45 import org.junit.AfterClass;
46 import org.junit.Before;
47 import org.junit.BeforeClass;
48 import org.junit.Test;
49 import org.junit.experimental.categories.Category;
50
51 @Category(LargeTests.class)
52 public class TestFromClientSide3 {
53 final Log LOG = LogFactory.getLog(getClass());
54 private final static HBaseTestingUtility TEST_UTIL
55 = new HBaseTestingUtility();
56 private static byte[] FAMILY = Bytes.toBytes("testFamily");
57 private static Random random = new Random();
58 private static int SLAVES = 3;
59 private static byte [] ROW = Bytes.toBytes("testRow");
60 private static final byte[] ANOTHERROW = Bytes.toBytes("anotherrow");
61 private static byte [] QUALIFIER = Bytes.toBytes("testQualifier");
62 private static byte [] VALUE = Bytes.toBytes("testValue");
63 private final static byte[] COL_QUAL = Bytes.toBytes("f1");
64 private final static byte[] VAL_BYTES = Bytes.toBytes("v1");
65 private final static byte[] ROW_BYTES = Bytes.toBytes("r1");
66
67
68
69
70
71 @BeforeClass
72 public static void setUpBeforeClass() throws Exception {
73 TEST_UTIL.getConfiguration().setBoolean(
74 "hbase.online.schema.update.enable", true);
75 TEST_UTIL.startMiniCluster(SLAVES);
76 }
77
78
79
80
81 @AfterClass
82 public static void tearDownAfterClass() throws Exception {
83 TEST_UTIL.shutdownMiniCluster();
84 }
85
86
87
88
89 @Before
90 public void setUp() throws Exception {
91
92 }
93
94
95
96
97 @After
98 public void tearDown() throws Exception {
99
100 }
101
102 private void randomCFPuts(HTable table, byte[] row, byte[] family, int nPuts)
103 throws Exception {
104 Put put = new Put(row);
105 for (int i = 0; i < nPuts; i++) {
106 byte[] qualifier = Bytes.toBytes(random.nextInt());
107 byte[] value = Bytes.toBytes(random.nextInt());
108 put.add(family, qualifier, value);
109 }
110 table.put(put);
111 }
112
113 private void performMultiplePutAndFlush(HBaseAdmin admin, HTable table,
114 byte[] row, byte[] family, int nFlushes, int nPuts) throws Exception {
115
116
117 HConnection conn = HConnectionManager.getConnection(TEST_UTIL
118 .getConfiguration());
119 HRegionLocation loc = table.getRegionLocation(row, true);
120 AdminProtos.AdminService.BlockingInterface server = conn.getAdmin(loc.getServerName());
121 byte[] regName = loc.getRegionInfo().getRegionName();
122
123 for (int i = 0; i < nFlushes; i++) {
124 randomCFPuts(table, row, family, nPuts);
125 List<String> sf = ProtobufUtil.getStoreFiles(server, regName, FAMILY);
126 int sfCount = sf.size();
127
128
129 admin.flush(table.getTableName());
130
131
132 while (ProtobufUtil.getStoreFiles(
133 server, regName, FAMILY).size() == sfCount) {
134 Thread.sleep(40);
135 }
136 }
137 }
138
139
140 @Test(timeout = 60000)
141 public void testAdvancedConfigOverride() throws Exception {
142
143
144
145
146
147
148
149
150
151 TEST_UTIL.getConfiguration().setInt("hbase.hstore.compaction.min", 3);
152
153 String tableName = "testAdvancedConfigOverride";
154 TableName TABLE =
155 TableName.valueOf(tableName);
156 HTable hTable = TEST_UTIL.createTable(TABLE, FAMILY, 10);
157 HBaseAdmin admin = new HBaseAdmin(TEST_UTIL.getConfiguration());
158 HConnection connection = HConnectionManager.getConnection(TEST_UTIL
159 .getConfiguration());
160
161
162 byte[] row = Bytes.toBytes(random.nextInt());
163 performMultiplePutAndFlush(admin, hTable, row, FAMILY, 3, 100);
164
165
166 HRegionLocation loc = hTable.getRegionLocation(row, true);
167 byte[] regionName = loc.getRegionInfo().getRegionName();
168 AdminProtos.AdminService.BlockingInterface server =
169 connection.getAdmin(loc.getServerName());
170 assertTrue(ProtobufUtil.getStoreFiles(
171 server, regionName, FAMILY).size() > 1);
172
173
174 admin.compact(TABLE.getName());
175
176
177 for (int i = 0; i < 10 * 1000 / 40; ++i) {
178
179 loc = hTable.getRegionLocation(row, true);
180 if (!loc.getRegionInfo().isOffline()) {
181 regionName = loc.getRegionInfo().getRegionName();
182 server = connection.getAdmin(loc.getServerName());
183 if (ProtobufUtil.getStoreFiles(
184 server, regionName, FAMILY).size() <= 1) {
185 break;
186 }
187 }
188 Thread.sleep(40);
189 }
190
191 assertTrue(ProtobufUtil.getStoreFiles(
192 server, regionName, FAMILY).size() <= 1);
193
194
195 LOG.info("hbase.hstore.compaction.min should now be 5");
196 HTableDescriptor htd = new HTableDescriptor(hTable.getTableDescriptor());
197 htd.setValue("hbase.hstore.compaction.min", String.valueOf(5));
198 admin.modifyTable(TABLE, htd);
199 Pair<Integer, Integer> st;
200 while (null != (st = admin.getAlterStatus(TABLE)) && st.getFirst() > 0) {
201 LOG.debug(st.getFirst() + " regions left to update");
202 Thread.sleep(40);
203 }
204 LOG.info("alter status finished");
205
206
207 performMultiplePutAndFlush(admin, hTable, row, FAMILY, 3, 10);
208
209
210 admin.compact(TABLE.getName());
211
212
213 Thread.sleep(10 * 1000);
214 loc = hTable.getRegionLocation(row, true);
215 regionName = loc.getRegionInfo().getRegionName();
216 server = connection.getAdmin(loc.getServerName());
217 int sfCount = ProtobufUtil.getStoreFiles(
218 server, regionName, FAMILY).size();
219 assertTrue(sfCount > 1);
220
221
222 LOG.info("hbase.hstore.compaction.min should now be 2");
223 HColumnDescriptor hcd = new HColumnDescriptor(htd.getFamily(FAMILY));
224 hcd.setValue("hbase.hstore.compaction.min", String.valueOf(2));
225 htd.addFamily(hcd);
226 admin.modifyTable(TABLE, htd);
227 while (null != (st = admin.getAlterStatus(TABLE)) && st.getFirst() > 0) {
228 LOG.debug(st.getFirst() + " regions left to update");
229 Thread.sleep(40);
230 }
231 LOG.info("alter status finished");
232
233
234 admin.compact(TABLE.getName());
235
236
237 for (int i = 0; i < 10 * 1000 / 40; ++i) {
238 loc = hTable.getRegionLocation(row, true);
239 regionName = loc.getRegionInfo().getRegionName();
240 try {
241 server = connection.getAdmin(loc.getServerName());
242 if (ProtobufUtil.getStoreFiles(
243 server, regionName, FAMILY).size() < sfCount) {
244 break;
245 }
246 } catch (Exception e) {
247 LOG.debug("Waiting for region to come online: " + regionName);
248 }
249 Thread.sleep(40);
250 }
251
252 assertTrue(ProtobufUtil.getStoreFiles(
253 server, regionName, FAMILY).size() < sfCount);
254
255
256 LOG.info("Removing CF config value");
257 LOG.info("hbase.hstore.compaction.min should now be 5");
258 hcd = new HColumnDescriptor(htd.getFamily(FAMILY));
259 hcd.setValue("hbase.hstore.compaction.min", null);
260 htd.addFamily(hcd);
261 admin.modifyTable(TABLE, htd);
262 while (null != (st = admin.getAlterStatus(TABLE)) && st.getFirst() > 0) {
263 LOG.debug(st.getFirst() + " regions left to update");
264 Thread.sleep(40);
265 }
266 LOG.info("alter status finished");
267 assertNull(hTable.getTableDescriptor().getFamily(FAMILY).getValue(
268 "hbase.hstore.compaction.min"));
269 }
270
271 @Test
272 public void testHTableExistsMethodSingleRegionSingleGet() throws Exception {
273
274
275
276 HTable table = TEST_UTIL.createTable(
277 Bytes.toBytes("testHTableExistsMethodSingleRegionSingleGet"), new byte[][] { FAMILY });
278
279 Put put = new Put(ROW);
280 put.add(FAMILY, QUALIFIER, VALUE);
281
282 Get get = new Get(ROW);
283
284 boolean exist = table.exists(get);
285 assertEquals(exist, false);
286
287 table.put(put);
288
289 exist = table.exists(get);
290 assertEquals(exist, true);
291 }
292
293 public void testHTableExistsMethodSingleRegionMultipleGets() throws Exception {
294
295 HTable table = TEST_UTIL.createTable(
296 Bytes.toBytes("testHTableExistsMethodSingleRegionMultipleGets"), new byte[][] { FAMILY });
297
298 Put put = new Put(ROW);
299 put.add(FAMILY, QUALIFIER, VALUE);
300 table.put(put);
301
302 List<Get> gets = new ArrayList<Get>();
303 gets.add(new Get(ROW));
304 gets.add(null);
305 gets.add(new Get(ANOTHERROW));
306
307 Boolean[] results = table.exists(gets);
308 assertEquals(results[0], true);
309 assertEquals(results[1], false);
310 assertEquals(results[2], false);
311 }
312
313 @Test
314 public void testHTableExistsMethodMultipleRegionsSingleGet() throws Exception {
315
316 HTable table = TEST_UTIL.createTable(
317 Bytes.toBytes("testHTableExistsMethodMultipleRegionsSingleGet"), new byte[][] { FAMILY }, 1,
318 new byte[] { 0x00 }, new byte[] { (byte) 0xff }, 255);
319 Put put = new Put(ROW);
320 put.add(FAMILY, QUALIFIER, VALUE);
321
322 Get get = new Get(ROW);
323
324 boolean exist = table.exists(get);
325 assertEquals(exist, false);
326
327 table.put(put);
328
329 exist = table.exists(get);
330 assertEquals(exist, true);
331 }
332
333 @Test
334 public void testHTableExistsMethodMultipleRegionsMultipleGets() throws Exception {
335 HTable table = TEST_UTIL.createTable(
336 Bytes.toBytes("testHTableExistsMethodMultipleRegionsMultipleGets"), new byte[][] { FAMILY },
337 1, new byte[] { 0x00 }, new byte[] { (byte) 0xff }, 255);
338 Put put = new Put(ROW);
339 put.add(FAMILY, QUALIFIER, VALUE);
340 table.put (put);
341
342 List<Get> gets = new ArrayList<Get>();
343 gets.add(new Get(ANOTHERROW));
344 gets.add(new Get(Bytes.add(ROW, new byte[] { 0x00 })));
345 gets.add(new Get(ROW));
346 gets.add(new Get(Bytes.add(ANOTHERROW, new byte[] { 0x00 })));
347
348 Boolean[] results = table.exists(gets);
349 assertEquals(results[0], false);
350 assertEquals(results[1], false);
351 assertEquals(results[2], true);
352 assertEquals(results[3], false);
353
354
355 put = new Put(new byte[] { 0x00 });
356 put.add(FAMILY, QUALIFIER, VALUE);
357 table.put(put);
358
359 gets = new ArrayList<Get>();
360 gets.add(new Get(new byte[] { 0x00 }));
361 gets.add(new Get(new byte[] { 0x00, 0x00 }));
362 results = table.exists(gets);
363 assertEquals(results[0], true);
364 assertEquals(results[1], false);
365
366
367 put = new Put(new byte[] { (byte) 0xff, (byte) 0xff });
368 put.add(FAMILY, QUALIFIER, VALUE);
369 table.put(put);
370
371 gets = new ArrayList<Get>();
372 gets.add(new Get(new byte[] { (byte) 0xff }));
373 gets.add(new Get(new byte[] { (byte) 0xff, (byte) 0xff }));
374 gets.add(new Get(new byte[] { (byte) 0xff, (byte) 0xff, (byte) 0xff }));
375 results = table.exists(gets);
376 assertEquals(results[0], false);
377 assertEquals(results[1], true);
378 assertEquals(results[2], false);
379 }
380
381 @Test
382 public void testGetEmptyRow() throws Exception {
383
384 HBaseAdmin admin = TEST_UTIL.getHBaseAdmin();
385 HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(Bytes.toBytes("test")));
386 desc.addFamily(new HColumnDescriptor(FAMILY));
387 admin.createTable(desc);
388 HTable table = new HTable(TEST_UTIL.getConfiguration(), "test");
389
390 Put put = new Put(ROW_BYTES);
391 put.add(FAMILY, COL_QUAL, VAL_BYTES);
392 table.put(put);
393 table.flushCommits();
394
395
396 Result res = null;
397 try {
398 res = table.get(new Get(new byte[0]));
399 fail();
400 } catch (IllegalArgumentException e) {
401
402 }
403 assertTrue(res == null);
404 res = table.get(new Get(Bytes.toBytes("r1-not-exist")));
405 assertTrue(res.isEmpty() == true);
406 res = table.get(new Get(ROW_BYTES));
407 assertTrue(Arrays.equals(res.getValue(FAMILY, COL_QUAL), VAL_BYTES));
408 table.close();
409 }
410 }