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.client; 019 020import java.io.IOException; 021import java.net.SocketAddress; 022import java.net.URI; 023import java.security.PrivilegedExceptionAction; 024import org.apache.hadoop.conf.Configuration; 025import org.apache.hadoop.hbase.security.User; 026import org.apache.hadoop.hbase.util.FutureUtils; 027import org.apache.hadoop.hbase.util.ReflectionUtils; 028import org.apache.yetus.audience.InterfaceAudience; 029 030/** 031 * The factory for creating {@link AsyncClusterConnection}. 032 */ 033@InterfaceAudience.Private 034public final class ClusterConnectionFactory { 035 036 public static final String HBASE_SERVER_CLUSTER_CONNECTION_IMPL = 037 "hbase.server.cluster.connection.impl"; 038 039 private ClusterConnectionFactory() { 040 } 041 042 private static AsyncClusterConnection createAsyncClusterConnection(Configuration conf, 043 ConnectionRegistry registry, SocketAddress localAddress, User user) throws IOException { 044 String clusterId = FutureUtils.get(registry.getClusterId()); 045 Class<? extends AsyncClusterConnection> clazz = 046 conf.getClass(HBASE_SERVER_CLUSTER_CONNECTION_IMPL, AsyncClusterConnectionImpl.class, 047 AsyncClusterConnection.class); 048 try { 049 return user 050 .runAs((PrivilegedExceptionAction<? extends AsyncClusterConnection>) () -> ReflectionUtils 051 .newInstance(clazz, conf, registry, clusterId, localAddress, user)); 052 } catch (Exception e) { 053 throw new IOException(e); 054 } 055 } 056 057 /** 058 * Create a new {@link AsyncClusterConnection} instance. 059 * <p/> 060 * Unlike what we have done in {@link ConnectionFactory}, here we just return an 061 * {@link AsyncClusterConnection} instead of a {@link java.util.concurrent.CompletableFuture}, 062 * which means this method could block on fetching the cluster id. This is just used to simplify 063 * the implementation, as when starting new region servers, we do not need to be event-driven. Can 064 * change later if we want a {@link java.util.concurrent.CompletableFuture} here. 065 */ 066 public static AsyncClusterConnection createAsyncClusterConnection(Configuration conf, 067 SocketAddress localAddress, User user) throws IOException { 068 return createAsyncClusterConnection(conf, ConnectionRegistryFactory.create(conf, user), 069 localAddress, user); 070 } 071 072 /** 073 * Create a new {@link AsyncClusterConnection} instance. 074 * <p/> 075 * This is usually used in replication, the given {@code uri} specifies the connection info of the 076 * remote cluster. 077 */ 078 public static AsyncClusterConnection createAsyncClusterConnection(URI uri, Configuration conf, 079 SocketAddress localAddress, User user) throws IOException { 080 ConnectionRegistry registry = uri != null 081 ? ConnectionRegistryFactory.create(uri, conf, user) 082 : ConnectionRegistryFactory.create(conf, user); 083 return createAsyncClusterConnection(conf, registry, localAddress, user); 084 } 085 086 /** 087 * Create a new {@link AsyncClusterConnection} instance to be used at server side where we have a 088 * {@link ConnectionRegistryEndpoint}. 089 */ 090 public static AsyncClusterConnection createAsyncClusterConnection( 091 ConnectionRegistryEndpoint endpoint, Configuration conf, SocketAddress localAddress, User user) 092 throws IOException { 093 ShortCircuitConnectionRegistry registry = new ShortCircuitConnectionRegistry(endpoint); 094 return createAsyncClusterConnection(conf, registry, localAddress, user); 095 } 096}