Skip to content

Instantly share code, notes, and snippets.

@kmuthukk
Created July 15, 2020 18:08
Show Gist options
  • Save kmuthukk/90bec4703148b3d97219a0c2aa8d8755 to your computer and use it in GitHub Desktop.
Save kmuthukk/90bec4703148b3d97219a0c2aa8d8755 to your computer and use it in GitHub Desktop.
A sample health check script to get 1 row from various partitions of the tables in a YCQL keyspace
# pip install yb-cassandra-driver
from cassandra.cluster import Cluster
# cluster = Cluster(['127.0.0.1'])
cluster = Cluster(['172.151.30.71', '172.151.28.193'])
num_checks_per_table=64
keyspace_name="ybdemo_keyspace"
session = cluster.connect()
def check_table_health(keyspace_name, table_name):
print("Checking health for: " + keyspace_name + "." + table_name);
results = session.execute(("SELECT column_name, position " +
"FROM system_schema.columns " +
"WHERE keyspace_name = %s AND table_name = %s " +
"""AND kind='partition_key' """),
(keyspace_name, table_name))
# Add the partition columns to an array sorted by the position of
# the column in the primary key.
partition_columns = [''] * 256
num_partition_columns = 0
for row in results:
partition_columns[row.position] = row.column_name
num_partition_columns = num_partition_columns + 1
del partition_columns[num_partition_columns:] # remove extra null elements from array
p_columns = ",".join(partition_columns)
print("Partition columns for " + keyspace_name + "." + table_name + ": (" + p_columns + ")");
stmt = ("SELECT {} FROM {}.{} " +
"WHERE partition_hash({}) >= ? " +
"AND partition_hash({}) <= ? LIMIT 1").format(p_columns,
keyspace_name,
table_name,
p_columns,
p_columns)
print("Stmt = {}".format(stmt))
prepared_stmt = session.prepare(stmt)
range_size = (64*1024)/num_checks_per_table
print("Performing {} checks for {}.{}".format(num_checks_per_table, keyspace_name, table_name))
for idx in range(num_checks_per_table):
l_bound = idx * range_size
u_bound = l_bound + range_size - 1
print("Checking {} to {} partition...".format(l_bound, u_bound))
# This may return 0 or 1 rows (because of LIMIT 1), but we are not really interested
# in the result. This is just a check to see if the tablet backing the query is alive
# (has a leader) and is responding.
results = session.execute(prepared_stmt, (l_bound, u_bound))
print("--------------------------------------------------------")
def check_keyspace_health(keyspace_name):
print("Checking health for keyspace: " + keyspace_name);
print("--------------------------------------------------------")
tables = []
results = session.execute("select table_name from system_schema.tables where keyspace_name = %s",
(keyspace_name, ));
for row in results:
check_table_health(keyspace_name, row.table_name)
# Main
check_keyspace_health(keyspace_name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment