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  
19  package org.apache.hadoop.hbase.replication.regionserver;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  import org.apache.hadoop.hbase.classification.InterfaceAudience;
24  import org.apache.hadoop.conf.Configuration;
25  import org.apache.hadoop.fs.FileSystem;
26  import org.apache.hadoop.fs.Path;
27  import org.apache.hadoop.hbase.regionserver.wal.ProtobufLogReader;
28  import org.apache.hadoop.hbase.wal.WAL.Reader;
29  import org.apache.hadoop.hbase.wal.WAL.Entry;
30  import org.apache.hadoop.hbase.wal.WALFactory;
31  
32  import java.io.IOException;
33  
34  /**
35   * Wrapper class around WAL to help manage the implementation details
36   * such as compression.
37   */
38  @InterfaceAudience.Private
39  public class ReplicationWALReaderManager {
40  
41    private static final Log LOG = LogFactory.getLog(ReplicationWALReaderManager.class);
42    private final FileSystem fs;
43    private final Configuration conf;
44    private long position = 0;
45    private Reader reader;
46    private Path lastPath;
47  
48    /**
49     * Creates the helper but doesn't open any file
50     * Use setInitialPosition after using the constructor if some content needs to be skipped
51     * @param fs
52     * @param conf
53     */
54    public ReplicationWALReaderManager(FileSystem fs, Configuration conf) {
55      this.fs = fs;
56      this.conf = conf;
57    }
58  
59    /**
60     * Opens the file at the current position
61     * @param path
62     * @return an WAL reader.
63     * @throws IOException
64     */
65    public Reader openReader(Path path) throws IOException {
66      // Detect if this is a new file, if so get a new reader else
67      // reset the current reader so that we see the new data
68      if (this.reader == null || !this.lastPath.equals(path)) {
69        this.closeReader();
70        this.reader = WALFactory.createReader(this.fs, path, this.conf);
71        this.lastPath = path;
72      } else {
73        try {
74          this.reader.reset();
75        } catch (NullPointerException npe) {
76          throw new IOException("NPE resetting reader, likely HDFS-4380", npe);
77        }
78      }
79      return this.reader;
80    }
81  
82    /**
83     * Get the next entry, returned and also added in the array
84     * @return a new entry or null
85     * @throws IOException
86     */
87    public Entry readNextAndSetPosition() throws IOException {
88      Entry entry = this.reader.next();
89      // Store the position so that in the future the reader can start
90      // reading from here. If the above call to next() throws an
91      // exception, the position won't be changed and retry will happen
92      // from the last known good position
93      this.position = this.reader.getPosition();
94      // We need to set the CC to null else it will be compressed when sent to the sink
95      if (entry != null) {
96        entry.setCompressionContext(null);
97      }
98      return entry;
99    }
100 
101   /**
102    * Advance the reader to the current position
103    * @throws IOException
104    */
105   public void seek() throws IOException {
106     if (this.position != 0) {
107       this.reader.seek(this.position);
108     }
109   }
110 
111   /**
112    * Get the position that we stopped reading at
113    * @return current position, cannot be negative
114    */
115   public long getPosition() {
116     return this.position;
117   }
118 
119   public void setPosition(long pos) {
120     this.position = pos;
121   }
122 
123   public long currentTrailerSize() {
124     long size = -1L;
125     if (reader instanceof ProtobufLogReader) {
126       final ProtobufLogReader pblr = (ProtobufLogReader)reader;
127       size = pblr.trailerSize();
128     }
129     return size;
130   }
131 
132   /**
133    * Close the current reader
134    * @throws IOException
135    */
136   public void closeReader() throws IOException {
137     if (this.reader != null) {
138       this.reader.close();
139       this.reader = null;
140     }
141   }
142 
143   /**
144    * Tell the helper to reset internal state
145    */
146   void finishCurrentFile() {
147     this.position = 0;
148     try {
149       this.closeReader();
150     } catch (IOException e) {
151       LOG.warn("Unable to close reader", e);
152     }
153   }
154 
155 }