Skip to content

Commit 8711fae

Browse files
committed
HBASE-26181 Region server and master could use itself as ConnectionRegistry
1 parent c8d9d4d commit 8711fae

File tree

3 files changed

+118
-37
lines changed

3 files changed

+118
-37
lines changed

hbase-server/src/main/java/org/apache/hadoop/hbase/client/ClusterConnectionFactory.java

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@
1818
package org.apache.hadoop.hbase.client;
1919

2020
import java.io.IOException;
21+
import java.net.InetSocketAddress;
2122
import java.net.SocketAddress;
2223
import java.security.PrivilegedExceptionAction;
2324
import org.apache.hadoop.conf.Configuration;
25+
import org.apache.hadoop.hbase.regionserver.HRegionServer;
2426
import org.apache.hadoop.hbase.security.User;
2527
import org.apache.hadoop.hbase.util.FutureUtils;
2628
import org.apache.hadoop.hbase.util.ReflectionUtils;
@@ -38,18 +40,8 @@ public final class ClusterConnectionFactory {
3840
private ClusterConnectionFactory() {
3941
}
4042

41-
/**
42-
* Create a new {@link AsyncClusterConnection} instance.
43-
* <p/>
44-
* Unlike what we have done in {@link ConnectionFactory}, here we just return an
45-
* {@link AsyncClusterConnection} instead of a {@link java.util.concurrent.CompletableFuture},
46-
* which means this method could block on fetching the cluster id. This is just used to simplify
47-
* the implementation, as when starting new region servers, we do not need to be event-driven. Can
48-
* change later if we want a {@link java.util.concurrent.CompletableFuture} here.
49-
*/
50-
public static AsyncClusterConnection createAsyncClusterConnection(Configuration conf,
51-
SocketAddress localAddress, User user) throws IOException {
52-
ConnectionRegistry registry = ConnectionRegistryFactory.getRegistry(conf);
43+
private static AsyncClusterConnection createAsyncClusterConnection(Configuration conf,
44+
ConnectionRegistry registry, SocketAddress localAddress, User user) throws IOException {
5345
String clusterId = FutureUtils.get(registry.getClusterId());
5446
Class<? extends AsyncClusterConnection> clazz =
5547
conf.getClass(HBASE_SERVER_CLUSTER_CONNECTION_IMPL, AsyncClusterConnectionImpl.class,
@@ -62,4 +54,32 @@ public static AsyncClusterConnection createAsyncClusterConnection(Configuration
6254
throw new IOException(e);
6355
}
6456
}
57+
58+
/**
59+
* Create a new {@link AsyncClusterConnection} instance.
60+
* <p/>
61+
* Unlike what we have done in {@link ConnectionFactory}, here we just return an
62+
* {@link AsyncClusterConnection} instead of a {@link java.util.concurrent.CompletableFuture},
63+
* which means this method could block on fetching the cluster id. This is just used to simplify
64+
* the implementation, as when starting new region servers, we do not need to be event-driven. Can
65+
* change later if we want a {@link java.util.concurrent.CompletableFuture} here.
66+
*/
67+
public static AsyncClusterConnection createAsyncClusterConnection(Configuration conf,
68+
SocketAddress localAddress, User user) throws IOException {
69+
return createAsyncClusterConnection(conf, ConnectionRegistryFactory.getRegistry(conf),
70+
localAddress, user);
71+
}
72+
73+
/**
74+
* Create a new {@link AsyncClusterConnection} instance for a region server.
75+
*/
76+
public static AsyncClusterConnection createAsyncClusterConnection(HRegionServer regionServer)
77+
throws IOException {
78+
RegionServerRegistry registry = new RegionServerRegistry(regionServer);
79+
Configuration conf = regionServer.getConfiguration();
80+
InetSocketAddress localAddress =
81+
new InetSocketAddress(regionServer.getRSRpcServices().getSocketAddress().getAddress(), 0);
82+
User user = regionServer.getUserProvider().getCurrent();
83+
return createAsyncClusterConnection(conf, registry, localAddress, user);
84+
}
6585
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package org.apache.hadoop.hbase.client;
19+
20+
import java.io.IOException;
21+
import java.util.List;
22+
import java.util.Optional;
23+
import java.util.concurrent.CompletableFuture;
24+
import org.apache.hadoop.hbase.HRegionLocation;
25+
import org.apache.hadoop.hbase.RegionLocations;
26+
import org.apache.hadoop.hbase.ServerName;
27+
import org.apache.hadoop.hbase.regionserver.HRegionServer;
28+
import org.apache.yetus.audience.InterfaceAudience;
29+
30+
/**
31+
* Connection registry implementation for region server.
32+
*/
33+
@InterfaceAudience.Private
34+
public class RegionServerRegistry implements ConnectionRegistry {
35+
36+
private final HRegionServer regionServer;
37+
38+
public RegionServerRegistry(HRegionServer regionServer) {
39+
this.regionServer = regionServer;
40+
}
41+
42+
@Override
43+
public CompletableFuture<RegionLocations> getMetaRegionLocations() {
44+
CompletableFuture<RegionLocations> future = new CompletableFuture<>();
45+
Optional<List<HRegionLocation>> locs =
46+
regionServer.getMetaRegionLocationCache().getMetaRegionLocations();
47+
if (locs.isPresent()) {
48+
List<HRegionLocation> list = locs.get();
49+
if (list.isEmpty()) {
50+
future.completeExceptionally(new IOException("no meta location available"));
51+
} else {
52+
future.complete(new RegionLocations(list));
53+
}
54+
} else {
55+
future.completeExceptionally(new IOException("no meta location available"));
56+
}
57+
return future;
58+
}
59+
60+
@Override
61+
public CompletableFuture<String> getClusterId() {
62+
return CompletableFuture.completedFuture(regionServer.getClusterId());
63+
}
64+
65+
@Override
66+
public CompletableFuture<ServerName> getActiveMaster() {
67+
CompletableFuture<ServerName> future = new CompletableFuture<>();
68+
Optional<ServerName> activeMaster = regionServer.getActiveMaster();
69+
if (activeMaster.isPresent()) {
70+
future.complete(activeMaster.get());
71+
} else {
72+
future.completeExceptionally(new IOException("no active master available"));
73+
}
74+
return future;
75+
}
76+
77+
@Override
78+
public void close() {
79+
// nothing
80+
}
81+
}

hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/HRegionServer.java

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -863,26 +863,6 @@ public boolean registerService(Service instance) {
863863
return true;
864864
}
865865

866-
private Configuration cleanupConfiguration() {
867-
Configuration conf = this.conf;
868-
// We use ZKConnectionRegistry for all the internal communication, primarily for these reasons:
869-
// - Decouples RS and master life cycles. RegionServers can continue be up independent of
870-
// masters' availability.
871-
// - Configuration management for region servers (cluster internal) is much simpler when adding
872-
// new masters or removing existing masters, since only clients' config needs to be updated.
873-
// - We need to retain ZKConnectionRegistry for replication use anyway, so we just extend it for
874-
// other internal connections too.
875-
conf.set(HConstants.CLIENT_CONNECTION_REGISTRY_IMPL_CONF_KEY,
876-
HConstants.ZK_CONNECTION_REGISTRY_CLASS);
877-
if (conf.get(HConstants.CLIENT_ZOOKEEPER_QUORUM) != null) {
878-
// Use server ZK cluster for server-issued connections, so we clone
879-
// the conf and unset the client ZK related properties
880-
conf = new Configuration(this.conf);
881-
conf.unset(HConstants.CLIENT_ZOOKEEPER_QUORUM);
882-
}
883-
return conf;
884-
}
885-
886866
/**
887867
* Run test on configured codecs to make sure supporting libs are in place.
888868
*/
@@ -907,11 +887,7 @@ public String getClusterId() {
907887
*/
908888
protected final synchronized void setupClusterConnection() throws IOException {
909889
if (asyncClusterConnection == null) {
910-
Configuration conf = cleanupConfiguration();
911-
InetSocketAddress localAddress = new InetSocketAddress(this.rpcServices.isa.getAddress(), 0);
912-
User user = userProvider.getCurrent();
913-
asyncClusterConnection =
914-
ClusterConnectionFactory.createAsyncClusterConnection(conf, localAddress, user);
890+
asyncClusterConnection = ClusterConnectionFactory.createAsyncClusterConnection(this);
915891
}
916892
}
917893

@@ -4022,4 +3998,8 @@ public List<ServerName> getRegionServers() {
40223998
public MetaRegionLocationCache getMetaRegionLocationCache() {
40233999
return this.metaRegionLocationCache;
40244000
}
4001+
4002+
public UserProvider getUserProvider() {
4003+
return userProvider;
4004+
}
40254005
}

0 commit comments

Comments
 (0)