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