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 static org.testng.Assert.assertFalse;
21  import static org.testng.Assert.assertNotNull;
22  import static org.testng.Assert.assertTrue;
23  
24  import java.util.ArrayList;
25  import java.util.Arrays;
26  import java.util.List;
27  
28  import org.apache.hadoop.hbase.Cell;
29  import org.apache.hadoop.hbase.CellUtil;
30  import org.apache.hadoop.hbase.client.Get;
31  import org.apache.hadoop.hbase.client.Put;
32  import org.apache.hadoop.hbase.client.Result;
33  import org.apache.hadoop.hbase.util.Bytes;
34  import org.testng.ITestContext;
35  import org.testng.annotations.Test;
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(timeOut = 10_000)
47      public void testReadInterleaved(ITestContext context) throws Exception {
48          TransactionManager tm = newTransactionManager(context);
49          TTable table = new TTable(connection, 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.addColumn(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(timeOut = 10_000)
66      public void testReadWithSeveralUncommitted(ITestContext context) throws Exception {
67          TransactionManager tm = newTransactionManager(context);
68          TTable table = new TTable(connection, TEST_TABLE);
69  
70          // Put some data on the DB
71          Transaction t = tm.begin();
72          Put put = new Put(row);
73          put.addColumn(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.addColumn(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 }