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. Tests may set
049   * 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,
057    Pair<EventLoopGroup, Class<? extends Channel>>> EVENT_LOOP_CONFIG_MAP = new HashMap<>();
058
059  /**
060   * Shutdown constructor.
061   */
062  private NettyRpcClientConfigHelper() {
063  }
064
065  /**
066   * Set the EventLoopGroup and channel class for {@code AsyncRpcClient}.
067   */
068  public static void setEventLoopConfig(Configuration conf, EventLoopGroup group,
069    Class<? extends Channel> channelClass) {
070    Preconditions.checkNotNull(group, "group is null");
071    Preconditions.checkNotNull(channelClass, "channel class is null");
072    conf.set(EVENT_LOOP_CONFIG, CONFIG_NAME);
073    EVENT_LOOP_CONFIG_MAP.put(CONFIG_NAME,
074      Pair.<EventLoopGroup, Class<? extends Channel>> newPair(group, channelClass));
075  }
076
077  /**
078   * The {@link NettyRpcClient} will create its own {@code NioEventLoopGroup}.
079   */
080  public static void createEventLoopPerClient(Configuration conf) {
081    conf.set(EVENT_LOOP_CONFIG, "");
082    EVENT_LOOP_CONFIG_MAP.clear();
083  }
084
085  private static volatile Pair<EventLoopGroup, Class<? extends Channel>> DEFAULT_EVENT_LOOP;
086
087  private static Pair<EventLoopGroup, Class<? extends Channel>>
088    getDefaultEventLoopConfig(Configuration conf) {
089    Pair<EventLoopGroup, Class<? extends Channel>> eventLoop = DEFAULT_EVENT_LOOP;
090    if (eventLoop != null) {
091      return eventLoop;
092    }
093    synchronized (NettyRpcClientConfigHelper.class) {
094      eventLoop = DEFAULT_EVENT_LOOP;
095      if (eventLoop != null) {
096        return eventLoop;
097      }
098      int threadCount = conf.getInt(HBASE_NETTY_EVENTLOOP_RPCCLIENT_THREADCOUNT_KEY, 0);
099      eventLoop = new Pair<>(
100        new NioEventLoopGroup(threadCount,
101          new DefaultThreadFactory("RPCClient-NioEventLoopGroup", true, Thread.NORM_PRIORITY)),
102        NioSocketChannel.class);
103      DEFAULT_EVENT_LOOP = eventLoop;
104    }
105    return eventLoop;
106  }
107
108  static Pair<EventLoopGroup, Class<? extends Channel>> getEventLoopConfig(Configuration conf) {
109    String name = conf.get(EVENT_LOOP_CONFIG);
110    if (name == null) {
111      return getDefaultEventLoopConfig(conf);
112    }
113    if (StringUtils.isBlank(name)) {
114      return null;
115    }
116    return EVENT_LOOP_CONFIG_MAP.get(name);
117  }
118}