Implement RoundRobin logic in RoundRobinInetAddressResolver#resolveAll
Motivation: Now the ```resolveAll``` method of RoundRobinInetAddressResolver returns results without any rotation and shuffling. As a result, it doesn't force any round-robin for clients that get a result of ```resolveAll``` and use addresses from the result one by one for a connection establishing until success. This commit implements round-robin in RoundRobinInetAddressResolver#resolveAll. These improvements inspired by the discussion here: https://github.com/AsyncHttpClient/async-http-client/issues/1285 Modifications: Rotate collection from internal ```resolveAll``` call by index, which is incremented every call to RoundRobinInetAddressResolver#resolveAll method. Random replaced by an incrementing counter, which makes code cheaper and guarantees predictable address order in tests. Result: Improved ```RoundRobinInetAddressResolver``` is compatible with clients that use ```resolveAll``` result.
This commit is contained in:
parent
b4e5965424
commit
5eebe9a06c
@ -19,13 +19,15 @@ import io.netty.util.concurrent.EventExecutor;
|
|||||||
import io.netty.util.concurrent.Future;
|
import io.netty.util.concurrent.Future;
|
||||||
import io.netty.util.concurrent.FutureListener;
|
import io.netty.util.concurrent.FutureListener;
|
||||||
import io.netty.util.concurrent.Promise;
|
import io.netty.util.concurrent.Promise;
|
||||||
import io.netty.util.internal.ThreadLocalRandom;
|
|
||||||
import io.netty.util.internal.UnstableApi;
|
import io.netty.util.internal.UnstableApi;
|
||||||
|
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A {@link NameResolver} that resolves {@link InetAddress} and force Round Robin by choosing a single address
|
* A {@link NameResolver} that resolves {@link InetAddress} and force Round Robin by choosing a single address
|
||||||
@ -36,6 +38,7 @@ import java.util.List;
|
|||||||
@UnstableApi
|
@UnstableApi
|
||||||
public class RoundRobinInetAddressResolver extends InetNameResolver {
|
public class RoundRobinInetAddressResolver extends InetNameResolver {
|
||||||
private final NameResolver<InetAddress> nameResolver;
|
private final NameResolver<InetAddress> nameResolver;
|
||||||
|
private final AtomicInteger index = new AtomicInteger();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param executor the {@link EventExecutor} which is used to notify the listeners of the {@link Future} returned by
|
* @param executor the {@link EventExecutor} which is used to notify the listeners of the {@link Future} returned by
|
||||||
@ -52,19 +55,18 @@ public class RoundRobinInetAddressResolver extends InetNameResolver {
|
|||||||
// hijack the doResolve request, but do a doResolveAll request under the hood.
|
// hijack the doResolve request, but do a doResolveAll request under the hood.
|
||||||
// Note that InetSocketAddress.getHostName() will never incur a reverse lookup here,
|
// Note that InetSocketAddress.getHostName() will never incur a reverse lookup here,
|
||||||
// because an unresolved address always has a host name.
|
// because an unresolved address always has a host name.
|
||||||
resolveAll(inetHost).addListener(new FutureListener<List<InetAddress>>() {
|
nameResolver.resolveAll(inetHost).addListener(new FutureListener<List<InetAddress>>() {
|
||||||
@Override
|
@Override
|
||||||
public void operationComplete(Future<List<InetAddress>> future) throws Exception {
|
public void operationComplete(Future<List<InetAddress>> future) throws Exception {
|
||||||
if (future.isSuccess()) {
|
if (future.isSuccess()) {
|
||||||
List<InetAddress> inetAddresses = future.getNow();
|
List<InetAddress> inetAddresses = future.getNow();
|
||||||
int numAddresses = inetAddresses.size();
|
int numAddresses = inetAddresses.size();
|
||||||
if (numAddresses == 0) {
|
if (numAddresses > 0) {
|
||||||
promise.setFailure(new UnknownHostException(inetHost));
|
// if there are multiple addresses: we shall pick one by one
|
||||||
|
// to support the round robin distribution
|
||||||
|
promise.setSuccess(inetAddresses.get(index.getAndIncrement() % numAddresses));
|
||||||
} else {
|
} else {
|
||||||
// if there are multiple addresses: we shall pick one at random
|
promise.setFailure(new UnknownHostException(inetHost));
|
||||||
// this is to support the round robin distribution
|
|
||||||
promise.setSuccess(inetAddresses.get(
|
|
||||||
numAddresses == 1 ? 0 : ThreadLocalRandom.current().nextInt(numAddresses)));
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
promise.setFailure(future.cause());
|
promise.setFailure(future.cause());
|
||||||
@ -74,7 +76,25 @@ public class RoundRobinInetAddressResolver extends InetNameResolver {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void doResolveAll(String inetHost, Promise<List<InetAddress>> promise) throws Exception {
|
protected void doResolveAll(String inetHost, final Promise<List<InetAddress>> promise) throws Exception {
|
||||||
nameResolver.resolveAll(inetHost, promise);
|
nameResolver.resolveAll(inetHost).addListener(new FutureListener<List<InetAddress>>() {
|
||||||
|
@Override
|
||||||
|
public void operationComplete(Future<List<InetAddress>> future) throws Exception {
|
||||||
|
if (future.isSuccess()) {
|
||||||
|
List<InetAddress> inetAddresses = future.getNow();
|
||||||
|
if (!inetAddresses.isEmpty()) {
|
||||||
|
// create a copy to make sure that it's modifiable random access collection
|
||||||
|
List<InetAddress> result = new ArrayList<InetAddress>(inetAddresses);
|
||||||
|
// rotate by different distance each time to force round robin distribution
|
||||||
|
Collections.rotate(result, index.getAndIncrement());
|
||||||
|
promise.setSuccess(result);
|
||||||
|
} else {
|
||||||
|
promise.setSuccess(inetAddresses);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
promise.setFailure(future.cause());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user