mirror of https://github.com/grpc/grpc-java.git
core: emit lists of lists from NameResolver
This commit is contained in:
parent
03d9450968
commit
a05ab57561
|
|
@ -42,6 +42,8 @@ import java.net.InetSocketAddress;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.ScheduledExecutorService;
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
import java.util.concurrent.ScheduledFuture;
|
import java.util.concurrent.ScheduledFuture;
|
||||||
|
|
@ -154,14 +156,15 @@ class DnsNameResolver extends NameResolver {
|
||||||
savedListener.onError(Status.UNAVAILABLE.withCause(e));
|
savedListener.onError(Status.UNAVAILABLE.withCause(e));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
ArrayList<ResolvedServerInfo> servers =
|
List<ResolvedServerInfo> servers =
|
||||||
new ArrayList<ResolvedServerInfo>(inetAddrs.length);
|
new ArrayList<ResolvedServerInfo>(inetAddrs.length);
|
||||||
for (int i = 0; i < inetAddrs.length; i++) {
|
for (int i = 0; i < inetAddrs.length; i++) {
|
||||||
InetAddress inetAddr = inetAddrs[i];
|
InetAddress inetAddr = inetAddrs[i];
|
||||||
servers.add(
|
servers.add(
|
||||||
new ResolvedServerInfo(new InetSocketAddress(inetAddr, port), Attributes.EMPTY));
|
new ResolvedServerInfo(new InetSocketAddress(inetAddr, port), Attributes.EMPTY));
|
||||||
}
|
}
|
||||||
savedListener.onUpdate(servers, Attributes.EMPTY);
|
savedListener.onUpdate(
|
||||||
|
Collections.singletonList(servers), Attributes.EMPTY);
|
||||||
} finally {
|
} finally {
|
||||||
synchronized (DnsNameResolver.this) {
|
synchronized (DnsNameResolver.this) {
|
||||||
resolving = false;
|
resolving = false;
|
||||||
|
|
|
||||||
|
|
@ -42,30 +42,30 @@ import java.util.List;
|
||||||
import javax.annotation.concurrent.GuardedBy;
|
import javax.annotation.concurrent.GuardedBy;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A {@link LoadBalancer} that provides simple round-robin and pick-first routing mechanism over the
|
* A {@link LoadBalancer} that provides no load balancing mechanism over the
|
||||||
* addresses from the {@link NameResolver}.
|
* addresses from the {@link NameResolver}. The channel's default behavior
|
||||||
|
* (currently pick-first) is used for all addresses found.
|
||||||
*/
|
*/
|
||||||
// TODO(zhangkun83): Only pick-first is implemented. We need to implement round-robin.
|
|
||||||
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/1771")
|
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/1771")
|
||||||
public final class SimpleLoadBalancerFactory extends LoadBalancer.Factory {
|
public final class DummyLoadBalancerFactory extends LoadBalancer.Factory {
|
||||||
|
|
||||||
private static final SimpleLoadBalancerFactory instance = new SimpleLoadBalancerFactory();
|
private static final DummyLoadBalancerFactory instance = new DummyLoadBalancerFactory();
|
||||||
|
|
||||||
private SimpleLoadBalancerFactory() {
|
private DummyLoadBalancerFactory() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static SimpleLoadBalancerFactory getInstance() {
|
public static DummyLoadBalancerFactory getInstance() {
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T> LoadBalancer<T> newLoadBalancer(String serviceName, TransportManager<T> tm) {
|
public <T> LoadBalancer<T> newLoadBalancer(String serviceName, TransportManager<T> tm) {
|
||||||
return new SimpleLoadBalancer<T>(tm);
|
return new DummyLoadBalancer<T>(tm);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class SimpleLoadBalancer<T> extends LoadBalancer<T> {
|
private static class DummyLoadBalancer<T> extends LoadBalancer<T> {
|
||||||
private static final Status SHUTDOWN_STATUS =
|
private static final Status SHUTDOWN_STATUS =
|
||||||
Status.UNAVAILABLE.augmentDescription("SimpleLoadBalancer has shut down");
|
Status.UNAVAILABLE.augmentDescription("DummyLoadBalancer has shut down");
|
||||||
|
|
||||||
private final Object lock = new Object();
|
private final Object lock = new Object();
|
||||||
|
|
||||||
|
|
@ -80,7 +80,7 @@ public final class SimpleLoadBalancerFactory extends LoadBalancer.Factory {
|
||||||
|
|
||||||
private final TransportManager<T> tm;
|
private final TransportManager<T> tm;
|
||||||
|
|
||||||
private SimpleLoadBalancer(TransportManager<T> tm) {
|
private DummyLoadBalancer(TransportManager<T> tm) {
|
||||||
this.tm = tm;
|
this.tm = tm;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -107,18 +107,19 @@ public final class SimpleLoadBalancerFactory extends LoadBalancer.Factory {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handleResolvedAddresses(
|
public void handleResolvedAddresses(
|
||||||
List<ResolvedServerInfo> updatedServers, Attributes config) {
|
List<? extends List<ResolvedServerInfo>> updatedServers, Attributes config) {
|
||||||
InterimTransport<T> savedInterimTransport;
|
InterimTransport<T> savedInterimTransport;
|
||||||
final EquivalentAddressGroup newAddresses;
|
final EquivalentAddressGroup newAddresses;
|
||||||
synchronized (lock) {
|
synchronized (lock) {
|
||||||
if (closed) {
|
if (closed) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
ArrayList<SocketAddress> newAddressList =
|
ArrayList<SocketAddress> newAddressList = new ArrayList<SocketAddress>();
|
||||||
new ArrayList<SocketAddress>(updatedServers.size());
|
for (List<ResolvedServerInfo> servers : updatedServers) {
|
||||||
for (ResolvedServerInfo server : updatedServers) {
|
for (ResolvedServerInfo server : servers) {
|
||||||
newAddressList.add(server.getAddress());
|
newAddressList.add(server.getAddress());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
newAddresses = new EquivalentAddressGroup(newAddressList);
|
newAddresses = new EquivalentAddressGroup(newAddressList);
|
||||||
if (newAddresses.equals(addresses)) {
|
if (newAddresses.equals(addresses)) {
|
||||||
return;
|
return;
|
||||||
|
|
@ -66,14 +66,17 @@ public abstract class LoadBalancer<T> {
|
||||||
public void shutdown() { }
|
public void shutdown() { }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handles newly resolved addresses and service config from name resolution system.
|
* Handles newly resolved addresses and service config from name resolution system. Sublists
|
||||||
|
* should be considered equivalent with an {@link EquivalentAddressGroup}, but may be flattened
|
||||||
|
* into a single list if needed.
|
||||||
*
|
*
|
||||||
* <p>Implementations should not modify the given {@code servers}.
|
* <p>Implementations should not modify the given {@code servers}.
|
||||||
*
|
*
|
||||||
* @param servers the resolved server addresses. Never empty.
|
* @param servers the resolved server addresses, never empty.
|
||||||
* @param config extra configuration data from naming system.
|
* @param config extra configuration data from naming system.
|
||||||
*/
|
*/
|
||||||
public void handleResolvedAddresses(List<ResolvedServerInfo> servers, Attributes config) { }
|
public void handleResolvedAddresses(List<? extends List<ResolvedServerInfo>> servers,
|
||||||
|
Attributes config) { }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handles an error from the name resolution system.
|
* Handles an error from the name resolution system.
|
||||||
|
|
|
||||||
|
|
@ -157,8 +157,8 @@ public abstract class ManagedChannelBuilder<T extends ManagedChannelBuilder<T>>
|
||||||
/**
|
/**
|
||||||
* Provides a custom {@link LoadBalancer.Factory} for the channel.
|
* Provides a custom {@link LoadBalancer.Factory} for the channel.
|
||||||
*
|
*
|
||||||
* <p>If this method is not called, the builder will use {@link SimpleLoadBalancerFactory} for the
|
* <p>If this method is not called, the builder will use {@link DummyLoadBalancerFactory}
|
||||||
* channel.
|
* for the channel.
|
||||||
*/
|
*/
|
||||||
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/1771")
|
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/1771")
|
||||||
public abstract T loadBalancerFactory(LoadBalancer.Factory loadBalancerFactory);
|
public abstract T loadBalancerFactory(LoadBalancer.Factory loadBalancerFactory);
|
||||||
|
|
|
||||||
|
|
@ -120,10 +120,12 @@ public abstract class NameResolver {
|
||||||
*
|
*
|
||||||
* <p>Implementations will not modify the given {@code servers}.
|
* <p>Implementations will not modify the given {@code servers}.
|
||||||
*
|
*
|
||||||
* @param servers the resolved server addresses. An empty list will trigger {@link #onError}
|
* @param servers the resolved server addresses. Sublists should be considered to be
|
||||||
|
* an {@link EquivalentAddressGroup}. An empty list or all sublists being empty
|
||||||
|
* will trigger {@link #onError}
|
||||||
* @param config extra configuration data from naming system
|
* @param config extra configuration data from naming system
|
||||||
*/
|
*/
|
||||||
void onUpdate(List<ResolvedServerInfo> servers, Attributes config);
|
void onUpdate(List<? extends List<ResolvedServerInfo>> servers, Attributes config);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handles an error from the resolver.
|
* Handles an error from the resolver.
|
||||||
|
|
|
||||||
|
|
@ -41,12 +41,12 @@ import io.grpc.Attributes;
|
||||||
import io.grpc.ClientInterceptor;
|
import io.grpc.ClientInterceptor;
|
||||||
import io.grpc.CompressorRegistry;
|
import io.grpc.CompressorRegistry;
|
||||||
import io.grpc.DecompressorRegistry;
|
import io.grpc.DecompressorRegistry;
|
||||||
|
import io.grpc.DummyLoadBalancerFactory;
|
||||||
import io.grpc.LoadBalancer;
|
import io.grpc.LoadBalancer;
|
||||||
import io.grpc.ManagedChannelBuilder;
|
import io.grpc.ManagedChannelBuilder;
|
||||||
import io.grpc.NameResolver;
|
import io.grpc.NameResolver;
|
||||||
import io.grpc.NameResolverRegistry;
|
import io.grpc.NameResolverRegistry;
|
||||||
import io.grpc.ResolvedServerInfo;
|
import io.grpc.ResolvedServerInfo;
|
||||||
import io.grpc.SimpleLoadBalancerFactory;
|
|
||||||
|
|
||||||
import java.net.SocketAddress;
|
import java.net.SocketAddress;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
|
|
@ -213,7 +213,7 @@ public abstract class AbstractManagedChannelImplBuilder
|
||||||
new ExponentialBackoffPolicy.Provider(),
|
new ExponentialBackoffPolicy.Provider(),
|
||||||
firstNonNull(nameResolverFactory, NameResolverRegistry.getDefaultRegistry()),
|
firstNonNull(nameResolverFactory, NameResolverRegistry.getDefaultRegistry()),
|
||||||
getNameResolverParams(),
|
getNameResolverParams(),
|
||||||
firstNonNull(loadBalancerFactory, SimpleLoadBalancerFactory.getInstance()),
|
firstNonNull(loadBalancerFactory, DummyLoadBalancerFactory.getInstance()),
|
||||||
transportFactory,
|
transportFactory,
|
||||||
firstNonNull(decompressorRegistry, DecompressorRegistry.getDefaultInstance()),
|
firstNonNull(decompressorRegistry, DecompressorRegistry.getDefaultInstance()),
|
||||||
firstNonNull(compressorRegistry, CompressorRegistry.getDefaultInstance()),
|
firstNonNull(compressorRegistry, CompressorRegistry.getDefaultInstance()),
|
||||||
|
|
@ -279,7 +279,8 @@ public abstract class AbstractManagedChannelImplBuilder
|
||||||
@Override
|
@Override
|
||||||
public void start(final Listener listener) {
|
public void start(final Listener listener) {
|
||||||
listener.onUpdate(
|
listener.onUpdate(
|
||||||
Collections.singletonList(new ResolvedServerInfo(address, Attributes.EMPTY)),
|
Collections.singletonList(
|
||||||
|
Collections.singletonList(new ResolvedServerInfo(address, Attributes.EMPTY))),
|
||||||
Attributes.EMPTY);
|
Attributes.EMPTY);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -175,8 +175,8 @@ public final class ManagedChannelImpl extends ManagedChannel implements WithLogI
|
||||||
|
|
||||||
this.nameResolver.start(new NameResolver.Listener() {
|
this.nameResolver.start(new NameResolver.Listener() {
|
||||||
@Override
|
@Override
|
||||||
public void onUpdate(List<ResolvedServerInfo> servers, Attributes config) {
|
public void onUpdate(List<? extends List<ResolvedServerInfo>> servers, Attributes config) {
|
||||||
if (servers.isEmpty()) {
|
if (serversAreEmpty(servers)) {
|
||||||
onError(Status.UNAVAILABLE.withDescription("NameResolver returned an empty list"));
|
onError(Status.UNAVAILABLE.withDescription("NameResolver returned an empty list"));
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
|
|
@ -201,6 +201,16 @@ public final class ManagedChannelImpl extends ManagedChannel implements WithLogI
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static boolean serversAreEmpty(List<? extends List<ResolvedServerInfo>> servers) {
|
||||||
|
for (List<ResolvedServerInfo> serverInfos : servers) {
|
||||||
|
if (!serverInfos.isEmpty()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
static NameResolver getNameResolver(String target, NameResolver.Factory nameResolverFactory,
|
static NameResolver getNameResolver(String target, NameResolver.Factory nameResolverFactory,
|
||||||
Attributes nameResolverParams) {
|
Attributes nameResolverParams) {
|
||||||
|
|
|
||||||
|
|
@ -40,6 +40,8 @@ import static org.mockito.Mockito.times;
|
||||||
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.verify;
|
||||||
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||||
|
|
||||||
|
import com.google.common.collect.Iterables;
|
||||||
|
|
||||||
import io.grpc.internal.FakeClock;
|
import io.grpc.internal.FakeClock;
|
||||||
import io.grpc.internal.SharedResourceHolder.Resource;
|
import io.grpc.internal.SharedResourceHolder.Resource;
|
||||||
|
|
||||||
|
|
@ -102,7 +104,7 @@ public class DnsNameResolverTest {
|
||||||
@Mock
|
@Mock
|
||||||
private NameResolver.Listener mockListener;
|
private NameResolver.Listener mockListener;
|
||||||
@Captor
|
@Captor
|
||||||
private ArgumentCaptor<List<ResolvedServerInfo>> resultCaptor;
|
private ArgumentCaptor<List<List<ResolvedServerInfo>>> resultCaptor;
|
||||||
@Captor
|
@Captor
|
||||||
private ArgumentCaptor<Status> statusCaptor;
|
private ArgumentCaptor<Status> statusCaptor;
|
||||||
|
|
||||||
|
|
@ -149,14 +151,14 @@ public class DnsNameResolverTest {
|
||||||
assertEquals(1, fakeExecutor.runDueTasks());
|
assertEquals(1, fakeExecutor.runDueTasks());
|
||||||
verify(mockListener).onUpdate(resultCaptor.capture(), any(Attributes.class));
|
verify(mockListener).onUpdate(resultCaptor.capture(), any(Attributes.class));
|
||||||
assertEquals(name, resolver.invocations.poll());
|
assertEquals(name, resolver.invocations.poll());
|
||||||
assertAnswerMatches(answer1, 81, resultCaptor.getValue());
|
assertAnswerMatches(answer1, 81, Iterables.getOnlyElement(resultCaptor.getValue()));
|
||||||
assertEquals(0, fakeClock.numPendingTasks());
|
assertEquals(0, fakeClock.numPendingTasks());
|
||||||
|
|
||||||
resolver.refresh();
|
resolver.refresh();
|
||||||
assertEquals(1, fakeExecutor.runDueTasks());
|
assertEquals(1, fakeExecutor.runDueTasks());
|
||||||
verify(mockListener, times(2)).onUpdate(resultCaptor.capture(), any(Attributes.class));
|
verify(mockListener, times(2)).onUpdate(resultCaptor.capture(), any(Attributes.class));
|
||||||
assertEquals(name, resolver.invocations.poll());
|
assertEquals(name, resolver.invocations.poll());
|
||||||
assertAnswerMatches(answer2, 81, resultCaptor.getValue());
|
assertAnswerMatches(answer2, 81, Iterables.getOnlyElement(resultCaptor.getValue()));
|
||||||
assertEquals(0, fakeClock.numPendingTasks());
|
assertEquals(0, fakeClock.numPendingTasks());
|
||||||
|
|
||||||
resolver.shutdown();
|
resolver.shutdown();
|
||||||
|
|
@ -201,7 +203,7 @@ public class DnsNameResolverTest {
|
||||||
assertEquals(1, fakeExecutor.runDueTasks());
|
assertEquals(1, fakeExecutor.runDueTasks());
|
||||||
verify(mockListener).onUpdate(resultCaptor.capture(), any(Attributes.class));
|
verify(mockListener).onUpdate(resultCaptor.capture(), any(Attributes.class));
|
||||||
assertEquals(name, resolver.invocations.poll());
|
assertEquals(name, resolver.invocations.poll());
|
||||||
assertAnswerMatches(answer, 81, resultCaptor.getValue());
|
assertAnswerMatches(answer, 81, Iterables.getOnlyElement(resultCaptor.getValue()));
|
||||||
|
|
||||||
verifyNoMoreInteractions(mockListener);
|
verifyNoMoreInteractions(mockListener);
|
||||||
}
|
}
|
||||||
|
|
@ -229,7 +231,7 @@ public class DnsNameResolverTest {
|
||||||
assertEquals(0, fakeClock.numPendingTasks());
|
assertEquals(0, fakeClock.numPendingTasks());
|
||||||
verify(mockListener).onUpdate(resultCaptor.capture(), any(Attributes.class));
|
verify(mockListener).onUpdate(resultCaptor.capture(), any(Attributes.class));
|
||||||
assertEquals(name, resolver.invocations.poll());
|
assertEquals(name, resolver.invocations.poll());
|
||||||
assertAnswerMatches(answer, 81, resultCaptor.getValue());
|
assertAnswerMatches(answer, 81, Iterables.getOnlyElement(resultCaptor.getValue()));
|
||||||
|
|
||||||
verifyNoMoreInteractions(mockListener);
|
verifyNoMoreInteractions(mockListener);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -55,13 +55,14 @@ import org.mockito.MockitoAnnotations;
|
||||||
|
|
||||||
import java.net.SocketAddress;
|
import java.net.SocketAddress;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/** Unit test for {@link SimpleLoadBalancerFactory}. */
|
/** Unit test for {@link DummyLoadBalancerFactory}. */
|
||||||
@RunWith(JUnit4.class)
|
@RunWith(JUnit4.class)
|
||||||
public class SimpleLoadBalancerTest {
|
public class DummyLoadBalancerTest {
|
||||||
private LoadBalancer<Transport> loadBalancer;
|
private LoadBalancer<Transport> loadBalancer;
|
||||||
|
|
||||||
private ArrayList<ResolvedServerInfo> servers;
|
private List<List<ResolvedServerInfo>> servers;
|
||||||
private EquivalentAddressGroup addressGroup;
|
private EquivalentAddressGroup addressGroup;
|
||||||
|
|
||||||
@Mock private TransportManager<Transport> mockTransportManager;
|
@Mock private TransportManager<Transport> mockTransportManager;
|
||||||
|
|
@ -73,13 +74,14 @@ public class SimpleLoadBalancerTest {
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
MockitoAnnotations.initMocks(this);
|
MockitoAnnotations.initMocks(this);
|
||||||
loadBalancer = SimpleLoadBalancerFactory.getInstance().newLoadBalancer(
|
loadBalancer = DummyLoadBalancerFactory.getInstance().newLoadBalancer(
|
||||||
"fakeservice", mockTransportManager);
|
"fakeservice", mockTransportManager);
|
||||||
servers = new ArrayList<ResolvedServerInfo>();
|
servers = new ArrayList<List<ResolvedServerInfo>>();
|
||||||
|
servers.add(new ArrayList<ResolvedServerInfo>());
|
||||||
ArrayList<SocketAddress> addresses = new ArrayList<SocketAddress>();
|
ArrayList<SocketAddress> addresses = new ArrayList<SocketAddress>();
|
||||||
for (int i = 0; i < 3; i++) {
|
for (int i = 0; i < 3; i++) {
|
||||||
SocketAddress addr = new FakeSocketAddress("server" + i);
|
SocketAddress addr = new FakeSocketAddress("server" + i);
|
||||||
servers.add(new ResolvedServerInfo(addr, Attributes.EMPTY));
|
servers.get(0).add(new ResolvedServerInfo(addr, Attributes.EMPTY));
|
||||||
addresses.add(addr);
|
addresses.add(addr);
|
||||||
}
|
}
|
||||||
addressGroup = new EquivalentAddressGroup(addresses);
|
addressGroup = new EquivalentAddressGroup(addresses);
|
||||||
|
|
@ -62,6 +62,7 @@ import io.grpc.ClientInterceptor;
|
||||||
import io.grpc.Compressor;
|
import io.grpc.Compressor;
|
||||||
import io.grpc.CompressorRegistry;
|
import io.grpc.CompressorRegistry;
|
||||||
import io.grpc.DecompressorRegistry;
|
import io.grpc.DecompressorRegistry;
|
||||||
|
import io.grpc.DummyLoadBalancerFactory;
|
||||||
import io.grpc.IntegerMarshaller;
|
import io.grpc.IntegerMarshaller;
|
||||||
import io.grpc.LoadBalancer;
|
import io.grpc.LoadBalancer;
|
||||||
import io.grpc.ManagedChannel;
|
import io.grpc.ManagedChannel;
|
||||||
|
|
@ -69,7 +70,6 @@ import io.grpc.Metadata;
|
||||||
import io.grpc.MethodDescriptor;
|
import io.grpc.MethodDescriptor;
|
||||||
import io.grpc.NameResolver;
|
import io.grpc.NameResolver;
|
||||||
import io.grpc.ResolvedServerInfo;
|
import io.grpc.ResolvedServerInfo;
|
||||||
import io.grpc.SimpleLoadBalancerFactory;
|
|
||||||
import io.grpc.Status;
|
import io.grpc.Status;
|
||||||
import io.grpc.StringMarshaller;
|
import io.grpc.StringMarshaller;
|
||||||
import io.grpc.TransportManager;
|
import io.grpc.TransportManager;
|
||||||
|
|
@ -120,7 +120,7 @@ public class ManagedChannelImplTest {
|
||||||
private final SocketAddress socketAddress = new SocketAddress() {};
|
private final SocketAddress socketAddress = new SocketAddress() {};
|
||||||
private final ResolvedServerInfo server = new ResolvedServerInfo(socketAddress, Attributes.EMPTY);
|
private final ResolvedServerInfo server = new ResolvedServerInfo(socketAddress, Attributes.EMPTY);
|
||||||
private SpyingLoadBalancerFactory loadBalancerFactory =
|
private SpyingLoadBalancerFactory loadBalancerFactory =
|
||||||
new SpyingLoadBalancerFactory(SimpleLoadBalancerFactory.getInstance());
|
new SpyingLoadBalancerFactory(DummyLoadBalancerFactory.getInstance());
|
||||||
|
|
||||||
@Rule public final ExpectedException thrown = ExpectedException.none();
|
@Rule public final ExpectedException thrown = ExpectedException.none();
|
||||||
|
|
||||||
|
|
@ -494,7 +494,7 @@ public class ManagedChannelImplTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void nameResolverReturnsEmptyList() {
|
public void nameResolverReturnsEmptySubLists() {
|
||||||
String errorDescription = "NameResolver returned an empty list";
|
String errorDescription = "NameResolver returned an empty list";
|
||||||
|
|
||||||
// Name resolution is started as soon as channel is created
|
// Name resolution is started as soon as channel is created
|
||||||
|
|
@ -527,7 +527,7 @@ public class ManagedChannelImplTest {
|
||||||
assertEquals(1, loadBalancerFactory.balancers.size());
|
assertEquals(1, loadBalancerFactory.balancers.size());
|
||||||
LoadBalancer<?> loadBalancer = loadBalancerFactory.balancers.get(0);
|
LoadBalancer<?> loadBalancer = loadBalancerFactory.balancers.get(0);
|
||||||
doThrow(ex).when(loadBalancer).handleResolvedAddresses(
|
doThrow(ex).when(loadBalancer).handleResolvedAddresses(
|
||||||
Matchers.<List<ResolvedServerInfo>>anyObject(), any(Attributes.class));
|
Matchers.<List<List<ResolvedServerInfo>>>anyObject(), any(Attributes.class));
|
||||||
|
|
||||||
// NameResolver returns addresses.
|
// NameResolver returns addresses.
|
||||||
nameResolverFactory.allResolved();
|
nameResolverFactory.allResolved();
|
||||||
|
|
@ -806,7 +806,7 @@ public class ManagedChannelImplTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
void resolved() {
|
void resolved() {
|
||||||
listener.onUpdate(servers, Attributes.EMPTY);
|
listener.onUpdate(Collections.singletonList(servers), Attributes.EMPTY);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override public void shutdown() {
|
@Override public void shutdown() {
|
||||||
|
|
|
||||||
|
|
@ -150,15 +150,17 @@ class GrpclbLoadBalancer<T> extends LoadBalancer<T> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handleResolvedAddresses(
|
public void handleResolvedAddresses(
|
||||||
List<ResolvedServerInfo> updatedServers, Attributes config) {
|
List<? extends List<ResolvedServerInfo>> updatedServers, Attributes config) {
|
||||||
synchronized (lock) {
|
synchronized (lock) {
|
||||||
if (closed) {
|
if (closed) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
ArrayList<SocketAddress> addrs = new ArrayList<SocketAddress>(updatedServers.size());
|
ArrayList<SocketAddress> addrs = new ArrayList<SocketAddress>(updatedServers.size());
|
||||||
for (ResolvedServerInfo serverInfo : updatedServers) {
|
for (List<ResolvedServerInfo> serverInfos : updatedServers) {
|
||||||
|
for (ResolvedServerInfo serverInfo : serverInfos) {
|
||||||
addrs.add(serverInfo.getAddress());
|
addrs.add(serverInfo.getAddress());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
EquivalentAddressGroup newLbAddresses = new EquivalentAddressGroup(addrs);
|
EquivalentAddressGroup newLbAddresses = new EquivalentAddressGroup(addrs);
|
||||||
if (!newLbAddresses.equals(lbAddresses)) {
|
if (!newLbAddresses.equals(lbAddresses)) {
|
||||||
lbAddresses = newLbAddresses;
|
lbAddresses = newLbAddresses;
|
||||||
|
|
|
||||||
|
|
@ -430,7 +430,8 @@ public class GrpclbLoadBalancerTest {
|
||||||
lbAddressGroup = buildAddressGroup(lbServerInfo);
|
lbAddressGroup = buildAddressGroup(lbServerInfo);
|
||||||
Transport lbTransport = new Transport();
|
Transport lbTransport = new Transport();
|
||||||
when(mockTransportManager.getTransport(eq(lbAddressGroup))).thenReturn(lbTransport);
|
when(mockTransportManager.getTransport(eq(lbAddressGroup))).thenReturn(lbTransport);
|
||||||
loadBalancer.handleResolvedAddresses(Collections.singletonList(lbServerInfo), Attributes.EMPTY);
|
loadBalancer.handleResolvedAddresses(
|
||||||
|
Collections.singletonList(Collections.singletonList(lbServerInfo)), Attributes.EMPTY);
|
||||||
verify(mockTransportManager).getTransport(eq(lbAddressGroup));
|
verify(mockTransportManager).getTransport(eq(lbAddressGroup));
|
||||||
return lbTransport;
|
return lbTransport;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue