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