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.apache.omid.transaction.CellUtils.hasCell;
21  import static org.apache.omid.transaction.CellUtils.hasShadowCell;
22  import static org.testng.Assert.assertTrue;
23  
24  import java.util.Arrays;
25  
26  import org.apache.hadoop.hbase.client.Get;
27  import org.apache.hadoop.hbase.client.Put;
28  import org.apache.hadoop.hbase.client.Result;
29  import org.apache.hadoop.hbase.util.Bytes;
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  import org.testng.ITestContext;
33  import org.testng.annotations.Test;
34  
35  @Test(groups = "sharedHBase")
36  public class TestMarkPutAsCommitted extends OmidTestBase {
37  
38      private static final Logger LOG = LoggerFactory.getLogger(TestMarkPutAsCommitted.class);
39  
40      private static final String TEST_FAMILY = "data";
41  
42      static final byte[] row = Bytes.toBytes("test-sc");
43      static final byte[] family = Bytes.toBytes(TEST_FAMILY);
44      private static final byte[] qualifier = Bytes.toBytes("testdata-1");
45      private static final byte[] data1 = Bytes.toBytes("testWrite-1");
46      private static final byte[] data2 = Bytes.toBytes("testWrite-2");
47  
48      @Test(timeOut = 60_000)
49      public void testShadowCellsExistanceInAutocommit(ITestContext context) throws Exception {
50  
51          TransactionManager tm = newTransactionManager(context);
52  
53          TTable table = new TTable(connection, TEST_TABLE);
54  
55          HBaseTransaction t1 = (HBaseTransaction) tm.begin();
56  
57          // Test shadow cells are created properly
58          Put put = new Put(row);
59          put.addColumn(family, qualifier, data1);
60          
61          put = TTable.markPutAsCommitted(put, t1.getWriteTimestamp(), t1.getWriteTimestamp());
62        
63          table.getHTable().put(put);
64  
65          // After markPutAsCommitted test that both cell and shadow cell are there
66          assertTrue(hasCell(row, family, qualifier, t1.getStartTimestamp(), new TTableCellGetterAdapter(table)),
67                  "Cell should be there");
68          assertTrue(hasShadowCell(row, family, qualifier, t1.getStartTimestamp(), new TTableCellGetterAdapter(table)),
69                  "Shadow cell should be there");
70      }
71  
72      @Test(timeOut = 60_000)
73      public void testReadAfterAutocommit(ITestContext context) throws Exception {
74  
75          TransactionManager tm = newTransactionManager(context);
76  
77          TTable table = new TTable(connection, TEST_TABLE);
78  
79          HBaseTransaction t1 = (HBaseTransaction) tm.begin();
80  
81          Put put = new Put(row);
82          put.addColumn(family, qualifier, data1);
83  
84          table.put(t1, put);
85  
86          tm.commit(t1);
87  
88          // After commit test that both cell and shadow cell are there
89          assertTrue(hasCell(row, family, qualifier, t1.getStartTimestamp(), new TTableCellGetterAdapter(table)),
90                  "Cell should be there");
91          assertTrue(hasShadowCell(row, family, qualifier, t1.getStartTimestamp(), new TTableCellGetterAdapter(table)),
92                  "Shadow cell should be there");
93  
94          Transaction t2 = tm.begin();
95          Get get = new Get(row);
96          get.addColumn(family, qualifier);
97  
98          Result getResult = table.get(t2, get);
99          assertTrue(Arrays.equals(data1, getResult.getValue(family, qualifier)), "Values should be the same");
100         
101         
102         HBaseTransaction t3 = (HBaseTransaction) tm.begin();
103 
104         Put put1 = new Put(row);
105         put1.addColumn(family, qualifier, data2);
106 
107         put1 = TTable.markPutAsCommitted(put1, t3.getWriteTimestamp(), t3.getWriteTimestamp());
108 
109        table.getHTable().put(put1);
110 
111         // After markPutAsCommitted test that both cell and shadow cell are there
112         assertTrue(hasCell(row, family, qualifier, t1.getStartTimestamp(), new TTableCellGetterAdapter(table)),
113                 "Cell should be there");
114         assertTrue(hasShadowCell(row, family, qualifier, t1.getStartTimestamp(), new TTableCellGetterAdapter(table)),
115                 "Shadow cell should be there");
116 
117         Transaction t4 = tm.begin();
118 
119         getResult = table.get(t4, get);
120         //Test that t4 reads t3's write even though t3 was not committed 
121         assertTrue(Arrays.equals(data2, getResult.getValue(family, qualifier)), "Values should be the same");
122     }
123 }