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.util.HashMap;
021import java.util.Map;
022import org.apache.commons.lang3.StringUtils;
023import org.apache.hadoop.conf.Configuration;
024import org.apache.hadoop.hbase.util.Pair;
025import org.apache.yetus.audience.InterfaceAudience;
026
027import org.apache.hbase.thirdparty.com.google.common.base.Preconditions;
028import org.apache.hbase.thirdparty.io.netty.channel.Channel;
029import org.apache.hbase.thirdparty.io.netty.channel.EventLoopGroup;
030import org.apache.hbase.thirdparty.io.netty.channel.nio.NioEventLoopGroup;
031import org.apache.hbase.thirdparty.io.netty.channel.socket.nio.NioSocketChannel;
032import org.apache.hbase.thirdparty.io.netty.util.concurrent.DefaultThreadFactory;
033
034/**
035 * Helper class for passing netty event loop config to {@link AsyncFSWALProvider}.
036 * @since 2.0.0
037 */
038@InterfaceAudience.Private
039public final class NettyAsyncFSWALConfigHelper {
040
041  private static final String EVENT_LOOP_CONFIG = "hbase.wal.async.event-loop.config";
042
043  private static final String CONFIG_NAME = "global-event-loop";
044
045  private static final Map<String,
046    Pair<EventLoopGroup, Class<? extends Channel>>> EVENT_LOOP_CONFIG_MAP = new HashMap<>();
047
048  /**
049   * Set the EventLoopGroup and channel class for {@code AsyncFSWALProvider}.
050   */
051  public static void setEventLoopConfig(Configuration conf, EventLoopGroup group,
052    Class<? extends Channel> channelClass) {
053    Preconditions.checkNotNull(group, "group is null");
054    Preconditions.checkNotNull(channelClass, "channel class is null");
055    conf.set(EVENT_LOOP_CONFIG, CONFIG_NAME);
056    EVENT_LOOP_CONFIG_MAP.put(CONFIG_NAME,
057      Pair.<EventLoopGroup, Class<? extends Channel>> newPair(group, channelClass));
058  }
059
060  static Pair<EventLoopGroup, Class<? extends Channel>> getEventLoopConfig(Configuration conf) {
061    String name = conf.get(EVENT_LOOP_CONFIG);
062    if (StringUtils.isBlank(name)) {
063      // create new event loop group if config is empty
064      return Pair.<EventLoopGroup, Class<? extends Channel>> newPair(
065        new NioEventLoopGroup(0, new DefaultThreadFactory("AsyncFSWAL", true, Thread.MAX_PRIORITY)),
066        NioSocketChannel.class);
067    }
068    return EVENT_LOOP_CONFIG_MAP.get(name);
069  }
070
071  private NettyAsyncFSWALConfigHelper() {
072  }
073}