本篇内容介绍了“Mysql 5.7中Gtid相关内部数据结构有哪些”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
1、 Gtid基本格式
单个Gtid:
e859a28b-b66d-11e7-8371-000c291f347d:1
前一部分是server_uuid,后面一部分是执行事物的唯一标志,通常是自增的。内部使用Gtid这种数据结构表示,后面会描述。
区间Gtid:
e859a28b-b66d-11e7-8371-000c291f347d:1-5
前一部分是server_uuid,后面一部分是执行事物的唯一标志集合,在内部使用Gtid_set中某个Sidno对应的Interval节点表示,后面会描述。
2、server_uuid的生成
既然说到了server_uuid这里就开始讨论server_uuid的生成。server_uuid实际上是一个32字节+1字节(/0)的字符串。Mysql启动的时候会调用init_server_auto_options() 读取auto.cnf文件。如果没有读取到则调用generate_server_uuid()调用生成一个server_id。
实际上在这个函数里面会看到server_uuid至少和下面部分有关:
1、mysql启动时间
2、线程Lwp有关
3、一个随机的内存地址有关
请看代码片段:
strncpy(server_uuid, uuid.c_ptr(), UUID_LENGTH);
调用栈帧:
#0 init_server_auto_options () at /root/mysql5.7.14/percona-server-5.7.14-7/sql/mysqld.cc:3810
#1 0x0000000000ec625e in mysqld_main (argc=97, argv=0x2e9af08) at /root/mysql5.7.14/percona-server-5.7.14-7/sql/mysqld.cc:4962
#2 0x0000000000ebd604 in main (argc=10, argv=0x7fffffffe458) at /root/mysql5.7.14/percona-server-5.7.14-7/sql/main.cc:25
3、server_uuid的内部表示binary_log::Uuid
binary_log::Uuid是server_uuid的内部表示实际上核心就是一个16字节的内存空间,如下:
/** The number of bytes in the data of a Uuid. */
static const size_t BYTE_LENGTH= 16;
/** The data for this Uuid. */
unsigned char bytes[BYTE_LENGTH];
server_uuid和binary_log::Uuid之间可以互相转换,在Sid_map中binary_log::Uuid表示的server_uuid实际上就是其sid。
4、类结构Gtid
本结构是单个Gtid的内部表示其核心元素包括:
/// SIDNO of this Gtid.
rpl_sidno sidno;
/// GNO of this Gtid.
rpl_gno gno;
其中gno就是我们说的事物唯一标志,而sidno其实是server_uuid的内部表示binary_log::Uuid (sid)通过hash算法得出的一个查找表中的值。参考函数Sid_map::add_sid本函数则根据binary_log::Uuid (sid)返回一个sidno。
5、类结构Sid_map
既然说到了hash算法那么需要一个内部结构来存储这种整个hash查找表,在Mysql中使用Sid_map来作为这样一个结构,其中包含一个可变数组和一个hash查找表其作用用来已经在注释里面给出。全局只有一个Sid_map。会在Gtid模块初始化的时候分配内存。Sid_map核心元素如下:
/// Read-write lock that protects updates to the number of SIDNOs.
mutable Checkable_rwlock *sid_lock;
/**
Array that maps SIDNO to SID; the element at index N points to a
Node with SIDNO N-1.
*/
Prealloced_array_sidno_to_sid; //因为sidno是一个连续的数值那么更具sidno找到sid只需要简单的做
//数组查找即可这里将node指针存入
/**
Hash that maps SID to SIDNO. The keys in this array are of type
rpl_sid.
*/
HASH _sid_to_sidno; //因为sid是一个数据结构其核心为bytes关键值存储了16字节根据server_uuid
//转换而来的无规律内存空间,需要使用hash查找表快速定位
/**
Array that maps numbers in the interval [0, get_max_sidno()-1] to
SIDNOs, in order of increasing SID.
@see Sid_map::get_sorted_sidno.
*/
Prealloced_array _sorted;//额外的一个关于sidno的数组,具体作用未知