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.procedure2.store.wal;
20  
21  import java.io.IOException;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  import org.apache.hadoop.hbase.classification.InterfaceAudience;
26  import org.apache.hadoop.hbase.classification.InterfaceStability;
27  import org.apache.hadoop.fs.FSDataInputStream;
28  import org.apache.hadoop.fs.FileStatus;
29  import org.apache.hadoop.fs.FileSystem;
30  import org.apache.hadoop.fs.Path;
31  import org.apache.hadoop.hbase.procedure2.store.ProcedureStoreTracker;
32  import org.apache.hadoop.hbase.protobuf.generated.ProcedureProtos.ProcedureWALHeader;
33  import org.apache.hadoop.hbase.protobuf.generated.ProcedureProtos.ProcedureWALTrailer;
34  
35  /**
36   * Describes a WAL File
37   */
38  @InterfaceAudience.Private
39  @InterfaceStability.Evolving
40  public class ProcedureWALFile implements Comparable<ProcedureWALFile> {
41    private static final Log LOG = LogFactory.getLog(ProcedureWALFile.class);
42  
43    private ProcedureWALHeader header;
44    private FSDataInputStream stream;
45    private FileStatus logStatus;
46    private FileSystem fs;
47    private Path logFile;
48    private long startPos;
49  
50    public ProcedureWALFile(final FileSystem fs, final FileStatus logStatus) {
51      this.fs = fs;
52      this.logStatus = logStatus;
53      this.logFile = logStatus.getPath();
54    }
55  
56    public ProcedureWALFile(FileSystem fs, Path logFile, ProcedureWALHeader header, long startPos) {
57      this.fs = fs;
58      this.logFile = logFile;
59      this.header = header;
60      this.startPos = startPos;
61    }
62  
63    public void open() throws IOException {
64      if (stream == null) {
65        stream = fs.open(logFile);
66      }
67  
68      if (header == null) {
69        header = ProcedureWALFormat.readHeader(stream);
70        startPos = stream.getPos();
71      } else {
72        stream.seek(startPos);
73      }
74    }
75  
76    public ProcedureWALTrailer readTrailer() throws IOException {
77      try {
78        return ProcedureWALFormat.readTrailer(stream, startPos, logStatus.getLen());
79      } finally {
80        stream.seek(startPos);
81      }
82    }
83  
84    public void readTracker(ProcedureStoreTracker tracker) throws IOException {
85      ProcedureWALTrailer trailer = readTrailer();
86      try {
87        stream.seek(trailer.getTrackerPos());
88        tracker.readFrom(stream);
89      } finally {
90        stream.seek(startPos);
91      }
92    }
93  
94    public void close() {
95      if (stream == null) return;
96      try {
97        stream.close();
98      } catch (IOException e) {
99        LOG.warn("unable to close the wal file: " + logFile, e);
100     } finally {
101       stream = null;
102     }
103   }
104 
105   public FSDataInputStream getStream() {
106     return stream;
107   }
108 
109   public ProcedureWALHeader getHeader() {
110     return header;
111   }
112 
113   public boolean isCompacted() {
114     return header.getType() == ProcedureWALFormat.LOG_TYPE_COMPACTED;
115   }
116 
117   public long getLogId() {
118     return header.getLogId();
119   }
120 
121   public long getSize() {
122     return logStatus.getLen();
123   }
124 
125   public void removeFile() throws IOException {
126     close();
127     fs.delete(logFile, false);
128   }
129 
130   @Override
131   public int compareTo(final ProcedureWALFile other) {
132     long diff = header.getLogId() - other.header.getLogId();
133     return (diff < 0) ? -1 : (diff > 0) ? 1 : 0;
134   }
135 
136   @Override
137   public boolean equals(Object o) {
138     if (this == o) return true;
139     if (!(o instanceof ProcedureWALFile)) return false;
140     return compareTo((ProcedureWALFile)o) == 0;
141   }
142 
143   @Override
144   public int hashCode() {
145     return logFile.hashCode();
146   }
147 
148   @Override
149   public String toString() {
150     return logFile.toString();
151   }
152 }