pvillard31 commented on code in PR #11919:
URL: https://github.com/apache/nifi/pull/11919#discussion_r4614314301
##########
nifi-extension-bundles/nifi-redis-bundle/nifi-redis-extensions/src/test/java/org/apache/nifi/redis/service/ITRedisDistributedMapCacheClientService.java:
##########
@@ -173,9 +211,133 @@ public void testReplaceCreatesWhenKeyAbsent() throws
IOException {
assertEquals("v1", cacheClient.get(key, STRING_SERIALIZER,
STRING_DESERIALIZER));
}
- private static void flushDatabase() {
+ @Test
+ public void
testPutIfAbsentSetsExpirationWithoutChangingExistingExpiration() throws
IOException {
+ assertTrue(cacheClient.putIfAbsent("ttl", "v1", STRING_SERIALIZER,
STRING_SERIALIZER));
+ try (RedisConnection connection = redisConnectionPool.getConnection())
{
+ final byte[] key = "ttl".getBytes(StandardCharsets.UTF_8);
+ final long ttl = connection.keyCommands().ttl(key);
+ assertTrue(ttl > 0 && ttl <= 60);
+ connection.keyCommands().expire(key, 20);
+ assertFalse(cacheClient.putIfAbsent("ttl", "v2",
STRING_SERIALIZER, STRING_SERIALIZER));
+ assertTrue(connection.keyCommands().ttl(key) <= 20);
+ }
+ assertEquals("v1", cacheClient.get("ttl", STRING_SERIALIZER,
STRING_DESERIALIZER));
+ }
+
+ @Test
+ public void testGetAndPutIfAbsentDoesNotRefreshExistingExpiration() throws
IOException {
+ assertNull(cacheClient.getAndPutIfAbsent("ttl", "v1",
STRING_SERIALIZER, STRING_SERIALIZER, STRING_DESERIALIZER));
+ try (RedisConnection connection = redisConnectionPool.getConnection())
{
+ final byte[] key = "ttl".getBytes(StandardCharsets.UTF_8);
+ assertTrue(connection.keyCommands().ttl(key) > 0);
+ connection.keyCommands().expire(key, 20);
+ assertEquals("v1", cacheClient.getAndPutIfAbsent("ttl", "v2",
STRING_SERIALIZER, STRING_SERIALIZER, STRING_DESERIALIZER));
+ assertTrue(connection.keyCommands().ttl(key) <= 20);
+ }
+ }
+
+ @Test
+ public void
testReplacePreservesExpirationOnConflictAndRefreshesOnSuccess() throws
IOException {
+ cacheClient.put("ttl", "v1", STRING_SERIALIZER, STRING_SERIALIZER);
+ final AtomicCacheEntry<String, String, byte[]> entry =
cacheClient.fetch("ttl", STRING_SERIALIZER, STRING_DESERIALIZER);
+ try (RedisConnection connection = redisConnectionPool.getConnection())
{
+ final byte[] key = "ttl".getBytes(StandardCharsets.UTF_8);
+ connection.keyCommands().expire(key, 20);
+ assertFalse(cacheClient.replace(new AtomicCacheEntry<>("ttl",
"v2", new byte[0]), STRING_SERIALIZER, STRING_SERIALIZER));
+ assertTrue(connection.keyCommands().ttl(key) <= 20);
+ entry.setValue("v2");
+ assertTrue(cacheClient.replace(entry, STRING_SERIALIZER,
STRING_SERIALIZER));
+ assertTrue(connection.keyCommands().ttl(key) > 20);
+ }
+ }
+
+ @Test
+ public void testBinaryAndEmptyValuesAreDistinctFromMissingKeys() throws
IOException {
+ final Serializer<byte[]> serializer = (value, output) ->
output.write(value);
+ final Deserializer<byte[]> deserializer = value -> value;
+ final byte[] key = {0, (byte) 255, 1};
+ final byte[] empty = new byte[0];
+ final byte[] binary = {0, (byte) 255, 2};
+ assertNull(cacheClient.getAndPutIfAbsent(key, empty, serializer,
serializer, deserializer));
+ assertArrayEquals(empty, cacheClient.getAndPutIfAbsent(key, binary,
serializer, serializer, deserializer));
+ assertFalse(cacheClient.replace(new AtomicCacheEntry<>(key, binary,
null), serializer, serializer));
+ assertTrue(cacheClient.replace(new AtomicCacheEntry<>(key, binary,
empty), serializer, serializer));
+ assertArrayEquals(binary, cacheClient.fetch(key, serializer,
deserializer).getRevision().orElseThrow());
+ assertFalse(cacheClient.replace(new AtomicCacheEntry<>(new byte[]{3},
binary, empty), serializer, serializer));
+ }
+
+ @Test
+ public void testZeroTtlCreatesPersistentEntries() throws IOException {
+ testRunner.disableControllerService(cacheClient);
+ testRunner.setProperty(cacheClient, RedisUtils.TTL, "0 secs");
+ testRunner.enableControllerService(cacheClient);
+ assertTrue(cacheClient.putIfAbsent("put", "v1", STRING_SERIALIZER,
STRING_SERIALIZER));
+ assertNull(cacheClient.getAndPutIfAbsent("get", "v1",
STRING_SERIALIZER, STRING_SERIALIZER, STRING_DESERIALIZER));
+ assertTrue(cacheClient.replace(new AtomicCacheEntry<>("replace", "v1",
null), STRING_SERIALIZER, STRING_SERIALIZER));
+ try (RedisConnection connection = redisConnectionPool.getConnection())
{
+ for (String key : List.of("put", "get", "replace")) {
+ assertEquals(-1L,
connection.keyCommands().ttl(key.getBytes(StandardCharsets.UTF_8)));
+ }
+ // A replacement without TTL must remove an existing expiration,
as SET did before this change.
Review Comment:
Can we reword this comment to describe the expected behavior instead of the
history of the change?
##########
nifi-extension-bundles/nifi-redis-bundle/nifi-redis-extensions/src/test/java/org/apache/nifi/redis/service/ITRedisClusterDistributedMapCacheClientService.java:
##########
@@ -0,0 +1,29 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.nifi.redis.service;
+
+/**
+ * Runs the atomic cache contract against the Redis Cluster API, with all hash
slots assigned to one node.
+ * This exercises cluster command routing without requiring fixed Docker host
ports or a multi-node network.
+ */
+public class ITRedisClusterDistributedMapCacheClientService extends
ITRedisDistributedMapCacheClientService {
Review Comment:
With one node owning all slots, MOVED/ASK redirects are never exercised: did
you consider a multi-node cluster test, or should we document this gap?
##########
nifi-extension-bundles/nifi-redis-bundle/nifi-redis-extensions/src/main/resources/docs/org.apache.nifi.redis.service.RedisDistributedMapCacheClientService/additionalDetails.html:
##########
@@ -0,0 +1,52 @@
+<!DOCTYPE html>
Review Comment:
NiFi 2.x only loads additionalDetails.md for component documentation, so can
we convert this file to Markdown so the content is shown in the UI?
##########
nifi-extension-bundles/nifi-redis-bundle/nifi-redis-extensions/src/main/java/org/apache/nifi/redis/service/RedisDistributedMapCacheClientService.java:
##########
@@ -18,49 +18,73 @@
import org.apache.nifi.annotation.documentation.CapabilityDescription;
import org.apache.nifi.annotation.documentation.Tags;
-import org.apache.nifi.components.ValidationContext;
-import org.apache.nifi.components.ValidationResult;
import org.apache.nifi.distributed.cache.client.AtomicCacheEntry;
import
org.apache.nifi.distributed.cache.client.AtomicDistributedMapCacheClient;
import org.apache.nifi.distributed.cache.client.Deserializer;
import org.apache.nifi.distributed.cache.client.Serializer;
-import org.apache.nifi.redis.RedisConnectionPool;
-import org.apache.nifi.redis.RedisType;
+import org.apache.nifi.util.Tuple;
+import org.springframework.data.redis.connection.ReturnType;
+import org.springframework.data.redis.connection.SetCondition;
+import org.springframework.data.redis.core.types.Expiration;
-import java.io.ByteArrayOutputStream;
import java.io.IOException;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.List;
-
-import static org.apache.nifi.redis.util.RedisUtils.REDIS_CONNECTION_POOL;
+import java.nio.charset.StandardCharsets;
@Tags({ "redis", "distributed", "cache", "map" })
-@CapabilityDescription("An implementation of DistributedMapCacheClient that
uses Redis as the backing cache. This service relies on " +
- "the WATCH, MULTI, and EXEC commands in Redis, which are not fully
supported when Redis is clustered. As a result, this service " +
- "can only be used with a Redis Connection Pool that is configured for
standalone or sentinel mode. Sentinel mode can be used to " +
- "provide high-availability configurations.")
+@CapabilityDescription("An implementation of AtomicDistributedMapCacheClient
that uses Redis as the backing cache, including " +
Review Comment:
Existing standalone or sentinel users with an ACL that allows
WATCH/MULTI/EXEC but not EVAL will break after upgrade, so should we add a
migration note in the Jira or release notes?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]