您好,登錄后才能下訂單哦!
騰訊TBase是一款騰訊自研高性能HTAP數據庫,提供 高性能的OLTP和OLAP能力,同時保證 可擴展全局一致性分布式事務(ACID),為用戶提供高一致性的分布式數據庫服務和高性能的數據倉庫服務。一方面解決了傳統數據庫擴展不足、數據sharding之后數據庫事務的嚴格一致性難題、數據安全、跨地域容災等問題,同時具備了高性能事務處理、數據治理、混合負載支持等能力。
在OLTP方面,TBase采用 MVCC+全局時鐘+2PC+SSI的方式來實現全局一致性分布式事務,同時引入大量性能優化的設計來減少全局事務帶來的開銷。在小規模集群上,TBase能夠提供 超過300萬TPMTotal的事務處理吞吐量(工業界標準TPCC測試集)。
交易毫秒內完成
TBase已經覆蓋多個行業的標桿用戶,其中對內支持了微信廣告、微信支付、騰訊地圖等海量數據業務,一筆交易毫秒內即可完成, 支撐了微信支付50倍的交易增長。
TBase是一個提供寫可靠性,多主節點數據同步的關系數據庫集群平臺。你可以將TBase配置一臺或者多臺主機上,TBase數據存儲在多臺物理主機上面。數據表的存儲有兩種方式, 分別是distributed或者replicated ,當向TBase發送查詢 SQL時,TBase會自動向數據節點發出查詢語句并獲取最終結果。
TBase采用分布式集群架構(如下圖),該架構分布式為無共享(share nothing)模式,節點之間相應獨立,各自處理自己的數據,處理后的結果可能向上層匯總或在節點間流轉,各處理單元之間通過網絡協議進行通信,并行處理和擴展能力更好,這也意味著只需要簡單的x86服務器就可以部署TBase數據庫集群。
下面簡單解讀一下TBase的三大模塊:
Coordinator:協調節點(簡稱CN)
業務訪問入口,負責數據的分發和查詢規劃,多個節點位置對等,每個節點都提供相同的數據庫視圖;在功能上CN上只存儲系統的全局元數據,并不存儲實際的業務數據。
Datanode:數據節點(簡稱DN)
每個節點還存儲業務數據的分片在功能上,DN節點負責完成執行協調節點分發的執行請求。
GTM:全局事務管理器(Global Transaction Manager)
負責管理集群事務信息,同時管理集群的全局對象,比如序列等。
接下來,讓我們來看看如何從源碼開始,完成到TBase集群環境的搭建。
注意:所有需要安裝TBase集群的機器上都需要創建
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
本文的使用環境中,上述兩個參數如下
${SOURCECODE_PATH}=/data/tbase/TBase-master
${INSTALL_PATH}=/data/tbase/install
下面以兩臺服務器上搭建1GTM主,1GTM備,2CN主(CN主之間對等,因此無需備CN),2DN主,2DN備的集群,該集群為具備容災能力的最小配置
機器1:10.215.147.158 機器2:10.240.138.159
集群規劃如下:
參考Linux ssh互信配置
集群所有機器都需要配置
[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}
以上,已經配置好了所需要基礎環境,可以進入到集群初始化階段,為了方便用戶,TBase提供了專用的配置和操作工具: pgxc_ctl來協助用戶快速搭建并管理集群,首先需要將前文所述的節點的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
如下,是結合上文描述的IP,端口,數據庫目錄,二進制目錄等規劃來寫的pgxc_ctl.conf文件。具體實踐中只需按照自己的實際情況配置好即可.
#!/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 <<EOF #================================================ # Added to all the coordinator postgresql.conf # Original: $coordExtraConfig include_if_exists = '/data/tbase/global/global_tbase.conf' wal_level = replication wal_keep_segments = 256 max_wal_senders = 4 archive_mode = on archive_timeout = 1800 archive_command = 'echo 0' log_truncate_on_rotation = on log_filename = 'postgresql-%M.log' log_rotation_age = 4h log_rotation_size = 100MB hot_standby = on wal_sender_timeout = 30min wal_receiver_timeout = 30min shared_buffers = 1024MB max_pool_size = 2000 log_statement = 'ddl' log_destination = 'csvlog' logging_collector = on log_directory = 'pg_log' listen_addresses = '*' max_connections = 2000 EOF coordSpecificExtraConfig=(none none) coordExtraPgHba=coordExtraPgHba cat > $coordExtraPgHba <<EOF local all all trust host all all 0.0.0.0/0 trust host replication all 0.0.0.0/0 trust host all all ::1/128 trust host replication all ::1/128 trust EOF coordSpecificExtraPgHba=(none none) coordAdditionalSlaves=n cad1_Sync=n #---- Datanodes --------------------- dn1MstrDir=/data/tbase/data/dn001 dn2MstrDir=/data/tbase/data/dn002 dn1SlvDir=/data/tbase/data/dn001 dn2SlvDir=/data/tbase/data/dn002 dn1ALDir=/data/tbase/data/datanode_archlog dn2ALDir=/data/tbase/data/datanode_archlog primaryDatanode=dn001 datanodeNames=(dn001 dn002) datanodePorts=(40004 40004) datanodePoolerPorts=(41110 41110) datanodePgHbaEntries=(0.0.0.0/0) datanodeMasterServers=(10.215.147.158 10.240.138.159) datanodeMasterDirs=($dn1MstrDir $dn2MstrDir) dnWALSndr=4 datanodeMaxWALSenders=($dnWALSndr $dnWALSndr) datanodeSlave=y datanodeSlaveServers=(10.240.138.159 10.215.147.158) datanodeSlavePorts=(50004 540004) datanodeSlavePoolerPorts=(51110 51110) datanodeSlaveSync=n datanodeSlaveDirs=($dn1SlvDir $dn2SlvDir) datanodeArchLogDirs=($dn1ALDir/dn001 $dn2ALDir/dn002) datanodeExtraConfig=datanodeExtraConfig cat > $datanodeExtraConfig <<EOF #================================================ # Added to all the coordinator postgresql.conf # Original: $datanodeExtraConfig include_if_exists = '/data/tbase/global/global_tbase.conf' listen_addresses = '*' wal_level = replication wal_keep_segments = 256 max_wal_senders = 4 archive_mode = on archive_timeout = 1800 archive_command = 'echo 0' log_directory = 'pg_log' logging_collector = on log_truncate_on_rotation = on log_filename = 'postgresql-%M.log' log_rotation_age = 4h log_rotation_size = 100MB hot_standby = on wal_sender_timeout = 30min wal_receiver_timeout = 30min shared_buffers = 1024MB max_connections = 4000 max_pool_size = 4000 log_statement = 'ddl' log_destination = 'csvlog' wal_buffers = 1GB EOF datanodeSpecificExtraConfig=(none none) datanodeExtraPgHba=datanodeExtraPgHba cat > $datanodeExtraPgHba <<EOF local all all trust host all all 0.0.0.0/0 trust host replication all 0.0.0.0/0 trust host all all ::1/128 trust host replication all ::1/128 trust EOF datanodeSpecificExtraPgHba=(none none) datanodeAdditionalSlaves=n walArchive=n
在一個節點配置好配置文件后,需要預先將二進制包部署到所有節點所在的機器上,這個可以使用pgxc_ctl工具,執行 deploy all命令來完成。
[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. 登錄到所有節點,check二進制包是否分發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集群出錯,終端會打印出錯誤日志,通過查看錯誤原因,更改配置即可,或者可以通過/data/tbase/pgxc_ctl/pgxc_log路徑下的錯誤日志查看錯誤,排查配置文件的錯誤
[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
通過運行 pgxc_ctl 工具,執行 clean all命令刪除已經初始化的文件,修改pgxc_ctl.conf文件,重新執行 init 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 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.
當發現上面的輸出時,集群已經OK,另外也可以通過pgxc_ctl工具的 monitor all命令來查看集群狀態
[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
一般的如果配置的不是強同步模式,gtm salve,dn slave的故障不會影響訪問。
訪問TBase集群和訪問單機的PostgreSQL基本上無差別,我們可以通過任意一個CN訪問數據庫集群:例如通過連接CN節點select pgxc_node表即可查看集群的拓撲結構(當前的配置下備機不會展示在pgxc_node中),在Linux命令行下通過psql訪問的具體示例如下
[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來增加節點的管理靈活度,要求有一個default group才能使用,因此需要預先創建;一般情況下,會將節點的所有datanode節點加入到default group里 另外一方面,TBase的數據分布為了增加靈活度,加了中間邏輯層來維護數據記錄到物理節點的映射,我們叫sharding,所以需要預先創建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
至此,就可以跟使用單機數據庫一樣來訪問數據庫集群了
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)
通過pgxc_ctl工具的 stop all命令來停止集群,stop all 后面可以加上參數 -m fast或者是 -m immediate來決定如何停止各個節點。
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
通過pgxc_ctl工具的 start 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 start all
本文檔只是給用戶一個簡單的指引,演示如何從源碼開始,一步一步搭建一個完整的TBase集群,后續會有更多的文章來介紹TBase的特性使用,優化,問題定位等內容。
騰訊TBase GitHub 開源地址,請搜索關注“騰訊云數據庫”官方微信,回復“開源”即可獲取。
支撐微信支付的數據庫如何提供超300萬TPCC事務處理能力?
最佳實踐 | 騰訊HTAP數據庫TBase助力某省核心IT架構升級
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。