这是学习笔记的第 1995 篇文章
之前做了一个初版的生命周期设计,导致对于实现的难度低估,在实际设计的时候,碰到了一些意料之外的边界问题。
在和前端业务部门做了几次沟通之后,从数据的管理角度需要进一步的规范设计。
所以从难度上来说,低估会导致我有成倍的工作量,当然目标都立项了,再苦再累也得含着泪做完。
对于整个生命周期的管理,我做了如下的流程设计。
其中数据的抽取是周期性的,而数据管理的粒度则是分为表,字段,索引,然后根据这些粒度延伸出相关的变更历史信息。
在这个基础上可以扩展出一些功能,比如某个业务就对某个表的数据变化格外关注,那么它可以订阅这个数据变化,或者是和工单的数据打通,让数据的变化和流程内,流程外的变更关联起来。
对外提供的数据服务,是使用API的方式,提供变更列表的查询,对象变更的查询和相关的轨迹树。
- 覆盖场景
整个生命周期管理中,需要覆盖以下的一些场景:
1)新增表
2)删除表
3)表变更
a) 字段变更
b) 索引变更
- 应用场景
对后端管理来说,可以提供一个完整的列表信息,
l 模糊查看
1)根据时间维度来查看最近哪些表结构发生了变更,该接口暂不对外
² 精确查看
对于业务来说,可以提供基础的IP,端口信息,提供两种维度的查询,显示的是完整的列表,比如数据库test在2月10日包含10张表,2月12日新增了2张表,2月17日删除了1张表,变更表之间彼此没有关联
² 查询范围是2月10日之前,则显示10张表,状态为有效
² 查询范围是2月10日~2月12日,显示12张表信息,状态为有效
² 查询范围是2月12日~2月17日,显示12张表信息,其中11个为有效,1个为失效
² 查询范围是2月17日以后,显示12张表信息,其中11个为有效,1个为失效
查询范围默认为近3天,支持如下两种维度的查询:
根据对象维度来查看一个时间范围内的“数据库-表”列表
根据时间维度来查看一个时间范围内的“数据库-表“列表
其中对于数据抽取方面,算是得到了一些辅助信息,比如MySQL里面的information_schema.tables的字段create_time,update_time算是两个维度(DDL,DML)的跟踪时间,和我们认知里的实现方式不大一样,所以对于一张表,我们直接通过tables去查看是得不到较为准确的创建时间的。而这个信息不够统一,那么我们势必在后续管理中会产生混乱,所以生命周期管理的基础功能是紧迫而重要的。
对于时间的测试是其中的一个重点,如下是一些测试的日志,主要通过如下的方式来进行对比发现create_time,update_time的变化规律。
mysql--root@localhost:test 19:07:10>>drop table test_tab;
Query OK, 0 rows affected (0.01 sec)
mysql--root@localhost:test 19:08:16>>create table test_tab(id int primary key,name varchar(30),memo varchar(50));
Query OK, 0 rows affected (0.02 sec)
mysql--root@localhost:test 19:08:23>>show create table test_tab;
| test_tab | CREATE TABLE `test_tab` (
`id` int(11) NOT NULL,
`name` varchar(30) DEFAULT NULL,
`memo` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
1 row in set (0.00 sec)
mysql--root@localhost:test 19:08:47>>select *from information_schema.tables where table_name='test_tab' and table_schema='test'G
*************************** 1. row ***************************
TABLE_CATALOG: def
TABLE_SCHEMA: test
TABLE_NAME: test_tab
TABLE_TYPE: BASE TABLE
ENGINE: InnoDB
VERSION: 10
ROW_FORMAT: Dynamic
TABLE_ROWS: 0
AVG_ROW_LENGTH: 0
DATA_LENGTH: 16384
MAX_DATA_LENGTH: 0
INDEX_LENGTH: 0
DATA_FREE: 0
AUTO_INCREMENT: NULL
CREATE_TIME: 2019-05-28 19:08:23
UPDATE_TIME: NULL
CHECK_TIME: NULL
TABLE_COLLATION: utf8_general_ci
CHECKSUM: NULL
CREATE_OPTIONS:
TABLE_COMMENT:
1 row in set (0.00 sec)
mysql--root@localhost:test 19:08:55>>alter table test_tab add unique key idx_test_name(name);
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql--root@localhost:test 19:09:16>>select *from information_schema.tables where table_name='test_tab' and table_schema='test'G
*************************** 1. row ***************************
TABLE_CATALOG: def
TABLE_SCHEMA: test
TABLE_NAME: test_tab
TABLE_TYPE: BASE TABLE
ENGINE: InnoDB
VERSION: 10
ROW_FORMAT: Dynamic
TABLE_ROWS: 0
AVG_ROW_LENGTH: 0
DATA_LENGTH: 16384
MAX_DATA_LENGTH: 0
INDEX_LENGTH: 0
DATA_FREE: 0
AUTO_INCREMENT: NULL
CREATE_TIME: 2019-05-28 19:09:16
UPDATE_TIME: NULL
CHECK_TIME: NULL
TABLE_COLLATION: utf8_general_ci
CHECKSUM: NULL
CREATE_OPTIONS:
TABLE_COMMENT:
1 row in set (0.00 sec)
mysql--root@localhost:test 19:09:31>>alter table test_tab drop column memo;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql--root@localhost:test 19:09:50>>select *from information_schema.tables where table_name='test_tab' and table_schema='test'G
*************************** 1. row ***************************
TABLE_CATALOG: def
TABLE_SCHEMA: test
TABLE_NAME: test_tab
TABLE_TYPE: BASE TABLE
ENGINE: InnoDB
VERSION: 10
ROW_FORMAT: Dynamic
TABLE_ROWS: 0
AVG_ROW_LENGTH: 0
DATA_LENGTH: 16384
MAX_DATA_LENGTH: 0
INDEX_LENGTH: 16384
DATA_FREE: 0
AUTO_INCREMENT: NULL
CREATE_TIME: 2019-05-28 19:09:50
UPDATE_TIME: NULL
CHECK_TIME: NULL
TABLE_COLLATION: utf8_general_ci
CHECKSUM: NULL
CREATE_OPTIONS:
TABLE_COMMENT:
1 row in set (0.00 sec)
mysql--root@localhost:test 19:10:01>>insert into test_tab values(1,'aa');
Query OK, 1 row affected (0.01 sec)
mysql--root@localhost:test 19:10:10>>select *from information_schema.tables where table_name='test_tab' and table_schema='test'G
*************************** 1. row ***************************
TABLE_CATALOG: def
TABLE_SCHEMA: test
TABLE_NAME: test_tab
TABLE_TYPE: BASE TABLE
ENGINE: InnoDB
VERSION: 10
ROW_FORMAT: Dynamic
TABLE_ROWS: 1
AVG_ROW_LENGTH: 16384
DATA_LENGTH: 16384
MAX_DATA_LENGTH: 0
INDEX_LENGTH: 16384
DATA_FREE: 0
AUTO_INCREMENT: NULL
CREATE_TIME: 2019-05-28 19:09:50
UPDATE_TIME: 2019-05-28 19:10:10
CHECK_TIME: NULL
TABLE_COLLATION: utf8_general_ci
CHECKSUM: NULL
CREATE_OPTIONS:
TABLE_COMMENT:
1 row in set (0.01 sec)
mysql--root@localhost:test 19:10:20>>insert into test_tab values(2,'bb');
Query OK, 1 row affected (0.00 sec)
mysql--root@localhost:test 19:10:40>>select *from information_schema.tables where table_name='test_tab' and table_schema='test'G
*************************** 1. row ***************************
TABLE_CATALOG: def
TABLE_SCHEMA: test
TABLE_NAME: test_tab
TABLE_TYPE: BASE TABLE
ENGINE: InnoDB
VERSION: 10
ROW_FORMAT: Dynamic
TABLE_ROWS: 2
AVG_ROW_LENGTH: 8192
DATA_LENGTH: 16384
MAX_DATA_LENGTH: 0
INDEX_LENGTH: 16384
DATA_FREE: 0
AUTO_INCREMENT: NULL
CREATE_TIME: 2019-05-28 19:09:50
UPDATE_TIME: 2019-05-28 19:10:40
CHECK_TIME: NULL
TABLE_COLLATION: utf8_general_ci
CHECKSUM: NULL
CREATE_OPTIONS:
TABLE_COMMENT:
1 row in set (0.00 sec)
mysql--root@localhost:test 19:10:45>>delete from test_tab where id=1;
Query OK, 1 row affected (0.01 sec)
mysql--root@localhost:test 19:10:59>>select *from information_schema.tables where table_name='test_tab' and table_schema='test'G
*************************** 1. row ***************************
TABLE_CATALOG: def
TABLE_SCHEMA: test
TABLE_NAME: test_tab
TABLE_TYPE: BASE TABLE
ENGINE: InnoDB
VERSION: 10
ROW_FORMAT: Dynamic
TABLE_ROWS: 1
AVG_ROW_LENGTH: 16384
DATA_LENGTH: 16384
MAX_DATA_LENGTH: 0
INDEX_LENGTH: 16384
DATA_FREE: 0
AUTO_INCREMENT: NULL
CREATE_TIME: 2019-05-28 19:09:50
UPDATE_TIME: 2019-05-28 19:10:59
CHECK_TIME: NULL
TABLE_COLLATION: utf8_general_ci
CHECKSUM: NULL
CREATE_OPTIONS:
TABLE_COMMENT:
1 row in set (0.00 sec)
mysql--root@localhost:test 19:11:10>>update test_tab set name='ccc' where id=2;
Query OK, 1 row affected (0.02 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql--root@localhost:test 19:11:28>>select *from information_schema.tables where table_name='test_tab' and table_schema='test'G
*************************** 1. row ***************************
TABLE_CATALOG: def
TABLE_SCHEMA: test
TABLE_NAME: test_tab
TABLE_TYPE: BASE TABLE
ENGINE: InnoDB
VERSION: 10
ROW_FORMAT: Dynamic
TABLE_ROWS: 1
AVG_ROW_LENGTH: 16384
DATA_LENGTH: 16384
MAX_DATA_LENGTH: 0
INDEX_LENGTH: 16384
DATA_FREE: 0
AUTO_INCREMENT: NULL
CREATE_TIME: 2019-05-28 19:09:50
UPDATE_TIME: 2019-05-28 19:11:28
CHECK_TIME: NULL
TABLE_COLLATION: utf8_general_ci
CHECKSUM: NULL
CREATE_OPTIONS:
TABLE_COMMENT:
1 row in set (0.01 sec)
mysql--root@localhost:test 19:11:37>>update test_tab set name='aaaaaa' where id=100;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0 Changed: 0 Warnings: 0
mysql--root@localhost:test 19:12:13>>select *from information_schema.tables where table_name='test_tab' and table_schema='test'G
*************************** 1. row ***************************
TABLE_CATALOG: def
TABLE_SCHEMA: test
TABLE_NAME: test_tab
TABLE_TYPE: BASE TABLE
ENGINE: InnoDB
VERSION: 10
ROW_FORMAT: Dynamic
TABLE_ROWS: 1
AVG_ROW_LENGTH: 16384
DATA_LENGTH: 16384
MAX_DATA_LENGTH: 0
INDEX_LENGTH: 16384
DATA_FREE: 0
AUTO_INCREMENT: NULL
CREATE_TIME: 2019-05-28 19:09:50
UPDATE_TIME: 2019-05-28 19:11:28
CHECK_TIME: NULL
TABLE_COLLATION: utf8_general_ci
CHECKSUM: NULL
CREATE_OPTIONS:
TABLE_COMMENT:
1 row in set (0.01 sec)
mysql--root@localhost:test 19:12:18>>select *from test_tab;
---- ------
| id | name |
---- ------
| 2 | ccc |
---- ------
1 row in set (0.00 sec)
mysql--root@localhost:test 19:12:27>>update test_tab set name='ccc' where id=2;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 0
mysql--root@localhost:test 19:12:39>>select *from information_schema.tables where table_name='test_tab' and table_schema='test'G
*************************** 1. row ***************************
TABLE_CATALOG: def
TABLE_SCHEMA: test
TABLE_NAME: test_tab
TABLE_TYPE: BASE TABLE
ENGINE: InnoDB
VERSION: 10
ROW_FORMAT: Dynamic
TABLE_ROWS: 1
AVG_ROW_LENGTH: 16384
DATA_LENGTH: 16384
MAX_DATA_LENGTH: 0
INDEX_LENGTH: 16384
DATA_FREE: 0
AUTO_INCREMENT: NULL
CREATE_TIME: 2019-05-28 19:09:50
UPDATE_TIME: 2019-05-28 19:11:28
CHECK_TIME: NULL
TABLE_COLLATION: utf8_general_ci
CHECKSUM: NULL
CREATE_OPTIONS:
TABLE_COMMENT:
1 row in set (0.01 sec)