postgreSQL replication for some tables, postgreSQL for gerrit server

On all Machine

  1. download slony source code http://www.slony.info/downloads/2.2/source/
  2. tar -vxjf slony1-2.2.2.tar.bz2
  3. cd slony1-2.1.3/
  4. sudo apt-get install postgresql-server-dev-all, or install the version your postgres running
  5. ./configure –prefix=/usr/local/ –with-perltools, default is psql path, recommend using prefix because you may get different psql version and different path for each machine. –with-perltools will install tools such as slon_start and slon_watchdog2, which are usefull
  6. make all -j8
  7. “All of Slony-I is successfully made. Ready to install" if build pass
  8. sudo make install
  9. “All of Slony-I is successfully installed" if install pass
  10. “/usr/local/bin/slon" and “/usr/local/bin/slonik" will be install
  11. make sure the table you want to replicate has primary key
  12. setup ~/.pgpass file
  13. make sure psql can connect from Master to Slave and vice versa without password

On Master

  1. createlang -d reviewdb plpgsql, install plpgsql to target database

vim config.sh

# path to slon
basebin=/usr/local/bin/

# cluster name
CLUSTER=gerritDB

# DB name of Master and Slave
DBSERVER=reviewdb
DBSLAVE1=reviewdb

# IP of  Master and Slave
HOSTSERVER=10.116.56.22
HOSTSLAVE1=10.116.61.56

# DB user of Master and Slave
DBSERVER_USER=postgres
DBSLAVE1_USER=postgres

DBSERVER_PASSWORD=myubuntu
DBSLAVE1_PASSWORD=myubuntu

vim configmaster.sh

#!/bin/sh

. ./config.sh

$basebin/slonik<<_EOF_

cluster name=$CLUSTER;

node 1 admin conninfo=’dbname=$DBSERVER user=$DBSERVER_USER password=$DBSERVER_PASSWORD port=5432′;
node 2 admin conninfo=’dbname=$DBSLAVE1 host=$HOSTSLAVE1 user=$DBSLAVE1_USER password=$DBSLAVE1_PASSWORD port=5432′;

init cluster ( id=1, comment = ‘Master Node’);

create set ( id=1, origin=1, comment=’All test tables’ );
set add table ( set id=1, origin=1, id=1, fully qualified name=’public.account_ssh_keys’,comment=’account ssh keys’ );

store node ( id=2, comment=’Slave Node’,event node=1 );

store path ( server=1, client=2,conninfo=’dbname=$DBSERVER host=$HOSTSERVER user=$DBSERVER_USER port=5432′);
store path ( server=2, client=1,conninfo=’dbname=$DBSLAVE1 host=$HOSTSLAVE1 user=$DBSLAVE1_USER port=5432′);

store listen ( origin=1, provider=1, receiver=2 );

_EOF_

vim commitdata.sh

#!/bin/sh

. ./config.sh

$basebin/slonik<<_EOF_

cluster name=$CLUSTER;

node 1 admin conninfo=’dbname=$DBSERVER user=$DBSERVER_USER password=$DBSERVER_PASSWORD password=$DBSLAVE1_PASSWORD port=5432′;
node 2 admin conninfo=’dbname=$DBSLAVE1 host=$HOSTSLAVE1 user=$DBSLAVE1_USER password=$DBSLAVE1_PASSWORD port=5432′;

subscribe set ( id=1, provider=1, receiver=2, forward=no);

_EOF_

  1. run configmaster.sh on Master, no news is good news, it has no any output if success
  2. on Master and run slon gerritDB1 “dbname=reviewdb host=Master_IP user=postgres port=5432″
  3. on Slave and run slon gerritDB1 “dbname=reviewdb host=Slave_IP user=postgres port=5432″
  4. run commitdata.sh on Master

insert into SLAVE1 DB will fail, refer below error messaeg

testslave1=# insert into tb_master (id,name) values(5,’aaf’);
ERROR:  Slony-I: Table tb_master is replicated and cannot be modified on a subscriber node – role=0

在看到 slony 之前,其實我原本打算自己寫一個的 …

