Skip to content
This repository has been archived by the owner on May 10, 2022. It is now read-only.

Commit

Permalink
fix: fix block bug of InetAddressValidator.getInstance() (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
foreverneverer committed Nov 7, 2019
1 parent 9e92abe commit a9ddb5f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-validator</groupId>
<artifactId>commons-validator</artifactId>
<version>1.6</version>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>28.1-jre</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
*/
public class HostNameResolver {

private static final org.slf4j.Logger logger =
org.slf4j.LoggerFactory.getLogger(HostNameResolver.class);

public rpc_address[] resolve(String hostPort) throws IllegalArgumentException {
String[] pairs = hostPort.split(":");
if (pairs.length != 2) {
Expand All @@ -23,13 +26,15 @@ public rpc_address[] resolve(String hostPort) throws IllegalArgumentException {

try {
Integer port = Integer.valueOf(pairs[1]);
logger.info("start to resolve hostname {} into ip addresses", pairs[0]);
InetAddress[] resolvedAddresses = InetAddress.getAllByName(pairs[0]);
rpc_address[] results = new rpc_address[resolvedAddresses.length];
int size = 0;
for (InetAddress addr : resolvedAddresses) {
rpc_address rpcAddr = new rpc_address();
int ip = ByteBuffer.wrap(addr.getAddress()).order(ByteOrder.BIG_ENDIAN).getInt();
rpcAddr.address = ((long) ip << 32) + ((long) port << 16) + 1;
logger.info("resolved ip address {} from host {}", rpcAddr, pairs[0]);
results[size++] = rpcAddr;
}
return results;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// can be found in the LICENSE file in the root directory of this source tree.
package com.xiaomi.infra.pegasus.rpc.async;

import com.google.common.net.InetAddresses;
import com.xiaomi.infra.pegasus.base.error_code.error_types;
import com.xiaomi.infra.pegasus.base.rpc_address;
import com.xiaomi.infra.pegasus.operator.client_operator;
Expand All @@ -14,7 +15,6 @@
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
import org.apache.commons.validator.routines.InetAddressValidator;

public class MetaSession extends HostNameResolver {
public MetaSession(
Expand All @@ -27,7 +27,7 @@ public MetaSession(
clusterManager = manager;
metaList = new ArrayList<ReplicaSession>();

if (addrList.length == 1 && !InetAddressValidator.getInstance().isValid(addrList[0])) {
if (addrList.length == 1 && !InetAddresses.isInetAddress(addrList[0])) {
// if the given string is not a valid ip address,
// then take it as a hostname for a try.
resolveHost(addrList[0]);
Expand Down

0 comments on commit a9ddb5f

Please sign in to comment.