From 84c05badde2513db1f84ad5de2992e568676757b Mon Sep 17 00:00:00 2001 From: vimanikag Date: Thu, 22 May 2025 06:17:42 +0000 Subject: [PATCH 1/6] 11243::RLS cleanups test --- .../java/io/grpc/rls/LinkedHashLruCache.java | 16 ++++++++- .../io/grpc/rls/LinkedHashLruCacheTest.java | 36 +++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/rls/src/main/java/io/grpc/rls/LinkedHashLruCache.java b/rls/src/main/java/io/grpc/rls/LinkedHashLruCache.java index b39b463c762..2eb4ef1b572 100644 --- a/rls/src/main/java/io/grpc/rls/LinkedHashLruCache.java +++ b/rls/src/main/java/io/grpc/rls/LinkedHashLruCache.java @@ -215,7 +215,7 @@ public final List values() { protected final boolean fitToLimit() { boolean removedAnyUnexpired = false; if (estimatedSizeBytes <= estimatedMaxSizeBytes) { - // new size is larger no need to do cleanup + // there is space available so no need to do cleanup return false; } // cleanup expired entries @@ -337,4 +337,18 @@ public String toString() { .toString(); } } + + /** + * setting the estimatedMaxSizeBytes + */ + public final void setEstimatedMaxSizeBytes(long newSizeBytes) { + this.estimatedMaxSizeBytes = newSizeBytes; + } + + /** + * setting the estimatedSizeBytes + */ + public final void setEstimatedSizeBytes(long newSizeBytes) { + this.estimatedSizeBytes = newSizeBytes; + } } diff --git a/rls/src/test/java/io/grpc/rls/LinkedHashLruCacheTest.java b/rls/src/test/java/io/grpc/rls/LinkedHashLruCacheTest.java index f38b28d8416..b7f15f40bee 100644 --- a/rls/src/test/java/io/grpc/rls/LinkedHashLruCacheTest.java +++ b/rls/src/test/java/io/grpc/rls/LinkedHashLruCacheTest.java @@ -266,4 +266,40 @@ public int hashCode() { return Objects.hash(value, expireTime); } } + + @Test + public void testFitToLimitWithEstimatedMaxSizeBytes() { + Entry entry1 = new Entry("Entry1", ticker.read() + 10,4); + Entry entry2 = new Entry("Entry2", ticker.read() + 20,2); + Entry entry3 = new Entry("Entry3", ticker.read() + 30,1); + + cache.cache(1, entry1); + cache.cache(2, entry2); + cache.cache(3, entry3); + + assertThat(cache.estimatedSize()).isEqualTo(2); + cache.setEstimatedMaxSizeBytes(2); + + assertThat(cache.estimatedMaxSizeBytes()).isEqualTo(2); + assertThat(cache.fitToLimit()).isEqualTo(true); + assertThat(cache.estimatedSizeBytes()).isEqualTo(1); + } + + @Test + public void testFitToLimitWithEstimatedSizeBytes() { + Entry entry1 = new Entry("Entry1", ticker.read() + 10,4); + Entry entry2 = new Entry("Entry2", ticker.read() + 20,2); + Entry entry3 = new Entry("Entry3", ticker.read() + 30,1); + + cache.cache(1, entry1); + cache.cache(2, entry2); + cache.cache(3, entry3); + + assertThat(cache.estimatedSize()).isEqualTo(2); + cache.setEstimatedSizeBytes(7); + + assertThat(cache.estimatedMaxSizeBytes()).isEqualTo(5); + assertThat(cache.fitToLimit()).isEqualTo(true); + assertThat(cache.estimatedSizeBytes()).isEqualTo(5); + } } From 6495235fa71cbda44dfa02368596dbb918c8c0bb Mon Sep 17 00:00:00 2001 From: vimanikag Date: Mon, 26 May 2025 10:25:46 +0000 Subject: [PATCH 2/6] 11243::RLS cleanups test --- .../java/io/grpc/rls/LinkedHashLruCache.java | 18 ++++++++----- .../io/grpc/rls/LinkedHashLruCacheTest.java | 26 ++++--------------- 2 files changed, 16 insertions(+), 28 deletions(-) diff --git a/rls/src/main/java/io/grpc/rls/LinkedHashLruCache.java b/rls/src/main/java/io/grpc/rls/LinkedHashLruCache.java index 2eb4ef1b572..d0e386e3f43 100644 --- a/rls/src/main/java/io/grpc/rls/LinkedHashLruCache.java +++ b/rls/src/main/java/io/grpc/rls/LinkedHashLruCache.java @@ -135,7 +135,8 @@ public final V cache(K key, V value) { estimatedSizeBytes += size; existing = delegate.put(key, new SizedValue(size, value)); if (existing != null) { - evictionListener.onEviction(key, existing, EvictionType.REPLACED); + // estimatedSizeBytes decremented + estimatedSizeBytes -= existing.size; } return existing == null ? null : existing.value; } @@ -174,7 +175,8 @@ private V invalidate(K key, EvictionType cause) { checkNotNull(cause, "cause"); SizedValue existing = delegate.remove(key); if (existing != null) { - evictionListener.onEviction(key, existing, cause); + // estimatedSizeBytes decremented + estimatedSizeBytes -= existing.size; } return existing == null ? null : existing.value; } @@ -185,7 +187,8 @@ public final void invalidateAll() { while (iterator.hasNext()) { Map.Entry entry = iterator.next(); if (entry.getValue() != null) { - evictionListener.onEviction(entry.getKey(), entry.getValue(), EvictionType.EXPLICIT); + // estimatedSizeBytes decremented + estimatedSizeBytes -= entry.getValue().size; } iterator.remove(); } @@ -230,8 +233,8 @@ protected final boolean fitToLimit() { break; // Violates some constraint like minimum age so stop our cleanup } lruIter.remove(); - // eviction listener will update the estimatedSizeBytes - evictionListener.onEviction(entry.getKey(), entry.getValue(), EvictionType.SIZE); + // estimatedSizeBytes decremented + estimatedSizeBytes -= entry.getValue().size; removedAnyUnexpired = true; } return removedAnyUnexpired; @@ -270,7 +273,8 @@ private boolean cleanupExpiredEntries(int maxExpiredEntries, long now) { Map.Entry entry = lruIter.next(); if (isExpired(entry.getKey(), entry.getValue().value, now)) { lruIter.remove(); - evictionListener.onEviction(entry.getKey(), entry.getValue(), EvictionType.EXPIRED); + // estimatedSizeBytes decremented + estimatedSizeBytes -= entry.getValue().size; removedAny = true; maxExpiredEntries--; } @@ -294,7 +298,6 @@ private final class SizeHandlingEvictionListener implements EvictionListener Date: Mon, 26 May 2025 13:50:13 +0000 Subject: [PATCH 3/6] 11243::RLS cleanups test --- .../java/io/grpc/rls/LinkedHashLruCache.java | 14 +++++------ .../io/grpc/rls/LinkedHashLruCacheTest.java | 25 +++++++++++++++---- 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/rls/src/main/java/io/grpc/rls/LinkedHashLruCache.java b/rls/src/main/java/io/grpc/rls/LinkedHashLruCache.java index d0e386e3f43..c58a3ea55ed 100644 --- a/rls/src/main/java/io/grpc/rls/LinkedHashLruCache.java +++ b/rls/src/main/java/io/grpc/rls/LinkedHashLruCache.java @@ -135,6 +135,7 @@ public final V cache(K key, V value) { estimatedSizeBytes += size; existing = delegate.put(key, new SizedValue(size, value)); if (existing != null) { + evictionListener.onEviction(key, existing, EvictionType.REPLACED); // estimatedSizeBytes decremented estimatedSizeBytes -= existing.size; } @@ -175,6 +176,7 @@ private V invalidate(K key, EvictionType cause) { checkNotNull(cause, "cause"); SizedValue existing = delegate.remove(key); if (existing != null) { + evictionListener.onEviction(key, existing, cause); // estimatedSizeBytes decremented estimatedSizeBytes -= existing.size; } @@ -187,6 +189,7 @@ public final void invalidateAll() { while (iterator.hasNext()) { Map.Entry entry = iterator.next(); if (entry.getValue() != null) { + evictionListener.onEviction(entry.getKey(), entry.getValue(), EvictionType.EXPLICIT); // estimatedSizeBytes decremented estimatedSizeBytes -= entry.getValue().size; } @@ -233,6 +236,8 @@ protected final boolean fitToLimit() { break; // Violates some constraint like minimum age so stop our cleanup } lruIter.remove(); + // eviction listener is used to track the eviction type + evictionListener.onEviction(entry.getKey(), entry.getValue(), EvictionType.SIZE); // estimatedSizeBytes decremented estimatedSizeBytes -= entry.getValue().size; removedAnyUnexpired = true; @@ -273,6 +278,7 @@ private boolean cleanupExpiredEntries(int maxExpiredEntries, long now) { Map.Entry entry = lruIter.next(); if (isExpired(entry.getKey(), entry.getValue().value, now)) { lruIter.remove(); + evictionListener.onEviction(entry.getKey(), entry.getValue(), EvictionType.EXPIRED); // estimatedSizeBytes decremented estimatedSizeBytes -= entry.getValue().size; removedAny = true; @@ -298,6 +304,7 @@ private final class SizeHandlingEvictionListener implements EvictionListener Date: Thu, 29 May 2025 06:13:48 +0000 Subject: [PATCH 4/6] 11243 : RLS cleanups --- rls/src/main/java/io/grpc/rls/LinkedHashLruCache.java | 6 ------ 1 file changed, 6 deletions(-) diff --git a/rls/src/main/java/io/grpc/rls/LinkedHashLruCache.java b/rls/src/main/java/io/grpc/rls/LinkedHashLruCache.java index c58a3ea55ed..54031c534ff 100644 --- a/rls/src/main/java/io/grpc/rls/LinkedHashLruCache.java +++ b/rls/src/main/java/io/grpc/rls/LinkedHashLruCache.java @@ -136,7 +136,6 @@ public final V cache(K key, V value) { existing = delegate.put(key, new SizedValue(size, value)); if (existing != null) { evictionListener.onEviction(key, existing, EvictionType.REPLACED); - // estimatedSizeBytes decremented estimatedSizeBytes -= existing.size; } return existing == null ? null : existing.value; @@ -177,7 +176,6 @@ private V invalidate(K key, EvictionType cause) { SizedValue existing = delegate.remove(key); if (existing != null) { evictionListener.onEviction(key, existing, cause); - // estimatedSizeBytes decremented estimatedSizeBytes -= existing.size; } return existing == null ? null : existing.value; @@ -190,7 +188,6 @@ public final void invalidateAll() { Map.Entry entry = iterator.next(); if (entry.getValue() != null) { evictionListener.onEviction(entry.getKey(), entry.getValue(), EvictionType.EXPLICIT); - // estimatedSizeBytes decremented estimatedSizeBytes -= entry.getValue().size; } iterator.remove(); @@ -238,7 +235,6 @@ protected final boolean fitToLimit() { lruIter.remove(); // eviction listener is used to track the eviction type evictionListener.onEviction(entry.getKey(), entry.getValue(), EvictionType.SIZE); - // estimatedSizeBytes decremented estimatedSizeBytes -= entry.getValue().size; removedAnyUnexpired = true; } @@ -279,7 +275,6 @@ private boolean cleanupExpiredEntries(int maxExpiredEntries, long now) { if (isExpired(entry.getKey(), entry.getValue().value, now)) { lruIter.remove(); evictionListener.onEviction(entry.getKey(), entry.getValue(), EvictionType.EXPIRED); - // estimatedSizeBytes decremented estimatedSizeBytes -= entry.getValue().size; removedAny = true; maxExpiredEntries--; @@ -304,7 +299,6 @@ private final class SizeHandlingEvictionListener implements EvictionListener Date: Tue, 3 Jun 2025 14:25:08 +0000 Subject: [PATCH 5/6] 11243::RLS cleanups --- rls/src/main/java/io/grpc/rls/LinkedHashLruCache.java | 8 ++++---- rls/src/test/java/io/grpc/rls/LinkedHashLruCacheTest.java | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/rls/src/main/java/io/grpc/rls/LinkedHashLruCache.java b/rls/src/main/java/io/grpc/rls/LinkedHashLruCache.java index 54031c534ff..483ee53ca16 100644 --- a/rls/src/main/java/io/grpc/rls/LinkedHashLruCache.java +++ b/rls/src/main/java/io/grpc/rls/LinkedHashLruCache.java @@ -299,6 +299,10 @@ private final class SizeHandlingEvictionListener implements EvictionListener Date: Tue, 3 Jun 2025 18:10:07 +0000 Subject: [PATCH 6/6] 11243::RLS cleanups --- rls/src/main/java/io/grpc/rls/LinkedHashLruCache.java | 4 ---- rls/src/test/java/io/grpc/rls/LinkedHashLruCacheTest.java | 8 +++++--- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/rls/src/main/java/io/grpc/rls/LinkedHashLruCache.java b/rls/src/main/java/io/grpc/rls/LinkedHashLruCache.java index 483ee53ca16..5c0f8c554a3 100644 --- a/rls/src/main/java/io/grpc/rls/LinkedHashLruCache.java +++ b/rls/src/main/java/io/grpc/rls/LinkedHashLruCache.java @@ -345,8 +345,4 @@ public String toString() { .toString(); } } - - public final void setEstimatedSizeBytes(long newSizeBytes) { - this.estimatedSizeBytes = newSizeBytes; - } } diff --git a/rls/src/test/java/io/grpc/rls/LinkedHashLruCacheTest.java b/rls/src/test/java/io/grpc/rls/LinkedHashLruCacheTest.java index 8f2769f6f93..cfa73a9afe7 100644 --- a/rls/src/test/java/io/grpc/rls/LinkedHashLruCacheTest.java +++ b/rls/src/test/java/io/grpc/rls/LinkedHashLruCacheTest.java @@ -285,20 +285,22 @@ public void testFitToLimitUsingReSize() { } @Test - public void testFitToLimitWithEstimatedSizeBytes() { + public void testFitToLimitWithEntry() { Entry entry1 = new Entry("Entry1", ticker.read() + 10,4); Entry entry2 = new Entry("Entry2", ticker.read() + 20,2); Entry entry3 = new Entry("Entry3", ticker.read() + 30,1); + Entry entry4 = new Entry("Entry4", ticker.read() + 20,2); cache.cache(1, entry1); cache.cache(2, entry2); cache.cache(3, entry3); assertThat(cache.estimatedSize()).isEqualTo(2); - cache.setEstimatedSizeBytes(7); + cache.cache(4, entry4); + assertThat(cache.estimatedSize()).isEqualTo(3); assertThat(cache.estimatedMaxSizeBytes()).isEqualTo(5); - assertThat(cache.fitToLimit()).isEqualTo(true); + assertThat(cache.fitToLimit()).isEqualTo(false); assertThat(cache.estimatedSizeBytes()).isEqualTo(5); } }