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; 021 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.AsyncFSWAL; 026import org.apache.hadoop.hbase.regionserver.wal.AsyncProtobufLogWriter; 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.Pair; 031import org.apache.yetus.audience.InterfaceAudience; 032import org.apache.yetus.audience.InterfaceStability; 033import org.slf4j.Logger; 034import org.slf4j.LoggerFactory; 035import org.apache.hbase.thirdparty.com.google.common.base.Throwables; 036import org.apache.hbase.thirdparty.io.netty.channel.Channel; 037import org.apache.hbase.thirdparty.io.netty.channel.EventLoopGroup; 038import org.apache.hbase.thirdparty.io.netty.channel.nio.NioEventLoopGroup; 039import org.apache.hbase.thirdparty.io.netty.channel.socket.nio.NioSocketChannel; 040import org.apache.hbase.thirdparty.io.netty.util.concurrent.DefaultThreadFactory; 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 @Override 066 protected AsyncFSWAL createWAL() throws IOException { 067 return new AsyncFSWAL(CommonFSUtils.getWALFileSystem(conf), CommonFSUtils.getWALRootDir(conf), 068 getWALDirectoryName(factory.factoryId), 069 getWALArchiveDirectoryName(conf, factory.factoryId), conf, listeners, true, logPrefix, 070 META_WAL_PROVIDER_ID.equals(providerId) ? META_WAL_PROVIDER_ID : null, 071 eventLoopGroup, channelClass); 072 } 073 074 @Override 075 protected void doInit(Configuration conf) throws IOException { 076 Pair<EventLoopGroup, Class<? extends Channel>> eventLoopGroupAndChannelClass = 077 NettyAsyncFSWALConfigHelper.getEventLoopConfig(conf); 078 if (eventLoopGroupAndChannelClass != null) { 079 eventLoopGroup = eventLoopGroupAndChannelClass.getFirst(); 080 channelClass = eventLoopGroupAndChannelClass.getSecond(); 081 } else { 082 eventLoopGroup = new NioEventLoopGroup(1, 083 new DefaultThreadFactory("AsyncFSWAL", true, Thread.MAX_PRIORITY)); 084 channelClass = NioSocketChannel.class; 085 } 086 } 087 088 /** 089 * Public because of AsyncFSWAL. Should be package-private 090 */ 091 public static AsyncWriter createAsyncWriter(Configuration conf, FileSystem fs, Path path, 092 boolean overwritable, EventLoopGroup eventLoopGroup, 093 Class<? extends Channel> channelClass) throws IOException { 094 return createAsyncWriter(conf, fs, path, overwritable, WALUtil.getWALBlockSize(conf, fs, path), 095 eventLoopGroup, channelClass); 096 } 097 098 /** 099 * Public because of AsyncFSWAL. Should be package-private 100 */ 101 public static AsyncWriter createAsyncWriter(Configuration conf, FileSystem fs, Path path, 102 boolean overwritable, long blocksize, EventLoopGroup eventLoopGroup, 103 Class<? extends Channel> channelClass) throws IOException { 104 // Configuration already does caching for the Class lookup. 105 Class<? extends AsyncWriter> logWriterClass = conf.getClass( 106 "hbase.regionserver.hlog.async.writer.impl", AsyncProtobufLogWriter.class, AsyncWriter.class); 107 try { 108 AsyncWriter writer = logWriterClass.getConstructor(EventLoopGroup.class, Class.class) 109 .newInstance(eventLoopGroup, channelClass); 110 writer.init(fs, path, conf, overwritable, blocksize); 111 return writer; 112 } catch (Exception e) { 113 if (e instanceof CommonFSUtils.StreamLacksCapabilityException) { 114 LOG.error("The RegionServer async write ahead log provider " + 115 "relies on the ability to call " + e.getMessage() + " for proper operation during " + 116 "component failures, but the current FileSystem does not support doing so. Please " + 117 "check the config value of '" + CommonFSUtils.HBASE_WAL_DIR + "' and ensure " + 118 "it points to a FileSystem mount that has suitable capabilities for output streams."); 119 } else { 120 LOG.debug("Error instantiating log writer.", e); 121 } 122 Throwables.propagateIfPossible(e, IOException.class); 123 throw new IOException("cannot get log writer", e); 124 } 125 } 126}