sabbey37 commented on a change in pull request #6489: URL: https://github.com/apache/geode/pull/6489#discussion_r638842819
########## File path: geode-apis-compatible-with-redis/src/integrationTest/java/org/apache/geode/redis/internal/executor/sortedset/AbstractZAddIntegrationTest.java ########## @@ -0,0 +1,231 @@ +/* + * 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.sortedset; + +import static org.apache.geode.redis.RedisCommandArgumentsTestHelper.assertAtLeastNArgs; +import static org.apache.geode.redis.internal.RedisConstants.ERROR_INVALID_ZADD_OPTION_NX_XX; +import static org.apache.geode.redis.internal.RedisConstants.ERROR_NOT_A_VALID_FLOAT; +import static org.apache.geode.redis.internal.RedisConstants.ERROR_SYNTAX; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +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.params.ZAddParams; + +import org.apache.geode.redis.RedisIntegrationTest; +import org.apache.geode.redis.internal.RedisConstants; +import org.apache.geode.test.awaitility.GeodeAwaitility; + +public abstract class AbstractZAddIntegrationTest implements RedisIntegrationTest { + private JedisCluster jedis; + private static final int REDIS_CLIENT_TIMEOUT = + Math.toIntExact(GeodeAwaitility.getTimeout().toMillis()); + private static final String SORTED_SET_KEY = "ss_key"; + private static final int INITIAL_MEMBER_COUNT = 5; + + @Before + public void setUp() { + jedis = new JedisCluster(new HostAndPort("localhost", getPort()), REDIS_CLIENT_TIMEOUT); + } + + @After + public void tearDown() { + flushAll(); + jedis.close(); + } + + @Test + public void zaddErrors_givenWrongKeyType() { + final String STRING_KEY = "stringKey"; + jedis.set(STRING_KEY, "value"); + assertThatThrownBy( + () -> jedis.sendCommand(STRING_KEY, Protocol.Command.ZADD, STRING_KEY, "1", "member")) + .hasMessage("WRONGTYPE " + RedisConstants.ERROR_WRONG_TYPE); + } + + @Test + public void zaddErrors_givenTooFewArguments() { + assertAtLeastNArgs(jedis, Protocol.Command.ZADD, 3); + } + + @Test + public void zaddErrors_givenUnevenPairsOfArguments() { + assertThatThrownBy( + () -> jedis.sendCommand("fakeKey", Protocol.Command.ZADD, "fakeKey", "1", "member", "2")) + .hasMessageContaining("ERR syntax error"); + } + + @Test + public void zaddErrors_givenNonNumericScore() { + assertThatThrownBy( + () -> jedis.sendCommand("fakeKey", Protocol.Command.ZADD, "fakeKey", "xlerb", "member")) + .hasMessageContaining(ERROR_NOT_A_VALID_FLOAT); + } Review comment: Thank you for adding all these tests! Could we also add a test with an invalid double that is not the first score/member arg, like: ``` zadd blu 1 a 2 b gurgle c 4 d ``` It will pass, I think it'd just be a good test to have to make sure we're parsing all the score/member args and throwing an error. ########## File path: geode-apis-compatible-with-redis/src/distributedTest/java/org/apache/geode/redis/internal/executor/InfoDUnitTest.java ########## @@ -60,7 +61,7 @@ @BeforeClass public static void classSetup() { locatorProperties = new Properties(); - locatorProperties.setProperty(MAX_WAIT_TIME_RECONNECT, "15000"); + locatorProperties.setProperty(MAX_WAIT_TIME_RECONNECT, DEFAULT_MAX_WAIT_TIME_RECONNECT); Review comment: @upthewaterspout mentioned this probably has no effect unless we're doing an auto-reconnect: ``` I doubt any of these tests are doing auto-reconnect so ... probably this has no effect whatsoever. ``` I guess in a separate ticket/PR that could be investigated and this could be removed where it's not needed? ########## File path: geode-apis-compatible-with-redis/src/main/java/org/apache/geode/redis/internal/executor/sortedset/ZAddExecutor.java ########## @@ -0,0 +1,148 @@ +/* + * 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.sortedset; + + +import static org.apache.geode.redis.internal.RedisConstants.ERROR_INVALID_ZADD_OPTION_NX_XX; +import static org.apache.geode.redis.internal.RedisConstants.ERROR_NOT_A_VALID_FLOAT; +import static org.apache.geode.redis.internal.RedisConstants.ERROR_SYNTAX; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import org.apache.geode.redis.internal.executor.RedisResponse; +import org.apache.geode.redis.internal.netty.Coder; +import org.apache.geode.redis.internal.netty.Command; +import org.apache.geode.redis.internal.netty.ExecutionHandlerContext; + +public class ZAddExecutor extends SortedSetExecutor { + private ZAddExecutorState zAddExecutorState = new ZAddExecutorState(); + + @Override + public RedisResponse executeCommand(Command command, ExecutionHandlerContext context) { + zAddExecutorState.initialize(); + RedisSortedSetCommands redisSortedSetCommands = context.getRedisSortedSetCommands(); + List<byte[]> commandElements = command.getProcessedCommand(); + Iterator<byte[]> commandIterator = commandElements.iterator(); + + skipCommandAndKey(commandIterator); + + byte[] firstScore = findAndValidateZAddOptions(command, commandIterator, zAddExecutorState); + if (zAddExecutorState.exceptionMessage != null) { + return RedisResponse.error(zAddExecutorState.exceptionMessage); + } + + List<byte[]> scoresAndMembersToAdd = getScoresAndMembers(firstScore, commandIterator, + zAddExecutorState); + if (zAddExecutorState.exceptionMessage != null) { + return RedisResponse.error(zAddExecutorState.exceptionMessage); + } + + return RedisResponse + .integer(redisSortedSetCommands.zadd(command.getKey(), scoresAndMembersToAdd, + makeOptions(zAddExecutorState))); Review comment: Thinking about this more, we could actually avoid the extra allocation to a list here and the additional method iterating through the arguments by doing the following: ``` return RedisResponse .integer(redisSortedSetCommands.zadd(command.getKey(), commandElements.subList(zAddExecutorState.optionsFoundCount + 2, commandElements.size()), makeOptions(zAddExecutorState))); ``` Then, in the zadd method of `RedisSortedSet`, after getting the score we could attempt to parse the double and throw an error if we are unable to: ``` try { Double.valueOf(Coder.bytesToString(score)); } catch (NumberFormatException nfe) { throw new IllegalArgumentException(ERROR_NOT_A_VALID_FLOAT); } ``` This would also allow us to get rid of the `getScoresAndMembers` method here as well as that extra if block (lines 50-52). We'd also no longer need to return anything from `findAndValidateZAddOptions`. ########## File path: geode-apis-compatible-with-redis/src/main/java/org/apache/geode/redis/internal/executor/sortedset/ZAddExecutor.java ########## @@ -0,0 +1,148 @@ +/* + * 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.sortedset; + + +import static org.apache.geode.redis.internal.RedisConstants.ERROR_INVALID_ZADD_OPTION_NX_XX; +import static org.apache.geode.redis.internal.RedisConstants.ERROR_NOT_A_VALID_FLOAT; +import static org.apache.geode.redis.internal.RedisConstants.ERROR_SYNTAX; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import org.apache.geode.redis.internal.executor.RedisResponse; +import org.apache.geode.redis.internal.netty.Coder; +import org.apache.geode.redis.internal.netty.Command; +import org.apache.geode.redis.internal.netty.ExecutionHandlerContext; + +public class ZAddExecutor extends SortedSetExecutor { + private ZAddExecutorState zAddExecutorState = new ZAddExecutorState(); + + @Override + public RedisResponse executeCommand(Command command, ExecutionHandlerContext context) { + zAddExecutorState.initialize(); + RedisSortedSetCommands redisSortedSetCommands = context.getRedisSortedSetCommands(); + List<byte[]> commandElements = command.getProcessedCommand(); + Iterator<byte[]> commandIterator = commandElements.iterator(); + + skipCommandAndKey(commandIterator); + + byte[] firstScore = findAndValidateZAddOptions(command, commandIterator, zAddExecutorState); + if (zAddExecutorState.exceptionMessage != null) { + return RedisResponse.error(zAddExecutorState.exceptionMessage); + } + + List<byte[]> scoresAndMembersToAdd = getScoresAndMembers(firstScore, commandIterator, + zAddExecutorState); + if (zAddExecutorState.exceptionMessage != null) { + return RedisResponse.error(zAddExecutorState.exceptionMessage); + } + + return RedisResponse + .integer(redisSortedSetCommands.zadd(command.getKey(), scoresAndMembersToAdd, + makeOptions(zAddExecutorState))); + } + + private void skipCommandAndKey(Iterator<byte[]> commandIterator) { + commandIterator.next(); + commandIterator.next(); + } + + private byte[] findAndValidateZAddOptions(Command command, Iterator<byte[]> commandIterator, + ZAddExecutorState executorState) { + boolean scoreFound = false; + byte[] next = new byte[0]; + + while (commandIterator.hasNext() && !scoreFound) { + next = commandIterator.next(); + String subCommandString = Coder.bytesToString(next).toLowerCase(); + switch (subCommandString) { + case "nx": + executorState.nxFound = true; + executorState.optionsFoundCount++; + break; + case "xx": + executorState.xxFound = true; + executorState.optionsFoundCount++; + break; + default: + try { + Double.valueOf(subCommandString); + } catch (NumberFormatException nfe) { + executorState.exceptionMessage = ERROR_NOT_A_VALID_FLOAT; + } + scoreFound = true; + } + } + if ((command.getProcessedCommand().size() - executorState.optionsFoundCount - 2) % 2 != 0) { + executorState.exceptionMessage = ERROR_SYNTAX; + } else if (executorState.nxFound && executorState.xxFound) { + executorState.exceptionMessage = ERROR_INVALID_ZADD_OPTION_NX_XX; + } + return next; + } + + private List<byte[]> getScoresAndMembers(byte[] firstScore, Iterator<byte[]> commandIterator, + ZAddExecutorState executorState) { + List<byte[]> scoresAndMembersToAdd = new ArrayList<>(); + + // Already validated there's at least one pair + scoresAndMembersToAdd.add(firstScore); + scoresAndMembersToAdd.add(commandIterator.next()); + + // Any more? + while (commandIterator.hasNext()) { + byte[] score = commandIterator.next(); + try { + Double.valueOf(Coder.bytesToString(score)); + } catch (NumberFormatException nfe) { + executorState.exceptionMessage = ERROR_NOT_A_VALID_FLOAT; + return null; + } + scoresAndMembersToAdd.add(score); + + // We already validated even number of remaining params + byte[] member = commandIterator.next(); + scoresAndMembersToAdd.add(member); + } + return scoresAndMembersToAdd; + } + + private ZAddOptions makeOptions(ZAddExecutorState executorState) { + ZAddOptions.Exists existsOption = ZAddOptions.Exists.NONE; + + if (executorState.nxFound) { + existsOption = ZAddOptions.Exists.NX; + } + if (executorState.xxFound) { + existsOption = ZAddOptions.Exists.XX; + } + return new ZAddOptions(existsOption); + } + + class ZAddExecutorState { Review comment: This could be a `static` class ########## File path: geode-apis-compatible-with-redis/src/main/java/org/apache/geode/redis/internal/executor/sortedset/ZAddExecutor.java ########## @@ -0,0 +1,148 @@ +/* + * 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.sortedset; + + +import static org.apache.geode.redis.internal.RedisConstants.ERROR_INVALID_ZADD_OPTION_NX_XX; +import static org.apache.geode.redis.internal.RedisConstants.ERROR_NOT_A_VALID_FLOAT; +import static org.apache.geode.redis.internal.RedisConstants.ERROR_SYNTAX; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import org.apache.geode.redis.internal.executor.RedisResponse; +import org.apache.geode.redis.internal.netty.Coder; +import org.apache.geode.redis.internal.netty.Command; +import org.apache.geode.redis.internal.netty.ExecutionHandlerContext; + +public class ZAddExecutor extends SortedSetExecutor { + private ZAddExecutorState zAddExecutorState = new ZAddExecutorState(); Review comment: This could be final. -- 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. For queries about this service, please contact Infrastructure at: us...@infra.apache.org