Skip to content

Commit 6a30fac

Browse files
committed
Addressing review comments: Made lookup reason always as cache miss after a backed off entry is reprocessed, and the style related comments. Added assertions for lookup reason in unit tests.
1 parent 8ce971d commit 6a30fac

File tree

3 files changed

+22
-16
lines changed

3 files changed

+22
-16
lines changed

rls/src/main/java/io/grpc/rls/CachingRlsLbClient.java

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@ final class CachingRlsLbClient {
128128
private final RlsLbHelper helper;
129129
private final ManagedChannel rlsChannel;
130130
private final RouteLookupServiceStub rlsStub;
131-
private final RlsRequestFactory requestFactory;
132131
private final RlsPicker rlsPicker;
133132
private final ResolvedAddressFactory childLbResolvedAddressFactory;
134133
@GuardedBy("lock")
@@ -197,7 +196,7 @@ private CachingRlsLbClient(Builder builder) {
197196
ChannelLogLevel.DEBUG, "Can not get hostname from authority: {0}", helper.getAuthority());
198197
serverHost = helper.getAuthority();
199198
}
200-
requestFactory = new RlsRequestFactory(
199+
RlsRequestFactory requestFactory = new RlsRequestFactory(
201200
lbPolicyConfig.getRouteLookupConfig(), serverHost);
202201
rlsPicker = new RlsPicker(requestFactory, rlsConfig.lookupService());
203202
// It is safe to use helper.getUnsafeChannelCredentials() because the client authenticates the
@@ -303,7 +302,7 @@ private CachedRouteLookupResponse asyncRlsCall(
303302
// on this result
304303
return CachedRouteLookupResponse.backoffEntry(createBackOffEntry(
305304
routeLookupRequestKey, Status.RESOURCE_EXHAUSTED.withDescription("RLS throttled"),
306-
backoffPolicy, routeLookupReason));
305+
backoffPolicy));
307306
}
308307
final SettableFuture<RouteLookupResponse> response = SettableFuture.create();
309308
io.grpc.lookup.v1.RouteLookupRequest routeLookupRequest = REQUEST_CONVERTER.convert(
@@ -417,7 +416,7 @@ private void pendingRpcComplete(PendingCacheEntry entry) {
417416
// reattempt picks when the child LB is done connecting
418417
} catch (Exception e) {
419418
createBackOffEntry(entry.routeLookupRequestKey, Status.fromThrowable(e),
420-
entry.backoffPolicy, entry.routeLookupReason);
419+
entry.backoffPolicy);
421420
// Cache updated. updateBalancingState() to reattempt picks
422421
helper.triggerPendingRpcProcessing();
423422
}
@@ -439,9 +438,8 @@ private DataCacheEntry createDataEntry(
439438
}
440439

