1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.protobuf;
19
20 import static org.junit.Assert.*;
21
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27
28 import org.apache.hadoop.hbase.Cell;
29 import org.apache.hadoop.hbase.CellScanner;
30 import org.apache.hadoop.hbase.CellUtil;
31 import org.apache.hadoop.hbase.KeyValue;
32 import org.apache.hadoop.hbase.TableName;
33 import org.apache.hadoop.hbase.protobuf.generated.AdminProtos;
34 import org.apache.hadoop.hbase.protobuf.generated.WALProtos;
35 import org.apache.hadoop.hbase.regionserver.MultiVersionConcurrencyControl;
36 import org.apache.hadoop.hbase.regionserver.wal.WALEdit;
37 import org.apache.hadoop.hbase.testclassification.SmallTests;
38 import org.apache.hadoop.hbase.util.Bytes;
39 import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
40 import org.apache.hadoop.hbase.util.Pair;
41 import org.apache.hadoop.hbase.wal.WAL;
42 import org.apache.hadoop.hbase.wal.WALKey;
43 import org.junit.Test;
44 import org.junit.experimental.categories.Category;
45
46
47 @Category(SmallTests.class)
48 public class TestReplicationProtobuf {
49
50
51
52
53 @Test
54 public void testGetCellScanner() throws IOException {
55 List<Cell> a = new ArrayList<Cell>();
56 KeyValue akv = new KeyValue(Bytes.toBytes("a"), -1L);
57 a.add(akv);
58
59 a.add(new KeyValue(Bytes.toBytes("aa"), -1L));
60 a.add(new KeyValue(Bytes.toBytes("aaa"), -1L));
61 List<Cell> b = new ArrayList<Cell>();
62 KeyValue bkv = new KeyValue(Bytes.toBytes("b"), -1L);
63 a.add(bkv);
64 List<Cell> c = new ArrayList<Cell>();
65 KeyValue ckv = new KeyValue(Bytes.toBytes("c"), -1L);
66 c.add(ckv);
67 List<List<? extends Cell>> all = new ArrayList<List<? extends Cell>>();
68 all.add(a);
69 all.add(b);
70 all.add(c);
71 CellScanner scanner = ReplicationProtbufUtil.getCellScanner(all, 0);
72 testAdvanceHasSameRow(scanner, akv);
73
74 scanner.advance();
75
76 scanner.advance();
77 testAdvanceHasSameRow(scanner, bkv);
78 testAdvanceHasSameRow(scanner, ckv);
79 assertFalse(scanner.advance());
80 }
81
82 private void testAdvanceHasSameRow(CellScanner scanner, final KeyValue kv) throws IOException {
83 scanner.advance();
84 assertTrue(Bytes.equals(scanner.current().getRowArray(), scanner.current().getRowOffset(),
85 scanner.current().getRowLength(),
86 kv.getRowArray(), kv.getRowOffset(), kv.getRowLength()));
87 }
88
89 @Test
90 public void testWALEntryProtobufConstruction() throws Exception {
91 byte[] encodedRegionName = Bytes.toBytes("region");
92 TableName tableName = TableName.valueOf("table");
93 long ts = EnvironmentEdgeManager.currentTime();
94 MultiVersionConcurrencyControl mvcc = null;
95 Map<String, byte[]> extendedAttributes = new HashMap<String, byte[]>(1);
96 String attrKey = "attr";
97 byte[] attrValue = Bytes.toBytes("attrVal");
98 extendedAttributes.put(attrKey, attrValue);
99 WALKey key = new WALKey(encodedRegionName, tableName, ts, mvcc, extendedAttributes);
100 Cell cell = CellUtil.createCell(Bytes.toBytes("row"), Bytes.toBytes("f"), Bytes.toBytes("q"),
101 ts, KeyValue.Type.Put.getCode(), Bytes.toBytes("val"));
102 WALEdit edit = new WALEdit(1);
103 edit.add(cell);
104 WAL.Entry entry = new WAL.Entry(key, edit);
105
106 Pair<AdminProtos.ReplicateWALEntryRequest, CellScanner> pair =
107 ReplicationProtbufUtil.buildReplicateWALEntryRequest(new WAL.Entry[] {entry});
108 AdminProtos.ReplicateWALEntryRequest request = pair.getFirst();
109 AdminProtos.WALEntry protoWALEntry = request.getEntry(0);
110 WALProtos.WALKey protoWALKey = protoWALEntry.getKey();
111 assertArrayEquals(encodedRegionName, protoWALKey.getEncodedRegionName().toByteArray());
112 assertEquals(tableName, TableName.valueOf(protoWALKey.getTableName().toByteArray()));
113 assertEquals(ts, protoWALKey.getWriteTime());
114 assertEquals(extendedAttributes.size(), protoWALKey.getExtendedAttributesCount());
115 for (WALProtos.Attribute attr : protoWALKey.getExtendedAttributesList()) {
116 assertArrayEquals(extendedAttributes.get(attr.getKey()), attr.getValue().toByteArray());
117 }
118
119
120 assertEquals(edit.size(), protoWALEntry.getAssociatedCellCount());
121 }
122 }