Skip to content
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 9 additions & 15 deletions rls/src/main/java/io/grpc/rls/CachingRlsLbClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ final class CachingRlsLbClient {
@GuardedBy("lock")
private final RefCountedChildPolicyWrapperFactory refCountedChildPolicyWrapperFactory;
private final ChannelLogger logger;
private final ChildPolicyWrapper fallbackChildPolicyWrapper;

static {
MetricInstrumentRegistry metricInstrumentRegistry
Expand Down Expand Up @@ -226,6 +227,14 @@ private CachingRlsLbClient(Builder builder) {
lbPolicyConfig.getLoadBalancingPolicy(), childLbResolvedAddressFactory,
childLbHelperProvider,
new BackoffRefreshListener());
// TODO(creamsoup) wait until lb is ready
String defaultTarget = lbPolicyConfig.getRouteLookupConfig().defaultTarget();
if (defaultTarget != null && !defaultTarget.isEmpty()) {
logger.log(ChannelLogLevel.DEBUG, "starting fallback to {0}", defaultTarget);
fallbackChildPolicyWrapper = refCountedChildPolicyWrapperFactory.createOrGet(defaultTarget);
} else {
fallbackChildPolicyWrapper = null;
}

gaugeRegistration = helper.getMetricRecorder()
.registerBatchCallback(new BatchCallback() {
Expand Down Expand Up @@ -1022,12 +1031,8 @@ public PickResult pickSubchannel(PickSubchannelArgs args) {
}
}

private ChildPolicyWrapper fallbackChildPolicyWrapper;

/** Uses Subchannel connected to default target. */
private PickResult useFallback(PickSubchannelArgs args) {
// TODO(creamsoup) wait until lb is ready
startFallbackChildPolicy();
SubchannelPicker picker = fallbackChildPolicyWrapper.getPicker();
if (picker == null) {
return PickResult.withNoResult();
Expand All @@ -1052,17 +1057,6 @@ private String determineMetricsPickResult(PickResult pickResult) {
}
}

private void startFallbackChildPolicy() {
String defaultTarget = lbPolicyConfig.getRouteLookupConfig().defaultTarget();
synchronized (lock) {
if (fallbackChildPolicyWrapper != null) {
return;
}
logger.log(ChannelLogLevel.DEBUG, "starting fallback to {0}", defaultTarget);
fallbackChildPolicyWrapper = refCountedChildPolicyWrapperFactory.createOrGet(defaultTarget);
}
}

// GuardedBy CachingRlsLbClient.lock
void close() {
synchronized (lock) { // Lock is already held, but ErrorProne can't tell
Expand Down
16 changes: 11 additions & 5 deletions rls/src/test/java/io/grpc/rls/CachingRlsLbClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ public void uncaughtException(Thread t, Throwable e) {
private CachingRlsLbClient rlsLbClient;
private Map<String, ?> rlsChannelServiceConfig;
private String rlsChannelOverriddenAuthority;
private boolean isAlreadyClosed = false;

private void setUpRlsLbClient() {
fakeThrottler.resetCounts();
Expand All @@ -191,7 +192,9 @@ public void setUpMockMetricRecorder() {

@After
public void tearDown() throws Exception {
rlsLbClient.close();
if (!isAlreadyClosed) {
rlsLbClient.close();
}
assertWithMessage(
"On client shut down, RlsLoadBalancer must shut down with all its child loadbalancers.")
.that(lbProvider.loadBalancers).isEmpty();
Expand Down Expand Up @@ -372,12 +375,14 @@ public void get_updatesLbState() throws Exception {
ArgumentCaptor<SubchannelPicker> pickerCaptor = ArgumentCaptor.forClass(SubchannelPicker.class);
ArgumentCaptor<ConnectivityState> stateCaptor =
ArgumentCaptor.forClass(ConnectivityState.class);
inOrder.verify(helper, times(2))
inOrder.verify(helper, times(3))
.updateBalancingState(stateCaptor.capture(), pickerCaptor.capture());

assertThat(new HashSet<>(pickerCaptor.getAllValues())).hasSize(1);
// TRANSIENT_FAILURE is because the test setup pretends fallback is not available.
assertThat(stateCaptor.getAllValues())
.containsExactly(ConnectivityState.CONNECTING, ConnectivityState.READY);
.containsExactly(ConnectivityState.TRANSIENT_FAILURE, ConnectivityState.CONNECTING,
ConnectivityState.READY);
Metadata headers = new Metadata();
PickResult pickResult = getPickResultForCreate(pickerCaptor, headers);
assertThat(pickResult.getStatus().isOk()).isTrue();
Expand Down Expand Up @@ -439,7 +444,7 @@ public void timeout_not_changing_picked_subchannel() throws Exception {
ArgumentCaptor<SubchannelPicker> pickerCaptor = ArgumentCaptor.forClass(SubchannelPicker.class);
ArgumentCaptor<ConnectivityState> stateCaptor =
ArgumentCaptor.forClass(ConnectivityState.class);
verify(helper, times(4)).updateBalancingState(stateCaptor.capture(), pickerCaptor.capture());
verify(helper, times(5)).updateBalancingState(stateCaptor.capture(), pickerCaptor.capture());

Metadata headers = new Metadata();
PickResult pickResult = getPickResultForCreate(pickerCaptor, headers);
Expand Down Expand Up @@ -509,7 +514,7 @@ public void get_withAdaptiveThrottler() throws Exception {
ArgumentCaptor<SubchannelPicker> pickerCaptor = ArgumentCaptor.forClass(SubchannelPicker.class);
ArgumentCaptor<ConnectivityState> stateCaptor =
ArgumentCaptor.forClass(ConnectivityState.class);
inOrder.verify(helper, times(2))
inOrder.verify(helper, times(3))
.updateBalancingState(stateCaptor.capture(), pickerCaptor.capture());

Metadata headers = new Metadata();
Expand Down Expand Up @@ -700,6 +705,7 @@ public void metricGauges() throws ExecutionException, InterruptedException, Time
// Shutdown
rlsLbClient.close();
verify(mockGaugeRegistration).close();
isAlreadyClosed = true;
}

private static RouteLookupConfig getRouteLookupConfig() {
Expand Down
38 changes: 31 additions & 7 deletions rls/src/test/java/io/grpc/rls/RlsLoadBalancerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,13 @@ public void tearDown() {

@Test
public void lb_serverStatusCodeConversion() throws Exception {
deliverResolvedAddresses();
helper.getSynchronizationContext().execute(() -> {
try {
deliverResolvedAddresses();
} catch (Exception e) {
throw new RuntimeException(e);
}
});
InOrder inOrder = inOrder(helper);
inOrder.verify(helper)
.updateBalancingState(eq(ConnectivityState.CONNECTING), pickerCaptor.capture());
Expand Down Expand Up @@ -236,7 +242,13 @@ public void lb_serverStatusCodeConversion() throws Exception {

@Test
public void lb_working_withDefaultTarget_rlsResponding() throws Exception {
deliverResolvedAddresses();
helper.getSynchronizationContext().execute(() -> {
try {
deliverResolvedAddresses();
} catch (Exception e) {
throw new RuntimeException(e);
}
});
InOrder inOrder = inOrder(helper);
inOrder.verify(helper)
.updateBalancingState(eq(ConnectivityState.CONNECTING), pickerCaptor.capture());
Expand All @@ -257,7 +269,7 @@ public void lb_working_withDefaultTarget_rlsResponding() throws Exception {
inOrder.verifyNoMoreInteractions();

assertThat(res.getStatus().isOk()).isTrue();
assertThat(subchannels).hasSize(1);
assertThat(subchannels).hasSize(2); // includes fallback sub-channel
FakeSubchannel searchSubchannel = subchannels.getLast();
assertThat(subchannelIsReady(searchSubchannel)).isFalse();

Expand All @@ -277,7 +289,7 @@ public void lb_working_withDefaultTarget_rlsResponding() throws Exception {
// other rls picker itself is ready due to first channel.
assertThat(res.getStatus().isOk()).isTrue();
assertThat(subchannelIsReady(res.getSubchannel())).isFalse();
assertThat(subchannels).hasSize(2);
assertThat(subchannels).hasSize(3); // includes fallback sub-channel
FakeSubchannel rescueSubchannel = subchannels.getLast();

// search subchannel is down, rescue subchannel is connecting
Expand Down Expand Up @@ -393,7 +405,13 @@ public void lb_working_withoutDefaultTarget_noRlsResponse() throws Exception {
public void lb_working_withDefaultTarget_noRlsResponse() throws Exception {
fakeThrottler.nextResult = true;

deliverResolvedAddresses();
helper.getSynchronizationContext().execute(() -> {
try {
deliverResolvedAddresses();
} catch (Exception e) {
throw new RuntimeException(e);
}
});
InOrder inOrder = inOrder(helper);
inOrder.verify(helper)
.updateBalancingState(eq(ConnectivityState.CONNECTING), pickerCaptor.capture());
Expand Down Expand Up @@ -535,7 +553,13 @@ public void lb_working_withoutDefaultTarget() throws Exception {

@Test
public void lb_nameResolutionFailed() throws Exception {
deliverResolvedAddresses();
helper.getSynchronizationContext().execute(() -> {
try {
deliverResolvedAddresses();
} catch (Exception e) {
throw new RuntimeException(e);
}
});
InOrder inOrder = inOrder(helper);
inOrder.verify(helper)
.updateBalancingState(eq(ConnectivityState.CONNECTING), pickerCaptor.capture());
Expand All @@ -545,7 +569,7 @@ public void lb_nameResolutionFailed() throws Exception {
assertThat(subchannelIsReady(res.getSubchannel())).isFalse();

inOrder.verify(helper).createSubchannel(any(CreateSubchannelArgs.class));
assertThat(subchannels).hasSize(1);
assertThat(subchannels).hasSize(2); // includes fallback sub-channel

FakeSubchannel searchSubchannel = subchannels.getLast();
searchSubchannel.updateState(ConnectivityStateInfo.forNonError(ConnectivityState.READY));
Expand Down