From 1dd7be4b4338b10d11fa2f1089b6253086062d46 Mon Sep 17 00:00:00 2001 From: obdev Date: Fri, 9 Feb 2024 01:13:07 +0000 Subject: [PATCH] add more info to sql audit snapshot source field --- deps/oblib/src/lib/ob_define.h | 1 + .../ob_inner_table_schema.11001_11050.cpp | 4 +- .../ob_inner_table_schema.15001_15050.cpp | 4 +- .../inner_table/ob_inner_table_schema_def.py | 2 +- src/sql/monitor/ob_exec_stat.h | 3 +- src/sql/ob_sql_trans_control.cpp | 3 +- src/storage/tx/ob_trans_define_v4.cpp | 46 ++++++++++++++++++- src/storage/tx/ob_trans_define_v4.h | 3 +- .../mysql/desc_virtual_table_in_mysql.result | 2 +- .../r/mysql/desc_virtual_table_in_sys.result | 2 +- 10 files changed, 58 insertions(+), 12 deletions(-) diff --git a/deps/oblib/src/lib/ob_define.h b/deps/oblib/src/lib/ob_define.h index f2cc1425b2..e3edcbe0af 100644 --- a/deps/oblib/src/lib/ob_define.h +++ b/deps/oblib/src/lib/ob_define.h @@ -81,6 +81,7 @@ const int64_t MAX_IP_ADDR_LENGTH = INET6_ADDRSTRLEN; const int64_t MAX_IP_PORT_LENGTH = MAX_IP_ADDR_LENGTH + 6; const int64_t MAX_IP_PORT_SQL_LENGTH = MAX_IP_ADDR_LENGTH + 12; const uint64_t MAX_IFNAME_LENGTH = 128; +const int64_t OB_MAX_SNAPSHOT_SOURCE_LENGTH = 128; const int64_t OB_MAX_SQL_ID_LENGTH = 32; const int64_t OB_MAX_CLIENT_INFO_LENGTH = 64; const int64_t OB_MAX_MOD_NAME_LENGTH = 48; diff --git a/src/share/inner_table/ob_inner_table_schema.11001_11050.cpp b/src/share/inner_table/ob_inner_table_schema.11001_11050.cpp index 3d60388351..2ba58a4bd0 100644 --- a/src/share/inner_table/ob_inner_table_schema.11001_11050.cpp +++ b/src/share/inner_table/ob_inner_table_schema.11001_11050.cpp @@ -10873,7 +10873,7 @@ int ObInnerTableSchema::all_virtual_sql_audit_schema(ObTableSchema &table_schema 0, //part_key_pos ObVarcharType, //column_type CS_TYPE_INVALID, //column_collation_type - 16, //column_length + 128, //column_length -1, //column_precision -1, //column_scale false, //is_nullable @@ -12606,7 +12606,7 @@ int ObInnerTableSchema::all_virtual_sql_audit_all_virtual_sql_audit_i1_schema(Ob 0, //part_key_pos ObVarcharType, //column_type CS_TYPE_INVALID, //column_collation_type - 16, //column_length + 128, //column_length -1, //column_precision -1, //column_scale false,//is_nullable diff --git a/src/share/inner_table/ob_inner_table_schema.15001_15050.cpp b/src/share/inner_table/ob_inner_table_schema.15001_15050.cpp index 8cf28aa3be..bb7f63acdf 100644 --- a/src/share/inner_table/ob_inner_table_schema.15001_15050.cpp +++ b/src/share/inner_table/ob_inner_table_schema.15001_15050.cpp @@ -1327,7 +1327,7 @@ int ObInnerTableSchema::all_virtual_sql_audit_ora_schema(ObTableSchema &table_sc 0, //part_key_pos ObVarcharType, //column_type CS_TYPE_UTF8MB4_BIN, //column_collation_type - 16, //column_length + 128, //column_length 2, //column_precision -1, //column_scale false, //is_nullable @@ -3060,7 +3060,7 @@ int ObInnerTableSchema::all_virtual_sql_audit_ora_all_virtual_sql_audit_i1_schem 0, //part_key_pos ObVarcharType, //column_type CS_TYPE_UTF8MB4_BIN, //column_collation_type - 16, //column_length + 128, //column_length 2, //column_precision -1, //column_scale false,//is_nullable diff --git a/src/share/inner_table/ob_inner_table_schema_def.py b/src/share/inner_table/ob_inner_table_schema_def.py index 1c03e289ce..48f3cd4f0f 100644 --- a/src/share/inner_table/ob_inner_table_schema_def.py +++ b/src/share/inner_table/ob_inner_table_schema_def.py @@ -8078,7 +8078,7 @@ def_table_schema( ('ps_inner_stmt_id', 'int'), ('transaction_id', 'int'), ('snapshot_version', 'uint'), - ('snapshot_source', 'varchar:16'), + ('snapshot_source', 'varchar:128'), ('request_type', 'int'), ('is_batched_multi_stmt', 'bool'), ('ob_trace_info', 'varchar:4096'), diff --git a/src/sql/monitor/ob_exec_stat.h b/src/sql/monitor/ob_exec_stat.h index c5395a1fc6..56c07ed0f5 100644 --- a/src/sql/monitor/ob_exec_stat.h +++ b/src/sql/monitor/ob_exec_stat.h @@ -332,7 +332,7 @@ struct ObAuditRecordData { ObString get_snapshot_source() const { - return ObString(snapshot_.source_); + return ObString(snapshot_source_); } int16_t seq_; //packet->get_packet_header().seq_; always 0 currently @@ -404,6 +404,7 @@ struct ObAuditRecordData { bool partition_hit_;// flag for need das partition route or not bool is_perf_event_closed_; char flt_trace_id_[OB_MAX_UUID_STR_LENGTH + 1]; + char snapshot_source_[OB_MAX_SNAPSHOT_SOURCE_LENGTH + 1]; }; } //namespace sql diff --git a/src/sql/ob_sql_trans_control.cpp b/src/sql/ob_sql_trans_control.cpp index 392f5858da..7901fd2cf0 100644 --- a/src/sql/ob_sql_trans_control.cpp +++ b/src/sql/ob_sql_trans_control.cpp @@ -560,7 +560,8 @@ int ObSqlTransControl::start_stmt(ObExecContext &exec_ctx) ar_snapshot.version_ = snapshot.core_.version_; ar_snapshot.tx_id_ = snapshot.core_.tx_id_.get_id(); ar_snapshot.scn_ = snapshot.core_.scn_.cast_to_int(); - ar_snapshot.source_ = snapshot.get_source_name().ptr(); + (void)snapshot.generate_snapshot_source(audit_record.snapshot_source_, sizeof(audit_record.snapshot_source_)); + ar_snapshot.source_ = audit_record.snapshot_source_; } if (OB_SUCC(ret) && !session->has_start_stmt()) { OZ (session->set_start_stmt()); diff --git a/src/storage/tx/ob_trans_define_v4.cpp b/src/storage/tx/ob_trans_define_v4.cpp index 67e2cd91e4..3b61b238cd 100644 --- a/src/storage/tx/ob_trans_define_v4.cpp +++ b/src/storage/tx/ob_trans_define_v4.cpp @@ -1234,10 +1234,52 @@ void ObTxReadSnapshot::wait_consistency() } } } -ObString ObTxReadSnapshot::get_source_name() const + +const char* ObTxReadSnapshot::get_source_name() const { static const char* const SRC_NAME[] = { "INVALID", "GTS", "LOCAL", "WEAK_READ", "USER_SPECIFIED", "NONE" }; - return ObString(SRC_NAME[(int)source_]); + return SRC_NAME[(int)source_]; +} + +/* + * generate sql_audit's snapshot_source fileds + * contains: src, ls_id, ls_role, parts + * when shorter than 128 char like: + * "src:GLOBAL;ls_id:1001;ls_role:LEADER;parts:[(id:1001,epoch:1111),(id:1002,epoch:12222)]" + * when longer than 128 char, with "..." in the end + * "src:GLOBAL;ls_id:1001;ls_role:LEADER;parts:[(lsid:1001,epoch:1111),(lsid:1002,epoch:122..." +*/ +int ObTxReadSnapshot::generate_snapshot_source(char *buf, const int64_t buf_len) const +{ + int ret = OB_SUCCESS; + int64_t pos = 0; + if (OB_ISNULL(buf) || buf_len <= 0) { + ret = OB_INVALID_ARGUMENT; + TRANS_LOG(ERROR, "invaild arguments", K(ret), KPC(this), K(buf_len)); + } else { + const char *snapshot_src = get_source_name(); + const char *ls_role = role_to_string(snapshot_ls_role_); + uint64_t ls_id = snapshot_lsid_.id(); + int n = snprintf(buf, buf_len, "src:%s;ls_id:%ld;ls_role:%s;parts:", + snapshot_src, ls_id, ls_role); + if (n < 0){ + ret = OB_UNEXPECT_INTERNAL_ERROR; + TRANS_LOG(WARN, "fail to fill snapshot source", K(ret), KPC(this), K(n), K(pos), K(buf_len)); + } else { + pos += n; + pos += parts_.to_string(buf + pos, buf_len - pos); + if (pos >= buf_len - 1) { + // buf full, parts fill not complete + // replace end 3 chars with ... + buf[pos - 2] = '.'; + buf[pos - 3] = '.'; + buf[pos - 4] = '.'; + } + buf[pos - 1] = '\0'; + TRANS_LOG(DEBUG, "succeed to generate snapshot source", KPC(this), K(pos), K(buf_len), K(ObString(buf))); + } + } + return ret; } ObTxExecResult::ObTxExecResult() diff --git a/src/storage/tx/ob_trans_define_v4.h b/src/storage/tx/ob_trans_define_v4.h index 6d8930fbee..217715b58f 100644 --- a/src/storage/tx/ob_trans_define_v4.h +++ b/src/storage/tx/ob_trans_define_v4.h @@ -270,11 +270,12 @@ struct ObTxReadSnapshot void init_ls_read(const share::ObLSID &ls_id, const ObTxSnapshot &core); void specify_snapshot_scn(const share::SCN snapshot); void wait_consistency(); - ObString get_source_name() const; + const char* get_source_name() const; bool is_weak_read() const { return SRC::WEAK_READ_SERVICE == source_; }; bool is_none_read() const { return SRC::NONE == source_; } bool is_special() const { return SRC::SPECIAL == source_; } bool is_ls_snapshot() const { return SRC::LS == source_; } + int generate_snapshot_source(char *buf, const int64_t buf_len) const; void reset(); int assign(const ObTxReadSnapshot &); ObTxReadSnapshot(); diff --git a/tools/deploy/mysql_test/test_suite/inner_table/r/mysql/desc_virtual_table_in_mysql.result b/tools/deploy/mysql_test/test_suite/inner_table/r/mysql/desc_virtual_table_in_mysql.result index d1b821866d..cdfeca4d59 100644 --- a/tools/deploy/mysql_test/test_suite/inner_table/r/mysql/desc_virtual_table_in_mysql.result +++ b/tools/deploy/mysql_test/test_suite/inner_table/r/mysql/desc_virtual_table_in_mysql.result @@ -766,7 +766,7 @@ ps_client_stmt_id bigint(20) NO NULL ps_inner_stmt_id bigint(20) NO NULL transaction_id bigint(20) NO NULL snapshot_version bigint(20) unsigned NO NULL -snapshot_source varchar(16) NO NULL +snapshot_source varchar(128) NO NULL request_type bigint(20) NO NULL is_batched_multi_stmt tinyint(4) NO NULL ob_trace_info varchar(4096) NO NULL diff --git a/tools/deploy/mysql_test/test_suite/inner_table/r/mysql/desc_virtual_table_in_sys.result b/tools/deploy/mysql_test/test_suite/inner_table/r/mysql/desc_virtual_table_in_sys.result index 8d08a3a9da..d0ab7c96a7 100644 --- a/tools/deploy/mysql_test/test_suite/inner_table/r/mysql/desc_virtual_table_in_sys.result +++ b/tools/deploy/mysql_test/test_suite/inner_table/r/mysql/desc_virtual_table_in_sys.result @@ -839,7 +839,7 @@ ps_client_stmt_id bigint(20) NO NULL ps_inner_stmt_id bigint(20) NO NULL transaction_id bigint(20) NO NULL snapshot_version bigint(20) unsigned NO NULL -snapshot_source varchar(16) NO NULL +snapshot_source varchar(128) NO NULL request_type bigint(20) NO NULL is_batched_multi_stmt tinyint(4) NO NULL ob_trace_info varchar(4096) NO NULL