From bbf049b3d48e8529197fff64f9dd99955209bb7c Mon Sep 17 00:00:00 2001 From: seakider Date: Sat, 28 Dec 2024 23:31:01 +0800 Subject: [PATCH] Fixed - The Bloom filter encounters an error if a contains() request is made while it is being deleted and updated. #6197 Signed-off-by: xuxiaolei --- .../org/redisson/RedissonBloomFilter.java | 18 ++++++++++++---- .../org/redisson/RedissonBloomFilterTest.java | 21 ++++++++++++------- 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/redisson/src/main/java/org/redisson/RedissonBloomFilter.java b/redisson/src/main/java/org/redisson/RedissonBloomFilter.java index b5d88957d..a831f931d 100644 --- a/redisson/src/main/java/org/redisson/RedissonBloomFilter.java +++ b/redisson/src/main/java/org/redisson/RedissonBloomFilter.java @@ -171,12 +171,20 @@ public class RedissonBloomFilter extends RedissonExpirable implements RBloomF @Override public RFuture containsAsync(Collection objects) { - CompletionStage future = CompletableFuture.completedFuture(null); + CompletionStage f = CompletableFuture.completedFuture(null); if (size == 0) { - future = readConfigAsync(); + f = readConfigAsync().handle((r, e) -> { + if (e instanceof IllegalArgumentException) { + return 0L; + } + return null; + }); } - CompletionStage f = future.thenCompose(r -> { + f = f.thenCompose(r -> { + if (r != null) { + return CompletableFuture.completedFuture(r); + } List allIndexes = index(objects); List params = new ArrayList<>(); @@ -188,7 +196,9 @@ public class RedissonBloomFilter extends RedissonExpirable implements RBloomF return commandExecutor.evalWriteAsync(getRawName(), LongCodec.INSTANCE, RedisCommands.EVAL_LONG, "local size = redis.call('hget', KEYS[1], 'size');" + "local hashIterations = redis.call('hget', KEYS[1], 'hashIterations');" + - "assert(size == ARGV[1] and hashIterations == ARGV[2], 'Bloom filter config has been changed')" + + "if size ~= ARGV[1] or hashIterations ~= ARGV[2] then " + + "return 0;" + + "end;" + "local k = 0;" + "local c = 0;" + diff --git a/redisson/src/test/java/org/redisson/RedissonBloomFilterTest.java b/redisson/src/test/java/org/redisson/RedissonBloomFilterTest.java index 213b168d9..d242beb77 100644 --- a/redisson/src/test/java/org/redisson/RedissonBloomFilterTest.java +++ b/redisson/src/test/java/org/redisson/RedissonBloomFilterTest.java @@ -110,14 +110,6 @@ public class RedissonBloomFilterTest extends RedisDockerTest { assertThat(redisson.getKeys().count()).isZero(); } - @Test - public void testNotInitializedOnContains() { - Assertions.assertThrows(RedisException.class, () -> { - RBloomFilter filter = redisson.getBloomFilter("filter"); - filter.contains("32"); - }); - } - @Test public void testNotInitializedOnAdd() { Assertions.assertThrows(RedisException.class, () -> { @@ -194,4 +186,17 @@ public class RedissonBloomFilterTest extends RedisDockerTest { assertThat(newFilter.count()).isEqualTo(1); assertThat(newFilter.contains("123")).isTrue(); } + + @Test + public void testContainsException() { + RBloomFilter f1 = redisson.getBloomFilter("filter"); + assertThat(f1.contains("1")).isFalse(); + f1.tryInit(100, 0.03); + + RBloomFilter f2 = redisson.getBloomFilter("filter"); + f2.delete(); + f2.tryInit(200, 0.03); + + assertThat(f1.contains("1")).isFalse(); + } }