/
bank.clj
333 lines (298 loc) · 13.6 KB
/
bank.clj
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
(ns tarantool.bank
"Simulates transfers between bank accounts."
(:require [clojure.tools.logging :refer [info warn]]
[clojure.string :as str]
[clojure.core.reducers :as r]
[jepsen [cli :as cli]
[client :as client]
[checker :as checker]
[control :as c]
[generator :as gen]]
[jepsen.tests.bank :as bank]
[next.jdbc :as j]
[next.jdbc.sql :as sql]
[knossos.op :as op]
[jepsen.checker.timeline :as timeline]
[tarantool [client :as cl]
[db :as db]]))
(def table-name "accounts")
(def err-unable-to-distribute-balances (str
"Unable to distribute initial balances uniformly "
"for given total-amount and accounts"))
(defrecord BankClientWithLua [conn]
client/Client
(open! [this test node]
(let [conn (cl/open node test)]
(assoc this :conn conn :node node)))
(setup! [this test node]
(locking BankClientWithLua
(let [conn (cl/open node test)
; Distribute initial balances uniformly.
initial-balance-per-account (/ (:total-amount test)
(count (:accounts test)))]
(assert (integer? initial-balance-per-account)
err-unable-to-distribute-balances)
(Thread/sleep 10000) ; wait for leader election and joining to a cluster
(when (= node (first (db/primaries test)))
(cl/with-conn-failure-retry conn
(info "Creating table " table-name)
(j/execute! conn [(str "CREATE TABLE IF NOT EXISTS " table-name
"(id INT NOT NULL PRIMARY KEY,
balance INT NOT NULL)")])
(j/execute! conn [(str "SELECT LUA('return box.space."
(clojure.string/upper-case table-name)
":alter{ is_sync = true } or 1')")])
(doseq [a (:accounts test)]
(info "Populating account")
(sql/insert! conn table-name
{:id a
:balance initial-balance-per-account}))))
(assoc this :conn conn :node node))))
(invoke! [this test op]
(try
(case (:f op)
:read (->> (sql/query conn [(str "SELECT * FROM " table-name)])
(map (juxt :ID :BALANCE))
(into (sorted-map))
(assoc op :type :ok, :value))
:transfer
(let [{:keys [from to amount]} (:value op)
con (cl/open (first (db/primaries test)) test)
table (clojure.string/upper-case table-name)
r (-> con
(sql/query [(str "SELECT _WITHDRAW('" table "'," from "," to "," amount ")")])
first
:COLUMN_1)]
(if (false? r)
(assoc op :type :fail, :value {:from from :to to :amount amount})
(assoc op :type :ok))))))
(teardown! [_ test]
(when-not (:leave-db-running? test)
(info "Drop table" table-name)
(cl/with-conn-failure-retry conn
(j/execute! conn [(str "DROP TABLE IF EXISTS " table-name)]))))
(close! [_ test]))
; One bank account per table
(defrecord MultiBankClientWithLua [conn tbl-created?]
client/Client
(open! [this test node]
(assoc this :conn (cl/open node test)))
(setup! [this test node]
(locking tbl-created?
(let [conn (cl/open node test)
; Distribute initial balances uniformly.
initial-balance-per-account (/ (:total-amount test)
(count (:accounts test)))]
(assert (integer? initial-balance-per-account)
err-unable-to-distribute-balances)
(Thread/sleep 10000) ; wait for leader election and joining to a cluster
(when (= node (first (db/primaries test)))
(when (compare-and-set! tbl-created? false true)
(cl/with-conn-failure-retry conn
(doseq [a (:accounts test)]
(info "Creating table" table-name a)
(j/execute! conn [(str "CREATE TABLE IF NOT EXISTS " table-name a
"(id INT NOT NULL PRIMARY KEY,"
"account_id INT NOT NULL,"
"balance INT NOT NULL)")])
(j/execute! conn [(str "SELECT LUA('return box.space."
(clojure.string/upper-case (str table-name a))
":alter{ is_sync = true } or 1')")])
(info "Populating account" a)
(sql/insert! conn (str table-name a)
{:id 0
:account_id a
:balance initial-balance-per-account})))))
(assoc this :conn conn :node node))))
(invoke! [this test op]
(try
; op struct example: {:type :invoke, :f :read, :process 0, :time 65183208864}
(case (:f op)
:read
(->> (sql/query conn ["SELECT ACCOUNT_ID, BALANCE FROM ACCOUNTS0 UNION
SELECT ACCOUNT_ID, BALANCE FROM ACCOUNTS1 UNION
SELECT ACCOUNT_ID, BALANCE FROM ACCOUNTS2 UNION
SELECT ACCOUNT_ID, BALANCE FROM ACCOUNTS3 UNION
SELECT ACCOUNT_ID, BALANCE FROM ACCOUNTS4 UNION
SELECT ACCOUNT_ID, BALANCE FROM ACCOUNTS5 UNION
SELECT ACCOUNT_ID, BALANCE FROM ACCOUNTS6 UNION
SELECT ACCOUNT_ID, BALANCE FROM ACCOUNTS7 UNION
SELECT ACCOUNT_ID, BALANCE FROM ACCOUNTS8 UNION
SELECT ACCOUNT_ID, BALANCE FROM ACCOUNTS9"])
(map (juxt :ACCOUNT_ID :BALANCE))
(into (sorted-map))
(assoc op :type :ok, :value))
:transfer
(let [{:keys [from to amount]} (:value op)
table_from (str (clojure.string/upper-case table-name) from)
table_to (str (clojure.string/upper-case table-name) to)
con (cl/open (first (db/primaries test)) test)
r (-> con
(sql/query [(str "SELECT _WITHDRAW_MULTITABLE('" table_from "','" table_to "'," amount ")")])
first
:COLUMN_1)]
(if (false? r)
(assoc op :type :fail)
(assoc op :type :ok))))))
(teardown! [_ test]
(when-not (:leave-db-running? test)
(cl/with-conn-failure-retry conn
(doseq [a (:accounts test)]
(info "Drop table" table-name a)
(j/execute! conn [(str "DROP TABLE IF EXISTS " table-name a)])))))
(close! [_ test]))
(defrecord BankClient [conn]
client/Client
(open! [this test node]
(let [conn (cl/open node test)]
(assoc this :conn conn :node node)))
(setup! [this test node]
(locking BankClient
(let [conn (cl/open node test)
; Distribute initial balances uniformly.
initial-balance-per-account (/ (:total-amount test)
(count (:accounts test)))]
(assert (integer? initial-balance-per-account)
err-unable-to-distribute-balances)
(Thread/sleep 10000) ; wait for leader election and joining to a cluster
(when (= node (first (db/primaries test)))
(cl/with-conn-failure-retry conn
(info "Creating table " table-name)
(j/execute! conn [(str "CREATE TABLE IF NOT EXISTS " table-name
"(id INT NOT NULL PRIMARY KEY,
balance INT NOT NULL)")])
(j/execute! conn [(str "SELECT LUA('return box.space."
(clojure.string/upper-case table-name)
":alter{ is_sync = true } or 1')")])
(doseq [a (:accounts test)]
(info "Populating account")
(sql/insert! conn table-name
{:id a
:balance initial-balance-per-account}))))
(assoc this :conn conn :node node))))
(invoke! [this test op]
(try
(case (:f op)
:read (->> (sql/query conn [(str "SELECT * FROM " table-name)])
(map (juxt :ID :BALANCE))
(into (sorted-map))
(assoc op :type :ok, :value))
:transfer
; TODO: with-transaction is not supported due to
; https://github.com/tarantool/tarantool/issues/2016
(let [{:keys [from to amount]} (:value op)
con (cl/open (first (db/primaries test)) test)
b1 (-> con
(sql/query [(str "SELECT * FROM " table-name " WHERE id = ? ") from])
first
:BALANCE
(- amount))
b2 (-> con
(sql/query [(str "SELECT * FROM " table-name " WHERE id = ? ") to])
first
:BALANCE
(+ amount))]
(cond (or (neg? b1) (neg? b2))
(assoc op :type :fail, :value {:from from :to to :amount amount})
true
(do (j/execute! con [(str "UPDATE " table-name " SET balance = balance - ? WHERE id = ?") amount from])
(j/execute! con [(str "UPDATE " table-name " SET balance = balance + ? WHERE id = ?") amount to])
(assoc op :type :ok)))))))
(teardown! [_ test]
(when-not (:leave-db-running? test)
(info "Drop table" table-name)
(cl/with-conn-failure-retry conn
(j/execute! conn [(str "DROP TABLE IF EXISTS " table-name)]))))
(close! [_ test]))
; One bank account per table
(defrecord MultiBankClient [conn tbl-created?]
client/Client
(open! [this test node]
(assoc this :conn (cl/open node test)))
(setup! [this test node]
(locking tbl-created?
(let [conn (cl/open node test)
; Distribute initial balances uniformly.
initial-balance-per-account (/ (:total-amount test)
(count (:accounts test)))]
(assert (integer? initial-balance-per-account)
err-unable-to-distribute-balances)
(Thread/sleep 10000) ; wait for leader election and joining to a cluster
(when (= node (first (db/primaries test)))
(when (compare-and-set! tbl-created? false true)
(cl/with-conn-failure-retry conn
(doseq [a (:accounts test)]
(info "Creating table" table-name a)
(j/execute! conn [(str "CREATE TABLE IF NOT EXISTS " table-name a
"(id INT NOT NULL PRIMARY KEY,"
"balance INT NOT NULL)")])
(j/execute! conn [(str "SELECT LUA('return box.space."
(clojure.string/upper-case (str table-name a))
":alter{ is_sync = true } or 1')")])
(info "Populating account" a)
(sql/insert! conn (str table-name a)
{:id 0
:balance initial-balance-per-account})))))
(assoc this :conn conn :node node))))
(invoke! [this test op]
(try
(case (:f op)
:read
(->> (:accounts test)
(map (fn [x]
[x (->> (sql/query conn [(str "SELECT balance FROM " table-name
x)]
{:row-fn :BALANCE})
first)]))
(into (sorted-map))
(map (fn [[k {b :BALANCE}]] [k b]))
(into {})
(assoc op :type :ok, :value))
:transfer
; TODO: with-transaction is not supported due to
; https://github.com/tarantool/tarantool/issues/2016
(let [{:keys [from to amount]} (:value op)
from (str table-name from)
to (str table-name to)
con (cl/open (first (db/primaries test)) test)
b1 (-> con
(sql/query [(str "SELECT balance FROM " from)])
first
:BALANCE
(- amount))
b2 (-> con
(sql/query [(str "SELECT balance FROM " to)])
first
:BALANCE
(+ amount))]
(cond (neg? b1)
(assoc op :type :fail, :error [:negative from b1])
(neg? b2)
(assoc op :type :fail, :error [:negative to b2])
true
(do (j/execute! con [(str "UPDATE " from " SET balance = balance - ? WHERE id = 0") amount])
(j/execute! con [(str "UPDATE " to " SET balance = balance + ? WHERE id = 0") amount])
(assoc op :type :ok)))))))
(teardown! [_ test]
(when-not (:leave-db-running? test)
(cl/with-conn-failure-retry conn
(doseq [a (:accounts test)]
(info "Drop table" table-name a)
(j/execute! conn [(str "DROP TABLE IF EXISTS " table-name a)])))))
(close! [_ test]))
(defn workload
[opts]
(assoc (bank/test opts)
:client (BankClient. nil)))
(defn multitable-workload
[opts]
(assoc (workload opts)
:client (MultiBankClient. nil (atom false))))
(defn workload-lua
[opts]
(assoc (workload opts)
:client (BankClientWithLua. nil)))
(defn multitable-workload-lua
[opts]
(assoc (workload opts)
:client (MultiBankClientWithLua. nil (atom false))))