View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.ipc;
19  
20  import com.google.common.base.Preconditions;
21  
22  import io.netty.channel.Channel;
23  import io.netty.channel.EventLoopGroup;
24  
25  import java.util.HashMap;
26  import java.util.Map;
27  
28  import org.apache.commons.lang.StringUtils;
29  import org.apache.hadoop.conf.Configuration;
30  import org.apache.hadoop.hbase.classification.InterfaceAudience;
31  import org.apache.hadoop.hbase.classification.InterfaceStability;
32  import org.apache.hadoop.hbase.util.Pair;
33  
34  /**
35   * Helper class for passing config to {@link NettyRpcClient}.
36   * <p>
37   * As hadoop Configuration can not pass an Object directly, we need to find a way to pass the
38   * EventLoopGroup to {@code AsyncRpcClient} if we want to use a single {@code EventLoopGroup} for
39   * the whole process.
40   */
41  @InterfaceAudience.Public
42  @InterfaceStability.Evolving
43  public class NettyRpcClientConfigHelper {
44  
45    public static final String EVENT_LOOP_CONFIG = "hbase.rpc.client.event-loop.config";
46  
47    private static final String CONFIG_NAME = "global-event-loop";
48  
49    private static final Map<String, Pair<EventLoopGroup, Class<? extends Channel>>>
50      EVENT_LOOP_CONFIG_MAP = new HashMap<>();
51  
52    /**
53     * Set the EventLoopGroup and channel class for {@code AsyncRpcClient}.
54     */
55    public static void setEventLoopConfig(Configuration conf, EventLoopGroup group,
56        Class<? extends Channel> channelClass) {
57      Preconditions.checkNotNull(group, "group is null");
58      Preconditions.checkNotNull(channelClass, "channel class is null");
59      conf.set(EVENT_LOOP_CONFIG, CONFIG_NAME);
60      EVENT_LOOP_CONFIG_MAP.put(CONFIG_NAME,
61        Pair.<EventLoopGroup, Class<? extends Channel>> newPair(group, channelClass));
62    }
63  
64    /**
65     * The {@code AsyncRpcClient} will create its own {@code NioEventLoopGroup}.
66     */
67    public static void createEventLoopPerClient(Configuration conf) {
68      conf.set(EVENT_LOOP_CONFIG, "");
69      EVENT_LOOP_CONFIG_MAP.clear();
70    }
71  
72    static Pair<EventLoopGroup, Class<? extends Channel>> getEventLoopConfig(Configuration conf) {
73      String name = conf.get(EVENT_LOOP_CONFIG);
74      if (name == null) {
75        return DefaultNettyEventLoopConfig.GROUP_AND_CHANNEL_CLASS;
76      }
77      if (StringUtils.isBlank(name)) {
78        return null;
79      }
80      return EVENT_LOOP_CONFIG_MAP.get(name);
81    }
82  
83  }