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