diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/client/ClusterConnectionFactory.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/client/ClusterConnectionFactory.java index 0094ffad3993..b0e067f98192 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/client/ClusterConnectionFactory.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/client/ClusterConnectionFactory.java @@ -18,9 +18,11 @@ package org.apache.hadoop.hbase.client; import java.io.IOException; +import java.net.InetSocketAddress; import java.net.SocketAddress; import java.security.PrivilegedExceptionAction; import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.hbase.regionserver.HRegionServer; import org.apache.hadoop.hbase.security.User; import org.apache.hadoop.hbase.util.FutureUtils; import org.apache.hadoop.hbase.util.ReflectionUtils; @@ -38,18 +40,8 @@ public final class ClusterConnectionFactory { private ClusterConnectionFactory() { } - /** - * Create a new {@link AsyncClusterConnection} instance. - *
- * Unlike what we have done in {@link ConnectionFactory}, here we just return an - * {@link AsyncClusterConnection} instead of a {@link java.util.concurrent.CompletableFuture}, - * which means this method could block on fetching the cluster id. This is just used to simplify - * the implementation, as when starting new region servers, we do not need to be event-driven. Can - * change later if we want a {@link java.util.concurrent.CompletableFuture} here. - */ - public static AsyncClusterConnection createAsyncClusterConnection(Configuration conf, - SocketAddress localAddress, User user) throws IOException { - ConnectionRegistry registry = ConnectionRegistryFactory.getRegistry(conf); + private static AsyncClusterConnection createAsyncClusterConnection(Configuration conf, + ConnectionRegistry registry, SocketAddress localAddress, User user) throws IOException { String clusterId = FutureUtils.get(registry.getClusterId()); Class extends AsyncClusterConnection> clazz = conf.getClass(HBASE_SERVER_CLUSTER_CONNECTION_IMPL, AsyncClusterConnectionImpl.class, @@ -62,4 +54,32 @@ public static AsyncClusterConnection createAsyncClusterConnection(Configuration throw new IOException(e); } } + + /** + * Create a new {@link AsyncClusterConnection} instance. + * + * Unlike what we have done in {@link ConnectionFactory}, here we just return an + * {@link AsyncClusterConnection} instead of a {@link java.util.concurrent.CompletableFuture}, + * which means this method could block on fetching the cluster id. This is just used to simplify + * the implementation, as when starting new region servers, we do not need to be event-driven. Can + * change later if we want a {@link java.util.concurrent.CompletableFuture} here. + */ + public static AsyncClusterConnection createAsyncClusterConnection(Configuration conf, + SocketAddress localAddress, User user) throws IOException { + return createAsyncClusterConnection(conf, ConnectionRegistryFactory.getRegistry(conf), + localAddress, user); + } + + /** + * Create a new {@link AsyncClusterConnection} instance for a region server. + */ + public static AsyncClusterConnection createAsyncClusterConnection(HRegionServer regionServer) + throws IOException { + RegionServerRegistry registry = new RegionServerRegistry(regionServer); + Configuration conf = regionServer.getConfiguration(); + InetSocketAddress localAddress = + new InetSocketAddress(regionServer.getRSRpcServices().getSocketAddress().getAddress(), 0); + User user = regionServer.getUserProvider().getCurrent(); + return createAsyncClusterConnection(conf, registry, localAddress, user); + } } diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/client/RegionServerRegistry.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/client/RegionServerRegistry.java new file mode 100644 index 000000000000..cdfbb6d925f4 --- /dev/null +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/client/RegionServerRegistry.java @@ -0,0 +1,81 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.hbase.client; + +import java.io.IOException; +import java.util.List; +import java.util.Optional; +import java.util.concurrent.CompletableFuture; +import org.apache.hadoop.hbase.HRegionLocation; +import org.apache.hadoop.hbase.RegionLocations; +import org.apache.hadoop.hbase.ServerName; +import org.apache.hadoop.hbase.regionserver.HRegionServer; +import org.apache.yetus.audience.InterfaceAudience; + +/** + * Connection registry implementation for region server. + */ +@InterfaceAudience.Private +public class RegionServerRegistry implements ConnectionRegistry { + + private final HRegionServer regionServer; + + public RegionServerRegistry(HRegionServer regionServer) { + this.regionServer = regionServer; + } + + @Override + public CompletableFuture