Skip to content

Commit

Permalink
Filter out Stargate/coordinator nodes from calls to get live nodes or…
Browse files Browse the repository at this point in the history
… list tokens (#1433)

The http management implementation uses endpoint states for listing nodes and tokens, unlike JMX, which also contains Stargate nodes.
This breaks some calls which expect the Stargate nodes to behave like standard Cassandra nodes.
This commit filters out the endpoint state entries that don't have tokens to make sure Stargate nodes aren't accounted for when they shouldn't be.
  • Loading branch information
adejanovski authored Oct 23, 2023
1 parent 3912a6c commit 0c1c835
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,11 @@ public List<BigInteger> getTokens() {
EndpointStates endpointStates = apiClient.getEndpointStates();
List<BigInteger> tokenList = new ArrayList<>();
endpointStates.getEntity().forEach((Map<String, String> states) -> {
for (String token : states.get("TOKENS").split(",")) {
tokenList.add(new BigInteger(token));
// Stargate nodes are part of the endpoint states but do not have tokens
if (!isCoordinatorNode(states)) {
for (String token : states.get("TOKENS").split(",")) {
tokenList.add(new BigInteger(token));
}
}
});
// sort the list
Expand Down Expand Up @@ -405,7 +408,9 @@ public List<String> getLiveNodes() throws ReaperException {
List<String> liveNodes = new ArrayList<>();
EndpointStates endpoints = apiClient.getEndpointStates();
for (Map<String, String> states : endpoints.getEntity()) {
if (states.containsKey("IS_ALIVE") && Boolean.parseBoolean(states.get("IS_ALIVE"))) {
if (!isCoordinatorNode(states)
&& states.containsKey("IS_ALIVE")
&& Boolean.parseBoolean(states.get("IS_ALIVE"))) {
liveNodes.add(states.get("ENDPOINT_IP"));
}
}
Expand Down Expand Up @@ -499,8 +504,11 @@ public Map<String, String> getTokenToEndpointMap() {
Map<String, String> tokenMap = new HashMap<>();
for (Map<String, String> states : epStates.getEntity()) {
String ip = states.get("ENDPOINT_IP");
for (String token : states.get("TOKENS").split(",")) {
tokenMap.put(token, ip);
// Stargate nodes are part of the endpoint states but do not have tokens
if (!isCoordinatorNode(states)) {
for (String token : states.get("TOKENS").split(",")) {
tokenMap.put(token, ip);
}
}
}
return tokenMap;
Expand Down Expand Up @@ -637,4 +645,10 @@ Runnable notificationsTracker() {
}
};
}

// Coordinator nodes such as Stargate instances do not have tokens
@VisibleForTesting
static boolean isCoordinatorNode(Map<String, String> endpointState) {
return endpointState.getOrDefault("TOKENS", "null").equals("null");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public ICassandraManagementProxy connectAny(Collection<Node> nodes) throws Reape
return cassandraManagementProxy;
} catch (ReaperException | RuntimeException | UnknownHostException e) {
getHostConnectionCounters().decrementSuccessfulConnections(node.getHostname());
LOG.info("Unreachable host: ", e);
LOG.info("Unreachable host: {}", node.getHostname(), e);
} catch (InterruptedException expected) {
LOG.trace("Expected exception", expected);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -64,6 +65,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.tuple;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
Expand Down Expand Up @@ -487,7 +489,6 @@ public static HttpCassandraManagementProxy mockProxy(DefaultApi mockClient) {

@Test
public void testGetTokens() throws Exception {
DefaultApi mockClient = Mockito.mock(DefaultApi.class);
List<Map<String, String>> mockEntity = new ArrayList<>();
mockEntity.add(ImmutableMap.of(
"TOKENS", "1,2,3,4",
Expand All @@ -499,7 +500,17 @@ public void testGetTokens() throws Exception {
"IS_LOCAL", "false"
)
);
mockEntity.add(ImmutableMap.of(
"TOKENS", "null",
"IS_LOCAL", "false"
)
);
mockEntity.add(ImmutableMap.of(
"IS_LOCAL", "false"
)
);
EndpointStates mockEndpointStates = new EndpointStates().entity(mockEntity);
DefaultApi mockClient = Mockito.mock(DefaultApi.class);
when(mockClient.getEndpointStates()).thenReturn(mockEndpointStates);
mockProxy(mockClient);
assertThat(mockProxy(mockClient).getTokens()).containsOnly(
Expand Down Expand Up @@ -763,4 +774,28 @@ public void testGetPendingCompactionsFail() throws ReaperException {
metricsProxy);
proxy.getPendingCompactions();
}

@Test
//Test when endpoint state contains TOKENS and it is not null
public void testIsCoordinatorNode_WhenTokensNonNull() {
HashMap<String, String> endpointState = new HashMap<String, String>();
endpointState.put("TOKENS", "12345");
assertFalse(HttpCassandraManagementProxy.isCoordinatorNode(endpointState));
}

@Test
//Test when endpoint state contains TOKENS and it is null
public void testIsCoordinatorNode_WhenTokensNull() {
HashMap<String, String> endpointState = new HashMap<String, String>();
endpointState.put("TOKENS", "null");
assertTrue(HttpCassandraManagementProxy.isCoordinatorNode(endpointState));
}

@Test
//Test when endpoint state does not contain TOKENS
public void testIsCoordinatorNode_NoTokens() {
HashMap<String, String> endpointState = new HashMap<String, String>();
endpointState.put("OTHER", "value");
assertTrue(HttpCassandraManagementProxy.isCoordinatorNode(endpointState));
}
}

0 comments on commit 0c1c835

Please sign in to comment.