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