We learned in the YugabyteDB Tip View Metadata for YSQL/YCQL/System Tablets on a Server about the new (in YugabyteDB 2024.1) system view yb_local_tablets.
The view retrieves metadata for YSQL, YCQL, and system tablets of a node.
Unfortunately, it doesn’t specify whether a tablet is a Leader or a Follower on the node, but we can use a simple trick to determine this.
I have three nodes in my YugabyteDB cluster:
[root@yugabytedb yb2]# ysqlsh -h 127.0.0.1 -c "SELECT host FROM yb_servers() ORDER By host;"
host
-----------
127.0.0.1
127.0.0.2
127.0.0.3
(3 rows)
Let’s create a table and split it into three tablets.
[root@yugabytedb yb2]# ysqlsh -h 127.0.0.1 -c "CREATE TABLE my_table(c1 INT PRIMARY KEY) SPLIT INTO 3 TABLETS;"
CREATE TABLE
[root@yugabytedb yb2]# ysqlsh -h 127.0.0.1 -c "SELECT table_id, tablet_id FROM yb_local_tablets WHERE table_name = 'my_table' ORDER BY tablet_id;"
table_id | tablet_id
----------------------------------+----------------------------------
000033cc000030008000000000004008 | 0455e0c3ebdd4666ae4ca0c454f764c7
000033cc000030008000000000004008 | 383105a309364f5b8c5e81b12e04c660
000033cc000030008000000000004008 | 7b283c0cb248414aa39351af26d24aef
(3 rows)
One of the tablets should be a Leader, while the other two should be Followers. But which is which?
We can use the command line tool yb-admin to list each tablet for the table and their Leader host IP. We can load this output into a YSQL table for reference.
[root@yugabytedb yb2]# ysqlsh -h 127.0.0.1 -c "CREATE TABLE tablet_leaders(table_id TEXT, tablet_id TEXT, leader_ip TEXT);"
CREATE TABLE
[root@yugabytedb yb2]# yb-admin -master_addresses 127.0.0.1 list_tablets ysql.yugabyte tableid.000033cc000030008000000000004008 0 | awk -F'\\t' '{ print $1, $3 }' | sort -k 2 | sed '1h;1d;$!H;$!d;G' | sed -e 's/[[:space:]]*$//g' | sed 's/ \+/|/g' | ysqlsh -h 127.0.0.1 -c "COPY tablet_leaders FROM STDIN DELIMITER '|' CSV HEADER;"
COPY 3
[root@yugabytedb yb2]# ysqlsh -h 127.0.0.1 -c "SELECT * FROM tablet_leaders ORDER BY tablet_id;"
tablet_id | leader_ip
----------------------------------+----------------
0455e0c3ebdd4666ae4ca0c454f764c7 | 127.0.0.3:9100
383105a309364f5b8c5e81b12e04c660 | 127.0.0.1:9100
7b283c0cb248414aa39351af26d24aef | 127.0.0.2:9100
(3 rows)
We can now perform a simple join to display the Leader/Follower status for each Tablet!
[root@yugabytedb yb2]# ysqlsh -h 127.0.0.1 -c "SELECT tablet_id, CASE WHEN split_part(inet_server_addr()::TEXT, '/', 1) = split_part(leader_ip, ':', 1) THEN 'Yes' ELSE 'No' END is_leader FROM yb_local_tablets JOIN tablet_leaders USING (tablet_id) WHERE table_name = 'my_table' ORDER BY tablet_id;"
tablet_id | is_leader
----------------------------------+-----------
0455e0c3ebdd4666ae4ca0c454f764c7 | No
383105a309364f5b8c5e81b12e04c660 | Yes
7b283c0cb248414aa39351af26d24aef | No
(3 rows)
We’ll need to connect to the remaining nodes, running the same query, to show the Leader/Follower status for the those tablets.
[root@yugabytedb yb2]# ysqlsh -h 127.0.0.2 -c "SELECT tablet_id, CASE WHEN split_part(inet_server_addr()::TEXT, '/', 1) = split_part(leader_ip, ':', 1) THEN 'Yes' ELSE 'No' END is_leader FROM yb_local_tablets JOIN tablet_leaders USING (tablet_id) WHERE table_name = 'my_table' ORDER BY tablet_id;"
tablet_id | is_leader
----------------------------------+-----------
0455e0c3ebdd4666ae4ca0c454f764c7 | No
383105a309364f5b8c5e81b12e04c660 | No
7b283c0cb248414aa39351af26d24aef | Yes
(3 rows)
[root@yugabytedb yb2]# ysqlsh -h 127.0.0.3 -c "SELECT tablet_id, CASE WHEN split_part(inet_server_addr()::TEXT, '/', 1) = split_part(leader_ip, ':', 1) THEN 'Yes' ELSE 'No' END is_leader FROM yb_local_tablets JOIN tablet_leaders USING (tablet_id) WHERE table_name = 'my_table' ORDER BY tablet_id;"
tablet_id | is_leader
----------------------------------+-----------
0455e0c3ebdd4666ae4ca0c454f764c7 | Yes
383105a309364f5b8c5e81b12e04c660 | No
7b283c0cb248414aa39351af26d24aef | No
(3 rows)
Have Fun!