DonalEvans commented on a change in pull request #6877: URL: https://github.com/apache/geode/pull/6877#discussion_r714155205
########## File path: geode-apis-compatible-with-redis/src/integrationTest/java/org/apache/geode/redis/internal/executor/key/AbstractRenamenxIntegrationTest.java ########## @@ -0,0 +1,385 @@ +/* + * 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.geode.redis.internal.executor.key; + +import static org.apache.geode.redis.RedisCommandArgumentsTestHelper.assertExactNumberOfArgs; +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Random; +import java.util.Set; +import java.util.concurrent.BrokenBarrierException; +import java.util.concurrent.Callable; +import java.util.concurrent.CyclicBarrier; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import redis.clients.jedis.HostAndPort; +import redis.clients.jedis.JedisCluster; +import redis.clients.jedis.Protocol; +import redis.clients.jedis.exceptions.JedisDataException; + +import org.apache.geode.redis.RedisIntegrationTest; +import org.apache.geode.redis.internal.RedisConstants; +import org.apache.geode.redis.internal.data.RedisKey; +import org.apache.geode.redis.internal.services.StripedCoordinator; +import org.apache.geode.redis.internal.services.SynchronizedStripedCoordinator; +import org.apache.geode.test.awaitility.GeodeAwaitility; + +public abstract class AbstractRenamenxIntegrationTest implements RedisIntegrationTest { Review comment: To be consistent with the naming style of other similar commands (SETNX, SETEX etc.) could this test, the tests that extend it and the executor use "RenameNX" rather than "Renamenx"? ########## File path: geode-apis-compatible-with-redis/src/main/java/org/apache/geode/redis/internal/executor/key/RenamenxExecutor.java ########## @@ -0,0 +1,60 @@ +/* + * 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.geode.redis.internal.executor.key; + +import static org.apache.geode.redis.internal.RedisConstants.ERROR_NO_SUCH_KEY; + +import java.util.List; + +import org.apache.geode.redis.internal.data.RedisKey; +import org.apache.geode.redis.internal.data.RedisKeyExistsException; +import org.apache.geode.redis.internal.executor.AbstractExecutor; +import org.apache.geode.redis.internal.executor.RedisResponse; +import org.apache.geode.redis.internal.netty.Command; +import org.apache.geode.redis.internal.netty.ExecutionHandlerContext; + +public class RenamenxExecutor extends AbstractExecutor { Review comment: Would it be worth extracting common code in this executor and the RenameExecutor into a parent class? It's only a small amount of code duplication, but I think it should be fairly straightforward if you use "getKeyExistsResponse()", "executeRenameCommand()" and "getSuccessResponse()" methods that get overridden in each of the implementations. ########## File path: geode-apis-compatible-with-redis/src/integrationTest/java/org/apache/geode/redis/internal/executor/key/AbstractRenamenxIntegrationTest.java ########## @@ -0,0 +1,385 @@ +/* + * 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.geode.redis.internal.executor.key; + +import static org.apache.geode.redis.RedisCommandArgumentsTestHelper.assertExactNumberOfArgs; +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Random; +import java.util.Set; +import java.util.concurrent.BrokenBarrierException; +import java.util.concurrent.Callable; +import java.util.concurrent.CyclicBarrier; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import redis.clients.jedis.HostAndPort; +import redis.clients.jedis.JedisCluster; +import redis.clients.jedis.Protocol; +import redis.clients.jedis.exceptions.JedisDataException; + +import org.apache.geode.redis.RedisIntegrationTest; +import org.apache.geode.redis.internal.RedisConstants; +import org.apache.geode.redis.internal.data.RedisKey; +import org.apache.geode.redis.internal.services.StripedCoordinator; +import org.apache.geode.redis.internal.services.SynchronizedStripedCoordinator; +import org.apache.geode.test.awaitility.GeodeAwaitility; + +public abstract class AbstractRenamenxIntegrationTest implements RedisIntegrationTest { + private JedisCluster jedis; + private static final int REDIS_CLIENT_TIMEOUT = + Math.toIntExact(GeodeAwaitility.getTimeout().toMillis()); + private static Random rand; + + @Before + public void setUp() { + rand = new Random(); + jedis = new JedisCluster(new HostAndPort("localhost", getPort()), REDIS_CLIENT_TIMEOUT); + } + + @After + public void tearDown() { + flushAll(); + jedis.close(); + } + + @Test + public void errors_GivenWrongNumberOfArguments() { + assertExactNumberOfArgs(jedis, Protocol.Command.RENAMENX, 2); + } + + @Test + public void testNewKey() { + jedis.set("{user1}foo", "bar"); + long result = jedis.renamenx("{user1}foo", "{user1}newfoo"); + assertThat(result).isEqualTo(1L); + assertThat(jedis.get("{user1}newfoo")).isEqualTo("bar"); + } + + @Test + public void testOldKeyIsDeleted() { + jedis.set("{user1}foo", "bar"); + long result = jedis.renamenx("{user1}foo", "{user1}newfoo"); + assertThat(result).isEqualTo(1L); + assertThat(jedis.get("{user1}foo")).isNull(); + } + + @Test + public void testRenameKeyThatDoesNotExist() { + try { + jedis.renamenx("{user1}foo", "{user1}newfoo"); + } catch (JedisDataException e) { + assertThat(e.getMessage()).contains(RedisConstants.ERROR_NO_SUCH_KEY); + } + } + + @Test + public void testHashMap() { + jedis.hset("{user1}foo", "field", "va"); + long result = jedis.renamenx("{user1}foo", "{user1}newfoo"); + assertThat(result).isEqualTo(1L); + assertThat(jedis.hget("{user1}newfoo", "field")).isEqualTo("va"); + } + + @Test + public void testSet() { + jedis.sadd("{user1}foo", "data"); + long result = jedis.renamenx("{user1}foo", "{user1}newfoo"); + assertThat(result).isEqualTo(1L); + assertThat(jedis.smembers("{user1}newfoo")).contains("data"); Review comment: This assertion could be made stronger by changing it to `containsExactly`. ########## File path: geode-apis-compatible-with-redis/src/integrationTest/java/org/apache/geode/redis/internal/executor/key/AbstractRenamenxIntegrationTest.java ########## @@ -0,0 +1,385 @@ +/* + * 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.geode.redis.internal.executor.key; + +import static org.apache.geode.redis.RedisCommandArgumentsTestHelper.assertExactNumberOfArgs; +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Random; +import java.util.Set; +import java.util.concurrent.BrokenBarrierException; +import java.util.concurrent.Callable; +import java.util.concurrent.CyclicBarrier; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import redis.clients.jedis.HostAndPort; +import redis.clients.jedis.JedisCluster; +import redis.clients.jedis.Protocol; +import redis.clients.jedis.exceptions.JedisDataException; + +import org.apache.geode.redis.RedisIntegrationTest; +import org.apache.geode.redis.internal.RedisConstants; +import org.apache.geode.redis.internal.data.RedisKey; +import org.apache.geode.redis.internal.services.StripedCoordinator; +import org.apache.geode.redis.internal.services.SynchronizedStripedCoordinator; +import org.apache.geode.test.awaitility.GeodeAwaitility; + +public abstract class AbstractRenamenxIntegrationTest implements RedisIntegrationTest { + private JedisCluster jedis; + private static final int REDIS_CLIENT_TIMEOUT = + Math.toIntExact(GeodeAwaitility.getTimeout().toMillis()); + private static Random rand; + + @Before + public void setUp() { + rand = new Random(); + jedis = new JedisCluster(new HostAndPort("localhost", getPort()), REDIS_CLIENT_TIMEOUT); + } + + @After + public void tearDown() { + flushAll(); + jedis.close(); + } + + @Test + public void errors_GivenWrongNumberOfArguments() { + assertExactNumberOfArgs(jedis, Protocol.Command.RENAMENX, 2); + } + + @Test + public void testNewKey() { + jedis.set("{user1}foo", "bar"); + long result = jedis.renamenx("{user1}foo", "{user1}newfoo"); + assertThat(result).isEqualTo(1L); + assertThat(jedis.get("{user1}newfoo")).isEqualTo("bar"); + } + + @Test + public void testOldKeyIsDeleted() { + jedis.set("{user1}foo", "bar"); + long result = jedis.renamenx("{user1}foo", "{user1}newfoo"); + assertThat(result).isEqualTo(1L); + assertThat(jedis.get("{user1}foo")).isNull(); + } + + @Test + public void testRenameKeyThatDoesNotExist() { + try { + jedis.renamenx("{user1}foo", "{user1}newfoo"); + } catch (JedisDataException e) { + assertThat(e.getMessage()).contains(RedisConstants.ERROR_NO_SUCH_KEY); + } Review comment: This might be better as: ``` assertThatThrownBy(() -> jedis.renamenx("{user1}foo", "{user1}newfoo")) .hasMessageContaining(ERROR_NO_SUCH_KEY); ``` as otherwise the test will pass even if no exception is thrown. ########## File path: geode-apis-compatible-with-redis/src/integrationTest/java/org/apache/geode/redis/internal/executor/key/AbstractRenamenxIntegrationTest.java ########## @@ -0,0 +1,385 @@ +/* + * 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.geode.redis.internal.executor.key; + +import static org.apache.geode.redis.RedisCommandArgumentsTestHelper.assertExactNumberOfArgs; +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Random; +import java.util.Set; +import java.util.concurrent.BrokenBarrierException; +import java.util.concurrent.Callable; +import java.util.concurrent.CyclicBarrier; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import redis.clients.jedis.HostAndPort; +import redis.clients.jedis.JedisCluster; +import redis.clients.jedis.Protocol; +import redis.clients.jedis.exceptions.JedisDataException; + +import org.apache.geode.redis.RedisIntegrationTest; +import org.apache.geode.redis.internal.RedisConstants; +import org.apache.geode.redis.internal.data.RedisKey; +import org.apache.geode.redis.internal.services.StripedCoordinator; +import org.apache.geode.redis.internal.services.SynchronizedStripedCoordinator; +import org.apache.geode.test.awaitility.GeodeAwaitility; + +public abstract class AbstractRenamenxIntegrationTest implements RedisIntegrationTest { + private JedisCluster jedis; + private static final int REDIS_CLIENT_TIMEOUT = + Math.toIntExact(GeodeAwaitility.getTimeout().toMillis()); + private static Random rand; + + @Before + public void setUp() { + rand = new Random(); + jedis = new JedisCluster(new HostAndPort("localhost", getPort()), REDIS_CLIENT_TIMEOUT); + } + + @After + public void tearDown() { + flushAll(); + jedis.close(); + } + + @Test + public void errors_GivenWrongNumberOfArguments() { + assertExactNumberOfArgs(jedis, Protocol.Command.RENAMENX, 2); + } + + @Test + public void testNewKey() { + jedis.set("{user1}foo", "bar"); + long result = jedis.renamenx("{user1}foo", "{user1}newfoo"); + assertThat(result).isEqualTo(1L); + assertThat(jedis.get("{user1}newfoo")).isEqualTo("bar"); + } + + @Test + public void testOldKeyIsDeleted() { + jedis.set("{user1}foo", "bar"); + long result = jedis.renamenx("{user1}foo", "{user1}newfoo"); + assertThat(result).isEqualTo(1L); + assertThat(jedis.get("{user1}foo")).isNull(); + } + + @Test + public void testRenameKeyThatDoesNotExist() { + try { + jedis.renamenx("{user1}foo", "{user1}newfoo"); + } catch (JedisDataException e) { + assertThat(e.getMessage()).contains(RedisConstants.ERROR_NO_SUCH_KEY); + } + } + + @Test + public void testHashMap() { + jedis.hset("{user1}foo", "field", "va"); + long result = jedis.renamenx("{user1}foo", "{user1}newfoo"); + assertThat(result).isEqualTo(1L); + assertThat(jedis.hget("{user1}newfoo", "field")).isEqualTo("va"); + } + + @Test + public void testSet() { + jedis.sadd("{user1}foo", "data"); + long result = jedis.renamenx("{user1}foo", "{user1}newfoo"); + assertThat(result).isEqualTo(1L); + assertThat(jedis.smembers("{user1}newfoo")).contains("data"); + } + + @Test + public void testRenameSameKey() { + jedis.set("{user1}blue", "moon"); + assertThat(jedis.renamenx("{user1}blue", "{user1}blue")).isEqualTo(0L); + assertThat(jedis.get("{user1}blue")).isEqualTo("moon"); + } + + @Test + public void testRenameKeyToExistingKey() { + jedis.set("{user1}foo1", "bar1"); + jedis.set("{user1}foo12", "bar2"); + long result = jedis.renamenx("{user1}foo1", "{user1}foo12"); + assertThat(result).isEqualTo(0L); + } + + @Test + public void testConcurrentSets() throws ExecutionException, InterruptedException { + Set<String> stringsForK1 = new HashSet<>(); + + int numOfStrings = 500000; + Callable<Long> callable1 = + () -> addStringsToKeys(stringsForK1, "{user1}k1", numOfStrings, jedis); + Callable<Long> callable2 = () -> jedis.renamenx("{user1}k1", "{user1}k2"); + + ExecutorService pool = Executors.newFixedThreadPool(3); + Future<Long> future1 = pool.submit(callable1); + Thread.sleep(rand.nextInt(1000)); + Future<Long> future2 = pool.submit(callable2); + + future1.get(); + try { + future2.get(); + assertThat(jedis.scard("{user1}k2")).isEqualTo(numOfStrings); + assertThat(jedis.exists("{user1}k1")).isFalse(); + } catch (Exception e) { + assertThat(e.getMessage()).contains(RedisConstants.ERROR_NO_SUCH_KEY); + assertThat(jedis.scard("{user1}k1")).isEqualTo(numOfStrings); + assertThat(jedis.exists("{user1}k2")).isFalse(); + } Review comment: I'm not sure that we should be expecting to see `ERROR_NO_SUCH_KEY` here. It could be avoided entirely by calling `jedis.sadd("{user1}k1", "initialEntry")` at the start of the test to ensure that "{user1}k1" definitely exists before we try to do the rename. ########## File path: geode-apis-compatible-with-redis/src/integrationTest/java/org/apache/geode/redis/internal/executor/key/AbstractRenamenxIntegrationTest.java ########## @@ -0,0 +1,385 @@ +/* + * 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.geode.redis.internal.executor.key; + +import static org.apache.geode.redis.RedisCommandArgumentsTestHelper.assertExactNumberOfArgs; +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Random; +import java.util.Set; +import java.util.concurrent.BrokenBarrierException; +import java.util.concurrent.Callable; +import java.util.concurrent.CyclicBarrier; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import redis.clients.jedis.HostAndPort; +import redis.clients.jedis.JedisCluster; +import redis.clients.jedis.Protocol; +import redis.clients.jedis.exceptions.JedisDataException; + +import org.apache.geode.redis.RedisIntegrationTest; +import org.apache.geode.redis.internal.RedisConstants; +import org.apache.geode.redis.internal.data.RedisKey; +import org.apache.geode.redis.internal.services.StripedCoordinator; +import org.apache.geode.redis.internal.services.SynchronizedStripedCoordinator; +import org.apache.geode.test.awaitility.GeodeAwaitility; + +public abstract class AbstractRenamenxIntegrationTest implements RedisIntegrationTest { + private JedisCluster jedis; + private static final int REDIS_CLIENT_TIMEOUT = + Math.toIntExact(GeodeAwaitility.getTimeout().toMillis()); + private static Random rand; + + @Before + public void setUp() { + rand = new Random(); + jedis = new JedisCluster(new HostAndPort("localhost", getPort()), REDIS_CLIENT_TIMEOUT); + } + + @After + public void tearDown() { + flushAll(); + jedis.close(); + } + + @Test + public void errors_GivenWrongNumberOfArguments() { + assertExactNumberOfArgs(jedis, Protocol.Command.RENAMENX, 2); + } + + @Test + public void testNewKey() { + jedis.set("{user1}foo", "bar"); + long result = jedis.renamenx("{user1}foo", "{user1}newfoo"); + assertThat(result).isEqualTo(1L); + assertThat(jedis.get("{user1}newfoo")).isEqualTo("bar"); + } + + @Test + public void testOldKeyIsDeleted() { + jedis.set("{user1}foo", "bar"); + long result = jedis.renamenx("{user1}foo", "{user1}newfoo"); + assertThat(result).isEqualTo(1L); + assertThat(jedis.get("{user1}foo")).isNull(); + } + + @Test + public void testRenameKeyThatDoesNotExist() { + try { + jedis.renamenx("{user1}foo", "{user1}newfoo"); + } catch (JedisDataException e) { + assertThat(e.getMessage()).contains(RedisConstants.ERROR_NO_SUCH_KEY); + } + } + + @Test + public void testHashMap() { + jedis.hset("{user1}foo", "field", "va"); + long result = jedis.renamenx("{user1}foo", "{user1}newfoo"); + assertThat(result).isEqualTo(1L); + assertThat(jedis.hget("{user1}newfoo", "field")).isEqualTo("va"); + } + + @Test + public void testSet() { + jedis.sadd("{user1}foo", "data"); + long result = jedis.renamenx("{user1}foo", "{user1}newfoo"); + assertThat(result).isEqualTo(1L); + assertThat(jedis.smembers("{user1}newfoo")).contains("data"); + } + + @Test + public void testRenameSameKey() { + jedis.set("{user1}blue", "moon"); + assertThat(jedis.renamenx("{user1}blue", "{user1}blue")).isEqualTo(0L); + assertThat(jedis.get("{user1}blue")).isEqualTo("moon"); + } + + @Test + public void testRenameKeyToExistingKey() { + jedis.set("{user1}foo1", "bar1"); + jedis.set("{user1}foo12", "bar2"); + long result = jedis.renamenx("{user1}foo1", "{user1}foo12"); + assertThat(result).isEqualTo(0L); + } + + @Test + public void testConcurrentSets() throws ExecutionException, InterruptedException { + Set<String> stringsForK1 = new HashSet<>(); + + int numOfStrings = 500000; + Callable<Long> callable1 = + () -> addStringsToKeys(stringsForK1, "{user1}k1", numOfStrings, jedis); + Callable<Long> callable2 = () -> jedis.renamenx("{user1}k1", "{user1}k2"); + + ExecutorService pool = Executors.newFixedThreadPool(3); + Future<Long> future1 = pool.submit(callable1); + Thread.sleep(rand.nextInt(1000)); + Future<Long> future2 = pool.submit(callable2); + + future1.get(); + try { + future2.get(); + assertThat(jedis.scard("{user1}k2")).isEqualTo(numOfStrings); + assertThat(jedis.exists("{user1}k1")).isFalse(); + } catch (Exception e) { + assertThat(e.getMessage()).contains(RedisConstants.ERROR_NO_SUCH_KEY); + assertThat(jedis.scard("{user1}k1")).isEqualTo(numOfStrings); + assertThat(jedis.exists("{user1}k2")).isFalse(); + } + } + + @Test + public void should_succeed_givenTwoKeysOnDifferentStripes() { + List<String> listOfKeys = getKeysOnDifferentStripes(); + String oldKey = listOfKeys.get(0); + String newKey = listOfKeys.get(1); + + jedis.sadd(oldKey, "value1"); + + assertThat(jedis.renamenx(oldKey, newKey)).isEqualTo(1L); + assertThat(jedis.smembers(newKey)).containsExactly("value1"); + assertThat(jedis.exists(oldKey)).isFalse(); + } + + @Test + public void should_succeed_givenTwoKeysOnSameStripe() { + List<String> listOfKeys = new ArrayList<>(getKeysOnSameRandomStripe(2)); + String oldKey = listOfKeys.get(0); + String newKey = listOfKeys.get(1); + + jedis.sadd(oldKey, "value1"); + + assertThat(jedis.renamenx(oldKey, newKey)).isEqualTo(1L); + assertThat(jedis.smembers(newKey)).containsExactly("value1"); + assertThat(jedis.exists(oldKey)).isFalse(); + } + + @Test + public void shouldNotDeadlock_concurrentRenames_givenStripeContention() + throws ExecutionException, InterruptedException { + List<String> keysOnStripe1 = new ArrayList<>(getKeysOnSameRandomStripe(2)); + List<String> keysOnStripe2 = getKeysOnSameRandomStripe(2, keysOnStripe1.get(0)); + + for (int i = 0; i < 5; i++) { + doConcurrentRenamesDifferentKeys( + Arrays.asList(keysOnStripe1.get(0), keysOnStripe2.get(0)), + Arrays.asList(keysOnStripe2.get(1), keysOnStripe1.get(1))); + } + } + + @Test + public void shouldThrowError_givenKeyDeletedDuringRename() Review comment: This test should also probably be using `ConcurrentLoopingThreads` rather than the `ExecutorService`. It could use `runWithAction()` and assert that either we see the key renamed successfully or that we hit the `ERROR_NO_SUCH_KEY` error, and only stop looping when we do hit that error. The current test may pass without ever actually seeing the error. ########## File path: geode-apis-compatible-with-redis/src/integrationTest/java/org/apache/geode/redis/internal/executor/key/AbstractRenamenxIntegrationTest.java ########## @@ -0,0 +1,385 @@ +/* + * 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.geode.redis.internal.executor.key; + +import static org.apache.geode.redis.RedisCommandArgumentsTestHelper.assertExactNumberOfArgs; +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Random; +import java.util.Set; +import java.util.concurrent.BrokenBarrierException; +import java.util.concurrent.Callable; +import java.util.concurrent.CyclicBarrier; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import redis.clients.jedis.HostAndPort; +import redis.clients.jedis.JedisCluster; +import redis.clients.jedis.Protocol; +import redis.clients.jedis.exceptions.JedisDataException; + +import org.apache.geode.redis.RedisIntegrationTest; +import org.apache.geode.redis.internal.RedisConstants; +import org.apache.geode.redis.internal.data.RedisKey; +import org.apache.geode.redis.internal.services.StripedCoordinator; +import org.apache.geode.redis.internal.services.SynchronizedStripedCoordinator; +import org.apache.geode.test.awaitility.GeodeAwaitility; + +public abstract class AbstractRenamenxIntegrationTest implements RedisIntegrationTest { + private JedisCluster jedis; + private static final int REDIS_CLIENT_TIMEOUT = + Math.toIntExact(GeodeAwaitility.getTimeout().toMillis()); + private static Random rand; + + @Before + public void setUp() { + rand = new Random(); + jedis = new JedisCluster(new HostAndPort("localhost", getPort()), REDIS_CLIENT_TIMEOUT); + } + + @After + public void tearDown() { + flushAll(); + jedis.close(); + } + + @Test + public void errors_GivenWrongNumberOfArguments() { + assertExactNumberOfArgs(jedis, Protocol.Command.RENAMENX, 2); + } + + @Test + public void testNewKey() { + jedis.set("{user1}foo", "bar"); + long result = jedis.renamenx("{user1}foo", "{user1}newfoo"); + assertThat(result).isEqualTo(1L); + assertThat(jedis.get("{user1}newfoo")).isEqualTo("bar"); + } + + @Test + public void testOldKeyIsDeleted() { + jedis.set("{user1}foo", "bar"); + long result = jedis.renamenx("{user1}foo", "{user1}newfoo"); + assertThat(result).isEqualTo(1L); + assertThat(jedis.get("{user1}foo")).isNull(); + } + + @Test + public void testRenameKeyThatDoesNotExist() { + try { + jedis.renamenx("{user1}foo", "{user1}newfoo"); + } catch (JedisDataException e) { + assertThat(e.getMessage()).contains(RedisConstants.ERROR_NO_SUCH_KEY); + } + } + + @Test + public void testHashMap() { + jedis.hset("{user1}foo", "field", "va"); + long result = jedis.renamenx("{user1}foo", "{user1}newfoo"); + assertThat(result).isEqualTo(1L); + assertThat(jedis.hget("{user1}newfoo", "field")).isEqualTo("va"); + } + + @Test + public void testSet() { + jedis.sadd("{user1}foo", "data"); + long result = jedis.renamenx("{user1}foo", "{user1}newfoo"); + assertThat(result).isEqualTo(1L); + assertThat(jedis.smembers("{user1}newfoo")).contains("data"); + } + + @Test + public void testRenameSameKey() { + jedis.set("{user1}blue", "moon"); + assertThat(jedis.renamenx("{user1}blue", "{user1}blue")).isEqualTo(0L); + assertThat(jedis.get("{user1}blue")).isEqualTo("moon"); + } + + @Test + public void testRenameKeyToExistingKey() { + jedis.set("{user1}foo1", "bar1"); + jedis.set("{user1}foo12", "bar2"); + long result = jedis.renamenx("{user1}foo1", "{user1}foo12"); + assertThat(result).isEqualTo(0L); + } + + @Test + public void testConcurrentSets() throws ExecutionException, InterruptedException { + Set<String> stringsForK1 = new HashSet<>(); + + int numOfStrings = 500000; + Callable<Long> callable1 = + () -> addStringsToKeys(stringsForK1, "{user1}k1", numOfStrings, jedis); + Callable<Long> callable2 = () -> jedis.renamenx("{user1}k1", "{user1}k2"); + + ExecutorService pool = Executors.newFixedThreadPool(3); + Future<Long> future1 = pool.submit(callable1); + Thread.sleep(rand.nextInt(1000)); + Future<Long> future2 = pool.submit(callable2); + + future1.get(); + try { + future2.get(); + assertThat(jedis.scard("{user1}k2")).isEqualTo(numOfStrings); + assertThat(jedis.exists("{user1}k1")).isFalse(); + } catch (Exception e) { + assertThat(e.getMessage()).contains(RedisConstants.ERROR_NO_SUCH_KEY); + assertThat(jedis.scard("{user1}k1")).isEqualTo(numOfStrings); + assertThat(jedis.exists("{user1}k2")).isFalse(); + } + } + + @Test + public void should_succeed_givenTwoKeysOnDifferentStripes() { Review comment: This test and others testing stripe-related stuff seem to be testing internal behaviour rather than something a user would have access to or knowledge of (there is no concept of a "stripe" in Redis, that's an internal implementation that we're using) so it should probably be a unit test rather than an integration test. Also, this stripe-related behaviour is not unique to the RENAMENX command, so tests around it should probably be in the `SynchronizedStripedCoordinatorTest` class. ########## File path: geode-apis-compatible-with-redis/src/integrationTest/java/org/apache/geode/redis/internal/executor/key/AbstractRenamenxIntegrationTest.java ########## @@ -0,0 +1,385 @@ +/* + * 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.geode.redis.internal.executor.key; + +import static org.apache.geode.redis.RedisCommandArgumentsTestHelper.assertExactNumberOfArgs; +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Random; +import java.util.Set; +import java.util.concurrent.BrokenBarrierException; +import java.util.concurrent.Callable; +import java.util.concurrent.CyclicBarrier; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import redis.clients.jedis.HostAndPort; +import redis.clients.jedis.JedisCluster; +import redis.clients.jedis.Protocol; +import redis.clients.jedis.exceptions.JedisDataException; + +import org.apache.geode.redis.RedisIntegrationTest; +import org.apache.geode.redis.internal.RedisConstants; +import org.apache.geode.redis.internal.data.RedisKey; +import org.apache.geode.redis.internal.services.StripedCoordinator; +import org.apache.geode.redis.internal.services.SynchronizedStripedCoordinator; +import org.apache.geode.test.awaitility.GeodeAwaitility; + +public abstract class AbstractRenamenxIntegrationTest implements RedisIntegrationTest { + private JedisCluster jedis; + private static final int REDIS_CLIENT_TIMEOUT = + Math.toIntExact(GeodeAwaitility.getTimeout().toMillis()); + private static Random rand; + + @Before + public void setUp() { + rand = new Random(); + jedis = new JedisCluster(new HostAndPort("localhost", getPort()), REDIS_CLIENT_TIMEOUT); + } + + @After + public void tearDown() { + flushAll(); + jedis.close(); + } + + @Test + public void errors_GivenWrongNumberOfArguments() { + assertExactNumberOfArgs(jedis, Protocol.Command.RENAMENX, 2); + } + + @Test + public void testNewKey() { + jedis.set("{user1}foo", "bar"); + long result = jedis.renamenx("{user1}foo", "{user1}newfoo"); + assertThat(result).isEqualTo(1L); + assertThat(jedis.get("{user1}newfoo")).isEqualTo("bar"); + } + + @Test + public void testOldKeyIsDeleted() { + jedis.set("{user1}foo", "bar"); + long result = jedis.renamenx("{user1}foo", "{user1}newfoo"); + assertThat(result).isEqualTo(1L); + assertThat(jedis.get("{user1}foo")).isNull(); + } + + @Test + public void testRenameKeyThatDoesNotExist() { + try { + jedis.renamenx("{user1}foo", "{user1}newfoo"); + } catch (JedisDataException e) { + assertThat(e.getMessage()).contains(RedisConstants.ERROR_NO_SUCH_KEY); + } + } + + @Test + public void testHashMap() { + jedis.hset("{user1}foo", "field", "va"); + long result = jedis.renamenx("{user1}foo", "{user1}newfoo"); + assertThat(result).isEqualTo(1L); + assertThat(jedis.hget("{user1}newfoo", "field")).isEqualTo("va"); + } + + @Test + public void testSet() { + jedis.sadd("{user1}foo", "data"); + long result = jedis.renamenx("{user1}foo", "{user1}newfoo"); + assertThat(result).isEqualTo(1L); + assertThat(jedis.smembers("{user1}newfoo")).contains("data"); + } + + @Test + public void testRenameSameKey() { + jedis.set("{user1}blue", "moon"); + assertThat(jedis.renamenx("{user1}blue", "{user1}blue")).isEqualTo(0L); + assertThat(jedis.get("{user1}blue")).isEqualTo("moon"); + } + + @Test + public void testRenameKeyToExistingKey() { + jedis.set("{user1}foo1", "bar1"); + jedis.set("{user1}foo12", "bar2"); + long result = jedis.renamenx("{user1}foo1", "{user1}foo12"); + assertThat(result).isEqualTo(0L); + } + + @Test + public void testConcurrentSets() throws ExecutionException, InterruptedException { Review comment: This test should probably be using `ConcurrentLoopingThreads` to achieve what it's trying to test (that a rename and a set do not cause problems if they happen simultaneously) rather than just doing one call of sadd and one call of renamenx and hoping that the timing is right. ########## File path: geode-apis-compatible-with-redis/src/integrationTest/java/org/apache/geode/redis/internal/executor/key/AbstractRenamenxIntegrationTest.java ########## @@ -0,0 +1,385 @@ +/* + * 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.geode.redis.internal.executor.key; + +import static org.apache.geode.redis.RedisCommandArgumentsTestHelper.assertExactNumberOfArgs; +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Random; +import java.util.Set; +import java.util.concurrent.BrokenBarrierException; +import java.util.concurrent.Callable; +import java.util.concurrent.CyclicBarrier; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import redis.clients.jedis.HostAndPort; +import redis.clients.jedis.JedisCluster; +import redis.clients.jedis.Protocol; +import redis.clients.jedis.exceptions.JedisDataException; + +import org.apache.geode.redis.RedisIntegrationTest; +import org.apache.geode.redis.internal.RedisConstants; +import org.apache.geode.redis.internal.data.RedisKey; +import org.apache.geode.redis.internal.services.StripedCoordinator; +import org.apache.geode.redis.internal.services.SynchronizedStripedCoordinator; +import org.apache.geode.test.awaitility.GeodeAwaitility; + +public abstract class AbstractRenamenxIntegrationTest implements RedisIntegrationTest { + private JedisCluster jedis; + private static final int REDIS_CLIENT_TIMEOUT = Review comment: Rather than defining a constant here, you could use the constant with the same name in `RedisClusterStartupRule`. That constant was introduced after a lot of integration tests had already been written, so its use isn't super consistent, but going forward we should try to use it. ########## File path: geode-apis-compatible-with-redis/src/integrationTest/java/org/apache/geode/redis/internal/executor/key/AbstractRenamenxIntegrationTest.java ########## @@ -0,0 +1,385 @@ +/* + * 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.geode.redis.internal.executor.key; + +import static org.apache.geode.redis.RedisCommandArgumentsTestHelper.assertExactNumberOfArgs; +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Random; +import java.util.Set; +import java.util.concurrent.BrokenBarrierException; +import java.util.concurrent.Callable; +import java.util.concurrent.CyclicBarrier; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import redis.clients.jedis.HostAndPort; +import redis.clients.jedis.JedisCluster; +import redis.clients.jedis.Protocol; +import redis.clients.jedis.exceptions.JedisDataException; + +import org.apache.geode.redis.RedisIntegrationTest; +import org.apache.geode.redis.internal.RedisConstants; +import org.apache.geode.redis.internal.data.RedisKey; +import org.apache.geode.redis.internal.services.StripedCoordinator; +import org.apache.geode.redis.internal.services.SynchronizedStripedCoordinator; +import org.apache.geode.test.awaitility.GeodeAwaitility; + +public abstract class AbstractRenamenxIntegrationTest implements RedisIntegrationTest { + private JedisCluster jedis; + private static final int REDIS_CLIENT_TIMEOUT = + Math.toIntExact(GeodeAwaitility.getTimeout().toMillis()); + private static Random rand; + + @Before + public void setUp() { + rand = new Random(); + jedis = new JedisCluster(new HostAndPort("localhost", getPort()), REDIS_CLIENT_TIMEOUT); + } + + @After + public void tearDown() { + flushAll(); + jedis.close(); + } + + @Test + public void errors_GivenWrongNumberOfArguments() { + assertExactNumberOfArgs(jedis, Protocol.Command.RENAMENX, 2); + } + + @Test + public void testNewKey() { Review comment: For test names, we try to follow the convention `*EXPECTED BEHAVIOUR*_*CONDITIONS*` to make the intent of tests clearer. Would it be possible to change the test names in this class to match that pattern? There are a lot of examples of this in `AbstractZAddIntegrationTest`. ########## File path: geode-apis-compatible-with-redis/src/integrationTest/java/org/apache/geode/redis/internal/executor/key/AbstractRenamenxIntegrationTest.java ########## @@ -0,0 +1,385 @@ +/* + * 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.geode.redis.internal.executor.key; + +import static org.apache.geode.redis.RedisCommandArgumentsTestHelper.assertExactNumberOfArgs; +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Random; +import java.util.Set; +import java.util.concurrent.BrokenBarrierException; +import java.util.concurrent.Callable; +import java.util.concurrent.CyclicBarrier; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import redis.clients.jedis.HostAndPort; +import redis.clients.jedis.JedisCluster; +import redis.clients.jedis.Protocol; +import redis.clients.jedis.exceptions.JedisDataException; + +import org.apache.geode.redis.RedisIntegrationTest; +import org.apache.geode.redis.internal.RedisConstants; +import org.apache.geode.redis.internal.data.RedisKey; +import org.apache.geode.redis.internal.services.StripedCoordinator; +import org.apache.geode.redis.internal.services.SynchronizedStripedCoordinator; +import org.apache.geode.test.awaitility.GeodeAwaitility; + +public abstract class AbstractRenamenxIntegrationTest implements RedisIntegrationTest { + private JedisCluster jedis; + private static final int REDIS_CLIENT_TIMEOUT = + Math.toIntExact(GeodeAwaitility.getTimeout().toMillis()); + private static Random rand; + + @Before + public void setUp() { + rand = new Random(); + jedis = new JedisCluster(new HostAndPort("localhost", getPort()), REDIS_CLIENT_TIMEOUT); + } + + @After + public void tearDown() { + flushAll(); + jedis.close(); + } + + @Test + public void errors_GivenWrongNumberOfArguments() { + assertExactNumberOfArgs(jedis, Protocol.Command.RENAMENX, 2); + } + + @Test + public void testNewKey() { + jedis.set("{user1}foo", "bar"); + long result = jedis.renamenx("{user1}foo", "{user1}newfoo"); + assertThat(result).isEqualTo(1L); + assertThat(jedis.get("{user1}newfoo")).isEqualTo("bar"); + } + + @Test + public void testOldKeyIsDeleted() { + jedis.set("{user1}foo", "bar"); + long result = jedis.renamenx("{user1}foo", "{user1}newfoo"); + assertThat(result).isEqualTo(1L); + assertThat(jedis.get("{user1}foo")).isNull(); + } + + @Test + public void testRenameKeyThatDoesNotExist() { + try { + jedis.renamenx("{user1}foo", "{user1}newfoo"); + } catch (JedisDataException e) { + assertThat(e.getMessage()).contains(RedisConstants.ERROR_NO_SUCH_KEY); + } + } + + @Test + public void testHashMap() { + jedis.hset("{user1}foo", "field", "va"); + long result = jedis.renamenx("{user1}foo", "{user1}newfoo"); + assertThat(result).isEqualTo(1L); + assertThat(jedis.hget("{user1}newfoo", "field")).isEqualTo("va"); + } + + @Test + public void testSet() { + jedis.sadd("{user1}foo", "data"); + long result = jedis.renamenx("{user1}foo", "{user1}newfoo"); + assertThat(result).isEqualTo(1L); + assertThat(jedis.smembers("{user1}newfoo")).contains("data"); + } + + @Test + public void testRenameSameKey() { + jedis.set("{user1}blue", "moon"); + assertThat(jedis.renamenx("{user1}blue", "{user1}blue")).isEqualTo(0L); + assertThat(jedis.get("{user1}blue")).isEqualTo("moon"); + } + + @Test + public void testRenameKeyToExistingKey() { + jedis.set("{user1}foo1", "bar1"); + jedis.set("{user1}foo12", "bar2"); + long result = jedis.renamenx("{user1}foo1", "{user1}foo12"); + assertThat(result).isEqualTo(0L); + } + + @Test + public void testConcurrentSets() throws ExecutionException, InterruptedException { + Set<String> stringsForK1 = new HashSet<>(); + + int numOfStrings = 500000; + Callable<Long> callable1 = + () -> addStringsToKeys(stringsForK1, "{user1}k1", numOfStrings, jedis); Review comment: This method could be reworked a little, since we don't use the contents of `stringsForK1`. ########## File path: geode-apis-compatible-with-redis/src/integrationTest/java/org/apache/geode/redis/internal/executor/key/AbstractRenamenxIntegrationTest.java ########## @@ -0,0 +1,385 @@ +/* + * 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.geode.redis.internal.executor.key; + +import static org.apache.geode.redis.RedisCommandArgumentsTestHelper.assertExactNumberOfArgs; +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Random; +import java.util.Set; +import java.util.concurrent.BrokenBarrierException; +import java.util.concurrent.Callable; +import java.util.concurrent.CyclicBarrier; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import redis.clients.jedis.HostAndPort; +import redis.clients.jedis.JedisCluster; +import redis.clients.jedis.Protocol; +import redis.clients.jedis.exceptions.JedisDataException; + +import org.apache.geode.redis.RedisIntegrationTest; +import org.apache.geode.redis.internal.RedisConstants; +import org.apache.geode.redis.internal.data.RedisKey; +import org.apache.geode.redis.internal.services.StripedCoordinator; +import org.apache.geode.redis.internal.services.SynchronizedStripedCoordinator; +import org.apache.geode.test.awaitility.GeodeAwaitility; + +public abstract class AbstractRenamenxIntegrationTest implements RedisIntegrationTest { + private JedisCluster jedis; + private static final int REDIS_CLIENT_TIMEOUT = + Math.toIntExact(GeodeAwaitility.getTimeout().toMillis()); + private static Random rand; + + @Before + public void setUp() { + rand = new Random(); + jedis = new JedisCluster(new HostAndPort("localhost", getPort()), REDIS_CLIENT_TIMEOUT); + } + + @After + public void tearDown() { + flushAll(); + jedis.close(); + } + + @Test + public void errors_GivenWrongNumberOfArguments() { + assertExactNumberOfArgs(jedis, Protocol.Command.RENAMENX, 2); + } + + @Test + public void testNewKey() { + jedis.set("{user1}foo", "bar"); + long result = jedis.renamenx("{user1}foo", "{user1}newfoo"); + assertThat(result).isEqualTo(1L); + assertThat(jedis.get("{user1}newfoo")).isEqualTo("bar"); + } + + @Test + public void testOldKeyIsDeleted() { + jedis.set("{user1}foo", "bar"); + long result = jedis.renamenx("{user1}foo", "{user1}newfoo"); + assertThat(result).isEqualTo(1L); + assertThat(jedis.get("{user1}foo")).isNull(); + } + + @Test + public void testRenameKeyThatDoesNotExist() { + try { + jedis.renamenx("{user1}foo", "{user1}newfoo"); + } catch (JedisDataException e) { + assertThat(e.getMessage()).contains(RedisConstants.ERROR_NO_SUCH_KEY); + } + } + + @Test + public void testHashMap() { + jedis.hset("{user1}foo", "field", "va"); + long result = jedis.renamenx("{user1}foo", "{user1}newfoo"); + assertThat(result).isEqualTo(1L); + assertThat(jedis.hget("{user1}newfoo", "field")).isEqualTo("va"); + } + + @Test + public void testSet() { + jedis.sadd("{user1}foo", "data"); + long result = jedis.renamenx("{user1}foo", "{user1}newfoo"); + assertThat(result).isEqualTo(1L); + assertThat(jedis.smembers("{user1}newfoo")).contains("data"); + } + + @Test + public void testRenameSameKey() { + jedis.set("{user1}blue", "moon"); + assertThat(jedis.renamenx("{user1}blue", "{user1}blue")).isEqualTo(0L); + assertThat(jedis.get("{user1}blue")).isEqualTo("moon"); + } + + @Test + public void testRenameKeyToExistingKey() { + jedis.set("{user1}foo1", "bar1"); + jedis.set("{user1}foo12", "bar2"); + long result = jedis.renamenx("{user1}foo1", "{user1}foo12"); + assertThat(result).isEqualTo(0L); Review comment: Could this test also assert that the values associated with the two keys are not modified by the rename attempt: ``` assertThat(jedis.get("{user1}foo1")).isEqualTo("bar1"); assertThat(jedis.get("{user1}foo12")).isEqualTo("bar2"); ``` ########## File path: geode-apis-compatible-with-redis/src/integrationTest/java/org/apache/geode/redis/internal/executor/key/AbstractRenamenxIntegrationTest.java ########## @@ -0,0 +1,385 @@ +/* + * 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.geode.redis.internal.executor.key; + +import static org.apache.geode.redis.RedisCommandArgumentsTestHelper.assertExactNumberOfArgs; +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Random; +import java.util.Set; +import java.util.concurrent.BrokenBarrierException; +import java.util.concurrent.Callable; +import java.util.concurrent.CyclicBarrier; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import redis.clients.jedis.HostAndPort; +import redis.clients.jedis.JedisCluster; +import redis.clients.jedis.Protocol; +import redis.clients.jedis.exceptions.JedisDataException; + +import org.apache.geode.redis.RedisIntegrationTest; +import org.apache.geode.redis.internal.RedisConstants; +import org.apache.geode.redis.internal.data.RedisKey; +import org.apache.geode.redis.internal.services.StripedCoordinator; +import org.apache.geode.redis.internal.services.SynchronizedStripedCoordinator; +import org.apache.geode.test.awaitility.GeodeAwaitility; + +public abstract class AbstractRenamenxIntegrationTest implements RedisIntegrationTest { + private JedisCluster jedis; + private static final int REDIS_CLIENT_TIMEOUT = + Math.toIntExact(GeodeAwaitility.getTimeout().toMillis()); + private static Random rand; + + @Before + public void setUp() { + rand = new Random(); + jedis = new JedisCluster(new HostAndPort("localhost", getPort()), REDIS_CLIENT_TIMEOUT); + } + + @After + public void tearDown() { + flushAll(); + jedis.close(); + } + + @Test + public void errors_GivenWrongNumberOfArguments() { + assertExactNumberOfArgs(jedis, Protocol.Command.RENAMENX, 2); + } + + @Test + public void testNewKey() { + jedis.set("{user1}foo", "bar"); + long result = jedis.renamenx("{user1}foo", "{user1}newfoo"); + assertThat(result).isEqualTo(1L); + assertThat(jedis.get("{user1}newfoo")).isEqualTo("bar"); + } + + @Test + public void testOldKeyIsDeleted() { + jedis.set("{user1}foo", "bar"); + long result = jedis.renamenx("{user1}foo", "{user1}newfoo"); + assertThat(result).isEqualTo(1L); + assertThat(jedis.get("{user1}foo")).isNull(); + } + + @Test + public void testRenameKeyThatDoesNotExist() { + try { + jedis.renamenx("{user1}foo", "{user1}newfoo"); + } catch (JedisDataException e) { + assertThat(e.getMessage()).contains(RedisConstants.ERROR_NO_SUCH_KEY); + } + } + + @Test + public void testHashMap() { + jedis.hset("{user1}foo", "field", "va"); + long result = jedis.renamenx("{user1}foo", "{user1}newfoo"); + assertThat(result).isEqualTo(1L); + assertThat(jedis.hget("{user1}newfoo", "field")).isEqualTo("va"); + } + + @Test + public void testSet() { + jedis.sadd("{user1}foo", "data"); + long result = jedis.renamenx("{user1}foo", "{user1}newfoo"); + assertThat(result).isEqualTo(1L); + assertThat(jedis.smembers("{user1}newfoo")).contains("data"); + } + + @Test + public void testRenameSameKey() { + jedis.set("{user1}blue", "moon"); + assertThat(jedis.renamenx("{user1}blue", "{user1}blue")).isEqualTo(0L); + assertThat(jedis.get("{user1}blue")).isEqualTo("moon"); + } + + @Test + public void testRenameKeyToExistingKey() { + jedis.set("{user1}foo1", "bar1"); + jedis.set("{user1}foo12", "bar2"); + long result = jedis.renamenx("{user1}foo1", "{user1}foo12"); + assertThat(result).isEqualTo(0L); + } + + @Test + public void testConcurrentSets() throws ExecutionException, InterruptedException { + Set<String> stringsForK1 = new HashSet<>(); + + int numOfStrings = 500000; + Callable<Long> callable1 = + () -> addStringsToKeys(stringsForK1, "{user1}k1", numOfStrings, jedis); + Callable<Long> callable2 = () -> jedis.renamenx("{user1}k1", "{user1}k2"); + + ExecutorService pool = Executors.newFixedThreadPool(3); + Future<Long> future1 = pool.submit(callable1); + Thread.sleep(rand.nextInt(1000)); Review comment: If this sleep is absolutely necessary, it probably shouldn't be for a random amount of time, since a low value will make the test flaky. -- 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]
