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.client.RegionInfo;
025import org.apache.hadoop.hbase.io.asyncfs.monitor.StreamSlowMonitor;
026import org.apache.hadoop.hbase.regionserver.wal.FSHLog;
027import org.apache.hadoop.hbase.regionserver.wal.ProtobufLogWriter;
028import org.apache.hadoop.hbase.regionserver.wal.WALUtil;
029import org.apache.hadoop.hbase.util.CommonFSUtils;
030import org.apache.hadoop.hbase.util.CommonFSUtils.StreamLacksCapabilityException;
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  public static final String WRITER_IMPL = "hbase.regionserver.wal.writer.impl";
046
047  // Only public so classes back in regionserver.wal can access
048  public interface Writer extends WALProvider.Writer {
049    /**
050     * @throws IOException                    if something goes wrong initializing an output stream
051     * @throws StreamLacksCapabilityException if the given FileSystem can't provide streams that
052     *                                        meet the needs of the given Writer implementation.
053     */
054    void init(FileSystem fs, Path path, Configuration c, boolean overwritable, long blocksize,
055      StreamSlowMonitor monitor) throws IOException, CommonFSUtils.StreamLacksCapabilityException;
056  }
057
058  /**
059   * Public because of FSHLog. Should be package-private
060   * @param overwritable if the created writer can overwrite. For recovered edits, it is true and
061   *                     for WAL it is false. Thus we can distinguish WAL and recovered edits by
062   *                     this.
063   */
064  public static Writer createWriter(final Configuration conf, final FileSystem fs, final Path path,
065    final boolean overwritable) throws IOException {
066    return createWriter(conf, fs, path, overwritable,
067      WALUtil.getWALBlockSize(conf, fs, path, overwritable));
068  }
069
070  /**
071   * Public because of FSHLog. Should be package-private
072   */
073  public static Writer createWriter(final Configuration conf, final FileSystem fs, final Path path,
074    final boolean overwritable, long blocksize) throws IOException {
075    // Configuration already does caching for the Class lookup.
076    Class<? extends Writer> logWriterClass =
077      conf.getClass(WRITER_IMPL, ProtobufLogWriter.class, Writer.class);
078    Writer writer = null;
079    try {
080      writer = logWriterClass.getDeclaredConstructor().newInstance();
081      FileSystem rootFs = FileSystem.get(path.toUri(), conf);
082      writer.init(rootFs, path, conf, overwritable, blocksize,
083        StreamSlowMonitor.create(conf, path.getName()));
084      return writer;
085    } catch (Exception e) {
086      if (e instanceof CommonFSUtils.StreamLacksCapabilityException) {
087        LOG.error("The RegionServer write ahead log provider for FileSystem implementations "
088          + "relies on the ability to call " + e.getMessage() + " for proper operation during "
089          + "component failures, but the current FileSystem does not support doing so. Please "
090          + "check the config value of '" + CommonFSUtils.HBASE_WAL_DIR + "' and ensure "
091          + "it points to a FileSystem mount that has suitable capabilities for output streams.");
092      } else {
093        LOG.debug("Error instantiating log writer.", e);
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), abortable,
102      CommonFSUtils.getWALRootDir(conf), getWALDirectoryName(factory.factoryId),
103      getWALArchiveDirectoryName(conf, factory.factoryId), conf, listeners, true, logPrefix,
104      META_WAL_PROVIDER_ID.equals(providerId) ? META_WAL_PROVIDER_ID : null, null, null);
105  }
106
107  @Override
108  protected WAL createRemoteWAL(RegionInfo region, FileSystem remoteFs, Path remoteWALDir,
109    String prefix, String suffix) throws IOException {
110    return new FSHLog(CommonFSUtils.getWALFileSystem(conf), abortable,
111      CommonFSUtils.getWALRootDir(conf), getWALDirectoryName(factory.factoryId),
112      getWALArchiveDirectoryName(conf, factory.factoryId), conf, listeners, true, prefix, suffix,
113      remoteFs, remoteWALDir);
114  }
115
116  @Override
117  protected void doInit(Configuration conf) throws IOException {
118  }
119
120}