/
transfers.go
221 lines (169 loc) · 5.68 KB
/
transfers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package main
import (
"fmt"
"sync"
"strconv"
"math/rand"
"time"
"github.com/jackc/pgx"
)
const (
TRANSFER_CONNECTIONS = 10
INIT_AMOUNT = 10000
N_ITERATIONS = 10000
N_ACCOUNTS = 100000
)
var cfg1 = pgx.ConnConfig{
Host: "astro9",
Port: 15432,
Database: "postgres",
}
var cfg2 = pgx.ConnConfig{
Host: "astro9",
Port: 15433,
Database: "postgres",
}
var running = false
func prepare_db() {
var snapshot int64
var csn int64
var gtid string = "init"
conn1, err := pgx.Connect(cfg1)
checkErr(err)
defer conn1.Close()
conn2, err := pgx.Connect(cfg2)
checkErr(err)
defer conn2.Close()
exec(conn1, "drop extension if exists pg_dtm")
exec(conn1, "create extension pg_dtm")
exec(conn1, "drop table if exists t")
exec(conn1, "create table t(u int primary key, v int)")
exec(conn2, "drop extension if exists pg_dtm")
exec(conn2, "create extension pg_dtm")
exec(conn2, "drop table if exists t")
exec(conn2, "create table t(u int primary key, v int)")
exec(conn1, "begin transaction")
exec(conn2, "begin transaction")
snapshot = execQuery(conn1, "select dtm_extend($1)", gtid)
snapshot = execQuery(conn2, "select dtm_access($1, $2)", snapshot, gtid)
//for i := 0; i < N_ACCOUNTS; i++ {
// exec(conn1, "insert into t values($1, $2)", i, INIT_AMOUNT)
// exec(conn2, "insert into t values($1, $2)", i, INIT_AMOUNT)
//}
exec(conn1, "insert into t (select generate_series(0,$1-1), $2)",N_ACCOUNTS,0)
exec(conn2, "insert into t (select generate_series(0,$1-1), $2)",N_ACCOUNTS,0)
exec(conn1, "prepare transaction '" + gtid + "'")
exec(conn2, "prepare transaction '" + gtid + "'")
exec(conn1, "select dtm_begin_prepare($1)", gtid)
exec(conn2, "select dtm_begin_prepare($1)", gtid)
csn = execQuery(conn1, "select dtm_prepare($1, 0)", gtid)
csn = execQuery(conn2, "select dtm_prepare($1, $2)", gtid, csn)
exec(conn1, "select dtm_end_prepare($1, $2)", gtid, csn)
exec(conn2, "select dtm_end_prepare($1, $2)", gtid, csn)
exec(conn1, "commit prepared '" + gtid + "'")
exec(conn2, "commit prepared '" + gtid + "'")
}
func max(a, b int64) int64 {
if a >= b {
return a
}
return b
}
func transfer(id int, wg *sync.WaitGroup) {
var err error
var snapshot int64
var csn int64
nGlobalTrans := 0
conn1, err := pgx.Connect(cfg1)
checkErr(err)
defer conn1.Close()
conn2, err := pgx.Connect(cfg2)
checkErr(err)
defer conn2.Close()
for i := 0; i < N_ITERATIONS; i++ {
gtid := strconv.Itoa(id) + "." + strconv.Itoa(i)
amount := 2*rand.Intn(2) - 1
account1 := 2*id+1
account2 := 2*id+2
exec(conn1, "begin transaction")
exec(conn2, "begin transaction")
snapshot = execQuery(conn1, "select dtm_extend($1)", gtid)
snapshot = execQuery(conn2, "select dtm_access($1, $2)", snapshot, gtid)
exec(conn1, "update t set v = v - $1 where u=$2", amount, account1)
exec(conn2, "update t set v = v + $1 where u=$2", amount, account2)
exec(conn1, "prepare transaction '" + gtid + "'")
exec(conn2, "prepare transaction '" + gtid + "'")
exec(conn1, "select dtm_begin_prepare($1)", gtid)
exec(conn2, "select dtm_begin_prepare($1)", gtid)
csn = execQuery(conn1, "select dtm_prepare($1, 0)", gtid)
csn = execQuery(conn2, "select dtm_prepare($1, $2)", gtid, csn)
exec(conn1, "select dtm_end_prepare($1, $2)", gtid, csn)
exec(conn2, "select dtm_end_prepare($1, $2)", gtid, csn)
exec(conn1, "commit prepared '" + gtid + "'")
exec(conn2, "commit prepared '" + gtid + "'")
nGlobalTrans++
}
fmt.Printf("Test completed, performed %d global transactions\n", nGlobalTrans)
wg.Done()
}
func totalrep(wg *sync.WaitGroup) {
var snapshot int64
conn1, err := pgx.Connect(cfg1)
checkErr(err)
defer conn1.Close()
conn2, err := pgx.Connect(cfg2)
checkErr(err)
defer conn2.Close()
var prevSum int64 = 0
for running {
exec(conn1, "begin transaction")
exec(conn2, "begin transaction")
snapshot = execQuery(conn1, "select dtm_extend()")
snapshot = execQuery(conn2, "select dtm_access($1)", snapshot)
sum1 := execQuery(conn1, "select sum(v) from t")
sum2 := execQuery(conn2, "select sum(v) from t")
exec(conn1, "commit")
exec(conn2, "commit")
sum := sum1 + sum2
if (sum != prevSum) {
fmt.Printf("Total=%d snapshot=%d\n", sum, snapshot)
prevSum = sum
}
}
wg.Done()
}
func main() {
var transferWg sync.WaitGroup
var inspectWg sync.WaitGroup
prepare_db()
start := time.Now()
transferWg.Add(TRANSFER_CONNECTIONS)
for i:=0; i<TRANSFER_CONNECTIONS; i++ {
go transfer(i, &transferWg)
}
running = true
inspectWg.Add(1)
go totalrep(&inspectWg)
transferWg.Wait()
running = false
inspectWg.Wait()
fmt.Printf("Elapsed time %f sec\n", time.Since(start).Seconds())
fmt.Printf("TPS = %f\n", float64(TRANSFER_CONNECTIONS*N_ITERATIONS)/time.Since(start).Seconds())
}
func exec(conn *pgx.Conn, stmt string, arguments ...interface{}) {
var err error
_, err = conn.Exec(stmt, arguments... )
checkErr(err)
}
func execQuery(conn *pgx.Conn, stmt string, arguments ...interface{}) int64 {
var err error
var result int64
err = conn.QueryRow(stmt, arguments...).Scan(&result)
checkErr(err)
return result
}
func checkErr(err error) {
if err != nil {
panic(err)
}
}