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