1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase;
20
21
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertNotNull;
24 import static org.junit.Assert.assertTrue;
25 import static org.junit.Assert.fail;
26
27 import java.io.ByteArrayInputStream;
28 import java.io.ByteArrayOutputStream;
29 import java.io.DataInputStream;
30 import java.io.DataOutputStream;
31 import java.io.IOException;
32 import java.util.List;
33 import java.util.Map;
34 import java.util.NavigableSet;
35 import java.util.Set;
36
37 import org.apache.hadoop.hbase.client.Get;
38 import org.apache.hadoop.hbase.client.Scan;
39 import org.apache.hadoop.hbase.exceptions.DeserializationException;
40 import org.apache.hadoop.hbase.filter.BinaryComparator;
41 import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
42 import org.apache.hadoop.hbase.filter.Filter;
43 import org.apache.hadoop.hbase.filter.PrefixFilter;
44 import org.apache.hadoop.hbase.filter.RowFilter;
45 import org.apache.hadoop.hbase.io.TimeRange;
46 import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
47 import org.apache.hadoop.hbase.protobuf.generated.ClientProtos;
48 import org.apache.hadoop.hbase.protobuf.generated.ZooKeeperProtos.SplitLogTask.RecoveryMode;
49 import org.apache.hadoop.hbase.testclassification.SmallTests;
50 import org.apache.hadoop.hbase.util.Bytes;
51 import org.apache.hadoop.hbase.util.Writables;
52 import org.apache.hadoop.io.DataInputBuffer;
53 import org.junit.Test;
54 import org.junit.experimental.categories.Category;
55
56
57
58
59 @Category(SmallTests.class)
60 public class TestSerialization {
61 @Test public void testKeyValue() throws Exception {
62 final String name = "testKeyValue2";
63 byte[] row = name.getBytes();
64 byte[] fam = "fam".getBytes();
65 byte[] qf = "qf".getBytes();
66 long ts = System.currentTimeMillis();
67 byte[] val = "val".getBytes();
68 KeyValue kv = new KeyValue(row, fam, qf, ts, val);
69 ByteArrayOutputStream baos = new ByteArrayOutputStream();
70 DataOutputStream dos = new DataOutputStream(baos);
71 long l = KeyValue.write(kv, dos);
72 dos.close();
73 byte [] mb = baos.toByteArray();
74 ByteArrayInputStream bais = new ByteArrayInputStream(mb);
75 DataInputStream dis = new DataInputStream(bais);
76 KeyValue deserializedKv = KeyValue.create(dis);
77 assertTrue(Bytes.equals(kv.getBuffer(), deserializedKv.getBuffer()));
78 assertEquals(kv.getOffset(), deserializedKv.getOffset());
79 assertEquals(kv.getLength(), deserializedKv.getLength());
80 }
81
82 @Test public void testCreateKeyValueInvalidNegativeLength() {
83
84 KeyValue kv_0 = new KeyValue(Bytes.toBytes("myRow"), Bytes.toBytes("myCF"),
85 Bytes.toBytes("myQualifier"), 12345L, Bytes.toBytes("my12345"));
86
87 KeyValue kv_1 = new KeyValue(Bytes.toBytes("myRow"), Bytes.toBytes("myCF"),
88 Bytes.toBytes("myQualifier"), 12345L, Bytes.toBytes("my123"));
89
90 ByteArrayOutputStream baos = new ByteArrayOutputStream();
91 DataOutputStream dos = new DataOutputStream(baos);
92
93 long l = 0;
94 try {
95 l = KeyValue.oswrite(kv_0, dos, false);
96 l += KeyValue.oswrite(kv_1, dos, false);
97 assertEquals(100L, l);
98 } catch (IOException e) {
99 fail("Unexpected IOException" + e.getMessage());
100 }
101
102 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
103 DataInputStream dis = new DataInputStream(bais);
104
105 try {
106 KeyValue.create(dis);
107 assertTrue(kv_0.equals(kv_1));
108 } catch (Exception e) {
109 fail("Unexpected Exception" + e.getMessage());
110 }
111
112
113 try {
114
115 KeyValue.create(-1, dis);
116 fail("Expected corrupt stream");
117 } catch (Exception e) {
118 assertEquals("Failed read -1 bytes, stream corrupt?", e.getMessage());
119 }
120
121 }
122
123 @Test
124 public void testSplitLogTask() throws DeserializationException {
125 SplitLogTask slt = new SplitLogTask.Unassigned(ServerName.valueOf("mgr,1,1"),
126 RecoveryMode.LOG_REPLAY);
127 byte [] bytes = slt.toByteArray();
128 SplitLogTask sltDeserialized = SplitLogTask.parseFrom(bytes);
129 assertTrue(slt.equals(sltDeserialized));
130 }
131
132 @Test public void testCompareFilter() throws Exception {
133 Filter f = new RowFilter(CompareOp.EQUAL,
134 new BinaryComparator(Bytes.toBytes("testRowOne-2")));
135 byte [] bytes = f.toByteArray();
136 Filter ff = RowFilter.parseFrom(bytes);
137 assertNotNull(ff);
138 }
139
140 @Test public void testTableDescriptor() throws Exception {
141 final String name = "testTableDescriptor";
142 HTableDescriptor htd = createTableDescriptor(name);
143 byte [] mb = Writables.getBytes(htd);
144 HTableDescriptor deserializedHtd =
145 (HTableDescriptor)Writables.getWritable(mb, new HTableDescriptor());
146 assertEquals(htd.getTableName(), deserializedHtd.getTableName());
147 }
148
149
150
151
152
153 @Test public void testRegionInfo() throws Exception {
154 HRegionInfo hri = createRandomRegion("testRegionInfo");
155
156
157 byte [] hrib = hri.toByteArray();
158 HRegionInfo deserializedHri = HRegionInfo.parseFrom(hrib);
159 assertEquals(hri.getEncodedName(), deserializedHri.getEncodedName());
160 assertEquals(hri, deserializedHri);
161
162
163 hrib = hri.toDelimitedByteArray();
164 DataInputBuffer buf = new DataInputBuffer();
165 try {
166 buf.reset(hrib, hrib.length);
167 deserializedHri = HRegionInfo.parseFrom(buf);
168 assertEquals(hri.getEncodedName(), deserializedHri.getEncodedName());
169 assertEquals(hri, deserializedHri);
170 } finally {
171 buf.close();
172 }
173 }
174
175 @Test public void testRegionInfos() throws Exception {
176 HRegionInfo hri = createRandomRegion("testRegionInfos");
177 byte[] triple = HRegionInfo.toDelimitedByteArray(hri, hri, hri);
178 List<HRegionInfo> regions = HRegionInfo.parseDelimitedFrom(triple, 0, triple.length);
179 assertTrue(regions.size() == 3);
180 assertTrue(regions.get(0).equals(regions.get(1)));
181 assertTrue(regions.get(0).equals(regions.get(2)));
182 }
183
184 private HRegionInfo createRandomRegion(final String name) {
185 HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(name));
186 String [] families = new String [] {"info", "anchor"};
187 for (int i = 0; i < families.length; i++) {
188 htd.addFamily(new HColumnDescriptor(families[i]));
189 }
190 return new HRegionInfo(htd.getTableName(), HConstants.EMPTY_START_ROW,
191 HConstants.EMPTY_END_ROW);
192 }
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301 @Test public void testGet() throws Exception{
302 byte[] row = "row".getBytes();
303 byte[] fam = "fam".getBytes();
304 byte[] qf1 = "qf1".getBytes();
305
306 long ts = System.currentTimeMillis();
307 int maxVersions = 2;
308
309 Get get = new Get(row);
310 get.addColumn(fam, qf1);
311 get.setTimeRange(ts, ts+1);
312 get.setMaxVersions(maxVersions);
313
314 ClientProtos.Get getProto = ProtobufUtil.toGet(get);
315 Get desGet = ProtobufUtil.toGet(getProto);
316
317 assertTrue(Bytes.equals(get.getRow(), desGet.getRow()));
318 Set<byte[]> set = null;
319 Set<byte[]> desSet = null;
320
321 for(Map.Entry<byte[], NavigableSet<byte[]>> entry :
322 get.getFamilyMap().entrySet()){
323 assertTrue(desGet.getFamilyMap().containsKey(entry.getKey()));
324 set = entry.getValue();
325 desSet = desGet.getFamilyMap().get(entry.getKey());
326 for(byte [] qualifier : set){
327 assertTrue(desSet.contains(qualifier));
328 }
329 }
330
331 assertEquals(get.getMaxVersions(), desGet.getMaxVersions());
332 TimeRange tr = get.getTimeRange();
333 TimeRange desTr = desGet.getTimeRange();
334 assertEquals(tr.getMax(), desTr.getMax());
335 assertEquals(tr.getMin(), desTr.getMin());
336 }
337
338
339 @Test public void testScan() throws Exception {
340
341 byte[] startRow = "startRow".getBytes();
342 byte[] stopRow = "stopRow".getBytes();
343 byte[] fam = "fam".getBytes();
344 byte[] qf1 = "qf1".getBytes();
345
346 long ts = System.currentTimeMillis();
347 int maxVersions = 2;
348
349 Scan scan = new Scan(startRow, stopRow);
350 scan.addColumn(fam, qf1);
351 scan.setTimeRange(ts, ts+1);
352 scan.setMaxVersions(maxVersions);
353
354 ClientProtos.Scan scanProto = ProtobufUtil.toScan(scan);
355 Scan desScan = ProtobufUtil.toScan(scanProto);
356
357 assertTrue(Bytes.equals(scan.getStartRow(), desScan.getStartRow()));
358 assertTrue(Bytes.equals(scan.getStopRow(), desScan.getStopRow()));
359 assertEquals(scan.getCacheBlocks(), desScan.getCacheBlocks());
360 Set<byte[]> set = null;
361 Set<byte[]> desSet = null;
362
363 for(Map.Entry<byte[], NavigableSet<byte[]>> entry :
364 scan.getFamilyMap().entrySet()){
365 assertTrue(desScan.getFamilyMap().containsKey(entry.getKey()));
366 set = entry.getValue();
367 desSet = desScan.getFamilyMap().get(entry.getKey());
368 for(byte[] column : set){
369 assertTrue(desSet.contains(column));
370 }
371
372
373 scan = new Scan(startRow);
374 final String name = "testScan";
375 byte [] prefix = Bytes.toBytes(name);
376 scan.setFilter(new PrefixFilter(prefix));
377 scanProto = ProtobufUtil.toScan(scan);
378 desScan = ProtobufUtil.toScan(scanProto);
379 Filter f = desScan.getFilter();
380 assertTrue(f instanceof PrefixFilter);
381 }
382
383 assertEquals(scan.getMaxVersions(), desScan.getMaxVersions());
384 TimeRange tr = scan.getTimeRange();
385 TimeRange desTr = desScan.getTimeRange();
386 assertEquals(tr.getMax(), desTr.getMax());
387 assertEquals(tr.getMin(), desTr.getMin());
388 }
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567 protected static final int MAXVERSIONS = 3;
568 protected final static byte [] fam1 = Bytes.toBytes("colfamily1");
569 protected final static byte [] fam2 = Bytes.toBytes("colfamily2");
570 protected final static byte [] fam3 = Bytes.toBytes("colfamily3");
571 protected static final byte [][] COLUMNS = {fam1, fam2, fam3};
572
573
574
575
576
577
578
579 protected HTableDescriptor createTableDescriptor(final String name) {
580 return createTableDescriptor(name, MAXVERSIONS);
581 }
582
583
584
585
586
587
588
589
590 protected HTableDescriptor createTableDescriptor(final String name,
591 final int versions) {
592 HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(name));
593 htd.addFamily(new HColumnDescriptor(fam1)
594 .setMaxVersions(versions)
595 .setBlockCacheEnabled(false)
596 );
597 htd.addFamily(new HColumnDescriptor(fam2)
598 .setMaxVersions(versions)
599 .setBlockCacheEnabled(false)
600 );
601 htd.addFamily(new HColumnDescriptor(fam3)
602 .setMaxVersions(versions)
603 .setBlockCacheEnabled(false)
604 );
605 return htd;
606 }
607 }