Environment
- YugabyteDB Anywhere - YBA
Issue
Finding the tablet on which that particular primary key is on.
Use Case:
A particular partition key is throwing an error and we want to know which tablet that partition key is on.
Resolution
Overview
First of all the primary key, in this case, is going to be the partition key.
Steps:
1. We have to take the hashing range of each tablet and convert it to the decimal number format.
https://www.rapidtables.com/convert/number/hex-to-decimal.html
This link can help in the conversion.
2. Connect the shell
3. Run the below query:
For YCQL:
select partition_hash(id) from table.name where id = the-id-we-are-looking-for;
For YSQL:
select yb_hash_code(the-id-we-are-looking-for) from table.name where id = the-id-we-are-looking-for;
Note: be careful how you pass literal values to yb_hash_code(). You might want either use the field from the table or if you want to pass a literal make sure you cast it to the right type — that can impact the computation of the hash code.
E.g:
yugabyte=# select yb_hash_code(1),yb_hash_code(1::int), yb_hash_code(1::bigint), yb_hash_code(1::float);
yb_hash_code | yb_hash_code | yb_hash_code | yb_hash_code
--------------+--------------+--------------+--------------
4624 | 4624 | 60841 | 4391
If your column is indeed the default type that’s good, else a cast is necessary to get the correct result. I’d recommend always using an explicit cast if using a value to be safe. Though using the table column is simplest if that’s a possibility.
4. We can get the hashing range from the master leader UI: navigate to Tables -> Table name (i.e emp) . Click on the table name and it will open page for you with the tablet distribution for the given table. as shown in the screenshot (column 2):
Below are the detailed steps to find the exact location of the tablet:
The steps are for table emp
with PK empno
Table "public.emp"
Column | Type | Collation | Nullable | Default
------------+---------+-----------+----------+----------------------------------
empno | integer | | not null | generated by default as identity
ename | text | | not null |
job | text | | |
mgr | integer | | |
hiredate | date | | |
sal | integer | | |
comm | integer | | |
deptno | integer | | not null |
email | text | | |
other_info | jsonb | | |
Indexes:
"pk_emp" PRIMARY KEY, lsm (empno HASH)
"emp_email_uk" UNIQUE CONSTRAINT, lsm (email HASH)
Check constraints:
"emp_email_check" CHECK (email ~ '^[a-zA-Z0-9.!#$%&''*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$'::text)
Foreign-key constraints:
"fk_deptno" FOREIGN KEY (deptno) REFERENCES dept(deptno)
"fk_mgr" FOREIGN KEY (mgr) REFERENCES emp(empno)
Referenced by:
TABLE "emp" CONSTRAINT "fk_mgr" FOREIGN KEY (mgr) REFERENCES emp(empno)
Step 1: Find the decimal for the given value of PK using the yb_hash_code
function.
yb_demo=# select yb_hash_code(empno) from emp where empno = 7369;
yb_hash_code
--------------
3667
Step 2: Go to the master leader UI and navigate to Tables -> Table name (i.e emp) . Click on the table name and it will open page for you with the tablet distribution for the given table.
Note: You can try using the yb-admin
command also but it is more tedious
./bin/yb-admin --master_addresses=10.232.0.33:7100,10.232.0.34:7100,10.232.0.35:7100 list_tablets ysql.yb_demo emp
Tablet-UUID Range Leader-IP Leader-UUID
8ee6ab94f52d423282be189198858ee6 partition_key_start: "" partition_key_end: "\025U" 10.232.0.35:9100 13e37dff13fc4edebab3fb0e6729b22c
f8f27a803ffe4c83bd6d1fec0568cb40 partition_key_start: "\025U" partition_key_end: "*\252" 10.232.0.34:9100 9e3648b41c194ea1b429564886bdf5e9
f37222dde3e1485db9edc9996821edca partition_key_start: "*\252" partition_key_end: "?\377" 10.232.0.35:9100 13e37dff13fc4edebab3fb0e6729b22c
fd544139afd5438a9da76ada1da09039 partition_key_start: "?\377" partition_key_end: "UT" 10.232.0.33:9100 47276863de114b508170fb73a0851e9b
87a408731de54f3595f2b69cffd6a661 partition_key_start: "UT" partition_key_end: "j\251" 10.232.0.34:9100 9e3648b41c194ea1b429564886bdf5e9
fcecab7badf24bada7a38686dfb31d14 partition_key_start: "j\251" partition_key_end: "\177\376" 10.232.0.35:9100 13e37dff13fc4edebab3fb0e6729b22c
7fa4c0e423824264a5aa93d0efdf2c5b partition_key_start: "\177\376" partition_key_end: "\225S" 10.232.0.33:9100 47276863de114b508170fb73a0851e9b
f851c0c6b4b64eca91bc39fa7caf3e8c partition_key_start: "\225S" partition_key_end: "\252\250" 10.232.0.33:9100 47276863de114b508170fb73a0851e9b
b02242e6cab442c086c2ab3198dbdfea partition_key_start: "\252\250" partition_key_end: "\277\375" 10.232.0.33:9100 47276863de114b508170fb73a0851e9b
e528609b4fce4529a3e061358edd18f1 partition_key_start: "\277\375" partition_key_end: "\325R" 10.232.0.34:9100 9e3648b41c194ea1b429564886bdf5e9
Step 3: The master UI for the given tablet will show the hash split range for each tablet leader. You will have to take the hashing range of each tablet and convert it to the decimal number format.
https://www.rapidtables.com/convert/number/hex-to-decimal.html
This link can help in the conversion.
For the above example the hash split range will be hash_split: [0x0000, 0x1554].
The dec value for given primary key is 3667
which is between 0 [0x0000]
and 5460 [0x1554]
Attached screenshot for reference.
Based on the tablet leader you can find the follower tablet ID also.
The solution is not straightforward since it requires you to convert each hashing range of each tablet .
Comments
1 comment
How do we find hashing range of each tablet for each table?
Please sign in to leave a comment.