You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@geode.apache.org by GitBox <gi...@apache.org> on 2022/02/17 22:42:31 UTC

[GitHub] [geode] DonalEvans commented on a change in pull request #7380: GEODE-9947: Implemented LINDEX command

DonalEvans commented on a change in pull request #7380:
URL: https://github.com/apache/geode/pull/7380#discussion_r809516517



##########
File path: geode-docs/tools_modules/geode_for_redis.html.md.erb
##########
@@ -186,7 +186,7 @@ Could not connect to Redis at 127.0.0.1:6379: Connection refused
 | HINCRBY | HINCRBYFLOAT | HKEYS | HLEN |
 | HMGET | HMSET | HSCAN **[3]** | HSET |
 | HSETNX | HSTRLEN | HVALS | INCR |
-| INCRBY | INCRBYFLOAT | INFO **[4]** | KEYS |
+| INCRBY | INCRBYFLOAT | INFO **[4]** | KEYS | LINDEX |

Review comment:
       I think that the formatting of this section will get messed up if you just add another element like this, since it's currently set up as a table with 4 columns. Adding LINDEX here will (I think) add a fifth column with only LINDEX in it. This makes updating the list of commands a nightmare, since adding one means you need to adjust every command that comes after it in the list. We probably need a better way of handling this section if we're planning to modify it frequently. Maybe breaking it up by command type? That's work for another ticket though, I think.
   
   Luckily (for this PR, at least) if you add LPUSH, LPOP and LLEN to the list of supported commands too, then not only will the docs be up to date, but you can just add one extra row of 4 elements below this one and not have to worry about affecting the rest of the table.

##########
File path: geode-for-redis/src/main/java/org/apache/geode/redis/internal/data/RedisList.java
##########
@@ -42,6 +42,24 @@ public RedisList() {
     this.elementList = new SizeableByteArrayList();
   }
 
+  /**
+   * @param index index of desired element. Positive index starts at the head. Negative index starts
+   *        at the tail.
+   * @return element at index. nil if index is out of range.

Review comment:
       This should be "null" rather than "nil".

##########
File path: geode-for-redis/src/integrationTest/java/org/apache/geode/redis/internal/commands/executor/list/AbstractLIndexIntegrationTest.java
##########
@@ -0,0 +1,136 @@
+/*
+ * 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.redis.RedisCommandArgumentsTestHelper.assertExactNumberOfArgs;
+import static org.apache.geode.redis.internal.RedisConstants.ERROR_NOT_INTEGER;
+import static org.apache.geode.redis.internal.RedisConstants.ERROR_WRONG_TYPE;
+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.util.concurrent.atomic.AtomicReference;
+
+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 org.apache.geode.redis.ConcurrentLoopingThreads;
+import org.apache.geode.redis.RedisIntegrationTest;
+
+public abstract class AbstractLIndexIntegrationTest implements RedisIntegrationTest {
+  private static final String NON_EXISTENT_LIST_KEY = "{tag1}nonExistentKey";
+  private static final String LIST_KEY = "{tag1}listKey";
+  private static final String[] LIST_ELEMENTS =
+      {"aardvark", "bats", "chameleon", "deer", "elephant", "flamingo", "goat"};
+  private static final int INDEX = 3;
+  private JedisCluster jedis;
+
+  @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 lindex_wrongNumberOfArgs_returnsError() {
+    assertExactNumberOfArgs(jedis, Protocol.Command.LINDEX, 2);
+  }
+
+  @Test
+  public void lindex_withPositiveIndex_withNonExistentList_returnsNull() {
+    assertThat(jedis.lindex(NON_EXISTENT_LIST_KEY, INDEX)).isNull();
+  }
+
+  @Test
+  public void lindex_withNegativeIndex_withNonExistentList_returnsNull() {
+    assertThat(jedis.lindex(NON_EXISTENT_LIST_KEY, -INDEX)).isNull();
+  }
+
+  @Test
+  public void lindex_withPositiveIndex_returnsElemnt() {
+    jedis.lpush(LIST_KEY, LIST_ELEMENTS);
+    assertThat(jedis.lindex(LIST_KEY, INDEX)).isEqualTo("deer");
+  }
+
+  @Test
+  public void lindex_withNegativeIndex_returnsElemnt() {
+    jedis.lpush(LIST_KEY, LIST_ELEMENTS);
+    assertThat(jedis.lindex(LIST_KEY, -INDEX)).isEqualTo("chameleon");
+  }

Review comment:
       Could these tests be expanded to check every element in the list, just to cover the weird corner case where somehow we always return the same value regardless of index?

##########
File path: geode-for-redis/src/integrationTest/java/org/apache/geode/redis/internal/commands/executor/list/AbstractLIndexIntegrationTest.java
##########
@@ -0,0 +1,136 @@
+/*
+ * 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.redis.RedisCommandArgumentsTestHelper.assertExactNumberOfArgs;
+import static org.apache.geode.redis.internal.RedisConstants.ERROR_NOT_INTEGER;
+import static org.apache.geode.redis.internal.RedisConstants.ERROR_WRONG_TYPE;
+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.util.concurrent.atomic.AtomicReference;
+
+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 org.apache.geode.redis.ConcurrentLoopingThreads;
+import org.apache.geode.redis.RedisIntegrationTest;
+
+public abstract class AbstractLIndexIntegrationTest implements RedisIntegrationTest {
+  private static final String NON_EXISTENT_LIST_KEY = "{tag1}nonExistentKey";
+  private static final String LIST_KEY = "{tag1}listKey";
+  private static final String[] LIST_ELEMENTS =
+      {"aardvark", "bats", "chameleon", "deer", "elephant", "flamingo", "goat"};
+  private static final int INDEX = 3;
+  private JedisCluster jedis;
+
+  @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 lindex_wrongNumberOfArgs_returnsError() {
+    assertExactNumberOfArgs(jedis, Protocol.Command.LINDEX, 2);
+  }
+
+  @Test
+  public void lindex_withPositiveIndex_withNonExistentList_returnsNull() {
+    assertThat(jedis.lindex(NON_EXISTENT_LIST_KEY, INDEX)).isNull();
+  }
+
+  @Test
+  public void lindex_withNegativeIndex_withNonExistentList_returnsNull() {
+    assertThat(jedis.lindex(NON_EXISTENT_LIST_KEY, -INDEX)).isNull();
+  }
+
+  @Test
+  public void lindex_withPositiveIndex_returnsElemnt() {
+    jedis.lpush(LIST_KEY, LIST_ELEMENTS);
+    assertThat(jedis.lindex(LIST_KEY, INDEX)).isEqualTo("deer");
+  }
+
+  @Test
+  public void lindex_withNegativeIndex_returnsElemnt() {
+    jedis.lpush(LIST_KEY, LIST_ELEMENTS);
+    assertThat(jedis.lindex(LIST_KEY, -INDEX)).isEqualTo("chameleon");
+  }
+
+  @Test
+  public void lindex_withPositiveOutOfRangeIndex_returnsNull() {
+    jedis.lpush(LIST_KEY, LIST_ELEMENTS);
+    assertThat(jedis.lindex(LIST_KEY, 10)).isNull();
+  }
+
+  @Test
+  public void lindex_withNegativeOutOfRangeIndex_returnsNull() {
+    jedis.lpush(LIST_KEY, LIST_ELEMENTS);
+    assertThat(jedis.lindex(LIST_KEY, -10)).isNull();
+  }
+
+  @Test
+  public void lindex_withInvalidIndex_withNonExistentList_returnsNull() {
+    assertThat(jedis.sendCommand(NON_EXISTENT_LIST_KEY, Protocol.Command.LINDEX,
+        NON_EXISTENT_LIST_KEY, "b")).isNull();
+  }
+
+  @Test
+  public void lindex_withInvalidIndex_returnsError() {
+    jedis.lpush(LIST_KEY, LIST_ELEMENTS);
+    assertThatThrownBy(() -> jedis.sendCommand(LIST_KEY, Protocol.Command.LINDEX, LIST_KEY, "b"))
+        .hasMessage("ERR " + ERROR_NOT_INTEGER);
+  }
+
+  @Test
+  public void lindex_withWrongKeyType_returnsWrongTypeError() {
+    String key = "{tag1}ding";
+    jedis.set(key, "dong");
+    assertThatThrownBy(() -> jedis.lindex(key, 2)).hasMessage("WRONGTYPE " + ERROR_WRONG_TYPE);
+  }
+
+  @Test
+  public void lindex_withWrongKeyType_withInvalidIndex_returnsWrongTypeError() {
+    String key = "{tag1}ding";
+    jedis.set(key, "dong");
+    assertThatThrownBy(() -> jedis.sendCommand(LIST_KEY, Protocol.Command.LINDEX, key, "b"))
+        .hasMessage("WRONGTYPE " + ERROR_WRONG_TYPE);
+  }
+
+  @Test
+  public void ensureListConsistency_whenRunningConcurrently() {
+    final AtomicReference<String> lindexResultReference = new AtomicReference<>();
+    new ConcurrentLoopingThreads(1000,
+        i -> jedis.lpush(LIST_KEY, LIST_ELEMENTS),
+        i -> lindexResultReference.set(jedis.lindex(LIST_KEY, -7)))
+            .runWithAction(() -> {
+              assertThat(lindexResultReference).satisfiesAnyOf(
+                  lindexResult -> assertThat(lindexResult.get()).isNull(),
+                  lindexResult -> assertThat(lindexResult.get()).isEqualTo("goat"));
+              jedis.del(LIST_KEY);

Review comment:
       This test might be a little bit better if instead of checking the concurrency behaviour between creating a new list via LPUSH and checking the index via LINDEX, it was checking the behaviour when adding a different set of elements to an already existing list, and checking that the last element is as expected. By checking the last element, we ensure that the index is never out of range, so we're more likely to catch any issues where we're reading the list while still adding elements to it.




-- 
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-unsubscribe@geode.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org