441440
@GuardedBy("lock")
442-
private BackoffCacheEntry createBackOffEntry(
443-
RouteLookupRequestKey routeLookupRequestKey, Status status,
444-
@Nullable BackoffPolicy backoffPolicy, RouteLookupRequest.Reason routeLookupReason) {
441+
private BackoffCacheEntry createBackOffEntry(RouteLookupRequestKey routeLookupRequestKey,
442+
Status status, @Nullable BackoffPolicy backoffPolicy) {
445443
if (backoffPolicy == null) {
446444
backoffPolicy = backoffProvider.get();
447445
}
@@ -450,8 +448,7 @@ private BackoffCacheEntry createBackOffEntry(
450448
ChannelLogLevel.DEBUG,
451449
"[RLS Entry {0}] Transition to back off: status={1}, delayNanos={2}",
452450
routeLookupRequestKey, status, delayNanos);
453-
BackoffCacheEntry entry = new BackoffCacheEntry(routeLookupRequestKey, status, backoffPolicy,
454-
routeLookupReason);
451+
BackoffCacheEntry entry = new BackoffCacheEntry(routeLookupRequestKey, status, backoffPolicy);
455452
// Lock is held, so the task can't execute before the assignment
456453
entry.scheduledFuture = scheduledExecutorService.schedule(
457454
() -> refreshBackoffEntry(entry), delayNanos, TimeUnit.NANOSECONDS);
@@ -469,7 +466,7 @@ private void refreshBackoffEntry(BackoffCacheEntry entry) {
469466
logger.log(ChannelLogLevel.DEBUG,
470467
"[RLS Entry {0}] Calling RLS for transition to pending", entry.routeLookupRequestKey);
471468
linkedHashLruCache.invalidate(entry.routeLookupRequestKey);
472-
asyncRlsCall(entry.routeLookupRequestKey, entry.backoffPolicy, entry.routeLookupReason);
469+
asyncRlsCall(entry.routeLookupRequestKey, entry.backoffPolicy, RouteLookupRequest.Reason.REASON_MISS);
473470
}
474471
}
475472

@@ -779,15 +776,13 @@ private static final class BackoffCacheEntry extends CacheEntry {
779776

780777
private final Status status;
781778
private final BackoffPolicy backoffPolicy;
782-
private final RouteLookupRequest.Reason routeLookupReason;
783779
private Future<?> scheduledFuture;
784780

785781
BackoffCacheEntry(RouteLookupRequestKey routeLookupRequestKey, Status status,
786-
BackoffPolicy backoffPolicy, RouteLookupRequest.Reason routeLookupReason) {
782+
BackoffPolicy backoffPolicy) {
787783
super(routeLookupRequestKey);
788784
this.status = checkNotNull(status, "status");
789785
this.backoffPolicy = checkNotNull(backoffPolicy, "backoffPolicy");
790-
this.routeLookupReason = checkNotNull(routeLookupReason, "routeLookupReason");
791786
}
792787

793788
Status getStatus() {

rls/src/main/java/io/grpc/rls/RlsProtoData.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,12 @@ abstract static class RouteLookupRequest {
4747

4848
/** Names should match those in {@link io.grpc.lookup.v1.RouteLookupRequest.Reason} */
4949
enum Reason {
50-
REASON_UNKNOWN, // Unused
51-
REASON_MISS, // No data available in local cache
52-
REASON_STALE; // Data in local cache is stale
50+
/** Unused */
51+
REASON_UNKNOWN,
52+
/** No data available in local cache */
53+
REASON_MISS,
54+
/** Data in local cache is stale */
55+
REASON_STALE;
5356
}
5457

5558
/** Reason for making this request. */

rls/src/test/java/io/grpc/rls/CachingRlsLbClientTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ public void get_noError_lifeCycle() throws Exception {
239239
// cache hit for staled entry
240240
fakeClock.forwardTime(ROUTE_LOOKUP_CONFIG.staleAgeInNanos(), TimeUnit.NANOSECONDS);
241241

242+
rlsServerImpl.routeLookupReason = null;
242243
resp = getInSyncContext(routeLookupRequestKey);
243244
assertThat(resp.hasData()).isTrue();
244245

@@ -250,6 +251,7 @@ public void get_noError_lifeCycle() throws Exception {
250251

251252
resp = getInSyncContext(routeLookupRequestKey);
252253

254+
assertThat(rlsServerImpl.routeLookupReason).isEqualTo(io.grpc.lookup.v1.RouteLookupRequest.Reason.REASON_STALE);
253255
assertThat(resp.hasData()).isTrue();
254256

255257
// existing cache expired
@@ -298,6 +300,7 @@ public void rls_withCustomRlsChannelServiceConfig() throws Exception {
298300
routeLookupRequestKey,
299301
RouteLookupResponse.create(ImmutableList.of("target"), "header")));
300302

303+
rlsServerImpl.routeLookupReason = null;
301304
// initial request
302305
CachedRouteLookupResponse resp = getInSyncContext(routeLookupRequestKey);
303306
assertThat(resp.isPending()).isTrue();
@@ -307,6 +310,7 @@ public void rls_withCustomRlsChannelServiceConfig() throws Exception {
307310

308311
resp = getInSyncContext(routeLookupRequestKey);
309312
assertThat(resp.hasData()).isTrue();
313+
assertThat(rlsServerImpl.routeLookupReason).isEqualTo(io.grpc.lookup.v1.RouteLookupRequest.Reason.REASON_MISS);
310314

311315
assertThat(rlsChannelOverriddenAuthority).isEqualTo("bigtable.googleapis.com:443");
312316
assertThat(rlsChannelServiceConfig).isEqualTo(routeLookupChannelServiceConfig);
@@ -348,8 +352,10 @@ public void get_throttledAndRecover() throws Exception {
348352

349353
assertThat(resp.isPending()).isTrue();
350354

355+
rlsServerImpl.routeLookupReason = null;
351356
// server responses
352357
fakeClock.forwardTime(SERVER_LATENCY_MILLIS, TimeUnit.MILLISECONDS);
358+
assertThat(rlsServerImpl.routeLookupReason).isEqualTo(io.grpc.lookup.v1.RouteLookupRequest.Reason.REASON_MISS);
353359

354360
resp = getInSyncContext(routeLookupRequestKey);
355361

@@ -881,6 +887,7 @@ private static final class StaticFixedDelayRlsServerImpl
881887

882888
private Map<RlsProtoData.RouteLookupRequestKey, RouteLookupResponse> lookupTable =
883889
ImmutableMap.of();
890+
io.grpc.lookup.v1.RouteLookupRequest.Reason routeLookupReason;
884891

885892
public StaticFixedDelayRlsServerImpl(
886893
long responseDelayNano, ScheduledExecutorService scheduledExecutorService) {
@@ -903,6 +910,7 @@ public void routeLookup(final io.grpc.lookup.v1.RouteLookupRequest request,
903910
new Runnable() {
904911
@Override
905912
public void run() {
913+
routeLookupReason = request.getReason();
906914
RouteLookupResponse response =
907915
lookupTable.get(
908916
RlsProtoData.RouteLookupRequestKey.create(

0 commit comments

Comments
 (0)