nonbinaryprogrammer commented on a change in pull request #7408: URL: https://github.com/apache/geode/pull/7408#discussion_r825047945
########## File path: geode-for-redis/src/test/java/org/apache/geode/redis/internal/eventing/BlockingCommandListenerTest.java ########## @@ -0,0 +1,72 @@ +/* + * 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.eventing; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import org.junit.Test; +import org.mockito.ArgumentCaptor; + +import org.apache.geode.redis.internal.commands.Command; +import org.apache.geode.redis.internal.commands.RedisCommandType; +import org.apache.geode.redis.internal.netty.Coder; +import org.apache.geode.redis.internal.netty.ExecutionHandlerContext; + +public class BlockingCommandListenerTest { + + @Test + public void testTimeoutIsAdjusted() { + ExecutionHandlerContext context = mock(ExecutionHandlerContext.class); + List<byte[]> commandArgs = Arrays.asList("KEY".getBytes(), "0".getBytes()); Review comment: you can avoid a call to `getBytes()` by just doing `'0'` ########## File path: geode-for-redis/src/main/java/org/apache/geode/redis/internal/eventing/BlockingCommandListener.java ########## @@ -0,0 +1,119 @@ +/* + * 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.eventing; + +import java.util.Collections; +import java.util.List; + +import org.apache.geode.redis.internal.commands.Command; +import org.apache.geode.redis.internal.commands.RedisCommandType; +import org.apache.geode.redis.internal.commands.executor.RedisResponse; +import org.apache.geode.redis.internal.data.RedisKey; +import org.apache.geode.redis.internal.netty.Coder; +import org.apache.geode.redis.internal.netty.ExecutionHandlerContext; + +public class BlockingCommandListener implements EventListener { + + private final ExecutionHandlerContext context; + private final RedisCommandType command; + private final List<RedisKey> keys; + private final List<byte[]> commandArgs; + private final long timeoutNanos; + private final long timeSubmitted; + private Runnable cleanupTask; + + /** + * Constructor to create an instance of a BlockingCommandListener in response to a blocking + * command. When receiving a relevant event, blocking commands simply resubmit the command + * into the Netty pipeline. + * + * @param context the associated ExecutionHandlerContext + * @param command the blocking command associated with this listener + * @param keys the list of keys the command is interested in + * @param timeoutSeconds the timeout for the command to block in seconds + * @param commandArgs all arguments to the command which are used for resubmission + */ + public BlockingCommandListener(ExecutionHandlerContext context, RedisCommandType command, + List<RedisKey> keys, double timeoutSeconds, List<byte[]> commandArgs) { + this.context = context; + this.command = command; + this.timeoutNanos = (long) (timeoutSeconds * 1e9); + this.keys = Collections.unmodifiableList(keys); + this.commandArgs = commandArgs; + timeSubmitted = System.nanoTime(); + } + + @Override + public List<RedisKey> keys() { + return keys; + } + + @Override + public EventResponse process(RedisCommandType commandType, RedisKey key) { + if (!keys.contains(key)) { + return EventResponse.CONTINUE; + } + + resubmitCommand(); + return EventResponse.REMOVE_AND_STOP; + } + + @Override + public void resubmitCommand() { + // Recalculate the timeout since we've already been waiting + double adjustedTimeoutSeconds = 0; + if (timeoutNanos > 0) { + long adjustedTimeoutNanos = timeoutNanos - (System.nanoTime() - timeSubmitted); Review comment: I don't think there is any reason to have parens here ########## File path: geode-for-redis/src/integrationTest/java/org/apache/geode/redis/internal/commands/executor/list/AbstractBLPopIntegrationTest.java ########## @@ -0,0 +1,142 @@ +/* + * 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.commands.executor.list; + +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 static org.assertj.core.api.Assertions.assertThatThrownBy; + +import java.time.Duration; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.concurrent.Future; +import java.util.concurrent.atomic.AtomicBoolean; + +import org.junit.After; +import org.junit.Before; +import org.junit.ClassRule; +import org.junit.Test; +import redis.clients.jedis.HostAndPort; +import redis.clients.jedis.JedisCluster; +import redis.clients.jedis.Protocol; + +import org.apache.geode.redis.RedisIntegrationTest; +import org.apache.geode.redis.internal.RedisConstants; +import org.apache.geode.test.awaitility.GeodeAwaitility; +import org.apache.geode.test.junit.rules.ExecutorServiceRule; + +public abstract class AbstractBLPopIntegrationTest implements RedisIntegrationTest { + private static final String KEY = "key"; + + protected JedisCluster jedis; + + public abstract void awaitEventDistributorSize(int size) throws Exception; + + @ClassRule + public static ExecutorServiceRule executor = new ExecutorServiceRule(); + + @Before + public void setUp() { + jedis = new JedisCluster(new HostAndPort(BIND_ADDRESS, getPort()), REDIS_CLIENT_TIMEOUT); + } + + @After + public void tearDown() { + flushAll(); + jedis.close(); + } + + @Test + public void testInvalidArguments_throwErrors() { + assertThatThrownBy(() -> jedis.sendCommand("key", Protocol.Command.BLPOP)) + .hasMessageContaining("ERR wrong number of arguments for 'blpop' command"); + assertThatThrownBy(() -> jedis.sendCommand("key1", Protocol.Command.BLPOP, "key")) + .hasMessageContaining("ERR wrong number of arguments for 'blpop' command"); + } + + @Test + public void testInvalidTimeout_throwsError() { + assertThatThrownBy(() -> jedis.sendCommand("key1", Protocol.Command.BLPOP, "key1", + "0.A")) + .hasMessageContaining(RedisConstants.ERROR_TIMEOUT_INVALID); + } + + @Test + public void testKeysInDifferentSlots_throwsError() { + assertThatThrownBy(() -> jedis.sendCommand("key1", Protocol.Command.BLPOP, "key1", + "key2", "0")) + .hasMessageContaining(RedisConstants.ERROR_WRONG_SLOT); + } + Review comment: I'd like to see a test of what happens if we try to BLPOP a key that isn't a list ########## File path: geode-for-redis/src/main/java/org/apache/geode/redis/internal/data/RedisList.java ########## @@ -154,6 +160,35 @@ public long lpush(List<byte[]> elementsToAdd, Region<RedisKey, RedisData> region return popped; } + public static List<byte[]> blpop(ExecutionHandlerContext context, List<RedisKey> keys, + double timeoutSeconds) { + RegionProvider regionProvider = context.getRegionProvider(); + for (RedisKey key : keys) { + RedisList list = regionProvider.getTypedRedisData(REDIS_LIST, key, false); + if (!list.isNull()) { + byte[] poppedValue = list.lpop(context.getRegion(), key); + + // return the key and value + List<byte[]> result = new ArrayList<>(2); + result.add(key.toBytes()); + result.add(poppedValue); + return result; + } + } + + List<byte[]> commandArgs = new ArrayList<>(keys.size() + 1); + commandArgs.add(RedisCommandType.BLPOP.name().getBytes()); + keys.forEach(x -> commandArgs.add(x.toBytes())); + // This is a placeholder for the timeout. If the command is resubmitted it will be updated with + // an adjusted timeout value. + commandArgs.add("0".getBytes()); Review comment: why not just `commandArgs.add('0');`? ########## File path: geode-for-redis/src/test/java/org/apache/geode/redis/internal/eventing/BlockingCommandListenerTest.java ########## @@ -0,0 +1,72 @@ +/* + * 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.eventing; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import org.junit.Test; +import org.mockito.ArgumentCaptor; + +import org.apache.geode.redis.internal.commands.Command; +import org.apache.geode.redis.internal.commands.RedisCommandType; +import org.apache.geode.redis.internal.netty.Coder; +import org.apache.geode.redis.internal.netty.ExecutionHandlerContext; + +public class BlockingCommandListenerTest { + + @Test + public void testTimeoutIsAdjusted() { + ExecutionHandlerContext context = mock(ExecutionHandlerContext.class); + List<byte[]> commandArgs = Arrays.asList("KEY".getBytes(), "0".getBytes()); + BlockingCommandListener listener = + new BlockingCommandListener(context, RedisCommandType.BLPOP, Collections.emptyList(), + 1.0D, + commandArgs); + + listener.resubmitCommand(); + + ArgumentCaptor<Command> argumentCaptor = ArgumentCaptor.forClass(Command.class); + verify(context, times(1)).resubmitCommand(argumentCaptor.capture()); + + double timeout = Coder.bytesToDouble(argumentCaptor.getValue().getCommandArguments().get(0)); + assertThat(timeout).isLessThan(1.0D); + } + + @Test + public void testAdjustedTimeoutDoesNotBecomeNegative() { + ExecutionHandlerContext context = mock(ExecutionHandlerContext.class); + List<byte[]> commandArgs = Arrays.asList("KEY".getBytes(), "0".getBytes()); Review comment: same here ########## File path: geode-for-redis/src/integrationTest/java/org/apache/geode/redis/internal/eventing/EventDistributorTest.java ########## @@ -0,0 +1,195 @@ +/* + * 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.eventing; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.time.Duration; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Random; +import java.util.stream.Collectors; + +import org.junit.Test; + +import org.apache.geode.redis.ConcurrentLoopingThreads; +import org.apache.geode.redis.internal.commands.RedisCommandType; +import org.apache.geode.redis.internal.data.RedisKey; +import org.apache.geode.test.awaitility.GeodeAwaitility; + +public class EventDistributorTest { + + public static class TestEventListener implements EventListener { + private final List<RedisKey> keys; + private int fired = 0; + private final long timeout; + + public TestEventListener(RedisKey... keys) { + this(0, keys); + } + + public TestEventListener(long timeout, RedisKey... keys) { + this.keys = Arrays.stream(keys).collect(Collectors.toList()); + this.timeout = timeout; + } + + public int getFired() { + return fired; + } + + @Override + public EventResponse process(RedisCommandType commandType, RedisKey key) { + fired += 1; + return EventResponse.REMOVE_AND_STOP; + } + + @Override + public List<RedisKey> keys() { + return keys; + } + + @Override + public void resubmitCommand() {} + + @Override + public long getTimeout() { + return timeout; + } + + @Override + public void timeout() {} + + @Override + public void setCleanupTask(Runnable r) {} + + @Override + public void cleanup() {} + } + + @Test + public void firingEventRemovesListener() { + RedisKey keyA = new RedisKey("a".getBytes()); + RedisKey keyB = new RedisKey("b".getBytes()); + EventDistributor distributor = new EventDistributor(); + TestEventListener listener = new TestEventListener(keyA, keyB); + distributor.registerListener(listener); + + distributor.fireEvent(null, keyA); + assertThat(listener.getFired()).isEqualTo(1); + assertThat(distributor.size()).isEqualTo(0); + } + + @Test + public void firingEventRemovesFirstListener_whenMultipleExist() { + RedisKey keyA = new RedisKey("a".getBytes()); + RedisKey keyB = new RedisKey("b".getBytes()); + EventDistributor distributor = new EventDistributor(); + TestEventListener listener1 = new TestEventListener(keyA, keyB); + TestEventListener listener2 = new TestEventListener(keyA, keyB); + distributor.registerListener(listener1); + distributor.registerListener(listener2); + + assertThat(distributor.size()).isEqualTo(4); + + distributor.fireEvent(null, keyA); + assertThat(listener1.getFired()).isEqualTo(1); + assertThat(listener2.getFired()).isEqualTo(0); + assertThat(distributor.size()).isEqualTo(2); + + distributor.fireEvent(null, keyA); + assertThat(listener1.getFired()).isEqualTo(1); + assertThat(listener2.getFired()).isEqualTo(1); + assertThat(distributor.size()).isEqualTo(0); + } + + @Test + public void listenerIsRemovedAfterTimeout() { + RedisKey keyA = new RedisKey("a".getBytes()); + EventDistributor distributor = new EventDistributor(); + TestEventListener listener1 = new TestEventListener(1, keyA); Review comment: what are the units for this timeout? it doesn't say anywhere in the comments or code. If the timeout is in seconds then this seems like it has the potential to be a very flaky test ########## File path: geode-for-redis/src/integrationTest/java/org/apache/geode/redis/internal/eventing/EventDistributorTest.java ########## @@ -0,0 +1,195 @@ +/* + * 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.eventing; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.time.Duration; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Random; +import java.util.stream.Collectors; + +import org.junit.Test; + +import org.apache.geode.redis.ConcurrentLoopingThreads; +import org.apache.geode.redis.internal.commands.RedisCommandType; +import org.apache.geode.redis.internal.data.RedisKey; +import org.apache.geode.test.awaitility.GeodeAwaitility; + +public class EventDistributorTest { + + public static class TestEventListener implements EventListener { + private final List<RedisKey> keys; + private int fired = 0; + private final long timeout; + + public TestEventListener(RedisKey... keys) { + this(0, keys); + } + + public TestEventListener(long timeout, RedisKey... keys) { + this.keys = Arrays.stream(keys).collect(Collectors.toList()); + this.timeout = timeout; + } + + public int getFired() { + return fired; + } + + @Override + public EventResponse process(RedisCommandType commandType, RedisKey key) { + fired += 1; + return EventResponse.REMOVE_AND_STOP; + } + + @Override + public List<RedisKey> keys() { + return keys; + } + + @Override + public void resubmitCommand() {} + + @Override + public long getTimeout() { + return timeout; + } + + @Override + public void timeout() {} + + @Override + public void setCleanupTask(Runnable r) {} + + @Override + public void cleanup() {} + } + + @Test + public void firingEventRemovesListener() { + RedisKey keyA = new RedisKey("a".getBytes()); + RedisKey keyB = new RedisKey("b".getBytes()); + EventDistributor distributor = new EventDistributor(); + TestEventListener listener = new TestEventListener(keyA, keyB); + distributor.registerListener(listener); + + distributor.fireEvent(null, keyA); + assertThat(listener.getFired()).isEqualTo(1); + assertThat(distributor.size()).isEqualTo(0); + } + + @Test + public void firingEventRemovesFirstListener_whenMultipleExist() { + RedisKey keyA = new RedisKey("a".getBytes()); + RedisKey keyB = new RedisKey("b".getBytes()); + EventDistributor distributor = new EventDistributor(); + TestEventListener listener1 = new TestEventListener(keyA, keyB); + TestEventListener listener2 = new TestEventListener(keyA, keyB); + distributor.registerListener(listener1); + distributor.registerListener(listener2); + + assertThat(distributor.size()).isEqualTo(4); + + distributor.fireEvent(null, keyA); + assertThat(listener1.getFired()).isEqualTo(1); + assertThat(listener2.getFired()).isEqualTo(0); + assertThat(distributor.size()).isEqualTo(2); + + distributor.fireEvent(null, keyA); + assertThat(listener1.getFired()).isEqualTo(1); + assertThat(listener2.getFired()).isEqualTo(1); + assertThat(distributor.size()).isEqualTo(0); + } + + @Test + public void listenerIsRemovedAfterTimeout() { + RedisKey keyA = new RedisKey("a".getBytes()); + EventDistributor distributor = new EventDistributor(); + TestEventListener listener1 = new TestEventListener(1, keyA); Review comment: looking farther down the code it looks like that is in seconds. seems like the timeout should be less than the `atMost` in the awaitility -- 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: notifications-unsubscr...@geode.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org