OceanBase作为分布式数据库,查问题找对节点很关键。好在OceanBase执行的每一条SQL都能通过trace_id来关联起来,知道trace_id怎么知道是在哪个节点发起的呢,请看本文。
ob内部trace_id的生成函数如下,通过server ip来做处理的
- function parse_trace_id(trace) {
- var id = (trace.split('-')[0]).split('Y')[1]
- var uval = parseInt(id, 16);
- var ip = uval & 0xffffffff;
- var port = (uval >> 32) & 0xffff
- console.log(ip, port);
- var result = "" + (ip >> 24 & 0xff) + "." + (ip >> 16 & 0xff) + "." + (ip >> 8 & 0xff) + "." + (ip >> 0 & 0xff) + ":" + port
- $("#result").html("IP: " + result);
- alert(result);
- return result;
- }
1. 获取SQL的trace_id
从gv$ob_sql_audit中获取或者通过SELECT last_trace_id();获取
方式一:通过sql_audit中获取
select query_sql,trace_id from oceanbase.GV$OB_SQL_AUDIT where query_sql like 'xxx%' order by REQUEST_TIME desc limit 5;
方式二:通过当前session执行SELECT last_trace_id();获取
SELECT last_trace_id();
2. 执行python脚本解析trace_id获取IP地址和端口
- def get_observer_ip_from_trace_id(content):
- if content[0] == 'Y' and len(content) >= 12:
- sep = content.find('-')
- uval = int(content[1:sep], 16)
- ip = uval & 0xFFFFFFFF
- port = (uval >> 32) & 0xFFFF
- return "%d.%d.%d.%d:%d" % ((ip >> 24 & 0xFF), (ip >> 16 & 0xFF), (ip >> 8 & 0xFF), (ip >> 0 & 0xFF), port)
- else:
- return ""
-
- # 示例trace_id,你需要替换为实际的trace_id
- trace_id = "YB42AC1E81CB-00060AF2025B8782"
-
- # 调用函数并打印结果
- result = get_observer_ip_from_trace_id(trace_id)
- if result:
- print(f"Parsed IP and Port: {result}")
- else:
- print("Invalid trace_id format")