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 com.google.common.util.concurrent.SettableFuture;
21  import org.apache.hadoop.hbase.KeyValue;
22  import org.apache.hadoop.hbase.client.Put;
23  import org.apache.hadoop.hbase.client.Result;
24  import org.apache.hadoop.hbase.client.ResultScanner;
25  import org.apache.hadoop.hbase.client.Scan;
26  import org.apache.hadoop.hbase.util.Bytes;
27  import org.apache.omid.tso.client.AbortException;
28  import org.apache.omid.tso.client.ForwardingTSOFuture;
29  import org.apache.omid.tso.client.TSOClient;
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  import org.testng.ITestContext;
33  import org.testng.annotations.Test;
34  
35  import static org.mockito.Matchers.anySetOf;
36  import static org.mockito.Matchers.eq;
37  import static org.mockito.Mockito.doReturn;
38  import static org.mockito.Mockito.mock;
39  import static org.testng.Assert.assertEquals;
40  
41  @Test(groups = "sharedHBase")
42  public class TestTransactionCleanup extends OmidTestBase {
43  
44      private static final Logger LOG = LoggerFactory.getLogger(TestTransactionCleanup.class);
45  
46      private static final long START_TS = 1L;
47  
48      private byte[] row = Bytes.toBytes("row");
49      private byte[] family = Bytes.toBytes(TEST_FAMILY);
50      private byte[] qual = Bytes.toBytes("qual");
51      private byte[] data = Bytes.toBytes("data");
52  
53      // NOTE: This test is maybe redundant with runTestCleanupAfterConflict()
54      // and testCleanupWithDeleteRow() tests in TestTransactionCleanup class.
55      // Code in TestTransactionCleanup is a little more difficult to follow,
56      // lacks some assertions and includes some magic numbers, so we should
57      // try to review and improve the tests in these two classes in a further
58      // commit.
59      @Test
60      public void testTransactionIsCleanedUpAfterBeingAborted(ITestContext context) throws Exception {
61  
62          final int ROWS_MODIFIED = 1;
63  
64          // Prepare the mocking results
65          SettableFuture<Long> startTSF = SettableFuture.create();
66          startTSF.set(START_TS);
67          ForwardingTSOFuture<Long> stFF = new ForwardingTSOFuture<>(startTSF);
68  
69          SettableFuture<Long> abortingF = SettableFuture.create();
70          abortingF.setException(new AbortException());
71          ForwardingTSOFuture<Long> abortingFF = new ForwardingTSOFuture<>(abortingF);
72  
73          // Mock the TSO Client setting the right method responses
74          TSOClient mockedTSOClient = mock(TSOClient.class);
75  
76          doReturn(stFF)
77                  .when(mockedTSOClient).getNewStartTimestamp();
78  
79          doReturn(abortingFF)
80                  .when(mockedTSOClient).commit(eq(START_TS), anySetOf(HBaseCellId.class));
81  
82          try (TransactionManager tm = newTransactionManager(context, mockedTSOClient);
83               TTable txTable = new TTable(hbaseConf, TEST_TABLE)) {
84  
85              // Start a transaction and put some data in a column
86              Transaction tx = tm.begin();
87  
88              Put put = new Put(row);
89              put.add(family, qual, data);
90              txTable.put(tx, put);
91  
92              // Abort transaction when committing, so the cleanup
93              // process we want to test is triggered
94              try {
95                  tm.commit(tx);
96              } catch (RollbackException e) {
97                  // Expected
98              }
99  
100             // So now we have to check that the Delete marker introduced by the
101             // cleanup process is there
102             Scan scan = new Scan(row);
103             scan.setRaw(true); // Raw scan to obtain the deleted cells
104             ResultScanner resultScanner = txTable.getHTable().getScanner(scan);
105             int resultCount = 0;
106             for (Result result : resultScanner) {
107                 assertEquals(result.size(), 2); // Size == 2, including the put and delete from cleanup
108                 LOG.trace("Result {}", result);
109                 // The last element of the qualifier should have the Delete marker
110                 byte encodedType = result.getColumnLatestCell(family, qual).getTypeByte();
111                 assertEquals(KeyValue.Type.codeToType(encodedType), KeyValue.Type.Delete);
112                 resultCount++;
113             }
114             assertEquals(resultCount, ROWS_MODIFIED);
115         }
116     }
117 
118 }