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 java.util.Collections;
022import java.util.Map;
023import org.apache.hadoop.conf.Configuration;
024import org.apache.hadoop.hbase.client.MetricsConnection;
025import org.apache.hadoop.hbase.util.ReflectionUtils;
026import org.apache.yetus.audience.InterfaceAudience;
027
028import org.apache.hbase.thirdparty.com.google.common.collect.ImmutableMap;
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,
039    String> DEPRECATED_NAME_MAPPING = ImmutableMap.of("org.apache.hadoop.hbase.ipc.RpcClientImpl",
040      BlockingRpcClient.class.getName(), "org.apache.hadoop.hbase.ipc.AsyncRpcClient",
041      NettyRpcClient.class.getName());
042
043  /**
044   * Private Constructor
045   */
046  private RpcClientFactory() {
047  }
048
049  /** Helper method for tests only. Creates an {@code RpcClient} without metrics. */
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, Collections.emptyMap());
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, Map<String, byte[]> connectionAttributes) {
087    String rpcClientClass = getRpcClientClass(conf);
088    return ReflectionUtils.instantiateWithCustomCtor(
089      rpcClientClass, new Class[] { Configuration.class, String.class, SocketAddress.class,
090        MetricsConnection.class, Map.class },
091      new Object[] { conf, clusterId, localAddr, metrics, connectionAttributes });
092  }
093}