jdeppe-pivotal commented on a change in pull request #6359: URL: https://github.com/apache/geode/pull/6359#discussion_r619713002
########## File path: geode-apis-compatible-with-redis/src/distributedTest/java/org/apache/geode/redis/internal/executor/cluster/ClusterSlotsAndNodesDUnitTest.java ########## @@ -0,0 +1,164 @@ +/* + * 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.cluster; + +import static org.apache.geode.redis.internal.RegionProvider.REDIS_REGION_BUCKETS; +import static org.apache.geode.redis.internal.RegionProvider.REDIS_SLOTS_PER_BUCKET; +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.List; + +import org.assertj.core.data.Offset; +import org.junit.BeforeClass; +import org.junit.ClassRule; +import org.junit.Test; +import redis.clients.jedis.Jedis; + +import org.apache.geode.cache.control.RebalanceFactory; +import org.apache.geode.cache.control.ResourceManager; +import org.apache.geode.redis.ClusterNode; +import org.apache.geode.redis.ClusterNodes; +import org.apache.geode.test.awaitility.GeodeAwaitility; +import org.apache.geode.test.dunit.rules.ClusterStartupRule; +import org.apache.geode.test.dunit.rules.MemberVM; +import org.apache.geode.test.dunit.rules.RedisClusterStartupRule; + +public class ClusterSlotsAndNodesDUnitTest { + + @ClassRule + public static RedisClusterStartupRule cluster = new RedisClusterStartupRule(); + + private static final int JEDIS_TIMEOUT = + Math.toIntExact(GeodeAwaitility.getTimeout().toMillis()); + private static final String LOCAL_HOST = "127.0.0.1"; + private static MemberVM locator; + private static MemberVM server1; + private static MemberVM server2; + + private static Jedis jedis1; + private static Jedis jedis2; + + @BeforeClass + public static void classSetup() { + locator = cluster.startLocatorVM(0); + server1 = cluster.startRedisVM(1, locator.getPort()); + server2 = cluster.startRedisVM(2, locator.getPort()); Review comment: Fixed. ########## File path: geode-apis-compatible-with-redis/src/distributedTest/java/org/apache/geode/redis/internal/executor/cluster/JedisAndLettuceClusterDUnitTest.java ########## @@ -0,0 +1,94 @@ +/* + * 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.cluster; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.time.Duration; + +import io.lettuce.core.RedisClient; +import io.lettuce.core.RedisURI; +import io.lettuce.core.api.StatefulRedisConnection; +import io.lettuce.core.api.sync.RedisCommands; +import org.junit.BeforeClass; +import org.junit.ClassRule; +import org.junit.Test; +import redis.clients.jedis.HostAndPort; +import redis.clients.jedis.JedisCluster; + +import org.apache.geode.test.awaitility.GeodeAwaitility; +import org.apache.geode.test.dunit.rules.MemberVM; +import org.apache.geode.test.dunit.rules.RedisClusterStartupRule; + +public class JedisAndLettuceClusterDUnitTest { + + @ClassRule + public static RedisClusterStartupRule cluster = new RedisClusterStartupRule(); + + private static final int KEYS = 1000; + private static final int JEDIS_TIMEOUT = + Math.toIntExact(GeodeAwaitility.getTimeout().toMillis()); + private static final String LOCAL_HOST = "127.0.0.1"; + private static MemberVM locator; + private static MemberVM server1; + private static MemberVM server2; Review comment: Fixed. ########## File path: geode-apis-compatible-with-redis/src/distributedTest/java/org/apache/geode/redis/internal/executor/cluster/JedisAndLettuceClusterDUnitTest.java ########## @@ -0,0 +1,94 @@ +/* + * 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.cluster; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.time.Duration; + +import io.lettuce.core.RedisClient; +import io.lettuce.core.RedisURI; +import io.lettuce.core.api.StatefulRedisConnection; +import io.lettuce.core.api.sync.RedisCommands; +import org.junit.BeforeClass; +import org.junit.ClassRule; +import org.junit.Test; +import redis.clients.jedis.HostAndPort; +import redis.clients.jedis.JedisCluster; + +import org.apache.geode.test.awaitility.GeodeAwaitility; +import org.apache.geode.test.dunit.rules.MemberVM; +import org.apache.geode.test.dunit.rules.RedisClusterStartupRule; + +public class JedisAndLettuceClusterDUnitTest { + + @ClassRule + public static RedisClusterStartupRule cluster = new RedisClusterStartupRule(); + + private static final int KEYS = 1000; + private static final int JEDIS_TIMEOUT = + Math.toIntExact(GeodeAwaitility.getTimeout().toMillis()); + private static final String LOCAL_HOST = "127.0.0.1"; + private static MemberVM locator; + private static MemberVM server1; + private static MemberVM server2; + private static int redisServerPort1; + private static int redisServerPort2; + + @BeforeClass + public static void classSetup() { + locator = cluster.startLocatorVM(0); + server1 = cluster.startRedisVM(1, locator.getPort()); + server2 = cluster.startRedisVM(2, locator.getPort()); + + redisServerPort1 = cluster.getRedisPort(1); + redisServerPort2 = cluster.getRedisPort(2); Review comment: Fixed. ########## File path: geode-apis-compatible-with-redis/src/distributedTest/java/org/apache/geode/redis/internal/executor/cluster/JedisAndLettuceClusterDUnitTest.java ########## @@ -0,0 +1,94 @@ +/* + * 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.cluster; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.time.Duration; + +import io.lettuce.core.RedisClient; +import io.lettuce.core.RedisURI; +import io.lettuce.core.api.StatefulRedisConnection; +import io.lettuce.core.api.sync.RedisCommands; +import org.junit.BeforeClass; +import org.junit.ClassRule; +import org.junit.Test; +import redis.clients.jedis.HostAndPort; +import redis.clients.jedis.JedisCluster; + +import org.apache.geode.test.awaitility.GeodeAwaitility; +import org.apache.geode.test.dunit.rules.MemberVM; +import org.apache.geode.test.dunit.rules.RedisClusterStartupRule; + +public class JedisAndLettuceClusterDUnitTest { + + @ClassRule + public static RedisClusterStartupRule cluster = new RedisClusterStartupRule(); + + private static final int KEYS = 1000; + private static final int JEDIS_TIMEOUT = + Math.toIntExact(GeodeAwaitility.getTimeout().toMillis()); + private static final String LOCAL_HOST = "127.0.0.1"; + private static MemberVM locator; Review comment: Fixed. ########## File path: geode-apis-compatible-with-redis/src/main/java/org/apache/geode/redis/internal/cluster/BucketInfoRetrievalFunction.java ########## @@ -0,0 +1,108 @@ +/* + * 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.cluster; + +import java.io.Serializable; +import java.net.InetAddress; +import java.util.Set; + +import org.apache.geode.cache.Region; +import org.apache.geode.cache.execute.FunctionContext; +import org.apache.geode.cache.execute.FunctionService; +import org.apache.geode.cache.partition.PartitionRegionHelper; +import org.apache.geode.internal.cache.LocalDataSet; +import org.apache.geode.internal.cache.execute.InternalFunction; +import org.apache.geode.internal.inet.LocalHostUtil; +import org.apache.geode.redis.internal.RegionProvider; +import org.apache.geode.redis.internal.data.ByteArrayWrapper; +import org.apache.geode.redis.internal.data.RedisKey; + +public class BucketInfoRetrievalFunction implements InternalFunction<Void> { + + public static final String ID = "REDIS_BUCKET_SLOT_FUNCTION"; Review comment: Fixed. ########## File path: geode-apis-compatible-with-redis/src/main/java/org/apache/geode/redis/internal/ParameterRequirements/ClusterParameterRequirements.java ########## @@ -0,0 +1,47 @@ +/* + * 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.ParameterRequirements; + +import static org.apache.geode.redis.internal.RedisConstants.ERROR_UNKNOWN_CLUSTER_SUBCOMMAND; + +import org.apache.geode.redis.internal.netty.Command; +import org.apache.geode.redis.internal.netty.ExecutionHandlerContext; + +public class ClusterParameterRequirements implements ParameterRequirements { + @Override + public void checkParameters(Command command, ExecutionHandlerContext context) { + int numberOfArguments = command.getProcessedCommand().size(); + + if (numberOfArguments < 2) { + throw new RedisParametersMismatchException(command.wrongNumberOfArgumentsErrorMessage()); + } else if (numberOfArguments == 2) { + confirmKnownSubcommands(command); + } else { // numberOfArguments > 3 + throw new RedisParametersMismatchException( + String.format(ERROR_UNKNOWN_CLUSTER_SUBCOMMAND, command.getStringKey())); + } + } + + private void confirmKnownSubcommands(Command command) { + if (!command.getStringKey().equalsIgnoreCase("slots") && Review comment: Removed this check in favor of checking in the `Executor`. ########## File path: geode-apis-compatible-with-redis/src/main/java/org/apache/geode/redis/internal/executor/cluster/ClusterExecutor.java ########## @@ -0,0 +1,199 @@ +/* + * 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.cluster; + +import static org.apache.geode.redis.internal.RedisConstants.ERROR_UNKNOWN_CLUSTER_SUBCOMMAND; +import static org.apache.geode.redis.internal.RegionProvider.REDIS_REGION_BUCKETS; +import static org.apache.geode.redis.internal.RegionProvider.REDIS_SLOTS; +import static org.apache.geode.redis.internal.RegionProvider.REDIS_SLOTS_PER_BUCKET; +import static org.apache.geode.redis.internal.cluster.BucketInfoRetrievalFunction.MemberBuckets; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.TreeMap; + +import org.apache.commons.lang3.tuple.Pair; + +import org.apache.geode.cache.Region; +import org.apache.geode.cache.execute.FunctionService; +import org.apache.geode.cache.execute.ResultCollector; +import org.apache.geode.cache.partition.PartitionMemberInfo; +import org.apache.geode.cache.partition.PartitionRegionHelper; +import org.apache.geode.cache.partition.PartitionRegionInfo; +import org.apache.geode.distributed.DistributedMember; +import org.apache.geode.redis.internal.cluster.BucketInfoRetrievalFunction; +import org.apache.geode.redis.internal.data.RedisData; +import org.apache.geode.redis.internal.data.RedisKey; +import org.apache.geode.redis.internal.executor.AbstractExecutor; +import org.apache.geode.redis.internal.executor.RedisResponse; +import org.apache.geode.redis.internal.netty.Command; +import org.apache.geode.redis.internal.netty.ExecutionHandlerContext; + +public class ClusterExecutor extends AbstractExecutor { + + @Override + public RedisResponse executeCommand(Command command, ExecutionHandlerContext context) { + + List<byte[]> args = command.getProcessedCommand(); + String subCommand = new String(args.get(1)); + + StringBuilder strArgs = new StringBuilder(); + args.forEach(x -> strArgs.append(new String(x)).append(" ")); + + switch (subCommand.toLowerCase()) { + case "info": + return getInfo(context); + case "nodes": + return getNodes(context); + case "slots": + return getSlots(context); + default: { + return RedisResponse.error( + String.format(ERROR_UNKNOWN_CLUSTER_SUBCOMMAND, subCommand)); Review comment: Removing this error response return would require returning `null` which just doesn't look right to me (although functionally it will never get hit). However I've chosen to remove the subcommand check from the param validator in favor of keeping this code. This also means there is just one place to update when subsequent subcommands are added. ########## File path: geode-apis-compatible-with-redis/src/main/java/org/apache/geode/redis/internal/executor/cluster/ClusterExecutor.java ########## @@ -0,0 +1,199 @@ +/* + * 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.cluster; + +import static org.apache.geode.redis.internal.RedisConstants.ERROR_UNKNOWN_CLUSTER_SUBCOMMAND; +import static org.apache.geode.redis.internal.RegionProvider.REDIS_REGION_BUCKETS; +import static org.apache.geode.redis.internal.RegionProvider.REDIS_SLOTS; +import static org.apache.geode.redis.internal.RegionProvider.REDIS_SLOTS_PER_BUCKET; +import static org.apache.geode.redis.internal.cluster.BucketInfoRetrievalFunction.MemberBuckets; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.TreeMap; + +import org.apache.commons.lang3.tuple.Pair; + +import org.apache.geode.cache.Region; +import org.apache.geode.cache.execute.FunctionService; +import org.apache.geode.cache.execute.ResultCollector; +import org.apache.geode.cache.partition.PartitionMemberInfo; +import org.apache.geode.cache.partition.PartitionRegionHelper; +import org.apache.geode.cache.partition.PartitionRegionInfo; +import org.apache.geode.distributed.DistributedMember; +import org.apache.geode.redis.internal.cluster.BucketInfoRetrievalFunction; +import org.apache.geode.redis.internal.data.RedisData; +import org.apache.geode.redis.internal.data.RedisKey; +import org.apache.geode.redis.internal.executor.AbstractExecutor; +import org.apache.geode.redis.internal.executor.RedisResponse; +import org.apache.geode.redis.internal.netty.Command; +import org.apache.geode.redis.internal.netty.ExecutionHandlerContext; + +public class ClusterExecutor extends AbstractExecutor { + + @Override + public RedisResponse executeCommand(Command command, ExecutionHandlerContext context) { + + List<byte[]> args = command.getProcessedCommand(); + String subCommand = new String(args.get(1)); + + StringBuilder strArgs = new StringBuilder(); + args.forEach(x -> strArgs.append(new String(x)).append(" ")); + + switch (subCommand.toLowerCase()) { + case "info": + return getInfo(context); + case "nodes": + return getNodes(context); + case "slots": + return getSlots(context); + default: { + return RedisResponse.error( + String.format(ERROR_UNKNOWN_CLUSTER_SUBCOMMAND, subCommand)); + } + } + } + + private RedisResponse getSlots(ExecutionHandlerContext ctx) { + List<MemberBuckets> memberBuckets = getMemberBuckets(ctx); + + Map<Integer, String> primaryBucketToMemberMap = new HashMap<>(); + Map<String, Pair<String, Integer>> memberToHostPortMap = new TreeMap<>(); + int retrievedBucketCount = 0; + + for (MemberBuckets m : memberBuckets) { + memberToHostPortMap.put(m.getMemberId(), Pair.of(m.getHostAddress(), m.getPort())); + for (Integer id : m.getPrimaryBucketIds()) { + primaryBucketToMemberMap.put(id, m.getMemberId()); + retrievedBucketCount++; + } + } + + if (retrievedBucketCount != REDIS_REGION_BUCKETS) { + return RedisResponse.error("Internal error: bucket count mismatch " + retrievedBucketCount + + " != " + REDIS_REGION_BUCKETS); + } + + int index = 0; + List<Object> slots = new ArrayList<>(); + + for (int i = 0; i < REDIS_REGION_BUCKETS; i++) { + Pair<String, Integer> primaryHostAndPort = + memberToHostPortMap.get(primaryBucketToMemberMap.get(i)); + + List<Object> entry = new ArrayList<>(); + entry.add(index * REDIS_SLOTS_PER_BUCKET); + entry.add(((index + 1) * REDIS_SLOTS_PER_BUCKET) - 1); + entry.add(Arrays.asList(primaryHostAndPort.getLeft(), primaryHostAndPort.getRight())); + + slots.add(entry); + index++; + } + + return RedisResponse.array(slots); + } + + @SuppressWarnings("unchecked") + private List<MemberBuckets> getMemberBuckets( Review comment: I'm planning to add that when we address returning `MOVED` responses. which will definitely require something lighter and more dynamic. ########## File path: geode-apis-compatible-with-redis/src/main/java/org/apache/geode/redis/internal/ParameterRequirements/ClusterParameterRequirements.java ########## @@ -0,0 +1,47 @@ +/* + * 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.ParameterRequirements; + +import static org.apache.geode.redis.internal.RedisConstants.ERROR_UNKNOWN_CLUSTER_SUBCOMMAND; + +import org.apache.geode.redis.internal.netty.Command; +import org.apache.geode.redis.internal.netty.ExecutionHandlerContext; + +public class ClusterParameterRequirements implements ParameterRequirements { + @Override + public void checkParameters(Command command, ExecutionHandlerContext context) { + int numberOfArguments = command.getProcessedCommand().size(); + + if (numberOfArguments < 2) { + throw new RedisParametersMismatchException(command.wrongNumberOfArgumentsErrorMessage()); + } else if (numberOfArguments == 2) { + confirmKnownSubcommands(command); + } else { // numberOfArguments > 3 + throw new RedisParametersMismatchException( + String.format(ERROR_UNKNOWN_CLUSTER_SUBCOMMAND, command.getStringKey())); + } + } + + private void confirmKnownSubcommands(Command command) { + if (!command.getStringKey().equalsIgnoreCase("slots") && Review comment: Removed all of this in favor of just leaving the check in `ClusterExecutor`. ########## File path: geode-apis-compatible-with-redis/src/main/java/org/apache/geode/redis/internal/RedisConstants.java ########## @@ -54,4 +54,6 @@ public static final String ERROR_UNKNOWN_SLOWLOG_SUBCOMMAND = "Unknown subcommand or wrong number of arguments for '%s'. Try SLOWLOG HELP."; + public static final String ERROR_UNKNOWN_CLUSTER_SUBCOMMAND = + "Unknown subcommand or wrong number of arguments for '%s'. Try CLUSTER HELP."; Review comment: I've added a story. -- 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: [email protected]
