pivotal-jbarrett commented on a change in pull request #6359:
URL: https://github.com/apache/geode/pull/6359#discussion_r618633092



##########
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:
       While this command shouldn't be frequently invoked this pattern should 
be avoided here and elsewhere in the code.
   
   `String.equalsIgnoreCase` has to convert mismatching characters to uppercase 
to compare them. In this case we invoked this method up to 3 times. An 
alternative:
   
   ```java
   final String subCommand = command.getStringKey().toLower();
   if (!subCommand.equals("slots") && ...) {
     ...
   }
   ```
   At most converts the string to lower case 1 time. 
   
   Extra credit for avoid the string conversation in `getStringKey` and look at 
the just bytes of this command component directly. 
   
   ```java
   final byte[] subCommand = getProcessedCommand().get(1);
   if (subCommand[0] == 's' || subCommand[0] == 'S') {
     if (subCommand[1] == 'l' || subCommand[1] == 'L') {
       ..
     }
   } else if (subCommand[0] == 'I' ...) {
   } else {
     throw ...;
   }
   ```
   

##########
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:
       Is there some way for us to calculate the bucket/slot arrangement 
upfront and only update it on bucket movement? Is there a listener we could 
attach to for these events? This way we don't need to generate this data 
structure for scratch for each client request. 

##########
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:
       ```java
   public static final String ID = BucketInfoRetrievalFunction.class.getName();
   ```

##########
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:
       If we do this check for sub commands here again but do we do them in the 
parameter check too? Seems like a waste to do it in two places.




-- 
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]


Reply via email to