背景描述
SonarQube 自去年使用开始,已运行一年有余
它上面的 Project 数量已超过 1000
因为每个应用的每个 git 分支的每次 push 事件都会触发 Sonar 分析任务
这样会有很多的数据都存储下来,从而导致数据量很大
目前 Mysql 数据库占用磁盘空间达到100G
并且随着时间推移会继续增加
于是在想是否可以进行数据清理,以降低所使用的磁盘空间
如何进行数据清理?
那么如何进行数据清理呢?
经过不断探索与实践,形成如下解决方案:
1、调小【数据库清理器】相关参数
2、设置 master 为长期分支,并修改【保留短期分支】参数
3、清理非主分支的长期分支
4、MySQL Innodb DATA_FREE 清理
调小【数据库清理器】相关参数
配置—>通用配置 —> 数据库清理器
调小【数据库清理器】相关参数,缩短数据保留时间,以便尽早释放空间
设置 master 为长期分支
配置—>通用配置 —> Branches
默认情况下,名称以 “branch” 或 “release” 开头的分支将被视为长期分支,长期分支系统不会做自动清理。
将 master 设置为长期分支,那么其它分支就是短期分支,短期分支系统会做自动清理。
当然已经存在的长期分支不会自动更改为短期分支,需要先删除它,然后重新在这个分支执行 sonar 分析任务,那么它就会变为短期分支
此外,修改【清除不活跃的短分支前的保留天数】参数,改为10
使用长期分支模式后,每个 Project 分支页面如下:
短期分支会显示和长期分支有差异的 issue
清理非主分支的长期分支
修改为长期分支模式的两个月后,清理非主分支的长期分支
这里使用了 SonarQube API 进行了批量清理,清理任务花费了「三个多小时」~
相关 python 脚本可以参考:https://github.com/donhui/python-sonarqube/blob/master/sonarqube_branches_clean.py
MySQL Innodb DATA_FREE 清理
对 SonarQube 数据库进行分析:
mysql> SELECT table_schema "DB Name", Round(Sum(data_length + index_length) / 1024 / 1024, 1) "DB Size in MB" FROM information_schema.tables GROUP BY table_schema;
+--------------------------+---------------+
| DB Name | DB Size in MB |
+--------------------------+---------------+
| dingtalk_develop_members | 0.1 |
| information_schema | 0.2 |
| sonar | 90477.8 |
| sonar_statistics | 5.1 |
+--------------------------+---------------+
4 rows in set (0.30 sec)
mysql> SELECT table_name AS `Table`, round(((data_length + index_length) / 1024 / 1024), 2) `Size in MB` FROM information_schema.TABLES WHERE table_schema = 'sonar';
+--------------------------+------------+
| Table | Size in MB |
+--------------------------+------------+
| active_rule_parameters | 0.03 |
| active_rules | 0.42 |
| analysis_properties | 0.02 |
| ce_activity | 32.55 |
| ce_queue | 0.05 |
| ce_scanner_context | 619.05 |
| ce_task_characteristics | 51.19 |
| ce_task_input | 1.52 |
| default_qprofiles | 0.03 |
| duplications_index | 0.05 |
| es_queue | 0.03 |
| events | 6.20 |
| file_sources | 33973.41 |
| group_roles | 2.53 |
| groups | 0.02 |
| groups_users | 0.23 |
| internal_properties | 0.02 |
| issue_changes | 160.84 |
| issues | 372.92 |
| loaded_templates | 0.03 |
| manual_measures | 0.02 |
| metrics | 0.08 |
| notifications | 0.02 |
| org_qprofiles | 0.05 |
| organization_members | 0.05 |
| organizat