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

[#82] Update PeerMetadataHandler to support new cluster name query #83

Merged
merged 2 commits into from
Sep 23, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -60,7 +60,9 @@ public class PeerMetadataHandler extends StubMapping implements InternalStubMapp

static final UUID schemaVersion = java.util.UUID.randomUUID();

private static final String queryClusterName = "select cluster_name from system.local";
private static final Pattern queryClusterName =
Pattern.compile(
"select cluster_name from system.local( where key='local')?", Pattern.CASE_INSENSITIVE);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: select cluster_name from system.local( where key='local')? => SELECT cluster_name FROM system.local( WHERE key='local')?

Just going for consistency with other CQL patterns (even though it looks like this one slipped through before).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The queries here try to stick as close as possible to what the DS Java driver actually sends, and indeed, oddly enough, the driver usually sends SELECT but for the cluster name, it's select. But I agree with you, now that I turned the patterns case-insensitive we can safely harmonize everything and use SELECT consistently.

private static final RowsMetadata queryClusterNameMetadata;

static {
Expand All @@ -71,11 +73,12 @@ public class PeerMetadataHandler extends StubMapping implements InternalStubMapp
}

private static final Pattern queryPeers =
Pattern.compile("SELECT (.*) FROM system\\.(peers\\S*)");
Pattern.compile("SELECT (.*) FROM system\\.(peers\\S*)", Pattern.CASE_INSENSITIVE);
private static final Pattern queryLocal =
Pattern.compile("SELECT (.*) FROM system\\.local( WHERE key='local')*");
Pattern.compile(
"SELECT (.*) FROM system\\.local( WHERE key='local')?", Pattern.CASE_INSENSITIVE);
private static final Pattern queryPeersWithAddr =
Pattern.compile("SELECT \\* FROM system\\.peers WHERE peer='(.*)'");
Pattern.compile("SELECT \\* FROM system\\.peers WHERE peer='(.*)'", Pattern.CASE_INSENSITIVE);

// query the java driver makes when refreshing node (i.e. after it comes back up)
private static final String queryPeerWithNamedParam =
Expand All @@ -91,7 +94,7 @@ public PeerMetadataHandler() {

public PeerMetadataHandler(boolean supportsV2) {
this.supportsV2 = supportsV2;
queries.add(queryClusterName);
queryPatterns.add(queryClusterName);
queries.add(queryPeerWithNamedParam);
queryPatterns.add(queryPeers);
queryPatterns.add(queryLocal);
Expand Down Expand Up @@ -119,7 +122,8 @@ public List<Action> getActions(AbstractNode node, Frame frame) {
CqlMapper mapper = CqlMapper.forVersion(frame.protocolVersion);
Query query = (Query) frame.message;

if (query.query.equalsIgnoreCase(queryClusterName)) {
Matcher clusterNameMatcher = queryClusterName.matcher(query.query);
if (clusterNameMatcher.matches()) {
return handleClusterNameQuery(node, mapper);
} else {
// if querying for particular peer, return information for only that peer.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,27 @@ public void shouldHandleQueryClusterName() {
.hasColumn(0, 0, cluster.getName());
}

@Test
public void shouldHandleQueryClusterName2() {
// querying the local table for cluster_name should return ClusterSpec.getName()
List<Action> node0Actions =
handler.getActions(
node0, queryFrame("select cluster_name from system.local where key='local'"));

assertThat(node0Actions).hasSize(1);

Action node0Action = node0Actions.get(0);
assertThat(node0Action).isInstanceOf(MessageResponseAction.class);

Message node0Message = ((MessageResponseAction) node0Action).getMessage();

assertThat(node0Message)
.isRows()
.hasRows(1)
.hasColumnSpecs(1)
.hasColumn(0, 0, cluster.getName());
}

@Test
public void shouldHandleQueryAllPeers() {
// querying for peers should return a row for each other node in the cluster
Expand Down