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