Skip to content

Commit 1e0eae9

Browse files
authored
Add "WHERE key='local'" to accelerate queries against system.local (#434)
Just like other drivers (Python, GoCQL), the Java 4.x driver can query with this simple WHERE clause added, to improve query performance. Fixes: #282 Signed-off-by: Yaniv Kaul <[email protected]>
1 parent 50373b6 commit 1e0eae9

File tree

6 files changed

+16
-15
lines changed

6 files changed

+16
-15
lines changed

core/src/main/java/com/datastax/oss/driver/internal/core/channel/ProtocolInitHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
class ProtocolInitHandler extends ConnectInitHandler {
8484
private static final Logger LOG = LoggerFactory.getLogger(ProtocolInitHandler.class);
8585
private static final Query CLUSTER_NAME_QUERY =
86-
new Query("SELECT cluster_name FROM system.local");
86+
new Query("SELECT cluster_name FROM system.local WHERE key='local'");
8787

8888
private final InternalDriverContext context;
8989
private final long timeoutMillis;

core/src/main/java/com/datastax/oss/driver/internal/core/metadata/DefaultTopologyMonitor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,8 @@ public CompletionStage<Iterable<NodeInfo>> refreshNodeList() {
170170

171171
savePort(channel);
172172

173-
CompletionStage<AdminResult> localQuery = query(channel, "SELECT * FROM system.local");
173+
CompletionStage<AdminResult> localQuery =
174+
query(channel, "SELECT * FROM system.local WHERE key='local'");
174175
CompletionStage<AdminResult> peersV2Query = query(channel, "SELECT * FROM system.peers_v2");
175176
CompletableFuture<AdminResult> peersQuery = new CompletableFuture<>();
176177

core/src/test/java/com/datastax/oss/driver/internal/core/channel/ProtocolInitHandlerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ public void should_check_cluster_name_if_provided() {
434434
requestFrame = readOutboundFrame();
435435
assertThat(requestFrame.message).isInstanceOf(Query.class);
436436
Query query = (Query) requestFrame.message;
437-
assertThat(query.query).isEqualTo("SELECT cluster_name FROM system.local");
437+
assertThat(query.query).isEqualTo("SELECT cluster_name FROM system.local WHERE key='local'");
438438
assertThat(connectFuture).isNotDone();
439439

440440
writeInboundFrame(requestFrame, TestResponses.clusterNameResponse("expectedClusterName"));

core/src/test/java/com/datastax/oss/driver/internal/core/metadata/DefaultTopologyMonitorTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ public void should_refresh_node_list_from_local_and_peers() {
337337
AdminRow peer3 = mockPeersRow(3, UUID.randomUUID());
338338
AdminRow peer2 = mockPeersRow(2, node2.getHostId());
339339
topologyMonitor.stubQueries(
340-
new StubbedQuery("SELECT * FROM system.local", mockResult(local)),
340+
new StubbedQuery("SELECT * FROM system.local WHERE key='local'", mockResult(local)),
341341
new StubbedQuery("SELECT * FROM system.peers_v2", Collections.emptyMap(), null, true),
342342
new StubbedQuery("SELECT * FROM system.peers", mockResult(peer3, peer2)));
343343

@@ -447,7 +447,7 @@ public void should_warn_when_control_host_found_in_system_peers() {
447447
AdminRow peer2 = mockPeersRow(2, node2.getHostId());
448448
AdminRow peer1 = mockPeersRow(1, node2.getHostId()); // invalid
449449
topologyMonitor.stubQueries(
450-
new StubbedQuery("SELECT * FROM system.local", mockResult(local)),
450+
new StubbedQuery("SELECT * FROM system.local WHERE key='local'", mockResult(local)),
451451
new StubbedQuery("SELECT * FROM system.peers_v2", Collections.emptyMap(), null, true),
452452
new StubbedQuery("SELECT * FROM system.peers", mockResult(peer3, peer2, peer1)));
453453

@@ -478,7 +478,7 @@ public void should_warn_when_control_host_found_in_system_peers_v2() {
478478
AdminRow peer2 = mockPeersRow(2, node2.getHostId());
479479
AdminRow peer1 = mockPeersRow(1, node2.getHostId()); // invalid
480480
topologyMonitor.stubQueries(
481-
new StubbedQuery("SELECT * FROM system.local", mockResult(local)),
481+
new StubbedQuery("SELECT * FROM system.local WHERE key='local'", mockResult(local)),
482482
new StubbedQuery("SELECT * FROM system.peers_v2", mockResult(peer3, peer2, peer1)));
483483

484484
// When

integration-tests/src/test/java/com/datastax/oss/driver/core/ProtocolVersionMixedClusterIT.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ public void should_downgrade_if_peer_does_not_support_negotiated_version() {
7676
assertThat(protocolQueries(contactPoint, 4))
7777
.containsExactly(
7878
// Initial connection with protocol v4
79-
"SELECT cluster_name FROM system.local",
80-
"SELECT * FROM system.local",
79+
"SELECT cluster_name FROM system.local WHERE key='local'",
80+
"SELECT * FROM system.local WHERE key='local'",
8181
"SELECT * FROM system.peers_v2",
8282
"SELECT * FROM system.peers");
8383
}
@@ -104,8 +104,8 @@ public void should_keep_current_if_supported_by_all_peers() {
104104
assertThat(protocolQueries(contactPoint, 4))
105105
.containsExactly(
106106
// Initial connection with protocol v4
107-
"SELECT cluster_name FROM system.local",
108-
"SELECT * FROM system.local",
107+
"SELECT cluster_name FROM system.local WHERE key='local'",
108+
"SELECT * FROM system.local WHERE key='local'",
109109
"SELECT * FROM system.peers_v2",
110110
"SELECT * FROM system.peers");
111111
}
@@ -155,8 +155,8 @@ public void should_not_downgrade_and_force_down_old_nodes_if_version_forced() {
155155
assertThat(protocolQueries(contactPoint, 4))
156156
.containsExactly(
157157
// Initial connection with protocol v4
158-
"SELECT cluster_name FROM system.local",
159-
"SELECT * FROM system.local",
158+
"SELECT cluster_name FROM system.local WHERE key='local'",
159+
"SELECT * FROM system.local WHERE key='local'",
160160
"SELECT * FROM system.peers_v2",
161161
"SELECT * FROM system.peers");
162162

integration-tests/src/test/java/com/datastax/oss/driver/core/metadata/ZeroTokenNodesIT.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public void should_not_discover_zero_token_DC_when_option_is_disabled() {
9292

9393
Set<String> landedOn = new HashSet<>();
9494
for (int i = 0; i < 30; i++) {
95-
ResultSet rs = session.execute("SELECT * FROM system.local");
95+
ResultSet rs = session.execute("SELECT * FROM system.local WHERE key='local'");
9696
InetAddress broadcastRpcInetAddress =
9797
Objects.requireNonNull(rs.one()).getInetAddress("rpc_address");
9898
landedOn.add(Objects.requireNonNull(broadcastRpcInetAddress).toString());
@@ -130,7 +130,7 @@ public void should_discover_zero_token_DC_when_option_is_enabled() {
130130

131131
Set<String> landedOn = new HashSet<>();
132132
for (int i = 0; i < 30; i++) {
133-
ResultSet rs = session.execute("SELECT * FROM system.local");
133+
ResultSet rs = session.execute("SELECT * FROM system.local WHERE key='local'");
134134
InetAddress broadcastRpcInetAddress =
135135
Objects.requireNonNull(rs.one()).getInetAddress("rpc_address");
136136
landedOn.add(Objects.requireNonNull(broadcastRpcInetAddress).toString());
@@ -201,7 +201,7 @@ public void should_connect_to_zero_token_DC() {
201201

202202
Set<String> landedOn = new HashSet<>();
203203
for (int i = 0; i < 30; i++) {
204-
ResultSet rs = session.execute("SELECT * FROM system.local");
204+
ResultSet rs = session.execute("SELECT * FROM system.local WHERE key='local'");
205205
InetAddress broadcastRpcInetAddress =
206206
Objects.requireNonNull(rs.one()).getInetAddress("rpc_address");
207207
landedOn.add(Objects.requireNonNull(broadcastRpcInetAddress).toString());

0 commit comments

Comments
 (0)