kirklund commented on a change in pull request #6925: URL: https://github.com/apache/geode/pull/6925#discussion_r721754424
########## File path: geode-apis-compatible-with-redis/src/distributedTest/java/org/apache/geode/redis/internal/executor/string/MSetNXDUnitTest.java ########## @@ -0,0 +1,174 @@ +/* + * 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.string; + +import static org.apache.geode.test.dunit.rules.RedisClusterStartupRule.BIND_ADDRESS; +import static org.apache.geode.test.dunit.rules.RedisClusterStartupRule.REDIS_CLIENT_TIMEOUT; +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.List; +import java.util.concurrent.Future; +import java.util.concurrent.atomic.AtomicBoolean; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.ClassRule; +import org.junit.Ignore; +import org.junit.Test; +import redis.clients.jedis.HostAndPort; +import redis.clients.jedis.JedisCluster; + +import org.apache.geode.cache.control.RebalanceFactory; +import org.apache.geode.cache.control.ResourceManager; +import org.apache.geode.redis.ConcurrentLoopingThreads; +import org.apache.geode.test.dunit.rules.ClusterStartupRule; +import org.apache.geode.test.dunit.rules.MemberVM; +import org.apache.geode.test.dunit.rules.RedisClusterStartupRule; +import org.apache.geode.test.junit.rules.ExecutorServiceRule; + +public class MSetNXDUnitTest { + + @ClassRule + public static RedisClusterStartupRule clusterStartUp = new RedisClusterStartupRule(4); + + @ClassRule + public static ExecutorServiceRule executor = new ExecutorServiceRule(); + + private static final String HASHTAG = "{tag}"; + private static JedisCluster jedis; + private static MemberVM server1; + private static int locatorPort; + + private static MemberVM locator; + + @BeforeClass + public static void classSetup() { + locator = clusterStartUp.startLocatorVM(0); + locatorPort = locator.getPort(); + server1 = clusterStartUp.startRedisVM(1, locatorPort); + clusterStartUp.startRedisVM(2, locatorPort); + clusterStartUp.startRedisVM(3, locatorPort); + + int redisServerPort1 = clusterStartUp.getRedisPort(1); + jedis = new JedisCluster(new HostAndPort(BIND_ADDRESS, redisServerPort1), REDIS_CLIENT_TIMEOUT); + } + + @After + public void after() { + clusterStartUp.flushAll(); + } + + @AfterClass + public static void tearDown() { + jedis.close(); + } + + @Test + public void testMSetnx_concurrentInstancesHandleBucketMovement() { + int KEY_COUNT = 5000; + String[] keys = new String[KEY_COUNT]; + + for (int i = 0; i < keys.length; i++) { + keys[i] = HASHTAG + "key" + i; + } + String[] keysAndValues1 = makeKeysAndValues(keys, "valueOne"); + String[] keysAndValues2 = makeKeysAndValues(keys, "valueTwo"); + + new ConcurrentLoopingThreads(100, + i -> jedis.msetnx(keysAndValues1), + i -> jedis.msetnx(keysAndValues2), + i -> clusterStartUp.moveBucketForKey(keys[0])) + .runWithAction(() -> { + assertThat(jedis.mget(keys)).satisfiesAnyOf( + values -> assertThat(values) + .allSatisfy(value -> assertThat(value).startsWith("valueOne")), + values -> assertThat(values) + .allSatisfy(value -> assertThat(value).startsWith("valueTwo"))); + jedis.del(keys); + }); + } + + @Ignore("GEODE-9604") + @Test + public void testMSetnx_crashDoesNotLeaveInconsistencies() throws Exception { + int KEY_COUNT = 1000; + String[] keys = new String[KEY_COUNT]; + + for (int i = 0; i < keys.length; i++) { + keys[i] = HASHTAG + "key" + i; + } + String[] keysAndValues1 = makeKeysAndValues(keys, "valueOne"); + String[] keysAndValues2 = makeKeysAndValues(keys, "valueTwo"); + AtomicBoolean running = new AtomicBoolean(true); + + Future<?> future = executor.submit(() -> { + for (int i = 0; i < 20 && running.get(); i++) { + clusterStartUp.moveBucketForKey(keys[0], "server-3"); + // Sleep for a bit so that MSETs can execute + Thread.sleep(2000); Review comment: Another option that can be a bit cleaner and more efficient is to introduce a `CountDownLatch` above this block. All of these threads would then await the latch: ``` CountDownLatch latch = new CountDownLatch(1); ``` ``` Future<?> future = executor.submit(() -> { for (int i = 0; i < 20 && running.get(); i++) { clusterStartUp.moveBucketForKey(keys[0], "server-3"); // Sleep for a bit so that MSETs can execute latch.await(GeodeAwaitility.getTimeout().getSeconds(), TimeUnit.SECONDS); ``` And then later on where you're executing MSETs or whatever you want to sleep a bit for, you open that latch: ``` latch.countDown(); ``` If there are _n_ threads concurrently doing something you want to pause for, then use `new CountDownLatch(n)` and have each of those threads `countDown` when you reach the mid-point or whatever you're pausing for. -- 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]
