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 isMajor) throws IOException {
68      preCommitWriters();
69      Collection<StoreFile.Writer> writers = this.writers();
70      if (LOG.isDebugEnabled()) {
71        LOG.debug("Commit " + writers.size() + " writers");
72      }
73      List<Path> paths = new ArrayList<Path>();
74      for (Writer writer : writers) {
75        if (writer == null) {
76          continue;
77        }
78        writer.appendMetadata(maxSeqId, isMajor);
79        preCloseWriter(writer);
80        paths.add(writer.getPath());
81        writer.close();
82      }
83      return paths;
84    }
85  
86    /**
87     * Close all writers without throwing any exceptions. This is used when compaction failed usually.
88     */
89    public List<Path> abortWriters() {
90      List<Path> paths = new ArrayList<Path>();
91      for (StoreFile.Writer writer : writers()) {
92        try {
93          if (writer != null) {
94            paths.add(writer.getPath());
95            writer.close();
96          }
97        } catch (Exception ex) {
98          LOG.error("Failed to close the writer after an unfinished compaction.", ex);
99        }
100     }
101     return paths;
102   }
103 
104   protected abstract Collection<StoreFile.Writer> writers();
105 
106   /**
107    * Subclasses override this method to be called at the end of a successful sequence of append; all
108    * appends are processed before this method is called.
109    */
110   protected void preCommitWriters() throws IOException {
111   }
112 
113   /**
114    * Subclasses override this method to be called before we close the give writer. Usually you can
115    * append extra metadata to the writer.
116    */
117   protected void preCloseWriter(StoreFile.Writer writer) throws IOException {
118   }
119 }