001/**
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019package org.apache.hadoop.hbase.wal;
020
021import java.io.IOException;
022import org.apache.hadoop.conf.Configuration;
023import org.apache.hadoop.fs.FileSystem;
024import org.apache.hadoop.fs.Path;
025import org.apache.hadoop.hbase.regionserver.wal.FSHLog;
026import org.apache.hadoop.hbase.regionserver.wal.ProtobufLogWriter;
027import org.apache.hadoop.hbase.regionserver.wal.WALUtil;
028import org.apache.hadoop.hbase.util.CommonFSUtils;
029import org.apache.hadoop.hbase.util.CommonFSUtils.StreamLacksCapabilityException;
030import org.apache.hadoop.hbase.util.FSUtils;
031import org.apache.yetus.audience.InterfaceAudience;
032import org.apache.yetus.audience.InterfaceStability;
033import org.slf4j.Logger;
034import org.slf4j.LoggerFactory;
035
036/**
037 * A WAL provider that use {@link FSHLog}.
038 */
039@InterfaceAudience.Private
040@InterfaceStability.Evolving
041public class FSHLogProvider extends AbstractFSWALProvider<FSHLog> {
042
043  private static final Logger LOG = LoggerFactory.getLogger(FSHLogProvider.class);
044
045  // Only public so classes back in regionserver.wal can access
046  public interface Writer extends WALProvider.Writer {
047    /**
048     * @throws IOException if something goes wrong initializing an output stream
049     * @throws StreamLacksCapabilityException if the given FileSystem can't provide streams that
050     *         meet the needs of the given Writer implementation.
051     */
052    void init(FileSystem fs, Path path, Configuration c, boolean overwritable, long blocksize)
053        throws IOException, CommonFSUtils.StreamLacksCapabilityException;
054  }
055
056  /**
057   * Public because of FSHLog. Should be package-private
058   */
059  public static Writer createWriter(final Configuration conf, final FileSystem fs, final Path path,
060      final boolean overwritable) throws IOException {
061    return createWriter(conf, fs, path, overwritable, WALUtil.getWALBlockSize(conf, fs, path));
062  }
063
064  /**
065   * Public because of FSHLog. Should be package-private
066   */
067  public static Writer createWriter(final Configuration conf, final FileSystem fs, final Path path,
068    final boolean overwritable, long blocksize) throws IOException {
069    // Configuration already does caching for the Class lookup.
070    Class<? extends Writer> logWriterClass =
071        conf.getClass("hbase.regionserver.hlog.writer.impl", ProtobufLogWriter.class,
072            Writer.class);
073    Writer writer = null;
074    try {
075      writer = logWriterClass.getDeclaredConstructor().newInstance();
076      writer.init(fs, path, conf, overwritable, blocksize);
077      return writer;
078    } catch (Exception e) { 
079      if (e instanceof CommonFSUtils.StreamLacksCapabilityException) {
080        LOG.error("The RegionServer write ahead log provider for FileSystem implementations " +
081            "relies on the ability to call " + e.getMessage() + " for proper operation during " +
082            "component failures, but the current FileSystem does not support doing so. Please " +
083            "check the config value of '" + CommonFSUtils.HBASE_WAL_DIR + "' and ensure " +
084            "it points to a FileSystem mount that has suitable capabilities for output streams.");
085      } else {
086        LOG.debug("Error instantiating log writer.", e);
087      }
088      if (writer != null) {
089        try{
090          writer.close();
091        } catch(IOException ee){
092          LOG.error("cannot close log writer", ee);
093        }
094      }
095      throw new IOException("cannot get log writer", e);
096    }
097  }
098
099  @Override
100  protected FSHLog createWAL() throws IOException {
101    return new FSHLog(CommonFSUtils.getWALFileSystem(conf), CommonFSUtils.getWALRootDir(conf),
102        getWALDirectoryName(factory.factoryId),
103        getWALArchiveDirectoryName(conf, factory.factoryId), conf, listeners, true, logPrefix,
104        META_WAL_PROVIDER_ID.equals(providerId) ? META_WAL_PROVIDER_ID : null);
105  }
106
107  @Override
108  protected void doInit(Configuration conf) throws IOException {
109  }
110}