騰訊TBase是一款騰訊自研高性能HTAP數(shù)據(jù)庫(kù),提供 高性能的OLTP和OLAP能力,同時(shí)保證 可擴(kuò)展全局一致性分布式事務(wù)(ACID),為用戶提供高一致性的分布式數(shù)據(jù)庫(kù)服務(wù)和高性能的數(shù)據(jù)倉(cāng)庫(kù)服務(wù)。一方面解決了傳統(tǒng)數(shù)據(jù)庫(kù)擴(kuò)展不足、數(shù)據(jù)sharding之后數(shù)據(jù)庫(kù)事務(wù)的嚴(yán)格一致性難題、數(shù)據(jù)安全、跨地域容災(zāi)等問(wèn)題,同時(shí)具備了高性能事務(wù)處理、數(shù)據(jù)治理、混合負(fù)載支持等能力。
創(chuàng)新互聯(lián)建站-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比都安網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫(kù),直接使用。一站式都安網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋都安地區(qū)。費(fèi)用合理售后完善,十年實(shí)體公司更值得信賴。
在OLTP方面,TBase采用 MVCC+全局時(shí)鐘+2PC+SSI的方式來(lái)實(shí)現(xiàn)全局一致性分布式事務(wù),同時(shí)引入大量性能優(yōu)化的設(shè)計(jì)來(lái)減少全局事務(wù)帶來(lái)的開銷。在小規(guī)模集群上,TBase能夠提供 超過(guò)300萬(wàn)TPMTotal的事務(wù)處理吞吐量(工業(yè)界標(biāo)準(zhǔn)TPCC測(cè)試集)。
交易毫秒內(nèi)完成
TBase已經(jīng)覆蓋多個(gè)行業(yè)的標(biāo)桿用戶,其中對(duì)內(nèi)支持了微信廣告、微信支付、騰訊地圖等海量數(shù)據(jù)業(yè)務(wù),一筆交易毫秒內(nèi)即可完成, 支撐了微信支付50倍的交易增長(zhǎng)。
TBase是一個(gè)提供寫可靠性,多主節(jié)點(diǎn)數(shù)據(jù)同步的關(guān)系數(shù)據(jù)庫(kù)集群平臺(tái)。你可以將TBase配置一臺(tái)或者多臺(tái)主機(jī)上,TBase數(shù)據(jù)存儲(chǔ)在多臺(tái)物理主機(jī)上面。數(shù)據(jù)表的存儲(chǔ)有兩種方式, 分別是distributed或者replicated ,當(dāng)向TBase發(fā)送查詢 SQL時(shí),TBase會(huì)自動(dòng)向數(shù)據(jù)節(jié)點(diǎn)發(fā)出查詢語(yǔ)句并獲取最終結(jié)果。
TBase采用分布式集群架構(gòu)(如下圖),該架構(gòu)分布式為無(wú)共享(share nothing)模式,節(jié)點(diǎn)之間相應(yīng)獨(dú)立,各自處理自己的數(shù)據(jù),處理后的結(jié)果可能向上層匯總或在節(jié)點(diǎn)間流轉(zhuǎn),各處理單元之間通過(guò)網(wǎng)絡(luò)協(xié)議進(jìn)行通信,并行處理和擴(kuò)展能力更好,這也意味著只需要簡(jiǎn)單的x86服務(wù)器就可以部署TBase數(shù)據(jù)庫(kù)集群。
下面簡(jiǎn)單解讀一下TBase的三大模塊:
Coordinator:協(xié)調(diào)節(jié)點(diǎn)(簡(jiǎn)稱CN)
業(yè)務(wù)訪問(wèn)入口,負(fù)責(zé)數(shù)據(jù)的分發(fā)和查詢規(guī)劃,多個(gè)節(jié)點(diǎn)位置對(duì)等,每個(gè)節(jié)點(diǎn)都提供相同的數(shù)據(jù)庫(kù)視圖;在功能上CN上只存儲(chǔ)系統(tǒng)的全局元數(shù)據(jù),并不存儲(chǔ)實(shí)際的業(yè)務(wù)數(shù)據(jù)。
Datanode:數(shù)據(jù)節(jié)點(diǎn)(簡(jiǎn)稱DN)
每個(gè)節(jié)點(diǎn)還存儲(chǔ)業(yè)務(wù)數(shù)據(jù)的分片在功能上,DN節(jié)點(diǎn)負(fù)責(zé)完成執(zhí)行協(xié)調(diào)節(jié)點(diǎn)分發(fā)的執(zhí)行請(qǐng)求。
GTM:全局事務(wù)管理器(Global Transaction Manager)
負(fù)責(zé)管理集群事務(wù)信息,同時(shí)管理集群的全局對(duì)象,比如序列等。
接下來(lái),讓我們來(lái)看看如何從源碼開始,完成到TBase集群環(huán)境的搭建。
注意:所有需要安裝TBase集群的機(jī)器上都需要?jiǎng)?chuàng)建
mkdir /data useradd -d /data/tbase tbase
git clone https://github.com/Tencent/TBase
cd ${SOURCECODE_PATH} rm -rf ${INSTALL_PATH}/tbase_bin_v2.0 chmod +x configure* ./configure --prefix=${INSTALL_PATH}/tbase_bin_v2.0 --enable-user-switch --with-openssl --with-ossp-uuid CFLAGS=-g make clean make -sj make install chmod +x contrib/pgxc_ctl/make_signature cd contrib make -sj make install
本文的使用環(huán)境中,上述兩個(gè)參數(shù)如下
${SOURCECODE_PATH}=/data/tbase/TBase-master
${INSTALL_PATH}=/data/tbase/install
下面以兩臺(tái)服務(wù)器上搭建1GTM主,1GTM備,2CN主(CN主之間對(duì)等,因此無(wú)需備CN),2DN主,2DN備的集群,該集群為具備容災(zāi)能力的最小配置
機(jī)器1:10.215.147.158 機(jī)器2:10.240.138.159
集群規(guī)劃如下:
參考Linux ssh互信配置
集群所有機(jī)器都需要配置
[tbase@TENCENT64 ~]$ vim ~/.bashrc export TBASE_HOME=/data/tbase/install/tbase_bin_v2.0 export PATH=$TBASE_HOME/bin:$PATH export LD_LIBRARY_PATH=$TBASE_HOME/lib:${LD_LIBRARY_PATH}
以上,已經(jīng)配置好了所需要基礎(chǔ)環(huán)境,可以進(jìn)入到集群初始化階段,為了方便用戶,TBase提供了專用的配置和操作工具: pgxc_ctl來(lái)協(xié)助用戶快速搭建并管理集群,首先需要將前文所述的節(jié)點(diǎn)的ip,端口,目錄寫入到配置文件 pgxc_ctl.conf 中。
[tbase@TENCENT64 ~]$ mkdir /data/tbase/pgxc_ctl [tbase@TENCENT64 ~]$ cd /data/tbase/pgxc_ctl [tbase@TENCENT64 ~/pgxc_ctl]$ vim pgxc_ctl.conf
如下,是結(jié)合上文描述的IP,端口,數(shù)據(jù)庫(kù)目錄,二進(jìn)制目錄等規(guī)劃來(lái)寫的pgxc_ctl.conf文件。具體實(shí)踐中只需按照自己的實(shí)際情況配置好即可.
#!/bin/bash pgxcInstallDir=/data/tbase/install/tbase_bin_v2.0 pgxcOwner=tbase defaultDatabase=postgres pgxcUser=$pgxcOwner tmpDir=/tmp localTmpDir=$tmpDir configBackup=n configBackupHost=pgxc-linker configBackupDir=$HOME/pgxc configBackupFile=pgxc_ctl.bak #---- GTM ---------- gtmName=gtm gtmMasterServer=10.215.147.158 gtmMasterPort=50001 gtmMasterDir=/data/tbase/data/gtm gtmExtraConfig=none gtmMasterSpecificExtraConfig=none gtmSlave=y gtmSlaveServer=10.240.138.159 gtmSlavePort=50001 gtmSlaveDir=/data/tbase/data/gtm gtmSlaveSpecificExtraConfig=none #---- Coordinators ------- coordMasterDir=/data/tbase/data/coord coordMasterDir=/data/tbase/data/coord coordArchLogDir=/data/tbase/data/coord_archlog coordNames=(cn001 cn002 ) coordPorts=(30004 30004 ) poolerPorts=(31110 31110 ) coordPgHbaEntries=(0.0.0.0/0) coordMasterServers=(10.215.147.158 10.240.138.159) coordMasterDirs=($coordMasterDir $coordMasterDir) coordMaxWALsernder=2 coordMaxWALSenders=($coordMaxWALsernder $coordMaxWALsernder ) coordSlave=n coordSlaveSync=n coordArchLogDirs=($coordArchLogDir $coordArchLogDir) coordExtraConfig=coordExtraConfig cat > $coordExtraConfig <$coordExtraPgHba < $datanodeExtraConfig < $datanodeExtraPgHba <
在一個(gè)節(jié)點(diǎn)配置好配置文件后,需要預(yù)先將二進(jìn)制包部署到所有節(jié)點(diǎn)所在的機(jī)器上,這個(gè)可以使用pgxc_ctl工具,執(zhí)行 deploy all命令來(lái)完成。
[tbase@TENCENT64 ~/pgxc_ctl]$ pgxc_ctl /usr/bin/bash Installing pgxc_ctl_bash script as /data/tbase/pgxc_ctl/pgxc_ctl_bash. Installing pgxc_ctl_bash script as /data/tbase/pgxc_ctl/pgxc_ctl_bash. Reading configuration using /data/tbase/pgxc_ctl/pgxc_ctl_bash --home /data/tbase/pgxc_ctl --configuration /data/tbase/pgxc_ctl/pgxc_ctl.conf Finished reading configuration. ******** PGXC_CTL START *************** Current directory: /data/tbase/pgxc_ctl PGXC deploy all Deploying Postgres-XL components to all the target servers. Prepare tarball to deploy ... Deploying to the server 10.215.147.158. Deploying to the server 10.240.138.159. Deployment done. 登錄到所有節(jié)點(diǎn),check二進(jìn)制包是否分發(fā)OK [tbase@TENCENT64 ~/install]$ ls /data/tbase/install/tbase_bin_v2.0 bin include lib share
[tbase@TENCENT64 ~]$ pgxc_ctl /usr/bin/bash Installing pgxc_ctl_bash script as /data/tbase/pgxc_ctl/pgxc_ctl_bash. Installing pgxc_ctl_bash script as /data/tbase/pgxc_ctl/pgxc_ctl_bash. Reading configuration using /data/tbase/pgxc_ctl/pgxc_ctl_bash --home /data/tbase/pgxc_ctl --configuration /data/tbase/pgxc_ctl/pgxc_ctl.conf Finished reading configuration. ******** PGXC_CTL START *************** Current directory: /data/tbase/pgxc_ctl PGXC init all Initialize GTM master .... .... Initialize datanode slave dn001 Initialize datanode slave dn002 mkdir: cannot create directory '/data1/tbase': Permission denied chmod: cannot access '/data1/tbase/data/dn001': No such file or directory pg_ctl: directory "/data1/tbase/data/dn001" does not exist pg_basebackup: could not create directory "/data1/tbase": Permission denied
一般init集群出錯(cuò),終端會(huì)打印出錯(cuò)誤日志,通過(guò)查看錯(cuò)誤原因,更改配置即可,或者可以通過(guò)/data/tbase/pgxc_ctl/pgxc_log路徑下的錯(cuò)誤日志查看錯(cuò)誤,排查配置文件的錯(cuò)誤
[tbase@TENCENT64 ~]$ ll ~/pgxc_ctl/pgxc_log/ total 184 -rw-rw-r-- 1 tbase tbase 81123 Nov 13 17:22 14105_pgxc_ctl.log -rw-rw-r-- 1 tbase tbase 2861 Nov 13 17:58 15762_pgxc_ctl.log -rw-rw-r-- 1 tbase tbase 14823 Nov 14 07:59 16671_pgxc_ctl.log -rw-rw-r-- 1 tbase tbase 2721 Nov 13 16:52 18891_pgxc_ctl.log -rw-rw-r-- 1 tbase tbase 1409 Nov 13 16:20 22603_pgxc_ctl.log -rw-rw-r-- 1 tbase tbase 60043 Nov 13 16:33 28932_pgxc_ctl.log -rw-rw-r-- 1 tbase tbase 15671 Nov 14 07:57 6849_pgxc_ctl.log
通過(guò)運(yùn)行 pgxc_ctl 工具,執(zhí)行 clean all命令刪除已經(jīng)初始化的文件,修改pgxc_ctl.conf文件,重新執(zhí)行 init all命令重新發(fā)起初始化。
[tbase@TENCENT64 ~]$ pgxc_ctl /usr/bin/bash Installing pgxc_ctl_bash script as /data/tbase/pgxc_ctl/pgxc_ctl_bash. Installing pgxc_ctl_bash script as /data/tbase/pgxc_ctl/pgxc_ctl_bash. Reading configuration using /data/tbase/pgxc_ctl/pgxc_ctl_bash --home /data/tbase/pgxc_ctl --configuration /data/tbase/pgxc_ctl/pgxc_ctl.conf Finished reading configuration. ******** PGXC_CTL START *************** Current directory: /data/tbase/pgxc_ctl PGXC clean all [tbase@TENCENT64 ~]$ pgxc_ctl /usr/bin/bash Installing pgxc_ctl_bash script as /data/tbase/pgxc_ctl/pgxc_ctl_bash. Installing pgxc_ctl_bash script as /data/tbase/pgxc_ctl/pgxc_ctl_bash. Reading configuration using /data/tbase/pgxc_ctl/pgxc_ctl_bash --home /data/tbase/pgxc_ctl --configuration /data/tbase/pgxc_ctl/pgxc_ctl.conf Finished reading configuration. ******** PGXC_CTL START *************** Current directory: /data/tbase/pgxc_ctl PGXC init all Initialize GTM master EXECUTE DIRECT ON (dn002) 'ALTER NODE dn002 WITH (TYPE=''datanode'', HOST=''10.240.138.159'', PORT=40004, PREFERRED)'; EXECUTE DIRECT EXECUTE DIRECT ON (dn002) 'SELECT pgxc_pool_reload()'; pgxc_pool_reload ------------------ t (1 row) Done.
當(dāng)發(fā)現(xiàn)上面的輸出時(shí),集群已經(jīng)OK,另外也可以通過(guò)pgxc_ctl工具的 monitor all命令來(lái)查看集群狀態(tài)
[tbase@TENCENT64 ~/pgxc_ctl]$ pgxc_ctl /usr/bin/bash Installing pgxc_ctl_bash script as /data/tbase/pgxc_ctl/pgxc_ctl_bash. Installing pgxc_ctl_bash script as /data/tbase/pgxc_ctl/pgxc_ctl_bash. Reading configuration using /data/tbase/pgxc_ctl/pgxc_ctl_bash --home /data/tbase/pgxc_ctl --configuration /data/tbase/pgxc_ctl/pgxc_ctl.conf Finished reading configuration. ******** PGXC_CTL START *************** Current directory: /data/tbase/pgxc_ctl PGXC monitor all Running: gtm master Not running: gtm slave Running: coordinator master cn001 Running: coordinator master cn002 Running: datanode master dn001 Running: datanode slave dn001 Running: datanode master dn002 Not running: datanode slave dn002
一般的如果配置的不是強(qiáng)同步模式,gtm salve,dn slave的故障不會(huì)影響訪問(wèn)。
訪問(wèn)TBase集群和訪問(wèn)單機(jī)的PostgreSQL基本上無(wú)差別,我們可以通過(guò)任意一個(gè)CN訪問(wèn)數(shù)據(jù)庫(kù)集群:例如通過(guò)連接CN節(jié)點(diǎn)select pgxc_node表即可查看集群的拓?fù)浣Y(jié)構(gòu)(當(dāng)前的配置下備機(jī)不會(huì)展示在pgxc_node中),在Linux命令行下通過(guò)psql訪問(wèn)的具體示例如下
[tbase@TENCENT64 ~/pgxc_ctl]$ psql -h 10.215.147.158 -p 30004 -d postgres -U tbase psql (PostgreSQL 10.0 TBase V2) Type "help" for help. postgres=# \d Did not find any relations. postgres=# select * from pgxc_node; node_name | node_type | node_port | node_host | nodeis_primary | nodeis_preferred | node_id | node_cluster_name -----------+-----------+-----------+----------------+----------------+------------------+------------+------------------- gtm | G | 50001 | 10.215.147.158 | t | f | 428125959 | tbase_cluster cn001 | C | 30004 | 10.215.147.158 | f | f | -264077367 | tbase_cluster cn002 | C | 30004 | 10.240.138.159 | f | f | -674870440 | tbase_cluster dn001 | D | 40004 | 10.215.147.158 | t | t | 2142761564 | tbase_cluster dn002 | D | 40004 | 10.240.138.159 | f | f | -17499968 | tbase_cluster (5 rows)
TBase使用datanode group來(lái)增加節(jié)點(diǎn)的管理靈活度,要求有一個(gè)default group才能使用,因此需要預(yù)先創(chuàng)建;一般情況下,會(huì)將節(jié)點(diǎn)的所有datanode節(jié)點(diǎn)加入到default group里 另外一方面,TBase的數(shù)據(jù)分布為了增加靈活度,加了中間邏輯層來(lái)維護(hù)數(shù)據(jù)記錄到物理節(jié)點(diǎn)的映射,我們叫sharding,所以需要預(yù)先創(chuàng)建sharding,命令如下:
postgres=# create default node group default_group with (dn001,dn002); CREATE NODE GROUP postgres=# create sharding group to group default_group; CREATE SHARDING GROUP
至此,就可以跟使用單機(jī)數(shù)據(jù)庫(kù)一樣來(lái)訪問(wèn)數(shù)據(jù)庫(kù)集群了
postgres=# create database test; CREATE DATABASE postgres=# create user test with password 'test'; CREATE ROLE postgres=# alter database test owner to test; ALTER DATABASE postgres=# \c test test You are now connected to database "test" as user "test". test=> create table foo(id bigint, str text) distribute by shard(id); CREATE TABLE test=> insert into foo values(1, 'tencent'), (2, 'shenzhen'); COPY 2 test=> select * from foo; id | str ----+---------- 1 | tencent 2 | shenzhen (2 rows)
通過(guò)pgxc_ctl工具的 stop all命令來(lái)停止集群,stop all 后面可以加上參數(shù) -m fast或者是 -m immediate來(lái)決定如何停止各個(gè)節(jié)點(diǎn)。
PGXC stop all -m fast Stopping all the coordinator masters. Stopping coordinator master cn001. Stopping coordinator master cn002. Done. Stopping all the datanode slaves. Stopping datanode slave dn001. Stopping datanode slave dn002. pg_ctl: PID file "/data/tbase/data/dn002/postmaster.pid" does not exist Is server running? Stopping all the datanode masters. Stopping datanode master dn001. Stopping datanode master dn002. Done. Stop GTM slave waiting for server to shut down..... done server stopped Stop GTM master waiting for server to shut down.... done server stopped PGXC monitor all Not running: gtm master Not running: gtm slave Not running: coordinator master cn001 Not running: coordinator master cn002 Not running: datanode master dn001 Not running: datanode slave dn001 Not running: datanode master dn002 Not running: datanode slave dn002
通過(guò)pgxc_ctl工具的 start all命令來(lái)啟動(dòng)集群
[tbase@TENCENT64 ~]$ pgxc_ctl /usr/bin/bash Installing pgxc_ctl_bash script as /data/tbase/pgxc_ctl/pgxc_ctl_bash. Installing pgxc_ctl_bash script as /data/tbase/pgxc_ctl/pgxc_ctl_bash. Reading configuration using /data/tbase/pgxc_ctl/pgxc_ctl_bash --home /data/tbase/pgxc_ctl --configuration /data/tbase/pgxc_ctl/pgxc_ctl.conf Finished reading configuration. ******** PGXC_CTL START *************** Current directory: /data/tbase/pgxc_ctl PGXC start all
本文檔只是給用戶一個(gè)簡(jiǎn)單的指引,演示如何從源碼開始,一步一步搭建一個(gè)完整的TBase集群,后續(xù)會(huì)有更多的文章來(lái)介紹TBase的特性使用,優(yōu)化,問(wèn)題定位等內(nèi)容。
騰訊TBase GitHub 開源地址,請(qǐng)搜索關(guān)注“騰訊云數(shù)據(jù)庫(kù)”官方微信,回復(fù)“開源”即可獲取。
支撐微信支付的數(shù)據(jù)庫(kù)如何提供超300萬(wàn)TPCC事務(wù)處理能力?
最佳實(shí)踐 | 騰訊HTAP數(shù)據(jù)庫(kù)TBase助力某省核心IT架構(gòu)升級(jí)