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.FanOutOneBlockAsyncDFSOutput;
025import org.apache.hadoop.hbase.io.asyncfs.FanOutOneBlockAsyncDFSOutputHelper;
026import org.apache.hadoop.hbase.io.asyncfs.FanOutOneBlockAsyncDFSOutputSaslHelper;
027import org.apache.hadoop.hbase.io.asyncfs.monitor.StreamSlowMonitor;
028import org.apache.hadoop.hbase.regionserver.wal.AsyncFSWAL;
029import org.apache.hadoop.hbase.regionserver.wal.AsyncProtobufLogWriter;
030import org.apache.hadoop.hbase.regionserver.wal.WALUtil;
031import org.apache.hadoop.hbase.util.CommonFSUtils;
032import org.apache.hadoop.hbase.util.CommonFSUtils.StreamLacksCapabilityException;
033import org.apache.hadoop.hbase.util.Pair;
034import org.apache.yetus.audience.InterfaceAudience;
035import org.apache.yetus.audience.InterfaceStability;
036import org.slf4j.Logger;
037import org.slf4j.LoggerFactory;
038
039import org.apache.hbase.thirdparty.com.google.common.base.Throwables;
040import org.apache.hbase.thirdparty.io.netty.channel.Channel;
041import org.apache.hbase.thirdparty.io.netty.channel.EventLoopGroup;
042
043/**
044 * A WAL provider that use {@link AsyncFSWAL}.
045 */
046@InterfaceAudience.Private
047@InterfaceStability.Evolving
048public class AsyncFSWALProvider extends AbstractFSWALProvider<AsyncFSWAL> {
049
050  private static final Logger LOG = LoggerFactory.getLogger(AsyncFSWALProvider.class);
051
052  public static final String WRITER_IMPL = "hbase.regionserver.hlog.async.writer.impl";
053
054  // Only public so classes back in regionserver.wal can access
055  public interface AsyncWriter extends WALProvider.AsyncWriter {
056    /**
057     * @throws IOException if something goes wrong initializing an output stream
058     * @throws StreamLacksCapabilityException if the given FileSystem can't provide streams that
059     *         meet the needs of the given Writer implementation.
060     */
061    void init(FileSystem fs, Path path, Configuration c, boolean overwritable, long blocksize,
062        StreamSlowMonitor monitor) throws IOException, CommonFSUtils.StreamLacksCapabilityException;
063  }
064
065  private EventLoopGroup eventLoopGroup;
066
067  private Class<? extends Channel> channelClass;
068
069  @Override
070  protected AsyncFSWAL createWAL() throws IOException {
071    return new AsyncFSWAL(CommonFSUtils.getWALFileSystem(conf), this.abortable,
072      CommonFSUtils.getWALRootDir(conf), getWALDirectoryName(factory.factoryId),
073      getWALArchiveDirectoryName(conf, factory.factoryId), conf, listeners, true, logPrefix,
074      META_WAL_PROVIDER_ID.equals(providerId) ? META_WAL_PROVIDER_ID : null, eventLoopGroup,
075      channelClass, factory.getExcludeDatanodeManager().getStreamSlowMonitor(providerId));
076  }
077
078  @Override
079  protected void doInit(Configuration conf) throws IOException {
080    Pair<EventLoopGroup, Class<? extends Channel>> eventLoopGroupAndChannelClass =
081      NettyAsyncFSWALConfigHelper.getEventLoopConfig(conf);
082    eventLoopGroup = eventLoopGroupAndChannelClass.getFirst();
083    channelClass = eventLoopGroupAndChannelClass.getSecond();
084  }
085
086  /**
087   * Public because of AsyncFSWAL. Should be package-private
088   */
089  public static AsyncWriter createAsyncWriter(Configuration conf, FileSystem fs, Path path,
090      boolean overwritable, EventLoopGroup eventLoopGroup,
091      Class<? extends Channel> channelClass) throws IOException {
092    return createAsyncWriter(conf, fs, path, overwritable, WALUtil.getWALBlockSize(conf, fs, path),
093        eventLoopGroup, channelClass, StreamSlowMonitor.create(conf, path.getName()));
094  }
095
096  /**
097   * Public because of AsyncFSWAL. Should be package-private
098   */
099  public static AsyncWriter createAsyncWriter(Configuration conf, FileSystem fs, Path path,
100      boolean overwritable, long blocksize, EventLoopGroup eventLoopGroup,
101      Class<? extends Channel> channelClass, StreamSlowMonitor monitor) throws IOException {
102    // Configuration already does caching for the Class lookup.
103    Class<? extends AsyncWriter> logWriterClass = conf.getClass(
104      WRITER_IMPL, AsyncProtobufLogWriter.class, AsyncWriter.class);
105    try {
106      AsyncWriter writer = logWriterClass.getConstructor(EventLoopGroup.class, Class.class)
107          .newInstance(eventLoopGroup, channelClass);
108      writer.init(fs, path, conf, overwritable, blocksize, monitor);
109      return writer;
110    } catch (Exception e) {
111      if (e instanceof CommonFSUtils.StreamLacksCapabilityException) {
112        LOG.error("The RegionServer async write ahead log provider " +
113          "relies on the ability to call " + e.getMessage() + " for proper operation during " +
114          "component failures, but the current FileSystem does not support doing so. Please " +
115          "check the config value of '" + CommonFSUtils.HBASE_WAL_DIR + "' and ensure " +
116          "it points to a FileSystem mount that has suitable capabilities for output streams.");
117      } else {
118        LOG.debug("Error instantiating log writer.", e);
119      }
120      Throwables.propagateIfPossible(e, IOException.class);
121      throw new IOException("cannot get log writer", e);
122    }
123  }
124
125  /**
126   * Test whether we can load the helper classes for async dfs output.
127   */
128  public static boolean load() {
129    try {
130      Class.forName(FanOutOneBlockAsyncDFSOutput.class.getName());
131      Class.forName(FanOutOneBlockAsyncDFSOutputHelper.class.getName());
132      Class.forName(FanOutOneBlockAsyncDFSOutputSaslHelper.class.getName());
133      return true;
134    } catch (Throwable e) {
135      return false;
136    }
137  }
138}