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.ipc;
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 config to {@link NettyRpcClient}.
036 * <p>
037 * As hadoop Configuration can not pass an Object directly, we need to find a way to pass the
038 * EventLoopGroup to {@code AsyncRpcClient} if we want to use a single {@code EventLoopGroup} for
039 * the whole process.
040 * @since 2.0.0
041 */
042@InterfaceAudience.Public
043public final class NettyRpcClientConfigHelper {
044
045  public static final String EVENT_LOOP_CONFIG = "hbase.rpc.client.event-loop.config";
046
047  /**
048   * Name of property to change netty rpc client eventloop thread count. Default is 0.
049   * Tests may set this down from unlimited.
050   */
051  public static final String HBASE_NETTY_EVENTLOOP_RPCCLIENT_THREADCOUNT_KEY =
052    "hbase.netty.eventloop.rpcclient.thread.count";
053
054  private static final String CONFIG_NAME = "global-event-loop";
055
056  private static final Map<String, Pair<EventLoopGroup, Class<? extends Channel>>>
057    EVENT_LOOP_CONFIG_MAP = new HashMap<>();
058
059  /**
060   * Shutdown constructor.
061   */
062  private NettyRpcClientConfigHelper() {}
063
064  /**
065   * Set the EventLoopGroup and channel class for {@code AsyncRpcClient}.
066   */
067  public static void setEventLoopConfig(Configuration conf, EventLoopGroup group,
068      Class<? extends Channel> channelClass) {
069    Preconditions.checkNotNull(group, "group is null");
070    Preconditions.checkNotNull(channelClass, "channel class is null");
071    conf.set(EVENT_LOOP_CONFIG, CONFIG_NAME);
072    EVENT_LOOP_CONFIG_MAP.put(CONFIG_NAME,
073      Pair.<EventLoopGroup, Class<? extends Channel>> newPair(group, channelClass));
074  }
075
076  /**
077   * The {@link NettyRpcClient} will create its own {@code NioEventLoopGroup}.
078   */
079  public static void createEventLoopPerClient(Configuration conf) {
080    conf.set(EVENT_LOOP_CONFIG, "");
081    EVENT_LOOP_CONFIG_MAP.clear();
082  }
083
084  private static volatile Pair<EventLoopGroup, Class<? extends Channel>> DEFAULT_EVENT_LOOP;
085
086  private static Pair<EventLoopGroup, Class<? extends Channel>>
087    getDefaultEventLoopConfig(Configuration conf) {
088    Pair<EventLoopGroup, Class<? extends Channel>> eventLoop = DEFAULT_EVENT_LOOP;
089    if (eventLoop != null) {
090      return eventLoop;
091    }
092    synchronized (NettyRpcClientConfigHelper.class) {
093      eventLoop = DEFAULT_EVENT_LOOP;
094      if (eventLoop != null) {
095        return eventLoop;
096      }
097      int threadCount = conf.getInt(HBASE_NETTY_EVENTLOOP_RPCCLIENT_THREADCOUNT_KEY, 0);
098      eventLoop = new Pair<>(
099        new NioEventLoopGroup(threadCount,
100          new DefaultThreadFactory("RPCClient-NioEventLoopGroup", true, Thread.NORM_PRIORITY)),
101        NioSocketChannel.class);
102      DEFAULT_EVENT_LOOP = eventLoop;
103    }
104    return eventLoop;
105  }
106
107  static Pair<EventLoopGroup, Class<? extends Channel>> getEventLoopConfig(Configuration conf) {
108    String name = conf.get(EVENT_LOOP_CONFIG);
109    if (name == null) {
110      return getDefaultEventLoopConfig(conf);
111    }
112    if (StringUtils.isBlank(name)) {
113      return null;
114    }
115    return EVENT_LOOP_CONFIG_MAP.get(name);
116  }
117}