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.client.Delete;
21  import org.apache.hadoop.hbase.client.HTableInterface;
22  import org.slf4j.Logger;
23  import org.slf4j.LoggerFactory;
24  
25  import java.io.IOException;
26  import java.util.HashSet;
27  import java.util.Set;
28  
29  public class HBaseTransaction extends AbstractTransaction<HBaseCellId> {
30      private static final Logger LOG = LoggerFactory.getLogger(HBaseTransaction.class);
31  
32      HBaseTransaction(long transactionId, long epoch, Set<HBaseCellId> writeSet, AbstractTransactionManager tm) {
33          super(transactionId, epoch, writeSet, tm);
34      }
35  
36      @Override
37      public void cleanup() {
38          Set<HBaseCellId> writeSet = getWriteSet();
39          for (final HBaseCellId cell : writeSet) {
40              Delete delete = new Delete(cell.getRow());
41              delete.deleteColumn(cell.getFamily(), cell.getQualifier(), getStartTimestamp());
42              try {
43                  cell.getTable().delete(delete);
44              } catch (IOException e) {
45                  LOG.warn("Failed cleanup cell {} for Tx {}. This issue has been ignored", cell, getTransactionId(), e);
46              }
47          }
48          try {
49              flushTables();
50          } catch (IOException e) {
51              LOG.warn("Failed flushing tables for Tx {}", getTransactionId(), e);
52          }
53      }
54  
55      /**
56       * Flushes pending operations for tables touched by transaction
57       */
58      public void flushTables() throws IOException {
59  
60          for (HTableInterface writtenTable : getWrittenTables()) {
61              writtenTable.flushCommits();
62          }
63  
64      }
65  
66      // ****************************************************************************************************************
67      // Helper methods
68      // ****************************************************************************************************************
69  
70      private Set<HTableInterface> getWrittenTables() {
71          HashSet<HBaseCellId> writeSet = (HashSet<HBaseCellId>) getWriteSet();
72          Set<HTableInterface> tables = new HashSet<HTableInterface>();
73          for (HBaseCellId cell : writeSet) {
74              tables.add(cell.getTable());
75          }
76          return tables;
77      }
78  
79  }