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 org.apache.hbase.thirdparty.com.google.common.base.Preconditions; 021 022import org.apache.hbase.thirdparty.io.netty.channel.Channel; 023import org.apache.hbase.thirdparty.io.netty.channel.EventLoopGroup; 024 025import java.util.HashMap; 026import java.util.Map; 027 028import org.apache.commons.lang3.StringUtils; 029import org.apache.hadoop.conf.Configuration; 030import org.apache.yetus.audience.InterfaceAudience; 031import org.apache.hadoop.hbase.util.Pair; 032 033/** 034 * Helper class for passing config to {@link NettyRpcClient}. 035 * <p> 036 * As hadoop Configuration can not pass an Object directly, we need to find a way to pass the 037 * EventLoopGroup to {@code AsyncRpcClient} if we want to use a single {@code EventLoopGroup} for 038 * the whole process. 039 * @since 2.0.0 040 */ 041@InterfaceAudience.Public 042public class NettyRpcClientConfigHelper { 043 044 public static final String EVENT_LOOP_CONFIG = "hbase.rpc.client.event-loop.config"; 045 046 private static final String CONFIG_NAME = "global-event-loop"; 047 048 private static final Map<String, Pair<EventLoopGroup, Class<? extends Channel>>> 049 EVENT_LOOP_CONFIG_MAP = new HashMap<>(); 050 051 /** 052 * Set the EventLoopGroup and channel class for {@code AsyncRpcClient}. 053 */ 054 public static void setEventLoopConfig(Configuration conf, EventLoopGroup group, 055 Class<? extends Channel> channelClass) { 056 Preconditions.checkNotNull(group, "group is null"); 057 Preconditions.checkNotNull(channelClass, "channel class is null"); 058 conf.set(EVENT_LOOP_CONFIG, CONFIG_NAME); 059 EVENT_LOOP_CONFIG_MAP.put(CONFIG_NAME, 060 Pair.<EventLoopGroup, Class<? extends Channel>> newPair(group, channelClass)); 061 } 062 063 /** 064 * The {@code AsyncRpcClient} will create its own {@code NioEventLoopGroup}. 065 */ 066 public static void createEventLoopPerClient(Configuration conf) { 067 conf.set(EVENT_LOOP_CONFIG, ""); 068 EVENT_LOOP_CONFIG_MAP.clear(); 069 } 070 071 static Pair<EventLoopGroup, Class<? extends Channel>> getEventLoopConfig(Configuration conf) { 072 String name = conf.get(EVENT_LOOP_CONFIG); 073 if (name == null) { 074 return DefaultNettyEventLoopConfig.GROUP_AND_CHANNEL_CLASS; 075 } 076 if (StringUtils.isBlank(name)) { 077 return null; 078 } 079 return EVENT_LOOP_CONFIG_MAP.get(name); 080 } 081 082}