Skip to content

Commit

Permalink
add CQLSH command SHOW REPLICAS
Browse files Browse the repository at this point in the history
patch by Brad Schoening; reviewed by Stefan Miklosovic and Brandon Williams for CASSANDRA-17577
  • Loading branch information
bschoening authored and smiklosovic committed May 27, 2022
1 parent 4280604 commit 72d5b4d
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
4.2
* Add CQLSH command SHOW REPLICAS (CASSANDRA-17577)
* Add guardrail to allow disabling of SimpleStrategy (CASSANDRA-17647)
* Change default directory permission to 750 in packaging (CASSANDRA-17470)
* Adding support for TLS client authentication for internode communication (CASSANDRA-17513)
Expand Down
18 changes: 17 additions & 1 deletion bin/cqlsh.py
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,13 @@ def show_version(self):
def show_session(self, sessionid, partial_session=False):
print_trace_session(self, self.session, sessionid, partial_session)

def show_replicas(self, token_value, keyspace=None):
ks = self.current_keyspace if keyspace is None else keyspace
token_map = self.conn.metadata.token_map
nodes = token_map.get_replicas(ks, token_map.token_class(token_value))
addresses = [x.address for x in nodes]
print(f"{addresses}")

def get_connection_versions(self):
result, = self.session.execute("select * from system.local where key = 'local'")
vers = {
Expand Down Expand Up @@ -979,7 +986,7 @@ def handle_parse_error(self, cmdword, tokens, parsed, srcstr):
if parsed:
self.printerr('Improper %s command (problem at %r).' % (cmdword, parsed.remainder[0]))
else:
self.printerr('Improper %s command.' % cmdword)
self.printerr(f'Improper {cmdword} command.')

def do_use(self, parsed):
ksname = parsed.get_binding('ksname')
Expand Down Expand Up @@ -1578,6 +1585,11 @@ def do_show(self, parsed):
SHOW SESSION <sessionid>
Pretty-prints the requested tracing session.
SHOW REPLICAS <token> (<keyspace>)
Lists the replica nodes by IP address for the given token. The current
keyspace is used if one is not specified.
"""
showwhat = parsed.get_binding('what').lower()
if showwhat == 'version':
Expand All @@ -1588,6 +1600,10 @@ def do_show(self, parsed):
elif showwhat.startswith('session'):
session_id = parsed.get_binding('sessionid').lower()
self.show_session(UUID(session_id))
elif showwhat.startswith('replicas'):
token_id = parsed.get_binding('token')
keyspace = parsed.get_binding('keyspace')
self.show_replicas(token_id, keyspace)
else:
self.printerr('Wait, how do I show %r?' % (showwhat,))

Expand Down
15 changes: 15 additions & 0 deletions doc/modules/cassandra/pages/tools/cqlsh.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,21 @@ cqlsh> SHOW HOST
Connected to Prod_Cluster at 192.0.0.1:9042.
----

=== `SHOW REPLICAS`

Prints the IP addresses of the Cassandra nodes which are replicas for the
listed given token and keyspace. This command is available from Cassandra 4.2.

`Usage`: `SHOW REPLICAS <token> (<keyspace>)`

Example usage:

[source,none]
----
cqlsh> SHOW REPLICAS 95
['192.0.0.1', '192.0.0.2']
----

=== `SHOW SESSION`

Pretty prints a specific tracing session.
Expand Down
2 changes: 1 addition & 1 deletion pylib/cqlshlib/cqlshhandling.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def registrator(f):
'''

cqlsh_show_cmd_syntax_rules = r'''
<showCommand> ::= "SHOW" what=( "VERSION" | "HOST" | "SESSION" sessionid=<uuid> )
<showCommand> ::= "SHOW" what=( "VERSION" | "HOST" | "SESSION" sessionid=<uuid> | "REPLICAS" token=<integer> (keyspace=<keyspaceName>)? )
;
'''

Expand Down

0 comments on commit 72d5b4d

Please sign in to comment.