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