arjunashok commented on code in PR #58: URL: https://github.com/apache/cassandra-sidecar/pull/58#discussion_r1307685706
########## adapters/base/src/main/java/org/apache/cassandra/sidecar/adapters/base/TokenRangeReplicaProvider.java: ########## @@ -0,0 +1,249 @@ +/* + * 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.Collection; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import java.util.function.Function; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +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.utils.GossipInfoParser; +import org.jetbrains.annotations.NotNull; + +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; + +/** + * 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 = jmxClient.proxy(StorageJmxOperations.class, STORAGE_SERVICE_OBJ_NAME); + + // Retrieve map of primary token ranges to endpoints that describe the ring topology + Map<List<String>, List<String>> rangeToEndpointMappings = storage.getRangeToEndpointWithPortMap(keyspace); Review Comment: As you have called out, the native types are the return types of the JMX endpoints. I am transforming this into a holder `TokenRangeReplicas` further in the flow. Will look into moving it ahead. ########## adapters/base/src/main/java/org/apache/cassandra/sidecar/adapters/base/TokenRangeReplicaProvider.java: ########## @@ -0,0 +1,249 @@ +/* + * 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.Collection; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import java.util.function.Function; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +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.utils.GossipInfoParser; +import org.jetbrains.annotations.NotNull; + +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; + +/** + * 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 = jmxClient.proxy(StorageJmxOperations.class, STORAGE_SERVICE_OBJ_NAME); + + // Retrieve map of primary token ranges to endpoints that describe the ring topology + Map<List<String>, List<String>> rangeToEndpointMappings = storage.getRangeToEndpointWithPortMap(keyspace); + // Pending ranges include bootstrap tokens and leaving endpoints as represented in the Cassandra TokenMetadata + Map<List<String>, List<String>> pendingRangeMappings = storage.getPendingRangeToEndpointWithPortMap(keyspace); + + Set<String> replicaSet = Stream.concat(rangeToEndpointMappings.values().stream().flatMap(List::stream), + pendingRangeMappings.values().stream().flatMap(List::stream)) + .collect(Collectors.toSet()); + + Map<String, String> hostToDatacenter = groupHostsByDatacenter(replicaSet); + + // Retrieve map of all token ranges (pending & primary) to endpoints + List<TokenRangeReplicasResponse.ReplicaInfo> writeReplicas = + writeReplicasFromPendingRanges(rangeToEndpointMappings, + pendingRangeMappings, + hostToDatacenter, + partitioner, + keyspace); + + Map<String, String> replicaToStateMap = replicaToStateMap(replicaSet, storage); + + return new TokenRangeReplicasResponse( + replicaToStateMap, + writeReplicas, + mappingsToUnwrappedReplicaSet(rangeToEndpointMappings, hostToDatacenter, partitioner)); + } + + private Map<String, String> replicaToStateMap(Set<String> replicaSet, StorageJmxOperations storage) + { + List<String> joiningNodes = storage.getJoiningNodesWithPort(); + List<String> leavingNodes = storage.getLeavingNodesWithPort(); + List<String> movingNodes = storage.getMovingNodesWithPort(); + + String rawGossipInfo = getRawGossipInfo(); + GossipInfoResponse gossipInfo = GossipInfoParser.parse(rawGossipInfo); + + StateWithReplacement state = new StateWithReplacement(joiningNodes, leavingNodes, movingNodes, gossipInfo); + + return replicaSet.stream() + .collect(Collectors.toMap(Function.identity(), state::of)); + } + + private String getRawGossipInfo() + { + return jmxClient.proxy(ClusterMembershipJmxOperations.class, FAILURE_DETECTOR_OBJ_NAME) Review Comment: Will make the change similar to how RingHandler does this for consistency -- 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]

