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