下面是 slony 的環境要求,要求很低

  1. 所有機器的時間必須一致,建議安裝 crontab + NTP 同步機器的時間,也要設定為相同時區
  2. 資料庫編碼必須一致,例如都是 UTF8

這裡有安裝和設定教學:http://www.died.tw/2010/09/postgresql-slony-i-ubuntu.html

官方的教學文件:http://www.slony.info/documentation/tutorial.html

兩份教學大致上是同樣的概念,但是官方那份在 configmaster.sh 多了一行 store path,以我自己的過程,缺少了這一行是無法成功進行 replication 的,並且會一直顯示下面這樣的訊息

<stdin>:24: waiting for event (1,5000000007) to be confirmed on node 2

如果有出現錯誤訊息「PGRES_FATAL_ERROR load ‘$libdir/xxid’; – ERROR: could not access file “$libdir/xxid": 」

這是slony版本問題,我的 master, slave 機器使用的 slony 不一樣,因為我使用 apt-get 安裝的,兩台 ubuntu 版本不一樣,後來改自己 build 就沒這個問題了

如果 master, slave 兩邊的資料庫一開始並不一樣,也可以進行 slony replication 不會有問題,並且可以在首次 replication 的時候看到像下面這樣的訊息

NOTICE:  truncate of “public"."account_ssh_keys" succeeded

 

最近在找適合 gerrit server 的資料庫同步軟體

要給 gerrit 使用的

gerrit 是一套原代碼版本控制軟體,兼具有 commit review, permission control 功能

commit review 指的是,可以控制某些 branch 只有某群組的人有權限 submit ,必須經過某群組的人 review之後才能 submit commit

permission control 指的是避免不適當的人 submit commit, 甚至是直接拒絕某些群組的人 access 某些 branch 的代碼

由上可知,gerrit 權限管理的單位是 branch ,例如設定 refs/heads/stable/* 的 submit 權限只給 core_members groups,就代表只有 core_members 這個群組裡面的人有權限 submit commit 到 branch 名稱是 stable/main or stable/cr

群組這個資訊是放在 DB 裡面的,使用者 ssh pub key 也是放在 DB 裡面的。

gerrit 本身支援 replication ,可以透過內建的 replication 功能製作出 slave server,詳情閱讀 gerrit document

但是 gerrit 內建的 replication 只對 git 做 replication,而 gerrit 所使用的資料庫無法透過這個 replication 功能複寫到 slave server

所以我們必須手動去 slave server 設定好相關的群組和 ssh key,讓使用者 sync 可以有正確的權限操作

當有新增使用者或新增群組的時候,就要做一次設定,非常麻煩~

針對這個設定問題,有個錯誤的解決方法是,把 slave 的機器使用的 DB ip 指向 master DB,這個方式會造成 gerrit 自身的 replication 功能錯誤,因為 replication 過程會檢查資料庫裡面是否已經存在某 commit

在 gerrit 的 replication 機制裡面,slony 是最適合這個情境的解決方案,因為他可以選擇需要的 table 做複寫,我們只要把 groups, user , user ssh key 相關的表設定複寫就行

postgres install simple tutorial 首次安裝和設定帳號指令

sudo apt-get install postgresql

sudo -u postgres psql postgres

\password postgres

輸入 postgres 密碼,這個帳號是 postgres 預設最高權限帳號

sudo su postgres

createuser -A -D -P -E your_account

postgres 這個資料庫系統的帳號系統使用系統帳號名稱,但密碼不一樣

這邊建立的帳號是你的程式裡面用來連接資料庫用的帳號,密碼不要跟 postgres 一樣,不解釋,基本資安概念

更改 listen 位置,如果有需要從別的機器連線的話
sudo vim /etc/postgresql/8.4/main/postgresql.conf
listen_addresses = '*'
如果真的有別的機器要連進來的話,多設定這個,否則預設好像只給本機連線
sudo vim /etc/postgresql/8.4/main/pg_hba.conf
host    all         all         0.0.0.0/0             md5

createdb -E UTF-8 -O your_account app_db

sudo -su your_account psql app_db