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.regionserver;
19  
20  import java.io.IOException;
21  import java.util.ArrayList;
22  import java.util.Collection;
23  import java.util.List;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.hadoop.fs.Path;
28  import org.apache.hadoop.hbase.classification.InterfaceAudience;
29  import org.apache.hadoop.hbase.regionserver.StoreFile.Writer;
30  import org.apache.hadoop.hbase.regionserver.compactions.Compactor.CellSink;
31  
32  /**
33   * Base class for cell sink that separates the provided cells into multiple files.
34   */
35  @InterfaceAudience.Private
36  public abstract class AbstractMultiFileWriter implements CellSink {
37  
38    private static final Log LOG = LogFactory.getLog(AbstractMultiFileWriter.class);
39  
40    /** Factory that is used to produce single StoreFile.Writer-s */
41    protected WriterFactory writerFactory;
42  
43    /** Source scanner that is tracking KV count; may be null if source is not StoreScanner */
44    protected StoreScanner sourceScanner;
45  
46    public interface WriterFactory {
47      public StoreFile.Writer createWriter() throws IOException;
48    }
49  
50    /**
51     * Initializes multi-writer before usage.
52     * @param sourceScanner Optional store scanner to obtain the information about read progress.
53     * @param factory Factory used to produce individual file writers.
54     */
55    public void init(StoreScanner sourceScanner, WriterFactory factory) {
56      this.writerFactory = factory;
57      this.sourceScanner = sourceScanner;
58    }
59  
60    /**
61     * Commit all writers.
62     * <p>
63     * Notice that here we use the same <code>maxSeqId</code> for all output files since we haven't
64     * find an easy to find enough sequence ids for different output files in some corner cases. See
65     * comments in HBASE-15400 for more details.
66     */
67    public List<Path> commitWriters(long maxSeqId, boolean majorCompaction) throws IOException {
68      preCommitWriters();
69      Collection<StoreFile.Writer> writers = this.writers();
70      if (LOG.isDebugEnabled()) {
71        LOG.debug("Commit " + writers.size() + " writers, maxSeqId=" + maxSeqId
72            + ", majorCompaction=" + majorCompaction);
73      }
74      List<Path> paths = new ArrayList<Path>();
75      for (Writer writer : writers) {
76        if (writer == null) {
77          continue;
78        }
79        writer.appendMetadata(maxSeqId, majorCompaction);
80        preCloseWriter(writer);
81        paths.add(writer.getPath());
82        writer.close();
83      }
84      return paths;
85    }
86  
87    /**
88     * Close all writers without throwing any exceptions. This is used when compaction failed usually.
89     */
90    public List<Path> abortWriters() {
91      List<Path> paths = new ArrayList<Path>();
92      for (StoreFile.Writer writer : writers()) {
93        try {
94          if (writer != null) {
95            paths.add(writer.getPath());
96            writer.close();
97          }
98        } catch (Exception ex) {
99          LOG.error("Failed to close the writer after an unfinished compaction.", ex);
100       }
101     }
102     return paths;
103   }
104 
105   protected abstract Collection<StoreFile.Writer> writers();
106 
107   /**
108    * Subclasses override this method to be called at the end of a successful sequence of append; all
109    * appends are processed before this method is called.
110    */
111   protected void preCommitWriters() throws IOException {
112   }
113 
114   /**
115    * Subclasses override this method to be called before we close the give writer. Usually you can
116    * append extra metadata to the writer.
117    */
118   protected void preCloseWriter(StoreFile.Writer writer) throws IOException {
119   }
120 }