Skip to content

Commit 43155c1

Browse files
committed
Save changes. incomplete state with backoff timer changes not yet done because of the mixup in the Lru cache code logic, with expiry time.
1 parent 0ff3106 commit 43155c1

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -363,13 +363,13 @@ final CachedRouteLookupResponse get(final RouteLookupRequest request) {
363363
final CacheEntry cacheEntry;
364364
cacheEntry = linkedHashLruCache.read(request);
365365
if (cacheEntry == null
366-
|| cacheEntry instanceof BackoffCacheEntry
367-
&& (((BackoffCacheEntry) cacheEntry).isBackoffTimeEnded)) {
366+
|| cacheEntry instanceof BackoffCacheEntry && cacheEntry.isExpired(ticker.read())) {
368367
PendingCacheEntry pendingEntry = pendingCallCache.get(request);
369368
if (pendingEntry != null) {
370369
return CachedRouteLookupResponse.pendingResponse(pendingEntry);
371370
}
372-
return asyncRlsCall(request, /* backoffPolicy= */ null);
371+
return asyncRlsCall(request, cacheEntry instanceof BackoffCacheEntry
372+
? ((BackoffCacheEntry) cacheEntry).backoffPolicy : null);
373373
}
374374

375375
if (cacheEntry instanceof DataCacheEntry) {
@@ -463,7 +463,9 @@ private BackoffCacheEntry createBackOffEntry(
463463
request, status, delayNanos);
464464
BackoffCacheEntry entry = new BackoffCacheEntry(request, status, backoffPolicy);
465465
// Lock is held, so the task can't execute before the assignment
466-
entry.scheduledFuture = scheduledExecutorService.schedule(
466+
entry.backoffTimer = scheduledExecutorService.schedule(
467+
() -> refreshBackoffEntry(entry), delayNanos, TimeUnit.NANOSECONDS);
468+
entry.backoffTimer = scheduledExecutorService.schedule(
467469
() -> refreshBackoffEntry(entry), delayNanos, TimeUnit.NANOSECONDS);
468470
linkedHashLruCache.cacheAndClean(request, entry);
469471
return entry;
@@ -472,13 +474,12 @@ private BackoffCacheEntry createBackOffEntry(
472474
private void refreshBackoffEntry(BackoffCacheEntry entry) {
473475
synchronized (lock) {
474476
// This checks whether the task has been cancelled and prevents a second execution.
475-
if (!entry.scheduledFuture.cancel(false)) {
477+
if (!entry.backoffTimer.cancel(false)) {
476478
// Future was previously cancelled
477479
return;
478480
}
479481
logger.log(ChannelLogLevel.DEBUG,
480482
"[RLS Entry {0}] Calling RLS for transition to pending", entry.request);
481-
entry.isBackoffTimeEnded = true;
482483
// Cache updated. updateBalancingState() to reattempt picks
483484
helper.triggerPendingRpcProcessing();
484485
}
@@ -785,8 +786,7 @@ private static final class BackoffCacheEntry extends CacheEntry {
785786

786787
private final Status status;
787788
private final BackoffPolicy backoffPolicy;
788-
private Future<?> scheduledFuture;
789-
private boolean isBackoffTimeEnded;
789+
private Future<?> backoffTimer;
790790

791791
BackoffCacheEntry(RouteLookupRequest request, Status status, BackoffPolicy backoffPolicy) {
792792
super(request);
@@ -805,12 +805,12 @@ int getSizeBytes() {
805805

806806
@Override
807807
boolean isExpired(long now) {
808-
return scheduledFuture.isDone();
808+
return backoffTimer.isDone();
809809
}
810810

811811
@Override
812812
void cleanup() {
813-
scheduledFuture.cancel(false);
813+
backoffTimer.cancel(false);
814814
}
815815

816816
@Override

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,12 +360,14 @@ public void get_throttled_backoffBehavior() throws Exception {
360360
CachedRouteLookupResponse resp = getInSyncContext(routeLookupRequest);
361361
assertThat(resp.hasError()).isTrue();
362362

363-
// let it be throttled again
364363
fakeClock.forwardTime(10, TimeUnit.MILLISECONDS);
364+
// Assert that the same backoff policy is still in effect for the cache entry.
365+
// The below provider should not get used, so the back off time will still be set to 10ms.
366+
fakeBackoffProvider.nextPolicy = createBackoffPolicy(20, TimeUnit.MILLISECONDS);
367+
// let it be throttled again
365368
resp = getInSyncContext(routeLookupRequest);
366369
assertThat(resp.hasError()).isTrue();
367370

368-
// Assert that the backoff policy is still in effect for the cache entry.
369371
fakeClock.forwardTime(9, TimeUnit.MILLISECONDS);
370372
resp = getInSyncContext(routeLookupRequest);
371373
assertThat(resp.hasError()).isTrue();

0 commit comments

Comments
 (0)