Skip to content

Commit c4dc0eb

Browse files
sbernauerapurtell
andcommitted
HBASE-25292 [branch-2.4] Improve InetSocketAddress usage discipline
Co-authored-by: Andrew Purtell <[email protected]> (cherry picked from commit f813479)
1 parent 62f2383 commit c4dc0eb

File tree

25 files changed

+266
-273
lines changed

25 files changed

+266
-273
lines changed

hbase-client/src/main/java/org/apache/hadoop/hbase/client/AsyncConnectionImpl.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,6 @@ class AsyncConnectionImpl implements AsyncConnection {
7777
.setUncaughtExceptionHandler(Threads.LOGGING_EXCEPTION_HANDLER).build(),
7878
10, TimeUnit.MILLISECONDS);
7979

80-
private static final String RESOLVE_HOSTNAME_ON_FAIL_KEY = "hbase.resolve.hostnames.on.failure";
81-
8280
private final Configuration conf;
8381

8482
final AsyncConnectionConfiguration connConf;
@@ -93,8 +91,6 @@ class AsyncConnectionImpl implements AsyncConnection {
9391

9492
final RpcControllerFactory rpcControllerFactory;
9593

96-
private final boolean hostnameCanChange;
97-
9894
private final AsyncRegionLocator locator;
9995

10096
final AsyncRpcRetryingCallerFactory callerFactory;
@@ -137,7 +133,6 @@ public AsyncConnectionImpl(Configuration conf, ConnectionRegistry registry, Stri
137133
}
138134
this.rpcClient = RpcClientFactory.createClient(conf, clusterId, metrics.orElse(null));
139135
this.rpcControllerFactory = RpcControllerFactory.instantiate(conf);
140-
this.hostnameCanChange = conf.getBoolean(RESOLVE_HOSTNAME_ON_FAIL_KEY, true);
141136
this.rpcTimeout =
142137
(int) Math.min(Integer.MAX_VALUE, TimeUnit.NANOSECONDS.toMillis(connConf.getRpcTimeoutNs()));
143138
this.locator = new AsyncRegionLocator(this, RETRY_TIMER);
@@ -258,7 +253,7 @@ private ClientService.Interface createRegionServerStub(ServerName serverName) th
258253

259254
ClientService.Interface getRegionServerStub(ServerName serverName) throws IOException {
260255
return ConcurrentMapUtils.computeIfAbsentEx(rsStubs,
261-
getStubKey(ClientService.getDescriptor().getName(), serverName, hostnameCanChange),
256+
getStubKey(ClientService.getDescriptor().getName(), serverName),
262257
() -> createRegionServerStub(serverName));
263258
}
264259

@@ -272,7 +267,7 @@ private AdminService.Interface createAdminServerStub(ServerName serverName) thro
272267

273268
AdminService.Interface getAdminStub(ServerName serverName) throws IOException {
274269
return ConcurrentMapUtils.computeIfAbsentEx(adminSubs,
275-
getStubKey(AdminService.getDescriptor().getName(), serverName, hostnameCanChange),
270+
getStubKey(AdminService.getDescriptor().getName(), serverName),
276271
() -> createAdminServerStub(serverName));
277272
}
278273

hbase-client/src/main/java/org/apache/hadoop/hbase/client/ConnectionImplementation.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,6 @@ class ConnectionImplementation implements ClusterConnection, Closeable {
165165
"hbase.client.master.state.cache.timeout.sec";
166166
private static final Logger LOG = LoggerFactory.getLogger(ConnectionImplementation.class);
167167

168-
private static final String RESOLVE_HOSTNAME_ON_FAIL_KEY = "hbase.resolve.hostnames.on.failure";
169-
170-
private final boolean hostnamesCanChange;
171168
private final long pause;
172169
private final long pauseForCQTBE;// pause for CallQueueTooBigException, if specified
173170
// The mode tells if HedgedRead, LoadBalance mode is supported.
@@ -308,7 +305,6 @@ class ConnectionImplementation implements ClusterConnection, Closeable {
308305

309306
boolean shouldListen =
310307
conf.getBoolean(HConstants.STATUS_PUBLISHED, HConstants.STATUS_PUBLISHED_DEFAULT);
311-
this.hostnamesCanChange = conf.getBoolean(RESOLVE_HOSTNAME_ON_FAIL_KEY, true);
312308
Class<? extends ClusterStatusListener.Listener> listenerClass =
313309
conf.getClass(ClusterStatusListener.STATUS_LISTENER_CLASS,
314310
ClusterStatusListener.DEFAULT_STATUS_LISTENER_CLASS, ClusterStatusListener.Listener.class);
@@ -511,8 +507,8 @@ public Hbck getHbck(ServerName masterServer) throws IOException {
511507
if (isDeadServer(masterServer)) {
512508
throw new RegionServerStoppedException(masterServer + " is dead.");
513509
}
514-
String key = getStubKey(MasterProtos.HbckService.BlockingInterface.class.getName(),
515-
masterServer, this.hostnamesCanChange);
510+
String key =
511+
getStubKey(MasterProtos.HbckService.BlockingInterface.class.getName(), masterServer);
516512

517513
return new HBaseHbck(
518514
(MasterProtos.HbckService.BlockingInterface) computeIfAbsentEx(stubs, key, () -> {
@@ -1306,8 +1302,7 @@ private MasterProtos.MasterService.BlockingInterface makeStubNoRetries()
13061302
throw new MasterNotRunningException(sn + " is dead.");
13071303
}
13081304
// Use the security info interface name as our stub key
1309-
String key =
1310-
getStubKey(MasterProtos.MasterService.getDescriptor().getName(), sn, hostnamesCanChange);
1305+
String key = getStubKey(MasterProtos.MasterService.getDescriptor().getName(), sn);
13111306
MasterProtos.MasterService.BlockingInterface stub =
13121307
(MasterProtos.MasterService.BlockingInterface) computeIfAbsentEx(stubs, key, () -> {
13131308
BlockingRpcChannel channel = rpcClient.createBlockingRpcChannel(sn, user, rpcTimeout);
@@ -1355,8 +1350,7 @@ public AdminProtos.AdminService.BlockingInterface getAdmin(ServerName serverName
13551350
if (isDeadServer(serverName)) {
13561351
throw new RegionServerStoppedException(serverName + " is dead.");
13571352
}
1358-
String key = getStubKey(AdminProtos.AdminService.BlockingInterface.class.getName(), serverName,
1359-
this.hostnamesCanChange);
1353+
String key = getStubKey(AdminProtos.AdminService.BlockingInterface.class.getName(), serverName);
13601354
return (AdminProtos.AdminService.BlockingInterface) computeIfAbsentEx(stubs, key, () -> {
13611355
BlockingRpcChannel channel =
13621356
this.rpcClient.createBlockingRpcChannel(serverName, user, rpcTimeout);
@@ -1370,8 +1364,8 @@ public BlockingInterface getClient(ServerName serverName) throws IOException {
13701364
if (isDeadServer(serverName)) {
13711365
throw new RegionServerStoppedException(serverName + " is dead.");
13721366
}
1373-
String key = getStubKey(ClientProtos.ClientService.BlockingInterface.class.getName(),
1374-
serverName, this.hostnamesCanChange);
1367+
String key =
1368+
getStubKey(ClientProtos.ClientService.BlockingInterface.class.getName(), serverName);
13751369
return (ClientProtos.ClientService.BlockingInterface) computeIfAbsentEx(stubs, key, () -> {
13761370
BlockingRpcChannel channel =
13771371
this.rpcClient.createBlockingRpcChannel(serverName, user, rpcTimeout);

hbase-client/src/main/java/org/apache/hadoop/hbase/client/ConnectionUtils.java

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
import java.io.IOException;
2626
import java.lang.reflect.UndeclaredThrowableException;
27-
import java.net.InetAddress;
2827
import java.net.UnknownHostException;
2928
import java.util.Arrays;
3029
import java.util.List;
@@ -152,32 +151,17 @@ public boolean isTableDisabled(TableName tableName) throws IOException {
152151
}
153152

154153
/**
155-
* Return retires + 1. The returned value will be in range [1, Integer.MAX_VALUE].
154+
* Get a unique key for the rpc stub to the given server.
156155
*/
157-
static int retries2Attempts(int retries) {
158-
return Math.max(1, retries == Integer.MAX_VALUE ? Integer.MAX_VALUE : retries + 1);
156+
static String getStubKey(String serviceName, ServerName serverName) {
157+
return String.format("%s@%s", serviceName, serverName);
159158
}
160159

161160
/**
162-
* Get a unique key for the rpc stub to the given server.
161+
* Return retires + 1. The returned value will be in range [1, Integer.MAX_VALUE].
163162
*/
164-
static String getStubKey(String serviceName, ServerName serverName, boolean hostnameCanChange) {
165-
// Sometimes, servers go down and they come back up with the same hostname but a different
166-
// IP address. Force a resolution of the rsHostname by trying to instantiate an
167-
// InetSocketAddress, and this way we will rightfully get a new stubKey.
168-
// Also, include the hostname in the key so as to take care of those cases where the
169-
// DNS name is different but IP address remains the same.
170-
String hostname = serverName.getHostname();
171-
int port = serverName.getPort();
172-
if (hostnameCanChange) {
173-
try {
174-
InetAddress ip = InetAddress.getByName(hostname);
175-
return serviceName + "@" + hostname + "-" + ip.getHostAddress() + ":" + port;
176-
} catch (UnknownHostException e) {
177-
LOG.warn("Can not resolve " + hostname + ", please check your network", e);
178-
}
179-
}
180-
return serviceName + "@" + hostname + ":" + port;
163+
static int retries2Attempts(int retries) {
164+
return Math.max(1, retries == Integer.MAX_VALUE ? Integer.MAX_VALUE : retries + 1);
181165
}
182166

183167
static void checkHasFamilies(Mutation mutation) {

hbase-client/src/main/java/org/apache/hadoop/hbase/client/MetricsConnection.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ public class MetricsConnection implements StatisticTrackable {
6969
private static final String HEAP_BASE = "heapOccupancy_";
7070
private static final String CACHE_BASE = "cacheDroppingExceptions_";
7171
private static final String UNKNOWN_EXCEPTION = "UnknownException";
72+
private static final String NS_LOOKUPS = "nsLookups";
73+
private static final String NS_LOOKUPS_FAILED = "nsLookupsFailed";
7274
private static final String CLIENT_SVC = ClientService.getDescriptor().getName();
7375

7476
/** A container class for collecting details about the RPC call as it percolates. */
@@ -293,6 +295,8 @@ public Counter newMetric(Class<?> clazz, String name, String scope) {
293295
protected final Timer userRegionLockWaitingTimer;
294296
protected final Timer userRegionLockHeldTimer;
295297
protected final Histogram userRegionLockQueueHist;
298+
protected final Counter nsLookups;
299+
protected final Counter nsLookupsFailed;
296300

297301
// dynamic metrics
298302

@@ -360,6 +364,8 @@ protected Ratio getRatio() {
360364
registry.timer(name(this.getClass(), "userRegionLockHeldDuration", scope));
361365
this.userRegionLockQueueHist =
362366
registry.histogram(name(MetricsConnection.class, "userRegionLockQueueLength", scope));
367+
this.nsLookups = registry.counter(name(this.getClass(), NS_LOOKUPS, scope));
368+
this.nsLookupsFailed = registry.counter(name(this.getClass(), NS_LOOKUPS_FAILED, scope));
363369

364370
this.reporter = JmxReporter.forRegistry(this.registry).build();
365371
this.reporter.start();
@@ -564,4 +570,12 @@ public void incrCacheDroppingExceptions(Object exception) {
564570
CACHE_BASE + (exception == null ? UNKNOWN_EXCEPTION : exception.getClass().getSimpleName()),
565571
cacheDroppingExceptions, counterFactory).inc();
566572
}
573+
574+
public void incrNsLookups() {
575+
this.nsLookups.inc();
576+
}
577+
578+
public void incrNsLookupsFailed() {
579+
this.nsLookupsFailed.inc();
580+
}
567581
}

0 commit comments

Comments
 (0)