ezoerner commented on a change in pull request #6877:
URL: https://github.com/apache/geode/pull/6877#discussion_r718728646
##########
File path:
geode-apis-compatible-with-redis/src/integrationTest/java/org/apache/geode/redis/internal/executor/key/AbstractRenameIntegrationTest.java
##########
@@ -188,38 +244,39 @@ public void
shouldNotDeadlock_concurrentRenames_givenStripeContention()
}
@Test
- public void shouldThrowError_givenKeyDeletedDuringRename()
- throws ExecutionException, InterruptedException {
- CyclicBarrier startCyclicBarrier = new CyclicBarrier(2);
- ExecutorService pool = Executors.newFixedThreadPool(2);
-
- for (int i = 0; i < 100; i++) {
- jedis.set("{user1}oldKey", "foo");
+ public void shouldError_givenKeyDeletedDuringRename() {
+ int iterations = 2000;
- Runnable renameOldKeyToNewKey = () -> {
- cyclicBarrierAwait(startCyclicBarrier);
+ final AtomicReference<RuntimeException> renameException = new
AtomicReference<>(null);
- jedis.rename("{user1}oldKey", "{user1}newKey");
- };
+ jedis.set("{user1}oldKey", "foo");
- Runnable deleteOldKey = () -> {
- cyclicBarrierAwait(startCyclicBarrier);
- jedis.del("{user1}oldKey");
- };
-
- Future<?> future1 = pool.submit(renameOldKeyToNewKey);
- Future<?> future2 = pool.submit(deleteOldKey);
-
- try {
- future1.get();
- assertThat(jedis.get("{user1}newKey")).isEqualTo("foo");
- } catch (Exception e) {
- assertThat(e).hasMessageContaining("no such key");
- }
- future2.get();
-
- assertThat(jedis.get("{user1}oldKey")).isNull();
+ try {
+ new ConcurrentLoopingThreads(iterations,
+ i -> {
+ try {
+ jedis.rename("{user1}oldKey", "{user1}newKey");
+ } catch (RuntimeException e) {
+ renameException.set(e);
+ }
+ },
+ i -> jedis.del("{user1}oldKey"))
+ .runWithAction(() -> {
+ RuntimeException e = renameException.get();
+ if (e != null) {
+ throw e;
+ }
+ assertThat(jedis.get("{user1}newKey")).isEqualTo("foo");
+ assertThat(jedis.exists("{user1}oldKey")).isFalse();
+ jedis.set("{user1}oldKey", "foo");
+ });
+ } catch (RuntimeException e) {
+ assertThat(e).hasMessageContaining(ERROR_NO_SUCH_KEY);
Review comment:
The intention with this test would be that it would exit as successful
as soon as the no such key exception was thrown.
I did go back and re-verify that if the exception is not captured and
rethrown by the action then you get `expected:<"foo"> but was:<null>`, but you
don't if the exception is captured and rethrown. This is hard evidence that the
exception is not propagated to the caller.
Also, I ran your code above and got the following output:
```
running action
java.util.concurrent.ExecutionException: java.lang.RuntimeException: BANG!
```
... which shows that the action **IS** called before the exception is
propagated to the caller.
##########
File path:
geode-apis-compatible-with-redis/src/integrationTest/java/org/apache/geode/redis/internal/executor/key/AbstractRenameIntegrationTest.java
##########
@@ -188,38 +244,39 @@ public void
shouldNotDeadlock_concurrentRenames_givenStripeContention()
}
@Test
- public void shouldThrowError_givenKeyDeletedDuringRename()
- throws ExecutionException, InterruptedException {
- CyclicBarrier startCyclicBarrier = new CyclicBarrier(2);
- ExecutorService pool = Executors.newFixedThreadPool(2);
-
- for (int i = 0; i < 100; i++) {
- jedis.set("{user1}oldKey", "foo");
+ public void shouldError_givenKeyDeletedDuringRename() {
+ int iterations = 2000;
- Runnable renameOldKeyToNewKey = () -> {
- cyclicBarrierAwait(startCyclicBarrier);
+ final AtomicReference<RuntimeException> renameException = new
AtomicReference<>(null);
- jedis.rename("{user1}oldKey", "{user1}newKey");
- };
+ jedis.set("{user1}oldKey", "foo");
- Runnable deleteOldKey = () -> {
- cyclicBarrierAwait(startCyclicBarrier);
- jedis.del("{user1}oldKey");
- };
-
- Future<?> future1 = pool.submit(renameOldKeyToNewKey);
- Future<?> future2 = pool.submit(deleteOldKey);
-
- try {
- future1.get();
- assertThat(jedis.get("{user1}newKey")).isEqualTo("foo");
- } catch (Exception e) {
- assertThat(e).hasMessageContaining("no such key");
- }
- future2.get();
-
- assertThat(jedis.get("{user1}oldKey")).isNull();
+ try {
+ new ConcurrentLoopingThreads(iterations,
+ i -> {
+ try {
+ jedis.rename("{user1}oldKey", "{user1}newKey");
+ } catch (RuntimeException e) {
+ renameException.set(e);
+ }
+ },
+ i -> jedis.del("{user1}oldKey"))
+ .runWithAction(() -> {
+ RuntimeException e = renameException.get();
+ if (e != null) {
+ throw e;
+ }
+ assertThat(jedis.get("{user1}newKey")).isEqualTo("foo");
+ assertThat(jedis.exists("{user1}oldKey")).isFalse();
+ jedis.set("{user1}oldKey", "foo");
+ });
+ } catch (RuntimeException e) {
+ assertThat(e).hasMessageContaining(ERROR_NO_SUCH_KEY);
Review comment:
The intention with this test would be that it would exit as successful
as soon as the no such key exception was thrown.
I did go back and re-verify that if the exception is not captured and
rethrown by the action then you get `expected:<"foo"> but was:<null>`, but you
don't if the exception is captured and rethrown. This is hard evidence that the
exception is not propagated to the caller _before the action is called_.
Also, I ran your code above and got the following output:
```
running action
java.util.concurrent.ExecutionException: java.lang.RuntimeException: BANG!
```
... which shows that the action **IS** called before the exception is
propagated to the caller.
##########
File path:
geode-apis-compatible-with-redis/src/main/java/org/apache/geode/redis/internal/data/RedisKeyCommandsFunctionExecutor.java
##########
@@ -135,12 +137,22 @@ public void restore(RedisKey key, long ttl, byte[] data,
RestoreOptions options)
}
@Override
- public boolean rename(RedisKey oldKey, RedisKey newKey) {
+ public boolean rename(RedisKey oldKey, RedisKey newKey, boolean
ifTargetNotExists) {
List<RedisKey> lockOrdering = Arrays.asList(oldKey, newKey);
return stripedExecute(oldKey, lockOrdering,
- () ->
getRedisData(oldKey).rename(getRegionProvider().getLocalDataRegion(), oldKey,
- newKey));
+ () -> {
+ RedisData sourceData = getRedisData(oldKey);
+ // nonexistent source takes priority over nonexistent target
+ if (!sourceData.exists()) {
+ return false;
+ }
+ if (ifTargetNotExists && getRedisData(newKey).exists()) {
Review comment:
We still need to check for the existence of the source key first because
that takes priority over the target key.
##########
File path:
geode-apis-compatible-with-redis/src/main/java/org/apache/geode/redis/internal/data/RedisKeyCommandsFunctionExecutor.java
##########
@@ -135,12 +137,22 @@ public void restore(RedisKey key, long ttl, byte[] data,
RestoreOptions options)
}
@Override
- public boolean rename(RedisKey oldKey, RedisKey newKey) {
+ public boolean rename(RedisKey oldKey, RedisKey newKey, boolean
ifTargetNotExists) {
List<RedisKey> lockOrdering = Arrays.asList(oldKey, newKey);
return stripedExecute(oldKey, lockOrdering,
- () ->
getRedisData(oldKey).rename(getRegionProvider().getLocalDataRegion(), oldKey,
- newKey));
+ () -> {
+ RedisData sourceData = getRedisData(oldKey);
+ // nonexistent source takes priority over nonexistent target
+ if (!sourceData.exists()) {
+ return false;
+ }
+ if (ifTargetNotExists && getRedisData(newKey).exists()) {
Review comment:
yes, I believe that will work
##########
File path:
geode-apis-compatible-with-redis/src/main/java/org/apache/geode/redis/internal/data/RedisKeyCommandsFunctionExecutor.java
##########
@@ -135,12 +137,22 @@ public void restore(RedisKey key, long ttl, byte[] data,
RestoreOptions options)
}
@Override
- public boolean rename(RedisKey oldKey, RedisKey newKey) {
+ public boolean rename(RedisKey oldKey, RedisKey newKey, boolean
ifTargetNotExists) {
List<RedisKey> lockOrdering = Arrays.asList(oldKey, newKey);
return stripedExecute(oldKey, lockOrdering,
- () ->
getRedisData(oldKey).rename(getRegionProvider().getLocalDataRegion(), oldKey,
- newKey));
+ () -> {
+ RedisData sourceData = getRedisData(oldKey);
+ // nonexistent source takes priority over nonexistent target
+ if (!sourceData.exists()) {
+ return false;
+ }
+ if (ifTargetNotExists && getRedisData(newKey).exists()) {
Review comment:
For some reason the Region interface doesn't support `putIfAbsent` with
a callback argument, but using `create` should work.
--
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]