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.testclassification.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 @BeforeClass
71 public static void setUpBeforeClass() throws Exception {
72 TEST_UTIL.getConfiguration().setBoolean(
73 "hbase.online.schema.update.enable", true);
74 TEST_UTIL.startMiniCluster(SLAVES);
75 }
76
77
78
79
80 @AfterClass
81 public static void tearDownAfterClass() throws Exception {
82 TEST_UTIL.shutdownMiniCluster();
83 }
84
85
86
87
88 @Before
89 public void setUp() throws Exception {
90
91 }
92
93
94
95
96 @After
97 public void tearDown() throws Exception {
98
99 }
100
101 private void randomCFPuts(Table table, byte[] row, byte[] family, int nPuts)
102 throws Exception {
103 Put put = new Put(row);
104 for (int i = 0; i < nPuts; i++) {
105 byte[] qualifier = Bytes.toBytes(random.nextInt());
106 byte[] value = Bytes.toBytes(random.nextInt());
107 put.add(family, qualifier, value);
108 }
109 table.put(put);
110 }
111
112 private void performMultiplePutAndFlush(HBaseAdmin admin, HTable table,
113 byte[] row, byte[] family, int nFlushes, int nPuts)
114 throws Exception {
115
116
117 HRegionLocation loc = table.getRegionLocation(row, true);
118 AdminProtos.AdminService.BlockingInterface server =
119 admin.getConnection().getAdmin(loc.getServerName());
120 byte[] regName = loc.getRegionInfo().getRegionName();
121
122 for (int i = 0; i < nFlushes; i++) {
123 randomCFPuts(table, row, family, nPuts);
124 List<String> sf = ProtobufUtil.getStoreFiles(server, regName, FAMILY);
125 int sfCount = sf.size();
126
127
128 admin.flush(table.getTableName());
129
130
131 while (ProtobufUtil.getStoreFiles(
132 server, regName, FAMILY).size() == sfCount) {
133 Thread.sleep(40);
134 }
135 }
136 }
137
138
139 @Test(timeout = 60000)
140 public void testAdvancedConfigOverride() throws Exception {
141
142
143
144
145
146
147
148
149
150 TEST_UTIL.getConfiguration().setInt("hbase.hstore.compaction.min", 3);
151
152 String tableName = "testAdvancedConfigOverride";
153 TableName TABLE = TableName.valueOf(tableName);
154 HTable hTable = TEST_UTIL.createTable(TABLE, FAMILY, 10);
155 HBaseAdmin admin = TEST_UTIL.getHBaseAdmin();
156 ClusterConnection connection = (ClusterConnection)TEST_UTIL.getConnection();
157
158
159 byte[] row = Bytes.toBytes(random.nextInt());
160 performMultiplePutAndFlush(admin, hTable, row, FAMILY, 3, 100);
161
162
163 HRegionLocation loc = hTable.getRegionLocation(row, true);
164 byte[] regionName = loc.getRegionInfo().getRegionName();
165 AdminProtos.AdminService.BlockingInterface server =
166 connection.getAdmin(loc.getServerName());
167 assertTrue(ProtobufUtil.getStoreFiles(
168 server, regionName, FAMILY).size() > 1);
169
170
171 admin.compact(TABLE.getName());
172
173
174 for (int i = 0; i < 10 * 1000 / 40; ++i) {
175
176 loc = hTable.getRegionLocation(row, true);
177 if (!loc.getRegionInfo().isOffline()) {
178 regionName = loc.getRegionInfo().getRegionName();
179 server = connection.getAdmin(loc.getServerName());
180 if (ProtobufUtil.getStoreFiles(
181 server, regionName, FAMILY).size() <= 1) {
182 break;
183 }
184 }
185 Thread.sleep(40);
186 }
187
188 assertTrue(ProtobufUtil.getStoreFiles(
189 server, regionName, FAMILY).size() <= 1);
190
191
192 LOG.info("hbase.hstore.compaction.min should now be 5");
193 HTableDescriptor htd = new HTableDescriptor(hTable.getTableDescriptor());
194 htd.setValue("hbase.hstore.compaction.min", String.valueOf(5));
195 admin.modifyTable(TABLE, htd);
196 Pair<Integer, Integer> st;
197 while (null != (st = admin.getAlterStatus(TABLE)) && st.getFirst() > 0) {
198 LOG.debug(st.getFirst() + " regions left to update");
199 Thread.sleep(40);
200 }
201 LOG.info("alter status finished");
202
203
204 performMultiplePutAndFlush(admin, hTable, row, FAMILY, 3, 10);
205
206
207 admin.compact(TABLE.getName());
208
209
210 Thread.sleep(10 * 1000);
211 loc = hTable.getRegionLocation(row, true);
212 regionName = loc.getRegionInfo().getRegionName();
213 server = connection.getAdmin(loc.getServerName());
214 int sfCount = ProtobufUtil.getStoreFiles(
215 server, regionName, FAMILY).size();
216 assertTrue(sfCount > 1);
217
218
219 LOG.info("hbase.hstore.compaction.min should now be 2");
220 HColumnDescriptor hcd = new HColumnDescriptor(htd.getFamily(FAMILY));
221 hcd.setValue("hbase.hstore.compaction.min", String.valueOf(2));
222 htd.modifyFamily(hcd);
223 admin.modifyTable(TABLE, htd);
224 while (null != (st = admin.getAlterStatus(TABLE)) && st.getFirst() > 0) {
225 LOG.debug(st.getFirst() + " regions left to update");
226 Thread.sleep(40);
227 }
228 LOG.info("alter status finished");
229
230
231 admin.compact(TABLE.getName());
232
233
234 for (int i = 0; i < 10 * 1000 / 40; ++i) {
235 loc = hTable.getRegionLocation(row, true);
236 regionName = loc.getRegionInfo().getRegionName();
237 try {
238 server = connection.getAdmin(loc.getServerName());
239 if (ProtobufUtil.getStoreFiles(
240 server, regionName, FAMILY).size() < sfCount) {
241 break;
242 }
243 } catch (Exception e) {
244 LOG.debug("Waiting for region to come online: " + regionName);
245 }
246 Thread.sleep(40);
247 }
248
249 assertTrue(ProtobufUtil.getStoreFiles(
250 server, regionName, FAMILY).size() < sfCount);
251
252
253 LOG.info("Removing CF config value");
254 LOG.info("hbase.hstore.compaction.min should now be 5");
255 hcd = new HColumnDescriptor(htd.getFamily(FAMILY));
256 hcd.setValue("hbase.hstore.compaction.min", null);
257 htd.modifyFamily(hcd);
258 admin.modifyTable(TABLE, htd);
259 while (null != (st = admin.getAlterStatus(TABLE)) && st.getFirst() > 0) {
260 LOG.debug(st.getFirst() + " regions left to update");
261 Thread.sleep(40);
262 }
263 LOG.info("alter status finished");
264 assertNull(hTable.getTableDescriptor().getFamily(FAMILY).getValue(
265 "hbase.hstore.compaction.min"));
266 }
267
268 @Test
269 public void testHTableBatchWithEmptyPut() throws Exception {
270 Table table = TEST_UTIL.createTable(
271 Bytes.toBytes("testHTableBatchWithEmptyPut"), new byte[][] { FAMILY });
272 try {
273 List actions = (List) new ArrayList();
274 Object[] results = new Object[2];
275
276 Put put1 = new Put(ROW);
277 actions.add(put1);
278
279 Put put2 = new Put(ANOTHERROW);
280 put2.add(FAMILY, QUALIFIER, VALUE);
281 actions.add(put2);
282
283 table.batch(actions, results);
284 fail("Empty Put should have failed the batch call");
285 } catch (IllegalArgumentException iae) {
286
287 } finally {
288 table.close();
289 }
290 }
291
292 @Test
293 public void testHTableExistsMethodSingleRegionSingleGet() throws Exception {
294
295
296
297 Table table = TEST_UTIL.createTable(
298 Bytes.toBytes("testHTableExistsMethodSingleRegionSingleGet"), new byte[][] { FAMILY });
299
300 Put put = new Put(ROW);
301 put.add(FAMILY, QUALIFIER, VALUE);
302
303 Get get = new Get(ROW);
304
305 boolean exist = table.exists(get);
306 assertEquals(exist, false);
307
308 table.put(put);
309
310 exist = table.exists(get);
311 assertEquals(exist, true);
312 }
313
314 public void testHTableExistsMethodSingleRegionMultipleGets() throws Exception {
315
316 HTable table = TEST_UTIL.createTable(
317 Bytes.toBytes("testHTableExistsMethodSingleRegionMultipleGets"), new byte[][] { FAMILY });
318
319 Put put = new Put(ROW);
320 put.add(FAMILY, QUALIFIER, VALUE);
321 table.put(put);
322
323 List<Get> gets = new ArrayList<Get>();
324 gets.add(new Get(ROW));
325 gets.add(null);
326 gets.add(new Get(ANOTHERROW));
327
328 Boolean[] results = table.exists(gets);
329 assertEquals(results[0], true);
330 assertEquals(results[1], false);
331 assertEquals(results[2], false);
332 }
333
334 @Test
335 public void testHTableExistsMethodMultipleRegionsSingleGet() throws Exception {
336
337 Table table = TEST_UTIL.createTable(
338 TableName.valueOf("testHTableExistsMethodMultipleRegionsSingleGet"), new byte[][] { FAMILY },
339 1, new byte[] { 0x00 }, new byte[] { (byte) 0xff }, 255);
340 Put put = new Put(ROW);
341 put.add(FAMILY, QUALIFIER, VALUE);
342
343 Get get = new Get(ROW);
344
345 boolean exist = table.exists(get);
346 assertEquals(exist, false);
347
348 table.put(put);
349
350 exist = table.exists(get);
351 assertEquals(exist, true);
352 }
353
354 @Test
355 public void testHTableExistsMethodMultipleRegionsMultipleGets() throws Exception {
356 HTable table = TEST_UTIL.createTable(
357 TableName.valueOf("testHTableExistsMethodMultipleRegionsMultipleGets"),
358 new byte[][] { FAMILY }, 1, new byte[] { 0x00 }, new byte[] { (byte) 0xff }, 255);
359 Put put = new Put(ROW);
360 put.add(FAMILY, QUALIFIER, VALUE);
361 table.put (put);
362
363 List<Get> gets = new ArrayList<Get>();
364 gets.add(new Get(ANOTHERROW));
365 gets.add(new Get(Bytes.add(ROW, new byte[] { 0x00 })));
366 gets.add(new Get(ROW));
367 gets.add(new Get(Bytes.add(ANOTHERROW, new byte[] { 0x00 })));
368
369 LOG.info("Calling exists");
370 Boolean[] results = table.exists(gets);
371 assertEquals(results[0], false);
372 assertEquals(results[1], false);
373 assertEquals(results[2], true);
374 assertEquals(results[3], false);
375
376
377 put = new Put(new byte[] { 0x00 });
378 put.add(FAMILY, QUALIFIER, VALUE);
379 table.put(put);
380
381 gets = new ArrayList<Get>();
382 gets.add(new Get(new byte[] { 0x00 }));
383 gets.add(new Get(new byte[] { 0x00, 0x00 }));
384 results = table.exists(gets);
385 assertEquals(results[0], true);
386 assertEquals(results[1], false);
387
388
389 put = new Put(new byte[] { (byte) 0xff, (byte) 0xff });
390 put.add(FAMILY, QUALIFIER, VALUE);
391 table.put(put);
392
393 gets = new ArrayList<Get>();
394 gets.add(new Get(new byte[] { (byte) 0xff }));
395 gets.add(new Get(new byte[] { (byte) 0xff, (byte) 0xff }));
396 gets.add(new Get(new byte[] { (byte) 0xff, (byte) 0xff, (byte) 0xff }));
397 results = table.exists(gets);
398 assertEquals(results[0], false);
399 assertEquals(results[1], true);
400 assertEquals(results[2], false);
401 }
402
403 @Test
404 public void testGetEmptyRow() throws Exception {
405
406 Admin admin = TEST_UTIL.getHBaseAdmin();
407 HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(Bytes.toBytes("test")));
408 desc.addFamily(new HColumnDescriptor(FAMILY));
409 admin.createTable(desc);
410 Table table = new HTable(TEST_UTIL.getConfiguration(), desc.getTableName());
411
412 Put put = new Put(ROW_BYTES);
413 put.add(FAMILY, COL_QUAL, VAL_BYTES);
414 table.put(put);
415
416
417 Result res = null;
418 try {
419 res = table.get(new Get(new byte[0]));
420 fail();
421 } catch (IllegalArgumentException e) {
422
423 }
424 assertTrue(res == null);
425 res = table.get(new Get(Bytes.toBytes("r1-not-exist")));
426 assertTrue(res.isEmpty() == true);
427 res = table.get(new Get(ROW_BYTES));
428 assertTrue(Arrays.equals(res.getValue(FAMILY, COL_QUAL), VAL_BYTES));
429 table.close();
430 }
431 }