GitLab 是全球流行的源代码管理工具。在早期版本中,用户可以选择 MySQL 或 PostgreSQL,但从 12.1.0 版本开始,官方对 MySQL 的支持已完全取消。
新版 GitLab 中的许多功能都基于 PostgreSQL,这是许多使用 PostgreSQL 作为底层数据存储的产品的基准。
想象一下这样一个场景,一大群人被分成几个部分。每个部门甚至一个小团队都可能维护自己的 GitLab,这使得从组级别管理这些存储库变得很棘手。例如。
- 版本控制问题(开源和商业版本,高版本和低版本)
- 细粒度的权限控制
- 数据备份
- 基础设施利用率
具有良好可扩展性和高可用性的统一 GitLab 环境将
是最佳解决方案。但是传统的单机PostgreSQL数据库并不能满足以上需求,那么我们可以考虑在分布式数据库上运行GitLab吗?
CockroachDB和YugabyteDB是比较知名的新型开源分布式数据库,它们实现了PG协议,根据其官网的描述。
CockroachDB 支持 PostgreSQL 有线协议和大多数 PostgreSQL 语法。基于 PostgreSQL 构建的现有应用程序通常可以在不更改应用程序代码的情况下 迁移到CockroachDB 。
YugabyteDB是一个高性能、云原生的分布式 SQL 数据库,旨在支持所有 PostgreSQL 功能。
CockroachDB 说它支持大多数 PG 语法,YugabyteDB 说它支持所有 PG 特性。本系列评测文章用于比较这两个数据库对 GitLab 的支持程度,并在一定程度上反映与标准 PostgreSQL 的兼容性。
测试环境
- CockroachDB
defaultdb=# select version(); version ----------------------------------------------------------------------------------------- CockroachDB CCL v21.2.2 (x86_64-unknown-linux-gnu, built 2021/12/01 14:35:45, go1.16.6) (1 row)
- YugabyteDB
postgres=# select version(); version ------------------------------------------------------------------------------------------------------------ PostgreSQL 11.2-YB-2.9.1.0-b0 on x86_64-pc-linux-gnu, compiled by gcc (Homebrew gcc 5.5.0_4) 5.5.0, 64-bit (1 row)
- GitLab
GitLab information Version: 12.1.0-ee Revision: 1f2e6f3f6d8 Directory: /home/git/gitlab DB Adapter: PostgreSQL
使用标准 PostgreSQL 部署的 GitLab 包含以下数据库模式:
gitlab_production=# select C.relkind,count(C.relname) from pg_class C left join pg_namespace n on n.oid = C.relnamespace where n.nspname = 'public' group by C.relkind; relkind | count ---------+------- r | 249 i | 903 S | 231 (3 rows)
CockroachDB 启动流程
1. 数据库初始化
执行 GitLab 设置程序以生成所需的数据库模式。
dc@dc-virtual-machine:/home/git/gitlab$ sudo -u git -H bundle exec rake gitlab:setup RAILS_ENV=production This will create the necessary database tables and seed the database. You will lose any previous data stored in the database. Do you want to continue (yes/no)? yes
Dropped database 'gitlab'
Created database 'gitlab'
-- enable_extension("pg_trgm")
rake aborted! ActiveRecord::StatementInvalid: PG::FeatureNotSupported: ERROR: unimplemented: extension "pg_trgm" is not yet supported HINT: You have attempted to use a feature that is not yet implemented. See: https://go.crdb.dev/issue-v/51137/v21.2 : CREATE EXTENSION IF NOT EXISTS "pg_trgm"
/home/git/gitlab/config/initializers/peek.rb:18:in `async_exec_params' /home/git/gitlab/config/initializers/peek.rb:18:in `exec_params'
/home/git/gitlab/vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.3/lib/active_record/connection_adapters/postgresql_adapter.rb:611:in `block (2 levels) in exec_no_cache' ....
从上面的输出可以看出, GitLab 初始化依赖于 PostgreSQL 的 Extension 特性,但不幸的是,CockroachDB 目前不支持它,并且在数据库中没有创建对象时第一步失败。
gitlab=# select C.relkind,count(C.relname) from pg_class C left join pg_namespace n on n.oid = C.relnamespace where n.nspname = 'public' group by C.relkind;
Empty set
2. 访问 GitLab
当我们访问 GitLab 主页面时,它会返回 502 错误消息。
从日志看,是因为SQL执行报错时找不到目标表。
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: relation "geo_nodes" does not exist : SELECT a.attname, format_type(a.atttypid, a.atttypmod), pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod, c.collname, col_description(a.attrelid, a.attnum) AS comment FROM pg_attribute a LEFT JOIN pg_attrdef d ON a.attrelid = d.adrelid AND a.attnum = d.adnum LEFT JOIN pg_type t ON a.atttypid = t.oid LEFT JOIN pg_collation c ON a.attcollation = c.oid AND a.attcollation <> t.typcollation WHERE a.attrelid = '"geo_nodes"'::regclass AND a.attnum > 0 AND NOT a.attisdropped ORDER BY a.attnum
3.更新数据库版本
考虑到目前版本的 CockroachDB 不是最新版本,是不是最新版本已经支持扩展功能了?尝试将版本升级到 latest-v22.1:
defaultdb=# select version(); version
------------------------------------------------------------------------------------ CockroachDB CCL v22.1.0 (x86_64-pc-linux-gnu, built 2022/05/23 16:27:47, go1.17.6)
(1 row)
再次执行setup创建数据库,还是发现同样的问题“ ActiveRecord::StatementInvalid: PG::FeatureNotSupported: ERROR: unimplemented: extension "pg_trgm " is not yet support ”,表示新版本不支持扩展功能版本。
YugabyteDB启动流程
1. 数据库初始化
修改 GitLab 配置文件,将数据库连接切换到 YugabyteDB 并以同样的方式初始化一个新的存储库。
dc@dc-virtual-machine:/home/git/gitlab$ sudo -u git -H bundle exec rake gitlab:setup RAILS_ENV=production This will create the necessary database tables and seed the database. You will lose any previous data stored in the database. Do you want to continue (yes/no)? yes
Dropped database 'gitlab'
Created database 'gitlab'
-- enable_extension("pg_trgm") -> 2.5496s
-- enable_extension("plpgsql") -> 0.1143s
-- create_table("abuse_reports", {:id=>:serial, :force=>:cascade}) -> 0.3709s
-- create_table("appearances", {:id=>:serial, :force=>:cascade}) -> 0.3022s ... ...
-- create_table("issue_tracker_data", {:force=>:cascade}) -> 3.7627s
-- create_table("issues", {:id=>:serial, :force=>:cascade})
rake aborted! ActiveRecord::StatementInvalid: PG::InternalError: ERROR: index method "ybgin" not supported yet HINT: See https://github.com/YugaByte/yugabyte-db/issues/1337. Click '+' on the description to raise its priority : CREATE INDEX "index_issues_on_description_trigram" ON "issues" USING gin ("description" gin_trgm_ops)
/home/git/gitlab/vendor/bundle/ruby/2.6.0/gems/peek-pg-1.3.0/lib/peek/views/pg.rb:17:in `async_exec' /home/git/gitlab/vendor/bundle/ruby/2.6.0/gems/peek-pg-1.3.0/lib/peek/views/pg.rb:17:in `async_exec'
从上面的输出信息可以看出,一开始setup运行正常,可以正常创建extension和table,但是大约20分钟后,创建索引失败,因为YugabyteDB无法识别“gin”类型索引,取而代之的是“ybgin”。
查看到目前为止数据库生成的对象:
gitlab=# select C.relkind,count(C.relname) from pg_class C left join pg_namespace n on n.oid = C.relnamespace where n.nspname = 'public' group by C.relkind; relkind | count
---------+------- S | 113 i | 391 r | 117
(3 rows)
这种情况看起来比 CockroachDB 好一点,但仍然比完整的数据库模式差得多。
2. 访问 GitLab
此时GitLab主页面仍然无法访问,从日志中发现报错的原因是目标表丢失。
source=rack-timeout id=7gatOugcqB8 timeout=60000ms state=ready Started GET "/" for 10.3.74.126 at 2022-05-27 16:05:31 +0800 Processing by RootController#index as HTML Completed 500 Internal Server Error in 78ms (ActiveRecord: 58.8ms | Elasticsearch: 0.0ms)
ActiveRecord::StatementInvalid (PG::UndefinedTable: ERROR: relation "projects" does not exist LINE 8: WHERE a.attrelid = '"projects"'::regclass ^ : SELECT a.attname, format_type(a.atttypid, a.atttypmod), pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod, c.collname, col_description(a.attrelid, a.attnum) AS comment FROM pg_attribute a LEFT JOIN pg_attrdef d ON a.attrelid = d.adrelid AND a.attnum = d.adnum LEFT JOIN pg_type t ON a.atttypid = t.oid LEFT JOIN pg_collation c ON a.attcollation = c.oid AND a.attcollation <> t.typcollation WHERE a.attrelid = '"projects"'::regclass AND a.attnum > 0 AND NOT a.attisdropped ORDER BY a.attnum
):
3.更新数据库版本
同样,我们尝试将 YugabytesDB 升级到最新版本,看看是否已完成 Gin 索引兼容:
postgres=# select version(); version
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- PostgreSQL 11.2-YB-2.13.2.0-b0 on x86_64-pc-linux-gnu, compiled by clang version 12.0.1 (https://github.com/yugabyte/llvm-project.git bdb147e675d8c87cee72cc1f87c4b82855977d94), 64-bit
(1 row)
再次执行 setup 程序,过程比较顺利,大约 30 分钟后程序正常退出,没有错误。此时我们查看数据库中的对象。
gitlab=# select C.relkind,count(C.relname) from pg_class C left join pg_namespace n on n.oid = C.relnamespace where n.nspname = 'public' group by C.relkind; relkind | count
---------+------- S | 231 i | 903 r | 249
(3 rows)
您可以看到与标准 PostgreSQL 库的比较是完全一样的。打开浏览器访问GitLab主页会自动跳转到登录页面,查看日志不报错。
填写用户注册表并提交,新用户注册成功并自动跳转到GitLab主页面。
最初,GitLab 功能不受切换数据库的影响。更详细的测试将在下一期为您呈现。
测试结论
1. CockroachDB v21.2不支持Extension功能,所以GitLab无法初始化数据库,最终启动失败,但更新到最新版本v22.1后问题依旧存在。
2. YugabyteDB v2.9不支持Gin Index(Generalized倒排索引),导致创建部分表后报错,同样无法启动,但更新到最新版本v2.13后,问题是解决了,可以正常访问GitLab页面并注册用户了。
3. YugabyteDB支持PostgreSQL Extension,CockroachDB不支持。
下一步
接下来我们将尝试绕过GitLab数据库生成步骤,将一个标准的带有数据的GitLab库导入CockroachDB和YugabyteDB,选择一些常用的读写场景,然后比较它们的兼容性性能。
原文标题:Compatibility of GitLab on CockroachDB and YugabyteDB (I) — System Initialization
原文作者:he ao
原文地址:https://dzone.com/articles/compatibility-of-gitlab-on-cockroachdb-and-yugabyt