|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | +"fmt" |
| 5 | +"sync" |
| 6 | +"strconv" |
| 7 | +"math/rand" |
| 8 | +"time" |
| 9 | +"github.com/jackc/pgx" |
| 10 | +) |
| 11 | + |
| 12 | +const ( |
| 13 | +TRANSFER_CONNECTIONS=2 |
| 14 | +INIT_AMOUNT=10000 |
| 15 | +N_ITERATIONS=10000 |
| 16 | +N_ACCOUNTS=TRANSFER_CONNECTIONS//100000 |
| 17 | +ISOLATION_LEVEL="repeatable read" |
| 18 | +//ISOLATION_LEVEL = "read committed" |
| 19 | +) |
| 20 | + |
| 21 | + |
| 22 | +varcfg1= pgx.ConnConfig{ |
| 23 | +Host:"127.0.0.1", |
| 24 | +Port:5432, |
| 25 | +Database:"postgres", |
| 26 | + } |
| 27 | + |
| 28 | +varcfg2= pgx.ConnConfig{ |
| 29 | +Host:"127.0.0.1", |
| 30 | +Port:5433, |
| 31 | +Database:"postgres", |
| 32 | + } |
| 33 | + |
| 34 | + |
| 35 | +varrunning=false |
| 36 | + |
| 37 | +funcprepare_db() { |
| 38 | +// var xid int32 |
| 39 | + |
| 40 | +conn1,err:=pgx.Connect(cfg1) |
| 41 | +checkErr(err) |
| 42 | +deferconn1.Close() |
| 43 | + |
| 44 | +conn2,err:=pgx.Connect(cfg2) |
| 45 | +checkErr(err) |
| 46 | +deferconn2.Close() |
| 47 | + |
| 48 | +exec(conn1,"drop extension if exists pg_dtm") |
| 49 | +exec(conn1,"create extension pg_dtm") |
| 50 | +exec(conn1,"drop table if exists t") |
| 51 | +exec(conn1,"create table t(u int primary key, v int)") |
| 52 | + |
| 53 | +exec(conn2,"drop extension if exists pg_dtm") |
| 54 | +exec(conn2,"create extension pg_dtm") |
| 55 | +exec(conn2,"drop table if exists t") |
| 56 | +exec(conn2,"create table t(u int primary key, v int)") |
| 57 | + |
| 58 | +exec(conn1,"CREATE EXTENSION postgres_fdw"); |
| 59 | +exec(conn1,"CREATE SERVER dtm FOREIGN DATA WRAPPER postgres_fdw options (dbname 'postgres', host '127.0.0.1', port '5433')"); |
| 60 | +exec(conn1,"CREATE FOREIGN TABLE t_fdw() inherits (t) server dtm options(table_name 't')"); |
| 61 | +exec(conn1,"CREATE USER MAPPING for knizhnik SERVER dtm options (user 'knizhnik')"); |
| 62 | + |
| 63 | +// start transaction |
| 64 | +exec(conn1,"begin transaction isolation level "+ISOLATION_LEVEL) |
| 65 | +exec(conn2,"begin transaction isolation level "+ISOLATION_LEVEL) |
| 66 | + |
| 67 | +fori:=0;i<N_ACCOUNTS;i++ { |
| 68 | +exec(conn1,"insert into t values($1, $2)",i,INIT_AMOUNT) |
| 69 | +exec(conn2,"insert into t values($1, $2)",-i,INIT_AMOUNT) |
| 70 | + } |
| 71 | + |
| 72 | +exec(conn1,"commit") |
| 73 | +exec(conn2,"commit") |
| 74 | +} |
| 75 | + |
| 76 | +funcprogress(totalint,cCommitschanint,cAbortschanint) { |
| 77 | +commits:=0 |
| 78 | +aborts:=0 |
| 79 | +start:=time.Now() |
| 80 | +fornewcommits:=rangecCommits { |
| 81 | +newaborts:=<-cAborts |
| 82 | +commits+=newcommits |
| 83 | +aborts+=newaborts |
| 84 | +iftime.Since(start).Seconds()>10 { |
| 85 | +fmt.Printf( |
| 86 | +"progress %0.2f%%: %d commits, %d aborts\n", |
| 87 | +float32(commits)*100.0/float32(total),commits,aborts, |
| 88 | + ) |
| 89 | +start=time.Now() |
| 90 | + } |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +functransfer(idint,cCommitschanint,cAbortschanint,wg*sync.WaitGroup) { |
| 95 | +varerrerror |
| 96 | +varxidint32 |
| 97 | +varnAborts=0 |
| 98 | +varnCommits=0 |
| 99 | +varmyCommits=0 |
| 100 | + |
| 101 | +conn,err:=pgx.Connect(cfg1) |
| 102 | +checkErr(err) |
| 103 | +deferconn.Close() |
| 104 | + |
| 105 | +start:=time.Now() |
| 106 | +formyCommits<N_ITERATIONS { |
| 107 | +amount:=1 |
| 108 | +account1:=rand.Intn(N_ACCOUNTS) |
| 109 | +account2:=-rand.Intn(N_ACCOUNTS) |
| 110 | + |
| 111 | +exec(conn,"begin") |
| 112 | +xid=execQuery(conn,"select dtm_begin_transaction(2)") |
| 113 | +exec(conn,"select postgres_fdw_exec('t_fdw'::regclass::oid, 'select public.dtm_join_transaction("+strconv.Itoa(int(xid))+")')") |
| 114 | +exec(conn,"commit") |
| 115 | + |
| 116 | +exec(conn,"begin transaction isolation level "+ISOLATION_LEVEL) |
| 117 | + |
| 118 | +ok1:=execUpdate(conn,"update t set v = v - $1 where u=$2",amount,account1) |
| 119 | +ok2:=execUpdate(conn,"update t set v = v + $1 where u=$2",amount,account2) |
| 120 | + |
| 121 | +if!ok1||!ok2 { |
| 122 | +exec(conn,"rollback") |
| 123 | +nAborts+=1 |
| 124 | + }else { |
| 125 | +exec(conn,"commit") |
| 126 | +nCommits+=1 |
| 127 | +myCommits+=1 |
| 128 | + } |
| 129 | + |
| 130 | +iftime.Since(start).Seconds()>10 { |
| 131 | +cCommits<-nCommits |
| 132 | +cAborts<-nAborts |
| 133 | +nCommits=0 |
| 134 | +nAborts=0 |
| 135 | +start=time.Now() |
| 136 | + } |
| 137 | + } |
| 138 | +cCommits<-nCommits |
| 139 | +cAborts<-nAborts |
| 140 | +wg.Done() |
| 141 | +} |
| 142 | + |
| 143 | +funcinspect(wg*sync.WaitGroup) { |
| 144 | +varsumint64 |
| 145 | +varprevSumint64=0 |
| 146 | +varxidint32 |
| 147 | + |
| 148 | + { |
| 149 | +conn,err:=pgx.Connect(cfg1) |
| 150 | +checkErr(err) |
| 151 | + |
| 152 | +forrunning { |
| 153 | +exec(conn,"begin") |
| 154 | +xid=execQuery(conn,"select dtm_begin_transaction(2)") |
| 155 | +exec(conn,"select postgres_fdw_exec('t_fdw'::regclass::oid, 'select public.dtm_join_transaction("+strconv.Itoa(int(xid))+")')") |
| 156 | +exec(conn,"commit") |
| 157 | + |
| 158 | +exec(conn,"begin transaction isolation level "+ISOLATION_LEVEL) |
| 159 | + |
| 160 | +sum=execQuery64(conn,"select sum(v) from t") |
| 161 | + |
| 162 | +if (sum!=prevSum) { |
| 163 | +fmt.Printf("Total=%d xid=%d\n",sum,xid) |
| 164 | +prevSum=sum |
| 165 | + } |
| 166 | + |
| 167 | +exec(conn,"commit") |
| 168 | + } |
| 169 | +conn.Close() |
| 170 | + } |
| 171 | +wg.Done() |
| 172 | +} |
| 173 | + |
| 174 | +funcmain() { |
| 175 | +vartransferWg sync.WaitGroup |
| 176 | +varinspectWg sync.WaitGroup |
| 177 | + |
| 178 | +prepare_db() |
| 179 | + |
| 180 | +cCommits:=make(chanint) |
| 181 | +cAborts:=make(chanint) |
| 182 | +goprogress(TRANSFER_CONNECTIONS*N_ITERATIONS,cCommits,cAborts) |
| 183 | + |
| 184 | +transferWg.Add(TRANSFER_CONNECTIONS) |
| 185 | +fori:=0;i<TRANSFER_CONNECTIONS;i++ { |
| 186 | +gotransfer(i,cCommits,cAborts,&transferWg) |
| 187 | + } |
| 188 | +running=true |
| 189 | +inspectWg.Add(1) |
| 190 | +goinspect(&inspectWg) |
| 191 | + |
| 192 | +transferWg.Wait() |
| 193 | +running=false |
| 194 | +inspectWg.Wait() |
| 195 | + |
| 196 | +fmt.Printf("done\n") |
| 197 | +} |
| 198 | + |
| 199 | +funcexec(conn*pgx.Conn,stmtstring,arguments...interface{}) { |
| 200 | +varerrerror |
| 201 | +// fmt.Println(stmt) |
| 202 | +_,err=conn.Exec(stmt,arguments... ) |
| 203 | +checkErr(err) |
| 204 | +} |
| 205 | + |
| 206 | +funcexecUpdate(conn*pgx.Conn,stmtstring,arguments...interface{})bool { |
| 207 | +varerrerror |
| 208 | +// fmt.Println(stmt) |
| 209 | +_,err=conn.Exec(stmt,arguments... ) |
| 210 | +//if err != nil { |
| 211 | +// fmt.Println(err) |
| 212 | +//} |
| 213 | +returnerr==nil |
| 214 | +} |
| 215 | + |
| 216 | +funcexecQuery(conn*pgx.Conn,stmtstring,arguments...interface{})int32 { |
| 217 | +varerrerror |
| 218 | +varresultint32 |
| 219 | +err=conn.QueryRow(stmt,arguments...).Scan(&result) |
| 220 | +checkErr(err) |
| 221 | +returnresult |
| 222 | +} |
| 223 | + |
| 224 | +funcexecQuery64(conn*pgx.Conn,stmtstring,arguments...interface{})int64 { |
| 225 | +varerrerror |
| 226 | +varresultint64 |
| 227 | +err=conn.QueryRow(stmt,arguments...).Scan(&result) |
| 228 | +checkErr(err) |
| 229 | +returnresult |
| 230 | +} |
| 231 | + |
| 232 | +funccheckErr(errerror) { |
| 233 | +iferr!=nil { |
| 234 | +panic(err) |
| 235 | + } |
| 236 | +} |
| 237 | + |
| 238 | +// vim: expandtab ts=4 sts=4 sw=4 |