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.annotations.VisibleForTesting; 021import org.apache.hbase.thirdparty.com.google.common.collect.ImmutableMap; 022 023import java.net.SocketAddress; 024 025import org.apache.hadoop.conf.Configuration; 026import org.apache.yetus.audience.InterfaceAudience; 027import org.apache.hadoop.hbase.client.MetricsConnection; 028import org.apache.hadoop.hbase.util.ReflectionUtils; 029 030/** 031 * Factory to create a {@link org.apache.hadoop.hbase.ipc.RpcClient} 032 */ 033@InterfaceAudience.Private 034public final class RpcClientFactory { 035 036 public static final String CUSTOM_RPC_CLIENT_IMPL_CONF_KEY = "hbase.rpc.client.impl"; 037 038 private static final ImmutableMap<String, String> DEPRECATED_NAME_MAPPING = ImmutableMap.of( 039 "org.apache.hadoop.hbase.ipc.RpcClientImpl", BlockingRpcClient.class.getName(), 040 "org.apache.hadoop.hbase.ipc.AsyncRpcClient", NettyRpcClient.class.getName()); 041 042 /** 043 * Private Constructor 044 */ 045 private RpcClientFactory() { 046 } 047 048 /** Helper method for tests only. Creates an {@code RpcClient} without metrics. */ 049 @VisibleForTesting 050 public static RpcClient createClient(Configuration conf, String clusterId) { 051 return createClient(conf, clusterId, null); 052 } 053 054 /** 055 * Creates a new RpcClient by the class defined in the configuration or falls back to 056 * RpcClientImpl 057 * @param conf configuration 058 * @param clusterId the cluster id 059 * @param metrics the connection metrics 060 * @return newly created RpcClient 061 */ 062 public static RpcClient createClient(Configuration conf, String clusterId, 063 MetricsConnection metrics) { 064 return createClient(conf, clusterId, null, metrics); 065 } 066 067 private static String getRpcClientClass(Configuration conf) { 068 String rpcClientClass = conf.get(CUSTOM_RPC_CLIENT_IMPL_CONF_KEY); 069 if (rpcClientClass == null) { 070 return NettyRpcClient.class.getName(); 071 } 072 String mappedName = DEPRECATED_NAME_MAPPING.get(rpcClientClass); 073 return mappedName == null ? rpcClientClass : mappedName; 074 } 075 076 /** 077 * Creates a new RpcClient by the class defined in the configuration or falls back to 078 * RpcClientImpl 079 * @param conf configuration 080 * @param clusterId the cluster id 081 * @param localAddr client socket bind address. 082 * @param metrics the connection metrics 083 * @return newly created RpcClient 084 */ 085 public static RpcClient createClient(Configuration conf, String clusterId, 086 SocketAddress localAddr, MetricsConnection metrics) { 087 String rpcClientClass = getRpcClientClass(conf); 088 return ReflectionUtils.instantiateWithCustomCtor(rpcClientClass, new Class[] { 089 Configuration.class, String.class, SocketAddress.class, MetricsConnection.class }, 090 new Object[] { conf, clusterId, localAddr, metrics }); 091 } 092}