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.hadoop.hbase.mapreduce;
19  
20  import java.io.IOException;
21  import java.util.List;
22  
23  import org.apache.hadoop.conf.Configuration;
24  import org.apache.hadoop.hbase.classification.InterfaceAudience;
25  import org.apache.hadoop.hbase.client.Delete;
26  import org.apache.hadoop.hbase.client.HTable;
27  
28  import com.google.common.annotations.VisibleForTesting;
29  
30  /**
31   * Buffers writes for Deletes in addition to Puts. Buffering Deletes can significantly speed up
32   * MapReduce jobs. The order of both Mutation types is preserved in the write buffer, and a buffer
33   * flush can be triggered by either Put or Delete operations.
34   */
35  @InterfaceAudience.Private
36  public class BufferedHTable extends HTable {
37  
38    private boolean closed = false;
39  
40    public BufferedHTable(Configuration conf, String tableName) throws IOException {
41      super(conf, tableName);
42    }
43  
44    public BufferedHTable(Configuration conf, byte[] tableName) throws IOException {
45      super(conf, tableName);
46    }
47  
48    @Override
49    public void delete(Delete delete) throws IOException {
50      doDelete(delete);
51    }
52  
53    @Override
54    public void delete(List<Delete> deletes) throws IOException {
55      for (Delete delete : deletes) {
56        doDelete(delete);
57      }
58    }
59  
60    private void doDelete(Delete delete) throws IOException {
61      if (this.closed) {
62        throw new IllegalStateException("BufferedHTable was closed");
63      }
64  
65      this.currentWriteBufferSize += delete.heapSize();
66      this.writeAsyncBuffer.add(delete);
67  
68      // flush when write buffer size exceeds configured limit
69      if (this.currentWriteBufferSize > getWriteBufferSize()) {
70        flushCommits();
71      }
72    }
73  
74    @Override
75    public void close() throws IOException {
76      if (this.closed) {
77        return;
78      }
79      flushCommits();
80      this.closed = true;
81      super.close();
82    }
83  
84    @VisibleForTesting
85    long getCurrentWriteBufferSize() {
86      return this.currentWriteBufferSize;
87    }
88  }