Skip to content
This repository has been archived by the owner on Jun 24, 2021. It is now read-only.

scripts: add peers distribution of table regions #739

Merged
merged 2 commits into from
Apr 29, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 40 additions & 11 deletions scripts/table-regions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,29 @@ def main():
webContent = subprocess.check_output(["curl", "-sl", httpAPI])
region_info = json.loads(webContent)

table_region_leader = parse_regions(region_info["record_regions"])
indices_region_leader = []
table_region_leaders = parse_regions(region_info["record_regions"])
table_region_peers = parse_region_peers(region_info["record_regions"])
indices_region_leaders = []
indices_region_peers = []
for index_info in region_info["indices"]:
index_name = index_info["name"]
index_region_leader = parse_regions(index_info["regions"])
indices_region_leader.append({"name": index_name, "leader": index_region_leader})
index_region_leaders = parse_regions(index_info["regions"])
indices_region_leaders.append({"name": index_name, "leader": index_region_leaders})
index_region_peers = parse_region_peers(index_info["regions"])
indices_region_peers.append({"name": index_name, "peers": index_region_peers})
# print record
print("[RECORD - {}.{}] - Leaders Distribution:".format(args.database, args.table))
print_leaders(table_region_leaders)
print("[RECORD - {}.{}] - Peers Distribution:".format(args.database, args.table))
print_peers(table_region_peers)
# print indices
for index_region_info in indices_region_leaders:
print("[INDEX - {}] - Leaders Distribution:".format(index_region_info["name"]))
print_leaders(index_region_info["leader"])
for index_region_info in indices_region_peers:
print("[INDEX - {}] - Peers Distribution:".format(index_region_info["name"]))
print_peers(index_region_info["peers"])

print("region leader distribution for table \"{}.{}\":".format(args.database, args.table))
print_leader(table_region_leader)
for index_region_info in indices_region_leader:
print("region leader distribution for index \"{}\":".format(index_region_info["name"]))
print_leader(index_region_info["leader"])

def parse_args():
parser = argparse.ArgumentParser(description="Show leader distribution of a TiDB table.")
Expand All @@ -42,13 +53,31 @@ def parse_regions(regions):
info[store_id] = 1 + info.get(store_id, 0)
return info

def print_leader(info, indent = " "):
def parse_region_peers(regions):
info = {}
for region in regions:
for peer in region["peers"]:
if peer["store_id"] != None:
store_id = peer["store_id"]
info[store_id] = 1 + info.get(store_id, 0)
return info

def print_leaders(info, indent = " "):
total_leaders = 0
for store_id, num_leaders in info.items():
total_leaders += num_leaders
print("{}total leader count: {}".format(indent, total_leaders))
for store_id, num_leaders in info.items():
print("{}store: {}, num_leaders: {}, percentage: {}%".format(indent, store_id, num_leaders, (num_leaders*100.0)/total_leaders))
print("{}store: {}, num_leaders: {}, percentage: {}%".format(indent, store_id, num_leaders,format((num_leaders*100.0)/total_leaders,'.2f')))

def print_peers(info, indent = " "):
total_peers = 0
for store_id, num_peers in info.items():
total_peers += num_peers
print("{}total peers count: {}".format(indent, total_peers))
for store_id, num_peers in info.items():
print("{}store: {}, num_peers: {}, percentage: {}%".format(indent, store_id, num_peers, format((num_peers*100.0)/total_peers,'.2f')))


if __name__ == "__main__":
main()