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.omid.transaction;
19  
20  import org.apache.hadoop.hbase.Cell;
21  import org.apache.hadoop.hbase.CellUtil;
22  import org.apache.hadoop.hbase.client.Get;
23  import org.apache.hadoop.hbase.client.Put;
24  import org.apache.hadoop.hbase.client.Result;
25  import org.apache.hadoop.hbase.util.Bytes;
26  import org.testng.ITestContext;
27  import org.testng.annotations.Test;
28  
29  import java.util.ArrayList;
30  import java.util.Arrays;
31  import java.util.List;
32  
33  import static org.testng.Assert.assertFalse;
34  import static org.testng.Assert.assertNotNull;
35  import static org.testng.Assert.assertTrue;
36  
37  @Test(groups = "sharedHBase")
38  public class TestReadPath extends OmidTestBase {
39  
40      final byte[] family = Bytes.toBytes(TEST_FAMILY);
41      final byte[] row = Bytes.toBytes("row");
42      private final byte[] col = Bytes.toBytes("col1");
43      final byte[] data = Bytes.toBytes("data");
44      private final byte[] uncommitted = Bytes.toBytes("uncommitted");
45  
46      @Test
47      public void testReadInterleaved(ITestContext context) throws Exception {
48          TransactionManager tm = newTransactionManager(context);
49          TTable table = new TTable(hbaseConf, TEST_TABLE);
50  
51          // Put some data on the DB
52          Transaction t1 = tm.begin();
53          Transaction t2 = tm.begin();
54  
55          Put put = new Put(row);
56          put.add(family, col, data);
57          table.put(t1, put);
58          tm.commit(t1);
59  
60          Get get = new Get(row);
61          Result result = table.get(t2, get);
62          assertFalse(result.containsColumn(family, col), "Should be unable to read column");
63      }
64  
65      @Test
66      public void testReadWithSeveralUncommitted(ITestContext context) throws Exception {
67          TransactionManager tm = newTransactionManager(context);
68          TTable table = new TTable(hbaseConf, TEST_TABLE);
69  
70          // Put some data on the DB
71          Transaction t = tm.begin();
72          Put put = new Put(row);
73          put.add(family, col, data);
74          table.put(t, put);
75          tm.commit(t);
76          List<Transaction> running = new ArrayList<>();
77  
78          // Shade the data with uncommitted data
79          for (int i = 0; i < 10; ++i) {
80              t = tm.begin();
81              put = new Put(row);
82              put.add(family, col, uncommitted);
83              table.put(t, put);
84              running.add(t);
85          }
86  
87          // Try to read from row, it should ignore the uncommitted data and return the original committed value
88          t = tm.begin();
89          Get get = new Get(row);
90          Result result = table.get(t, get);
91          Cell cell = result.getColumnLatestCell(family, col);
92          assertNotNull(cell, "KeyValue is null");
93          byte[] value = CellUtil.cloneValue(cell);
94          assertTrue(Arrays.equals(data, value), "Read data doesn't match");
95          tm.commit(t);
96  
97          table.close();
98  
99          for (Transaction r : running) {
100             tm.rollback(r);
101         }
102 
103     }
104 
105 }