这样玩转 PolarDB 开源社区
1、搭建 PolarDB 开发环境
参考:
https://apsaradb.github.io/PolarDB-for-PostgreSQL/zh/development/dev-on-docker.html https://apsaradb.github.io/PolarDB-for-PostgreSQL/zh/development/customize-dev-env.html
在继续阅读之前, 你可以先阅读如下文章: 《PolarDB 100 问 | 如何构建PolarDB Docker镜像 OR 本地编译PolarDB?》
1、PolarDB开源社区提供了多种环境的Docker镜像作为开发环境供开发者选择.
支持的 CPU 架构包含:
linux/amd64(x86_64)
linux/arm64
支持的 Linux 发行版包含:
CentOS 7
Anolis 8
Rocky 8
Rocky 9
Debian 11
Debian 12
Ubuntu 20.04
Ubuntu 22.04
Ubuntu 24.04
通过如下方式即可拉取相应发行版的镜像:
docker pull registry.cn-hangzhou.aliyuncs.com/polardb_pg/polardb_pg_devel:centos7
docker pull registry.cn-hangzhou.aliyuncs.com/polardb_pg/polardb_pg_devel:anolis8
docker pull registry.cn-hangzhou.aliyuncs.com/polardb_pg/polardb_pg_devel:rocky8
docker pull registry.cn-hangzhou.aliyuncs.com/polardb_pg/polardb_pg_devel:rocky9
docker pull registry.cn-hangzhou.aliyuncs.com/polardb_pg/polardb_pg_devel:debian11
docker pull registry.cn-hangzhou.aliyuncs.com/polardb_pg/polardb_pg_devel:debian12
docker pull registry.cn-hangzhou.aliyuncs.com/polardb_pg/polardb_pg_devel:ubuntu20.04
docker pull registry.cn-hangzhou.aliyuncs.com/polardb_pg/polardb_pg_devel:ubuntu22.04
docker pull registry.cn-hangzhou.aliyuncs.com/polardb_pg/polardb_pg_devel:ubuntu24.04
另外,也提供了构建上述开发镜像的 Dockerfile,您可以根据自己的需要在 Dockerfile 中添加更多依赖,然后构建自己的开发镜像。
2、搭建PolarDB开发环境, 在开发环境中通过源码编译安装PolarDB
拉取一个你熟悉的PolarDB开发环境Docker镜像, 例如
docker pull registry.cn-hangzhou.aliyuncs.com/polardb_pg/polardb_pg_devel:ubuntu22.04
创建并运行容器
docker run -d -it -P --shm-size=1g --cap-add=SYS_PTRACE --cap-add SYS_ADMIN --privileged=true --name polardb_pg_devel registry.cn-hangzhou.aliyuncs.com/polardb_pg/polardb_pg_devel:ubuntu22.04 bash
进入容器后,克隆PolarDB源码(根据需要选择对应的分支),编译部署 PolarDB-PG 实例。
# 进入容器
docker exec -ti polardb_pg_devel bash cd /tmp
# 例如这里拉取 POLARDB_11_STABLE 分支; PS: 截止2024.9.24 PolarDB开源的最新分支为: POLARDB_15_STABLE
git clone -c core.symlinks=true --depth 1 -b POLARDB_11_STABLE https://github.com/ApsaraDB/PolarDB-for-PostgreSQL
cd /tmp/PolarDB-for-PostgreSQL
./polardb_build.sh --without-fbl --debug=off
# 验证PolarDB-PG
psql -c 'SELECT version();'
version
--------------------------------
PostgreSQL 11.9 (POLARDB 11.9)
(1 row)
# 在容器内关闭、启动PolarDB数据库:
pg_ctl stop -m fast -D ~/tmp_master_dir_polardb_pg_1100_bld
pg_ctl start -D ~/tmp_master_dir_polardb_pg_1100_bld
# 查看PolarDB的编译选项
pg_config
BINDIR = /home/postgres/tmp_basedir_polardb_pg_1100_bld/bin
DOCDIR = /home/postgres/tmp_basedir_polardb_pg_1100_bld/share/doc
HTMLDIR = /home/postgres/tmp_basedir_polardb_pg_1100_bld/share/doc
INCLUDEDIR = /home/postgres/tmp_basedir_polardb_pg_1100_bld/include
PKGINCLUDEDIR = /home/postgres/tmp_basedir_polardb_pg_1100_bld/include
INCLUDEDIR-SERVER = /home/postgres/tmp_basedir_polardb_pg_1100_bld/include/server
LIBDIR = /home/postgres/tmp_basedir_polardb_pg_1100_bld/lib
PKGLIBDIR = /home/postgres/tmp_basedir_polardb_pg_1100_bld/lib
LOCALEDIR = /home/postgres/tmp_basedir_polardb_pg_1100_bld/share/locale
MANDIR = /home/postgres/tmp_basedir_polardb_pg_1100_bld/share/man
SHAREDIR = /home/postgres/tmp_basedir_polardb_pg_1100_bld/share
SYSCONFDIR = /home/postgres/tmp_basedir_polardb_pg_1100_bld/etc
PGXS = /home/postgres/tmp_basedir_polardb_pg_1100_bld/lib/pgxs/src/makefiles/pgxs.mk
CONFIGURE = '--prefix=/home/postgres/tmp_basedir_polardb_pg_1100_bld''--with-pgport=5432''--with-openssl''--with-libxml''--with-perl''--with-python''--with-tcl''--with-pam''--with-gssapi''--enable-nls''--with-libxslt''--with-ldap''--with-uuid=e2fs''--with-icu''--with-llvm''CFLAGS= -g -pipe -Wall -grecord-gcc-switches -I/usr/include/et -O3''LDFLAGS=-Wl,-rpath,'''/../lib'''''CXXFLAGS=-g -pipe -Wall -grecord-gcc-switches -I/usr/include/et -O3'
CC = gcc
CPPFLAGS = -D_GNU_SOURCE -I/usr/include/libxml2
CFLAGS = -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -Wno-format-truncation -Wno-stringop-truncation -g -pipe -Wall -grecord-gcc-switches -I/usr/include/et -O3
CFLAGS_SL = -fPIC
LDFLAGS = -L ../../src/backend/polar_dma/libconsensus/polar_wrapper/lib -Wl,-rpath,'/../lib' -L/usr/lib/llvm-15/lib -Wl,--as-needed -Wl,-rpath,'/home/postgres/tmp_basedir_polardb_pg_1100_bld/lib',--enable-new-dtags
LDFLAGS_EX =
LDFLAGS_SL =
LIBS = -lpgcommon -lpgport -lxslt -lxml2 -lpam -lssl -lcrypto -lgssapi_krb5 -lz -lreadline -lcrypt -lm
VERSION = PostgreSQL 11.9
PX_VERSION_STR = PolarDB PX version 1.1
3、polardb_build.sh 构建选项说明
如无定制的需求,则可以按照下面给出的选项编译部署不同形态的 PolarDB-PG 集群并进行测试。
polardb_build.sh --help This script is to be used to compile PG core source code (PG engine code without files within contrib and external)
It can be called with following options:
--basedir=<temp dir for PG installation>, specifies which dir to install PG to, note this dir would be cleaned up before being used
--datadir=<temp dir for databases>], specifies which dir to store database cluster, note this dir would be cleaned up before being used
--conf=<file path for postgresql.conf>, specifies the configure file to use
--user=<user to start PG>, specifies which user to run PG as
--port=<port to run PG on>, specifies which port to run PG on
--debug=[on|off], specifies whether to compile PG with debug mode (affecting gcc flags)
-c,--coverage, specifies whether to build PG with coverage option
--nc,--nocompile, prevents re-compilation, re-installation, and re-initialization
-t,-r,--regress, runs regression test after compilation and installation.
-m --minimal compile with minimal extention set
--withrep init the database with a hot standby replica
--withstandby init the database with a hot standby replica
--pg_bld_rep_port=<port to run PG rep on>, specifies which port to run PG replica on
--pg_bld_standby_port=<port to run PG standby on>, specifies which port to run PG standby on
--repdir=<temp dir for databases>], specifies which dir to store replica data, note this dir would be cleaned up before being used
--storage=localfs, specify storage type
-e,--extension, run extension test
--with-tde, TDE enable
--with-dma, DMA enable
--with-pfsd, PFSD enable
--fault-injector, faultinjector enable
--without-fbl, run without flashback log
--extra-conf, add an extra conf file
Please lookup the following secion to find the default values for above options.
Typical command patterns to kick off this script:
1) To just cleanup, re-compile, re-install and get PG restart:
polardb_build.sh
2) To run all steps included 1), as well as run the ALL regression test cases:
polardb_build.sh -t
3) To cleanup and re-compile with code coverage option:
polardb_build.sh -c
4) To run the tests besides 3).
polardb_build.sh -c -t
5) To run with specific port, user, and/or configuration file
polardb_build.sh --port=5501 --user=pg001 --conf=/root/data/postgresql.conf
6) To run on local pfs
polardb_build.sh --storage=localfs
7) To run with a replica (it also works with --storage=localfs)
polardb_build.sh --withrep
8) To run with a standby (it also works with --storage=localfs)
polardb_build.sh --withstandby
9) To run all the tests (make check)(include src/test,src/pl,src/interfaces/ecpg,contrib,external)
polardb_build.sh -r-check-all
10) To run all the tests (make installcheck)(include src/test,src/pl,src/interfaces/ecpg,contrib,external)
polardb_build.sh -r-installcheck-all
4、构建一写多读的PolarDB集群, 可以测试ePQ MPP优化器功能.
在一个新的容器中测试
# 创新容器, 名为polardb_pg_devel_epq
docker run -d -it -P --shm-size=1g --cap-add=SYS_PTRACE --cap-add SYS_ADMIN --privileged=true --name polardb_pg_devel_epq registry.cn-hangzhou.aliyuncs.com/polardb_pg/polardb_pg_devel:ubuntu22.04 bash# 进入容器
docker exec -ti polardb_pg_devel_epq bash
# 下载PolarDB代码
cd /tmp
git clone -c core.symlinks=true --depth 1 -b POLARDB_11_STABLE https://github.com/ApsaraDB/PolarDB-for-PostgreSQL
cd /tmp/PolarDB-for-PostgreSQL
# 编译PolarDB, 并初始化一写多读的PolarDB集群
./polardb_build.sh --without-fbl --debug=off --withrep --initpx --storage=localfs
# 检查集群状态
psql
psql (11.9)
Type "help"forhelp.
postgres=# select * from pg_stat_replication ;
pid | usesysid | usename | application_name | client_addr | client_hostname | client_port | backend_start | backend_xmin | state | sent_lsn | write_lsn | flush_lsn | replay_lsn
| write_lag | flush_lag | replay_lag | sync_priority | sync_state
-------+----------+----------+------------------+-------------+-----------------+-------------+-------------------------------+--------------+-----------+-----------+-----------+-----------+------------
+-----------+-----------+------------+---------------+------------
19713 | 10 | postgres | replica2 | 127.0.0.1 | | 45658 | 2024-09-25 10:16:17.738143+08 | | streaming | 0/174FE60 | 0/174FE60 | 0/174FE60 | 0/174FE60
| | | | 0 | async
19334 | 10 | postgres | replica1 | 127.0.0.1 | | 59062 | 2024-09-25 10:16:15.719794+08 | | streaming | 0/174FE60 | 0/174FE60 | 0/174FE60 | 0/174FE60
| | | | 1 | sync
(2 rows)
测试epq, 参考如下文章“PolarDB/PostgreSQL TPCH测试”章节:
《开源PolarDB|PostgreSQL 应用开发者&DBA 公开课 - 5.7 PolarDB开源版本必学特性 - PolarDB 应用实践实验》
常见错误排查
1、如果在编译并启动PolarDB集群时报内存不足的错误, 可能是docker desktop的内存资源限制太少了, 可以修改一下(修改配置后需要重启docker daemon. 例如Linux: systemctl restart docker). 例如 16G内存的Mac我配置了limit 8G内存和4G swap. 配置请参考docker手册:
https://docs.docker.com/desktop/settings/
图形界面配置路径: Settings-Resources-Advanced-Memory/Swap
或者直接修改docker settings.json file at:
Mac: ~/Library/"Group Containers"/group.com.docker/settings.json
Windows: C:\Users\[USERNAME]\AppData\Roaming\Docker\settings.json
Linux: ~/.docker/desktop/settings.json
2、如果你是在宿主机clone的代码, 然后拷贝到容器内, 编译时可能遇到这样的错误:
In file included from ../../src/include/polar_dma/polar_dma.h:33,
from relpath.c:26:
../../src/include/polar_dma/polar_consensus_stats.h:1:1: error: expected identifier or ‘(’ before ‘.’ token
1 | ../../backend/polar_dma/libconsensus/polar_wrapper/include/polar_consensus_stats.h
| ^
In file included from relpath.c:26:
../../src/include/polar_dma/polar_dma.h:41:3: warning: data definition has no type or storage class
41 | } ConsensusState;
| ^~~~~~~~~~~~~~
../../src/include/polar_dma/polar_dma.h:41:3: warning: type defaults to ‘int’ in declaration of ‘ConsensusState’ [-Wimplicit-int]
../../src/include/polar_dma/polar_dma.h:200:36: error: unknown type name ‘ConsensusMemberInfo’; did you mean ‘ConsensusProcInfo’?
200 | extern void ConsensusGetMemberInfo(ConsensusMemberInfo *member_info);
| ^~~~~~~~~~~~~~~~~~~
| ConsensusProcInfo
../../src/include/polar_dma/polar_dma.h:201:36: error: unknown type name ‘ConsensusClusterInfo’; did you mean ‘ConsensusProcInfo’?
201 | extern int ConsensusGetClusterInfo(ConsensusClusterInfo *cluster_info);
| ^~~~~~~~~~~~~~~~~~~~
| ConsensusProcInfo
../../src/include/polar_dma/polar_dma.h:202:35: error: unknown type name ‘ConsensusStatsInfo’; did you mean ‘ConsensusStats’?
202 | extern void ConsensusGetStatsInfo(ConsensusStatsInfo *stats_info);
| ^~~~~~~~~~~~~~~~~~
| ConsensusStats
make[2]: *** [<builtin>: relpath.o] Error 1
make[2]: *** Waiting for unfinished jobs....
make[1]: *** [Makefile:42: all-common-recurse] Error 2
make: *** [GNUmakefile:11: all-src-recurse] Error 2
在宿主机克隆代码时加上core.symlinks=true参数可以解决.
git clone -c core.symlinks=true --depth 1 -b POLARDB_11_STABLE https://github.com/ApsaraDB/PolarDB-for-PostgreSQL
2、如何玩转 PolarDB 开源社区?
PolarDB 开源社区是由PolarDB 数据库企业用户、PolarDB 生态伙伴、PolarDB 产品研发、开发者、学生、高校老师、合作协会、合作社区和媒体、资本等组成的一个大的社区组织. PolarDB 开源社区由理事会领导, 并代表社区群体共同利益. 通过社区运营, 将社区的个体紧密连接在一起, 在连接和碰撞中产生创新与价值. 所有参与开源社区的个人、企业或机构都能在开源社区贡献自己的力量, 并从开源社区获得自己所需要的价值.
个人参与社区可以总结为几个阶段, 分别获得技能、圈子连接、战绩、影响力, 详细参考: 《2024在华科分享的PPT: PostgreSQL深耕中国的技术洞察》
生态伙伴通过参与社区, 提升自身品牌影响力、获得潜在商机.
用户通过参与社区, 分享业务最佳实践、问题痛点, 获得行业影响力, 了解前沿技术和解决方案.
PolarDB 开源社区的活动和内容产出围绕社区群体价值展开. 下面列举一些例子, 如何玩转PolarDB 开源社区?
技能
开源公开课, PolarDB开源社区会不定期邀请“PolarDB的资深技术专家、高校数据库专业领域老师、PolarDB MVP(数据库行业领袖)”共同打造高质量的开源数据库公开课, 帮助爱好者由浅入深体系化的了解PolarDB. 开源训练营, 与公开课不同, 开源训练营通常是一周(每天45分钟)左右的课程, 训练营通常针对一个比较聚焦的内容进行讲解, 把场景讲深讲透. 开源电子书, 一般由训练营、公开课等内容的讲师进行编制, 便于喜欢文字阅读学习的小伙伴使用.
视频链接:
https://space.bilibili.com/310191812/lists https://space.bilibili.com/2070322174/lists
技能、圈子连接
高校PolarDB 开源工作室, 通过在各大高校开设PolarDB 开源工作室, 定期的公开课, 培训, 实验, 校企交流等活动, 帮助对数据库行业或技术感兴趣的同学提升技能, 获得认证, 同时尽早和企业、行业大咖建立连接, 提升就业率.
战绩
开源人才认证, 针对PolarDB的开源人才认证, 帮助同学验证对PolarDB的掌握程度, 通过认证说明你掌握了该认证级别的PolarDB体系知识. 编程大赛, 例如编程之夏, 开源之夏等编程大赛, PolarDB会作为数据库子赛道参与进去, 感兴趣的学生可以关注每年举办的大赛. 参与编程比赛可以提升自己的能力、就业竞争力、考研加分(取决于目标学校的政策)等. 2024年全国大学生计算机系统能力大赛-PolarDB数据库创新设计赛(天池杯) https://tianchi.aliyun.com/competition/entrance/532261
技能、圈子连接、影响力
企业行, 通过组织大型企业的深入交流, 了解行业需求和痛点, 建立开源生态企业、数据库专家、业务专家的深度交流机会, 共同解决企业或行业难题. 沙龙, 通过组织线上/线下的活动沙龙, 搭台子建立用户、生态、数据库产品、资本等各方的资源连接, 充分沟通, 碰撞出商业机会, 技术创新点, 解决方案等. 同时通过优质的活动内容、热点信息等帮助社区/媒体类的伙伴提升流量. PolarDB 开发者峰会, 沙龙通常以省会城市为单位开展, 辐射周边企业, 而PolarDB 开发者峰会是年度全国大会, 上千人规模. 新产品发布、前沿发展趋势、用户分享、伙伴分享、展厅、现场体验等, 融合了技术和商业会议的元素. 博客, 钉钉、微信等实时交流社群, 建立实时交流的渠道, 有技术专家在里面可以回答各类数据库问题, 碰撞出好的行业解决方案. 帮助别人的同时提升自己或企业的影响力, 对于生态伙伴则能在其中发现商机. PolarDB AI助理, 使用PolarDB 知识库、各类数据库及底层高质量语料对通用大模型进行微调, 提升AI机器人在数据库领域的专业性, 不仅能回答使用数据库的问题, 也能回答内核开发的问题.
战绩、影响力
PolarDB SIG/实习平台, PolarDB SIG是数据库的兴趣小组, 例如有安全小组、AI小组(例如pg4ml for PolarDB)、优化器、存储、FDW、高可用、ARM一体机等等方向的兴趣小组. 学生通过参与PolarDB SIG开源兴趣小组, 通过贡献文档、解决方案、代码、领实习任务等, 获得PolarDB开源贡献, 同时由机会获得PolarDB开源项目的实习证书.
其他参考:
学习类的内容可以通过 PolarDB 学习图谱链接 找到. 活动信息会在 PolarDB开源官网 进行发布. 《手把手教你如何参与开源项目的协作、贡献代码: 以PolarDB开源项目为例》 《这么多人合作的PostgreSQL开源项目, 如何保持统一的代码风格? 原来有这个绝招》 加入PolarDB-PG钉钉群 进行交流: pic