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