Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter out Stargate/coordinator nodes from calls to get live nodes or list tokens #1433

Merged
merged 2 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
}
}