View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  
21  package org.apache.hadoop.hbase.regionserver.wal;
22  
23  import java.io.IOException;
24  import java.util.List;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.apache.hadoop.conf.Configuration;
29  import org.apache.hadoop.fs.FileSystem;
30  import org.apache.hadoop.fs.Path;
31  import org.apache.hadoop.hbase.HConstants;
32  import org.apache.hadoop.hbase.regionserver.wal.HLog.Reader;
33  import org.apache.hadoop.hbase.regionserver.wal.HLog.Writer;
34  
35  public class HLogFactory {
36      private static final Log LOG = LogFactory.getLog(HLogFactory.class);
37      
38      public static HLog createHLog(final FileSystem fs, final Path root, final String logName,
39          final Configuration conf) throws IOException {
40        return new FSHLog(fs, root, logName, conf);
41      }
42      
43      public static HLog createHLog(final FileSystem fs, final Path root, final String logName,
44          final String oldLogName, final Configuration conf) throws IOException {
45        return new FSHLog(fs, root, logName, oldLogName, conf);
46  }
47      
48      public static HLog createHLog(final FileSystem fs, final Path root, final String logName,
49          final Configuration conf, final List<WALActionsListener> listeners,
50          final String prefix) throws IOException {
51        return new FSHLog(fs, root, logName, conf, listeners, prefix);
52      }
53  
54      public static HLog createMetaHLog(final FileSystem fs, final Path root, final String logName,
55          final Configuration conf, final List<WALActionsListener> listeners,
56          final String prefix) throws IOException {
57        return new FSHLog(fs, root, logName, HConstants.HREGION_OLDLOGDIR_NAME, 
58              conf, listeners, false, prefix, true);
59      }
60      
61      /*
62       * WAL Reader
63       */
64      
65      private static Class<? extends Reader> logReaderClass;
66      
67      static void resetLogReaderClass() {
68        logReaderClass = null;
69      }
70      
71      /**
72       * Create a reader for the WAL. If you are reading from a file that's being written to
73       * and need to reopen it multiple times, use {@link HLog.Reader#reset()} instead of this method
74       * then just seek back to the last known good position.
75       * @return A WAL reader.  Close when done with it.
76       * @throws IOException
77       */
78      public static HLog.Reader createReader(final FileSystem fs,
79          final Path path, Configuration conf)
80      throws IOException {
81        try {
82  
83          if (logReaderClass == null) {
84  
85            logReaderClass = conf.getClass("hbase.regionserver.hlog.reader.impl",
86                SequenceFileLogReader.class, Reader.class);
87          }
88  
89  
90          HLog.Reader reader = logReaderClass.newInstance();
91          reader.init(fs, path, conf);
92          return reader;
93        } catch (IOException e) {
94          throw e;
95        }
96        catch (Exception e) {
97          throw new IOException("Cannot get log reader", e);
98        }
99      }
100     
101     /*
102      * WAL writer
103      */
104     
105     private static Class<? extends Writer> logWriterClass;
106     
107     /**
108      * Create a writer for the WAL.
109      * @return A WAL writer.  Close when done with it.
110      * @throws IOException
111      */
112     public static HLog.Writer createWriter(final FileSystem fs,
113         final Path path, Configuration conf)
114     throws IOException {
115       try {
116         if (logWriterClass == null) {
117           logWriterClass = conf.getClass("hbase.regionserver.hlog.writer.impl",
118               SequenceFileLogWriter.class, Writer.class);
119         }
120         HLog.Writer writer = (HLog.Writer) logWriterClass.newInstance();
121         writer.init(fs, path, conf);
122         return writer;
123       } catch (Exception e) {
124         throw new IOException("cannot get log writer", e);
125       }
126     }
127     
128 }