tbase和postgres-xl的比較

lt發表於2020-07-20
2020年6月,騰訊的分散式資料庫tbase 2.1版開源([1])。

騰訊在參考資料[1]的開頭說明“TBase is an advanced enterprise-level database management system based on prior work of Postgres-XL project”,這是很不錯的,體現了開源的精神。
下面對這一資料庫與它的前身 Postgres-XL[2]做一比較。
1.安裝
1.1 tbase原始碼安裝

tbase的最新發版的原始碼位於(https://github.com/Tencent/TBase/archive/v2.1.0.tar.gz)
而隨時更新的原始碼主儲存庫位於(https://github.com/Tencent/TBase/archive/master.zip),為了下面的測試內容和過程可重複,有參考和比較價值,這裡採用穩定的發版原始碼。

參考資料[1]的內容比較簡略,不適合於沒有經驗的人,我在上面就遇到了好幾個問題,在tbase技術支援專家的指導下,用了幾天反覆試驗才解決。

參考資料[1]上的詳細資料位於https://github.com/Tencent/TBase/wiki。這個頁面還貼心地貼出了支援團隊的微信二維碼,可以為使用者提供及時的技術支援。
上面的文章列表如下,除了前二篇是英文資料,其餘都是中文漢字資料,這對我們中國使用者是很方便的。
0、TBase_Quick_Start.EN
0、TBase_Use_Articles.EN
1、TBase_Quick_Start
2、TBase基本使用篇
3、TBase高階使用篇
4、TBase應用接入指南
5、TBase元件安裝及管理
6、TBase主備切換
7、TBase使用者許可權管理
8、TBase資料匯入匯出
9、TBase使用問題定位及效能優化

下面仍然用參考資料[1]的步驟來安裝。

第一部分Building的步驟看起來沒有太多問題,稍有linux/unix經驗的人都知道SOURCECODE_PATH和INSTALL_PATH是需要設定的環境變數,而如何解壓tar.gz壓縮包也是完全可以從其他資料查閱的。
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

但是,等到實際操作就會發現,這第3行是不容易一次成功的,問題在於,它沒有說明安裝的前提條件和依賴包。例如,在我的機器上,第一次執行在如下地方中斷了
checking for library containing readline... no
configure: error: readline library not found
If you have readline already installed, see config.log for details on the
failure.  It is possible the compiler isn't looking in the proper directory.
Use --without-readline to disable readline support.
使用上述提示的--without-readline選項來繼續安裝,又會報以下錯誤,
checking for inflate in -lz... no
configure: error: zlib library not found
If you have zlib already installed, see config.log for details on the
failure.  It is possible the compiler isn't looking in the proper directory.
Use --without-zlib to disable zlib support.

checking for CRYPTO_new_ex_data in -lcrypto... no
configure: error: library 'crypto' is required for OpenSSL
等等。
而且有些包是必須的,不能採用--without的選項忽略。
經查閱網上資料,採用下面的命令安裝了readline依賴包。
yum install readline readline-devel
再安裝openssl依賴包。
yum install openssl openssl-devel


文件本來完全可以說明這一點,並給出一個完整的依賴包安裝命令,方便使用者繼續從tbase原始碼安裝,如參考資料[3]所示:
yum install openssl openssl-devel pam pam-devel libxml2 libxml2-devel libxslt libxslt-devel perl perl-devel python-devel \
    perl-ExtUtils-Embed readline readline-devel  zlib zlib-devel gettext gettext-devel bison flex

這個步驟有個地方值得表揚,make -sj命令使用多執行緒靜默方式編譯,消除了大量的編譯命令顯示和輸出,還提高了編譯速度。

編譯完成後,${INSTALL_PATH}/tbase_bin_v2.0目錄下產生了bin、include、lib、share 4個
目錄,需要將其複製到將來實際安裝tbase的目錄,注意是複製而不是移動,因為用pgxc_ctl部署失敗有可能會刪除二進位制檔案,我就被迫再次編譯了,這裡文件也沒有提及。

第二部分Installation的安裝步驟首先要配置環境變數
把tbase使用者主目錄下的.bashrc資原始檔修改為

-bash-4.2$ cat .bashrc
# .bashrc

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi
PG_HOME=/data/tbase/install/tbase_bin_v2.0
export PATH="$PATH:$PG_HOME/bin"
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$PG_HOME/lib"
export LC_ALL=C
export TMPDIR=/data/tbase/tmp/
export PGHOST=a.a.a.a

其中TMPDIR的指定很重要,很重要,很重要,否則在執行過程中,會找不到臨時目錄,出現類似下面的錯誤,而參考資料[1]沒有提及這個問題。
sh: -c: line 0: syntax error near unexpected token `('
sh: -c: line 0: `( tar czCf (null) (null)/657023.tgz bin include lib share ) < /dev/null > (null)/STDOUT_657023_0 2>&1'
ERROR: Cannot open "(null)/STDOUT_657023_0" for read, No such file or directory

在我的linux版本上,使用者登入時實際呼叫的是.bash_profile
所以如果由於某種原因系統中缺少這個檔案,要編輯如下內容:
[tbase@a.a.a.a ~]$ cat  .bash_profile
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs

然後修改~/pgxc_ctl/pgxc_ctl.conf配置檔案。

pgxcInstallDir=${INSTALL_PATH}/tbase_bin_v2.0
GTM, 需要配置節點名、 IP埠和節點目錄。
#---- GTM ----------
gtmName=gtm
gtmMasterServer=xxx.xxx.xxx.1
gtmMasterPort=50001
gtmMasterDir=${GTM_MASTER_DATA_DIR}/data/gtm_master
If you do not need gtmSlave, you can directly set it to 'n' in the configuration of the corresponding node.
gtmSlave=n
GTM從節點不需要就不用填了。
協調節點同樣
coordNames=(cn001)
coordMasterCluster=(tbase_cluster)
coordPorts=(30004)
poolerPorts=(30014)
coordPgHbaEntries=(0.0.0.0/0)
coordMasterServers=(xxx.xxx.xxx.2)
coordMasterDirs=(${COORD_MASTER_DATA_DIR}/data/cn_master/cn001)
資料節點,這裡實際可以採用一個地址,但需要配兩個不同的埠
primaryDatanode=dn001
datanodeNames=(dn001 dn002)
datanodePorts=(20008 20009)
datanodePoolerPorts=(20018 20019)
datanodeMasterCluster=(tbase_cluster tbase_cluster)
datanodePgHbaEntries=(0.0.0.0/0)
datanodeMasterServers=(xxx.xxx.xxx.3 xxx.xxx.xxx.4)
datanodeMasterDirs=(${DATANODE_MASTER_DATA_DIR}/data/dn_master/dn001 ${DATANODE_MASTER_DATA_DIR}/data/dn_master/dn002)

如果直接編輯了這個檔案,然後執行pgxc_ctl,會有很多錯誤資訊,無法完成操作。
[tbase@a.a.a.a ~]$ pgxc_ctl
/bin/bash
Installing pgxc_ctl_bash script as /home/tbase/pgxc_ctl/pgxc_ctl_bash.
Installing pgxc_ctl_bash script as /home/tbase/pgxc_ctl/pgxc_ctl_bash.
Reading configuration using /home/tbase/pgxc_ctl/pgxc_ctl_bash --home /home/tbase/pgxc_ctl --configuration /home/tbase/pgxc_ctl/pgxc_ctl.conf
Finished reading configuration.
ERROR: Number of elements in coordinator master definitions are different coordNames and coordMaxWALSenders.  Check your configuration
ERROR: Number of elements in datanode master definitions are different datanodeNames and datanodeMaxWALSenders.  Check your configuration
ERROR: Found fundamental configuration error.

而參考資料[1]寫的“After the pgxc_ctl tool is started, a template of pgxc_ctl directory and configuration file will be generated in the current user's home directory”,其實也不正確。
執行pgxc_ctl工具並未產生模板檔案。

配置檔案對於新手是有難度的工作,tbase支援人員推薦我學習wiki目錄中的《5、TBase元件安裝及管理》以及《1、TBase_Quick_Start》文件。
為了加快安裝過程,技術支援專家為我提供了一個她做好的pgxc_ctl.conf配置檔案模板,我只要修改ip地址就足夠了。
這裡最大的一個坑正是ip地址,由於我用來安裝的主機具有106打頭的外網地址,可以遠端登入,而用ifconfig查不到這個地址,查到的地址是172打頭的內網地址,下面簡稱a.a.a.a。
我一開始不瞭解情況,替換技術支援專家提供的pgxc_ctl.conf配置檔案時,所有地址均採用了106打頭的地址,各種操作極慢,但卻沒有報錯。
[tbase@a.a.a.a pgxc_ctl]$ pgxc_ctl
/bin/bash
Installing pgxc_ctl_bash script as /home/tbase/pgxc_ctl/pgxc_ctl_bash.
Installing pgxc_ctl_bash script as /home/tbase/pgxc_ctl/pgxc_ctl_bash.
Reading configuration using /home/tbase/pgxc_ctl/pgxc_ctl_bash --home /home/tbase/pgxc_ctl --configuration /home/tbase/pgxc_ctl/pgxc_ctl.conf
Finished reading configuration.
   ******** PGXC_CTL START ***************

Current directory: /home/tbase/pgxc_ctl
PGXC deploy all
Deploying Postgres-XL components to all the target servers.
Prepare tarball to deploy ...
這實際上是把檔案複製到各節點。設定ssh等效的原因就是為了複製過程不需要輸入其他主機的口令。其實對於單機,deploy all操作是多餘的,因為各個節點實際上都是同一個路徑,無需把可執行檔案複製過去。
deploy all完成後,再在PGXC 提示符下輸入
init all
啟動過程中會出現各種異常資訊,可以用提示中的方法解決。

比如:LOG:  skipping missing configuration file "/data/tbase/global/global_tbase.conf"
其中儲存的是各種全域性引數,可以在相應目錄編輯類似以下內容。
max_wal_senders=32
shared_buffers = 2GB
unix_socket_directories='/data/tbase/tmp/'
還有下面的警告資訊,
WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.
要在每個協調和資料節點目錄中的pg_hba.conf檔案中新增一行
host all tbase a.a.a.a/32 trust
然後重新啟動叢集
安裝完成後,只能用127.0.0.1本地地址訪問,而不能用我在pgxc_ctl.conf配置檔案中指定的地址訪問。
用psql客戶端連線,
[tbase@a.a.a.a bin]$ psql postgres -h 127.0.0.1  -p 30004 -U tbase

postgres=# select 1;
長時間卡住。
最後報錯:
ERROR:  GTM error, could not obtain global timestamp. Current XID = 0, Autovac = 0
檢視gtm日誌也沒有找到原因。

請教技術支援專家後,她說pgxc_ctl.conf配置檔案中應該填寫內網地址。
替換完成後,所有的命令執行速度都極大地提高了,以前各種操作的慢估計都是在等待超時。
現在叢集登入後可以執行一些查詢了。
[tbase@a.a.a.a bin]$ /data/tbase/install/tbase_bin_v2.0/bin/psql -h   a.a.a.a    -p 30004 -U tbase -d postgres
psql (PostgreSQL 10.0 TBase V2)
Type "help" for help.

postgres=# select version();
                                                     version
------------------------------------------------------------------------------------------------------------------
 PostgreSQL 10.0 TBase V2 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-39), 64-bit

這裡有個缺陷,原始碼說明是2.1版,檢視版本資訊仍然是2.0版。

postgres=# select 1;
 ?column?
----------
        1
(1 row)

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 |   a.a.a.a   | t              | f                |  428125959 | tbase_cluster
 cn001     | C         |     30004 |   a.a.a.a   | f              | f                | -264077367 | tbase_cluster
 dn001     | D         |     40004 |   a.a.a.a   | t              | t                | 2142761564 | tbase_cluster
 dn002     | D         |     40005 |   a.a.a.a   | f              | t                |  -17499968 | tbase_cluster
(4 rows)
與postgres-xl的區別是,有一行gtm的內容。
但是配置還未完成,仍然無法建立資料庫和表,叢集還不能使用,create database超時報錯,其他操作類似。
postgres=# create database test LC_COLLATE='zh_CN.UTF-8' LC_CTYPE='zh_CN.UTF-8' encoding 'utf8'template template0;
ERROR:  Failed to get pooled connections
HINT:  This may happen because one or more nodes are currently unreachable, either because of node or network failure.
 Its also possible that the target node may have hit the connection limit or the pooler is configured with low connections.
 Please check if all nodes are running fine and also review max_connections and max_pool_size configuration parameters


原來~/.bashrc裡還有一個PGHOST=ip地址,改過來以後,進入pgxc_ctl工具介面,stop all再start all, 現在create database立刻報錯:
postgres=# create database test LC_COLLATE='zh_CN.UTF-8' LC_CTYPE='zh_CN.UTF-8' encoding 'utf8'template template0;
ERROR:  pgxc_node_set_query: nodename:dn002,backend_pid:1413946,message:node "cn001_1413921" does not exist,detail:,hint:

再次進入pgxc_ctl工具介面,clean all再init all,再次登入,可以建立資料庫,環境配置好了。
所以各種問題的原因就是ip,幾個配置檔案中的每一處都要改為ifconfig查詢到的實際地址,資料庫叢集才能使用。
後面就用此版本來做功能測試。

參考資料[1]還有一個問題是最後二步輸入的命令不明顯,雖然給出了截圖,還是不如直接用明顯的程式碼文字顯示。

其實,PGXC提示符下輸入help可以得到比較簡明的命令用法,比如monitor可以監控各種節點。
PGXC help
You are using pgxc_ctl, the configuration utility for PGXL
Type:
    help <command>
    where <command> is either add, Createdb, Createuser, clean,
        configure, deploy, failover, init, kill, log, monitor,
        prepare, q, reconnect, remove, set, show, start,
        stop or unregister
PGXC help monitor

monitor all
monitor nodename ...
monitor gtm [ master | slave | all ]
monitor gtm_proxy [ all | nodename ... ]
monitor coordinator nodename ...
monitor coordinator [ master | slave ] [ all | nodename ... ]
monitor datanode nodename ...
monitor datanode [ master | slave ] [ all | nodename ...  ]

Monitors if specified nodes are running
For more details, please see the pgxc_ctl documentation

add命令用於新增各種節點
PGXC help add

add gtm slave slave_name host port dir
add gtm_proxy name host port dir
add coordinator master name host port pooler dir extra_conf extra_pghba
add coordinator slave name host port pooler dir archDir
add datanode master name host port pooler dir xlogdir restore_datanode_name extra_conf extra_pghba
add datanode slave name host port pooler dir xlogdir archDir

Add the specified node to your postgres-xl cluster:
For more details, please see the pgxc_ctl documentation

PGXC add gtm master gtm a.a.a.a 50001 /data/tbase/g/data/gtm_master
這樣執行的結果會產生一個包含一個gtm配置資訊的pgxc_ctl.conf檔案。
總之,工具是有用的,關鍵是要用對。
下面postgres-xl安裝過程中,將會用到其中的一些命令。

1.2 postgres-xl原始碼安裝
tbase的最新發版的原始碼位於(https://www.postgres-xl.org/downloads/postgres-xl-10r1.1.tar.gz)。
釋出日期是2019-02-18,最近一年都沒有新版本釋出。
https://www.postgres-xl.org/download/中提供的
安裝步驟同樣過於簡略,不過從文件目錄中翻閱發現了下面一個堪稱典範的文字,tbase也許正缺少這麼一個適用於完全沒有經驗的人的文件。
https://www.postgres-xl.org/documentation/install-short.html
這前三行解釋了為什麼它對於入門非常合適。
The following short installation allows to install a simple cluster on a local machine with 1 Coordinator, 2 Datanodes and 1 GTM.
在單機上搭建一個包含一個協調節點、二個資料節點(一個節點不足以體驗分散式功能)、一個全域性事務管理節點的叢集。極其低的硬體門檻和最簡化的拓撲結構,導致最短時間內可以上手。實際上,我在tbase的
github站點上提的第一個問題也就是這個。

When installing a more complex cluster, you might change the number of Coordinators and Datanodes, and might have to start nodes on different servers. 
這行告訴我們更復雜的叢集包含更多的節點,以及部署在不同的機器上。
Also, you can instead use the pgxc_ctl utility, which simplifies the installation and configuration process.
這行告訴我們可以用工具來簡化安裝和配置,而不是直接上來就用工具,掩蓋了後臺操作的同時,也使排除故障變得艱難。實際上,tbase支援人員推薦我學習的《TBase元件安裝及管理》文件的內容與此類似。
然後就是直截了當的命令列,只要逐一複製就能執行,沒有複雜的編輯配置檔案儲存的步驟,每一步的輸出的含義都是明確的。

除了開頭幾行有些內容稍顯過時,比如沒有采取並行編譯,其餘都是不分版本通用的。下面簡單註釋如下:
#配置和編譯
./configure
gmake
su
gmake install
#建立使用者和目錄,並授權
adduser postgres
mkdir /usr/local/pgsql/data_coord1
mkdir /usr/local/pgsql/data_datanode_1
mkdir /usr/local/pgsql/data_datanode_2
mkdir /usr/local/pgsql/data_gtm
chown postgres /usr/local/pgsql/data_coord1
chown postgres /usr/local/pgsql/data_datanode_1
chown postgres /usr/local/pgsql/data_datanode_2
chown postgres /usr/local/pgsql/data_gtm
#初始化資料庫
su - postgres
/usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data_coord1 \
  --nodename coord1
/usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data_datanode_1 \
  --nodename datanode_1
/usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data_datanode_2 \
  --nodename datanode_2
#初始化GTM並啟動服務
/usr/local/pgsql/bin/initgtm -D /usr/local/pgsql/data_gtm -Z gtm
/usr/local/pgsql/bin/gtm -D /usr/local/pgsql/data_gtm >logfile 2>&1 &
#啟動節點服務,指定埠
/usr/local/pgsql/bin/postgres --datanode -p 15432 -c pooler_port=40101 \
  -D /usr/local/pgsql/data_datanode_1 >logfile 2>&1 &
/usr/local/pgsql/bin/postgres --datanode -p 15433 -c pooler_port=40102 \
  -D /usr/local/pgsql/data_datanode_2 >logfile 2>&1 &
/usr/local/pgsql/bin/postgres --coordinator -c pooler_port=40100 \
  -D /usr/local/pgsql/data_coord1 >logfile 2>&1 &
#在資料庫中新增節點資訊
/usr/local/pgsql/bin/psql -c "ALTER NODE coord1 \
  WITH (TYPE = 'coordinator', PORT = 5432)" postgres
/usr/local/pgsql/bin/psql -c "CREATE NODE datanode_1 \
  WITH (TYPE = 'datanode', PORT = 15432)" postgres
/usr/local/pgsql/bin/psql -c "CREATE NODE datanode_2 \
  WITH (TYPE = 'datanode', PORT = 15433)" postgres

/usr/local/pgsql/bin/psql -c "EXECUTE DIRECT ON (datanode_1) \
  'ALTER NODE datanode_1 WITH (TYPE = ''datanode'', PORT = 15432)'" postgres
/usr/local/pgsql/bin/psql -c "EXECUTE DIRECT ON (datanode_1) \
  'CREATE NODE datanode_2 WITH (TYPE = ''datanode'', PORT = 15433)'" postgres
/usr/local/pgsql/bin/psql -c "EXECUTE DIRECT ON (datanode_1) \
  'CREATE NODE coord1 WITH (TYPE = ''coordinator'', PORT = 5432)'" postgres

/usr/local/pgsql/bin/psql -c "EXECUTE DIRECT ON (datanode_2) \
  'ALTER NODE datanode_2 WITH (TYPE = ''datanode'', PORT = 15433)'" postgres
/usr/local/pgsql/bin/psql -c "EXECUTE DIRECT ON (datanode_2) \
  'CREATE NODE datanode_1 WITH (TYPE = ''datanode'', PORT = 15432)'" postgres
/usr/local/pgsql/bin/psql -c "EXECUTE DIRECT ON (datanode_2) \
  'CREATE NODE coord1 WITH (TYPE = ''coordinator'', PORT = 5432)'" postgres
#檢查叢集
/usr/local/pgsql/bin/psql -c "SELECT pgxc_pool_reload()" postgres
/usr/local/pgsql/bin/psql -c "EXECUTE DIRECT ON (datanode_1) \
  'SELECT pgxc_pool_reload()'" postgres
/usr/local/pgsql/bin/psql -c "EXECUTE DIRECT ON (datanode_2) \
  'SELECT pgxc_pool_reload()'" postgres
#建新測試資料庫並連線
/usr/local/pgsql/bin/createdb test
/usr/local/pgsql/bin/psql test

做完這幾步以後就可以輸入SQL檢視叢集的節點了。沒有任何與網路相關的操作。
test=# SELECT * FROM pgxc_node;
 node_name  | node_type | node_port | node_host | nodeis_primary | nodeis_preferred |   node_id
------------+-----------+-----------+-----------+----------------+------------------+-------------
 coord1     | C         |      5432 | localhost | f              | f                |  1885696643
 datanode_1 | D         |     15432 | localhost | f              | f                |  -675012441
 datanode_2 | D         |     15433 | localhost | f              | f                | -1047623914
(3 rows)
這個版本下面會棄用,因為要用pgxc_ctl 工具重新做一個叢集。
但順暢的安裝過程(編譯的順暢要歸功於安裝tbase時安裝的依賴包)確實給使用者帶來很大的滿足感,從而對此軟體產生了好感。
[1]https://github.com/Tencent/TBase
[2]https://www.postgres-xl.org/
[3]PostgreSQL原始碼安裝及配置(http://www.freeoa.net/osuport/db/postgresql-source-compile-install-config_3141.html)

相關文章