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.collect.ImmutableMap;
21  
22  import java.net.SocketAddress;
23  
24  import org.apache.hadoop.conf.Configuration;
25  import org.apache.hadoop.hbase.classification.InterfaceAudience;
26  import org.apache.hadoop.hbase.client.MetricsConnection;
27  import org.apache.hadoop.hbase.util.ReflectionUtils;
28  
29  /**
30   * Factory to create a {@link org.apache.hadoop.hbase.ipc.RpcClient}
31   */
32  @InterfaceAudience.Private
33  public final class RpcClientFactory {
34  
35    public static final String CUSTOM_RPC_CLIENT_IMPL_CONF_KEY = "hbase.rpc.client.impl";
36  
37    private static final ImmutableMap<String, String> DEPRECATED_NAME_MAPPING = ImmutableMap.of(
38      "org.apache.hadoop.hbase.ipc.RpcClientImpl", BlockingRpcClient.class.getName(),
39      "org.apache.hadoop.hbase.ipc.AsyncRpcClient", NettyRpcClient.class.getName());
40  
41    /**
42     * Private Constructor
43     */
44    private RpcClientFactory() {
45    }
46  
47    /** Helper method for tests only. Creates an {@code RpcClient} without metrics. */
48    public static RpcClient createClient(Configuration conf, String clusterId) {
49      return createClient(conf, clusterId, null);
50    }
51  
52    /**
53     * Creates a new RpcClient by the class defined in the configuration or falls back to
54     * RpcClientImpl
55     * @param conf configuration
56     * @param clusterId the cluster id
57     * @param metrics the connection metrics
58     * @return newly created RpcClient
59     */
60    public static RpcClient createClient(Configuration conf, String clusterId,
61        MetricsConnection metrics) {
62      return createClient(conf, clusterId, null, metrics);
63    }
64  
65    private static String getRpcClientClass(Configuration conf) {
66      String rpcClientClass = conf.get(CUSTOM_RPC_CLIENT_IMPL_CONF_KEY);
67      if (rpcClientClass == null) {
68        return BlockingRpcClient.class.getName();
69      }
70      String mappedName = DEPRECATED_NAME_MAPPING.get(rpcClientClass);
71      return mappedName == null ? rpcClientClass : mappedName;
72    }
73  
74    /**
75     * Creates a new RpcClient by the class defined in the configuration or falls back to
76     * RpcClientImpl
77     * @param conf configuration
78     * @param clusterId the cluster id
79     * @param localAddr client socket bind address.
80     * @param metrics the connection metrics
81     * @return newly created RpcClient
82     */
83    public static RpcClient createClient(Configuration conf, String clusterId,
84        SocketAddress localAddr, MetricsConnection metrics) {
85      String rpcClientClass = getRpcClientClass(conf);
86      return ReflectionUtils.instantiateWithCustomCtor(rpcClientClass, new Class[] {
87          Configuration.class, String.class, SocketAddress.class, MetricsConnection.class },
88        new Object[] { conf, clusterId, localAddr, metrics });
89    }
90  }