JeetKunDoug commented on code in PR #58:
URL: https://github.com/apache/cassandra-sidecar/pull/58#discussion_r1311694963


##########
adapters/base/src/main/java/org/apache/cassandra/sidecar/adapters/base/TokenRangeReplicaProvider.java:
##########
@@ -0,0 +1,269 @@
+/*
+ * 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.cassandra.sidecar.adapters.base;
+
+import java.math.BigInteger;
+import java.net.UnknownHostException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.apache.cassandra.sidecar.adapters.base.NodeInfo.NodeState;
+import org.apache.cassandra.sidecar.common.JmxClient;
+import org.apache.cassandra.sidecar.common.data.GossipInfoResponse;
+import org.apache.cassandra.sidecar.common.data.TokenRangeReplicasResponse;
+import 
org.apache.cassandra.sidecar.common.data.TokenRangeReplicasResponse.ReplicaInfo;
+import org.apache.cassandra.sidecar.common.utils.GossipInfoParser;
+import org.jetbrains.annotations.NotNull;
+
+import static java.util.stream.Collectors.toList;
+import static 
org.apache.cassandra.sidecar.adapters.base.ClusterMembershipJmxOperations.FAILURE_DETECTOR_OBJ_NAME;
+import static 
org.apache.cassandra.sidecar.adapters.base.EndpointSnitchJmxOperations.ENDPOINT_SNITCH_INFO_OBJ_NAME;
+import static 
org.apache.cassandra.sidecar.adapters.base.StorageJmxOperations.STORAGE_SERVICE_OBJ_NAME;
+import static 
org.apache.cassandra.sidecar.adapters.base.TokenRangeReplicas.generateTokenRangeReplicas;
+
+/**
+ * Aggregates the replica-set by token range
+ */
+public class TokenRangeReplicaProvider
+{
+    private final JmxClient jmxClient;
+
+    private static final Logger LOGGER = 
LoggerFactory.getLogger(TokenRangeReplicaProvider.class);
+
+    public TokenRangeReplicaProvider(JmxClient jmxClient)
+    {
+        this.jmxClient = jmxClient;
+    }
+
+    public TokenRangeReplicasResponse tokenRangeReplicas(String keyspace, 
Partitioner partitioner)
+    {
+        Objects.requireNonNull(keyspace, "keyspace must be non-null");
+
+        StorageJmxOperations storage = initializeStorageOps();
+
+        // Retrieve map of primary token ranges to endpoints that describe the 
ring topology
+        Map<List<String>, List<String>> naturalReplicaMappings = 
storage.getRangeToEndpointWithPortMap(keyspace);
+        LOGGER.debug("Natural token range mappingsfor keyspace={}, 
pendingRangeMappings={}",

Review Comment:
   NIT: missing space in comment
   ```suggestion
           LOGGER.debug("Natural token range mappings for keyspace={}, 
pendingRangeMappings={}",
   ```



##########
adapters/base/src/test/java/org/apache/cassandra/sidecar/adapters/base/TokenRangeReplicasTest.java:
##########
@@ -0,0 +1,824 @@
+/*
+ * 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.cassandra.sidecar.adapters.base;
+
+import java.math.BigInteger;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+
+import org.junit.jupiter.api.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.assertj.core.api.Assertions.assertThat;
+
+/**
+ * Tests for TokenRangeReplicas
+ */
+public class TokenRangeReplicasTest
+{
+    private static final Logger LOGGER = 
LoggerFactory.getLogger(TokenRangeReplicasTest.class);
+
+    private boolean hasOverlaps(List<TokenRangeReplicas> rangeList)
+    {
+        Collections.sort(rangeList);
+        for (int c = 0, i = 1; i < rangeList.size(); i++)
+        {
+            if (rangeList.get(c++).end().compareTo(rangeList.get(i).start()) > 
0) return true;
+        }
+        return false;
+    }
+
+    private boolean checkContains(List<TokenRangeReplicas> resultList, 
TokenRangeReplicas expected)
+    {
+        return resultList.stream()
+                         .map(TokenRangeReplicas::toString)
+                         .anyMatch(r -> r.equals(expected.toString()));
+    }

Review Comment:
   NIT: This is a personal preference of mine, but I like to see the _tests_ 
first in a test class, and helper methods at the end - these are details that I 
generally don't need to see until I'm trying to debug a failure or if the 
method doesn't have a good enough name (in which case, we should rename it).



##########
adapters/base/src/test/java/org/apache/cassandra/sidecar/adapters/base/TokenRangeReplicasTest.java:
##########
@@ -0,0 +1,824 @@
+/*
+ * 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.cassandra.sidecar.adapters.base;
+
+import java.math.BigInteger;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+
+import org.junit.jupiter.api.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.assertj.core.api.Assertions.assertThat;
+
+/**
+ * Tests for TokenRangeReplicas
+ */
+public class TokenRangeReplicasTest
+{
+    private static final Logger LOGGER = 
LoggerFactory.getLogger(TokenRangeReplicasTest.class);
+
+    private boolean hasOverlaps(List<TokenRangeReplicas> rangeList)
+    {
+        Collections.sort(rangeList);
+        for (int c = 0, i = 1; i < rangeList.size(); i++)
+        {
+            if (rangeList.get(c++).end().compareTo(rangeList.get(i).start()) > 
0) return true;
+        }
+        return false;
+    }
+
+    private boolean checkContains(List<TokenRangeReplicas> resultList, 
TokenRangeReplicas expected)
+    {
+        return resultList.stream()
+                         .map(TokenRangeReplicas::toString)
+                         .anyMatch(r -> r.equals(expected.toString()));
+    }
+
+    // non-overlapping ranges
+    @Test
+    public void simpleTest()
+    {
+        List<TokenRangeReplicas> simpleList = 
createSimpleTokenRangeReplicaList();
+        LOGGER.info("Input:" + simpleList);
+        List<TokenRangeReplicas> rangeList = 
TokenRangeReplicas.normalize(simpleList);

Review Comment:
   Given all this test does is assert that there are no overlaps in the final 
list, we should at least generate an input list that has some overlaps, or is 
there some other thing we could validate here, like the input list and output 
list are equal (because there should have been no change?) I think this 
works/passes, and would be a better assertion than just there were no overlaps 
(because there were no overlaps in the input), although maybe you do both?



##########
adapters/base/src/main/java/org/apache/cassandra/sidecar/adapters/base/NodeInfo.java:
##########
@@ -0,0 +1,69 @@
+/*
+ * 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.cassandra.sidecar.adapters.base;
+
+/**
+ * Holder class for Node related
+ */
+public class NodeInfo

Review Comment:
   The `nodetool/Ring.java` is producing _human readable_ output though, and 
otherwise I think Arjun's `Mode` example in C* seems more appropriate. I can 
imagine other usage of the enum in future work potentially, but more 
importantly it provides a  way to group all of the things in one place. You 
could, I suppose, just have a bunch of `public static final String` members on 
an otherwise static class just to have another container for the constant 
strings...
   
   We could, however, do something like this:
   ```java
   public enum NodeState
   {
       JOINING,
       LEAVING,
       MOVING,
       NORMAL,
       REPLACING;
   
       private final String displayName;
   
       NodeState()
       {
           String firstChar = 
String.valueOf(name().charAt(0)).toLowerCase(Locale.ROOT);
           displayName = name().toLowerCase().replaceFirst(firstChar, 
firstChar.toUpperCase(Locale.ROOT));
       }
   
       public String displayName() {
           return displayName;
       }
   }
   ```
   And then you can use `displayName` where you need the human-readable version.
   
   This gets rid of the hard-coding of string names, but maybe is otherwise 
more complicated - you could also just have a helper method somewhere that does 
the enum -> Title case conversion.



##########
adapters/base/src/main/java/org/apache/cassandra/sidecar/adapters/base/TokenRangeReplicas.java:
##########
@@ -0,0 +1,472 @@
+/*
+ * 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.cassandra.sidecar.adapters.base;
+
+import java.math.BigInteger;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.PriorityQueue;
+import java.util.Set;
+import java.util.function.Consumer;
+import java.util.stream.Collectors;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.jetbrains.annotations.NotNull;
+
+
+/**
+ * Representation of a token range (exclusive start and inclusive end - 
(start, end]) and the
+ * corresponding mapping to replica-set hosts. Static factory ensures that 
ranges are always unwrapped.
+ * Note: Range comparisons are used for ordering of ranges. eg. A.compareTo(B) 
<= 0 implies that
+ * range A occurs before range B, not their sizes.
+ */
+public class TokenRangeReplicas implements Comparable<TokenRangeReplicas>
+{
+    private final BigInteger start;
+    private final BigInteger end;
+
+    private final Partitioner partitioner;
+
+    private final Set<String> replicaSet;
+
+    private static final Logger LOGGER = 
LoggerFactory.getLogger(TokenRangeReplicas.class);
+
+    private TokenRangeReplicas(BigInteger start, BigInteger end, Partitioner 
partitioner, Set<String> replicaSet)
+    {
+        this.start = start;
+        this.end = end;
+        this.partitioner = partitioner;
+        this.replicaSet = replicaSet;
+    }
+
+    public static List<TokenRangeReplicas> 
generateTokenRangeReplicas(BigInteger start,
+                                                                      
BigInteger end,
+                                                                      
Partitioner partitioner,
+                                                                      
Set<String> replicaSet)
+    {
+        if (start.compareTo(end) > 0)
+        {
+            return unwrapRange(start, end, partitioner, replicaSet);
+        }
+
+        return Collections.singletonList(new TokenRangeReplicas(start, end, 
partitioner, replicaSet));
+    }
+
+
+    public BigInteger start()
+    {
+        return start;
+    }
+
+    public BigInteger end()
+    {
+        return end;
+    }
+
+    public Set<String> replicaSet()
+    {
+        return replicaSet;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public int compareTo(@NotNull TokenRangeReplicas other)
+    {
+        validateRangesForComparison(other);
+        int compareStart = this.start.compareTo(other.start);
+        return (compareStart != 0) ? compareStart : 
this.end.compareTo(other.end);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public boolean equals(Object o)
+    {
+        if (this == o)
+        {
+            return true;
+        }
+        if (o == null || getClass() != o.getClass())
+        {
+            return false;
+        }
+
+        TokenRangeReplicas that = (TokenRangeReplicas) o;
+
+        return Objects.equals(start, that.start)
+               && Objects.equals(end, that.end)
+               && partitioner == that.partitioner;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public int hashCode()
+    {
+        return Objects.hash(start, end, partitioner);
+    }
+
+    private void validateRangesForComparison(@NotNull TokenRangeReplicas other)
+    {
+        if (this.partitioner != other.partitioner)
+            throw new IllegalStateException("Token ranges being compared do 
not have the same partitioner");
+    }
+
+    protected boolean contains(TokenRangeReplicas other)
+    {
+        validateRangesForComparison(other);
+        return (other.start.compareTo(this.start) >= 0 && 
other.end.compareTo(this.end) <= 0);
+    }
+
+    /**
+     * For subset ranges, this is used to determine if a range is larger than 
the other by comparing start-end lengths
+     * If both ranges end at the min, we compare starting points to determine 
the result.
+     * When the left range is the only one ending at min, it is always the 
larger one since all subsequent ranges
+     * in the sorted range list have to be smaller.
+     * <p>
+     * This method assumes that the ranges are normalized and unwrapped, i.e.
+     * 'this' comes before 'other' AND there's no wrapping around the min token
+     *
+     * @param other the next range in the range list to compare
+     * @return true if "this" range is larger than the other
+     */
+    protected boolean isLarger(TokenRangeReplicas other)
+    {
+        validateRangesForComparison(other);
+        return 
this.end.subtract(this.start).compareTo(other.end.subtract(other.start)) > 0;
+    }
+
+    /**
+     * Determines intersection if the next range starts before the current 
range ends. This method assumes that
+     * the provided ranges are sorted and unwrapped.
+     * When the current range goes all the way to the end, we determine 
intersection if the next range starts
+     * after the current since all subsequent ranges have to be subsets.
+     *
+     * @param other the range we are currently processing to check if "this" 
intersects it
+     * @return true if "this" range intersects the other
+     */
+    protected boolean intersects(TokenRangeReplicas other)
+    {
+        if (this.compareTo(other) > 0)
+            throw new IllegalStateException(
+            String.format("Token ranges - (this:%s other:%s) are not ordered", 
this, other));
+
+        return this.end.compareTo(other.start) > 0 && 
this.start.compareTo(other.end) < 0; // Start exclusive (DONE)
+    }

Review Comment:
   Rather then throwing if this is < other, how about just reversing the 
comparison in the return statement:
   
   ```suggestion
       protected boolean intersects(TokenRangeReplicas other)
       {
           boolean inOrder = this.compareTo(other) <= 0;
           TokenRangeReplicas first = inOrder ? this : other;
           TokenRangeReplicas last = inOrder ? other : this;
   
           return first.end.compareTo(last.start) > 0 && 
first.start.compareTo(last.end) < 0; // Start exclusive (DONE)
       }
   ```



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to