View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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     * Little test to check we can basically convert list of a list of KVs into a CellScanner
51     * @throws IOException
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      // Add a few just to make it less regular.
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      // Skip over aa
74      scanner.advance();
75      // Skip over aaa
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     //the WALEdit's cells don't go on the protobuf; they're in a cell block
119     // elsewhere in the RPC protocol. We just mark the cell count
120     assertEquals(edit.size(), protoWALEntry.getAssociatedCellCount());
121   }
122 }