From 72d5b4d1b117a4a86cd578197ecdba667aa56343 Mon Sep 17 00:00:00 2001 From: Brad Schoening <5796692+bschoening@users.noreply.github.com> Date: Thu, 12 May 2022 20:03:25 -0400 Subject: [PATCH] add CQLSH command SHOW REPLICAS patch by Brad Schoening; reviewed by Stefan Miklosovic and Brandon Williams for CASSANDRA-17577 --- CHANGES.txt | 1 + bin/cqlsh.py | 18 +++++++++++++++++- doc/modules/cassandra/pages/tools/cqlsh.adoc | 15 +++++++++++++++ pylib/cqlshlib/cqlshhandling.py | 2 +- 4 files changed, 34 insertions(+), 2 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 6d9feb52ff..a7c8c576d6 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -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) diff --git a/bin/cqlsh.py b/bin/cqlsh.py index 637c95e70a..35eb429aba 100755 --- a/bin/cqlsh.py +++ b/bin/cqlsh.py @@ -600,6 +600,13 @@ class Shell(cmd.Cmd): 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 = { @@ -979,7 +986,7 @@ class Shell(cmd.Cmd): 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') @@ -1578,6 +1585,11 @@ class Shell(cmd.Cmd): SHOW SESSION Pretty-prints the requested tracing session. + + SHOW REPLICAS () + + 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': @@ -1588,6 +1600,10 @@ class Shell(cmd.Cmd): 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,)) diff --git a/doc/modules/cassandra/pages/tools/cqlsh.adoc b/doc/modules/cassandra/pages/tools/cqlsh.adoc index 8050ee5df0..0d40608c2c 100644 --- a/doc/modules/cassandra/pages/tools/cqlsh.adoc +++ b/doc/modules/cassandra/pages/tools/cqlsh.adoc @@ -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 ()` + +Example usage: + +[source,none] +---- +cqlsh> SHOW REPLICAS 95 +['192.0.0.1', '192.0.0.2'] +---- + === `SHOW SESSION` Pretty prints a specific tracing session. diff --git a/pylib/cqlshlib/cqlshhandling.py b/pylib/cqlshlib/cqlshhandling.py index aa1fbc0183..cc8590a44f 100644 --- a/pylib/cqlshlib/cqlshhandling.py +++ b/pylib/cqlshlib/cqlshhandling.py @@ -131,7 +131,7 @@ cqlsh_serial_consistency_level_syntax_rules = r''' ''' cqlsh_show_cmd_syntax_rules = r''' - ::= "SHOW" what=( "VERSION" | "HOST" | "SESSION" sessionid= ) + ::= "SHOW" what=( "VERSION" | "HOST" | "SESSION" sessionid= | "REPLICAS" token= (keyspace=)? ) ; '''