|
| 1 | +(ns tarantool.bank |
| 2 | + "Simulates transfers between bank accounts." |
| 3 | + (:require [clojure.tools.logging :refer [info warn]] |
| 4 | + [clojure.string :as str] |
| 5 | + [clojure.core.reducers :as r] |
| 6 | + [jepsen [cli :as cli] |
| 7 | + [client :as client] |
| 8 | + [checker :as checker] |
| 9 | + [control :as c] |
| 10 | + [generator :as gen]] |
| 11 | + [jepsen.tests.bank :as bank] |
| 12 | + [next.jdbc :as j] |
| 13 | + [next.jdbc.sql :as sql] |
| 14 | + [knossos.op :as op] |
| 15 | + [jepsen.checker.timeline :as timeline] |
| 16 | + [tarantool [client :as cl] |
| 17 | + [db :as db]])) |
| 18 | + |
| 19 | +(def table-name "accounts") |
| 20 | + |
| 21 | +(defrecord BankClient [conn] |
| 22 | + client/Client |
| 23 | + |
| 24 | + (open! [this test node] |
| 25 | + (let [conn (cl/open node test)] |
| 26 | + (assoc this :conn conn :node node))) |
| 27 | + |
| 28 | + (setup! [this test node] |
| 29 | + (locking BankClient |
| 30 | + (let [conn (cl/open node test)] |
| 31 | + (Thread/sleep 10000) ; wait for leader election and joining to a cluster |
| 32 | + (when (= node (first (db/primaries test))) |
| 33 | + (cl/with-conn-failure-retry conn |
| 34 | + (info (str "Creating table " table-name)) |
| 35 | + (j/execute! conn [(str "CREATE TABLE IF NOT EXISTS " table-name |
| 36 | + "(id INT NOT NULL PRIMARY KEY, |
| 37 | + balance INT NOT NULL)")]) |
| 38 | + (doseq [a (:accounts test)] |
| 39 | + (info "Populating account") |
| 40 | + (sql/insert! conn table-name {:id a |
| 41 | + :balance (if (= a (first (:accounts test))) |
| 42 | + (:total-amount test) |
| 43 | + 0)})))) |
| 44 | + (assoc this :conn conn :node node)))) |
| 45 | + |
| 46 | + (invoke! [this test op] |
| 47 | + ;(with-txn op [c conn] |
| 48 | + (try |
| 49 | + (case (:f op) |
| 50 | + :read (->> (sql/query conn [(str "SELECT * FROM " table-name)]) |
| 51 | + (map (juxt :ID :BALANCE)) |
| 52 | + (into (sorted-map)) |
| 53 | + (assoc op :type :ok, :value)) |
| 54 | + |
| 55 | + :transfer |
| 56 | + (let [{:keys [from to amount]} (:value op) |
| 57 | + con (cl/open (first (db/primaries test)) test) |
| 58 | + b1 (-> con |
| 59 | + (sql/query [(str "SELECT * FROM " table-name " WHERE id = ? ") from]) |
| 60 | + first |
| 61 | + :BALANCE |
| 62 | + (- amount)) |
| 63 | + b2 (-> con |
| 64 | + (sql/query [(str "SELECT * FROM " table-name " WHERE id = ? ") to]) |
| 65 | + first |
| 66 | + :BALANCE |
| 67 | + (+ amount))] |
| 68 | + (cond (or (neg? b1) (neg? b2)) |
| 69 | + (assoc op :type :fail, :value {:from from :to to :amount amount}) |
| 70 | + true |
| 71 | + ; TODO: with-transaction is not supported due to |
| 72 | + ; https://github.com/tarantool/tarantool/issues/2016 |
| 73 | + ;(cl/with-txn op [c con] |
| 74 | + (do (j/execute! con [(str "UPDATE " table-name " SET balance = balance - ? WHERE id = ?") amount from]) |
| 75 | + (j/execute! con [(str "UPDATE " table-name " SET balance = balance + ? WHERE id = ?") amount to]) |
| 76 | + (assoc op :type :ok))))))) |
| 77 | + |
| 78 | + (teardown! [_ test] |
| 79 | + (when-not (:leave-db-running? test) |
| 80 | + (info (str "Drop table" table-name)) |
| 81 | + (cl/with-conn-failure-retry conn |
| 82 | + (j/execute! conn [(str "DROP TABLE IF EXISTS " table-name)])))) |
| 83 | + |
| 84 | + (close! [_ test])) |
| 85 | + |
| 86 | +(defn workload |
| 87 | + [opts] |
| 88 | + (assoc (bank/test opts) |
| 89 | + :client (BankClient. nil))) |
| 90 | + |
| 91 | +; One bank account per table |
| 92 | +(defrecord MultiBankClient [conn tbl-created?] |
| 93 | + client/Client |
| 94 | + (open! [this test node] |
| 95 | + (assoc this :conn (cl/open node test))) |
| 96 | + |
| 97 | + (setup! [this test node] |
| 98 | + (locking tbl-created? |
| 99 | + (let [conn (cl/open node test)] |
| 100 | + (Thread/sleep 10000) ; wait for leader election and joining to a cluster |
| 101 | + (when (= node (first (db/primaries test))) |
| 102 | + (when (compare-and-set! tbl-created? false true) |
| 103 | + (cl/with-conn-failure-retry conn |
| 104 | + (doseq [a (:accounts test)] |
| 105 | + (info "Creating table" table-name a) |
| 106 | + (j/execute! conn [(str "CREATE TABLE IF NOT EXISTS " table-name a |
| 107 | + "(id INT NOT NULL PRIMARY KEY," |
| 108 | + "balance INT NOT NULL)")]) |
| 109 | + (info "Populating account" a) |
| 110 | + (sql/insert! conn (str table-name a) |
| 111 | + {:id 0 |
| 112 | + :balance (if (= a (first (:accounts test))) |
| 113 | + (:total-amount test) |
| 114 | + 0)}))))) |
| 115 | + (assoc this :conn conn :node node)))) |
| 116 | + |
| 117 | + (invoke! [this test op] |
| 118 | + ;(with-txn op [c conn] |
| 119 | + (try |
| 120 | + (case (:f op) |
| 121 | + :read |
| 122 | + (->> (:accounts test) |
| 123 | + (map (fn [x] |
| 124 | + [x (->> (sql/query conn [(str "SELECT balance FROM " table-name |
| 125 | + x)] |
| 126 | + {:row-fn :BALANCE}) |
| 127 | + first)])) |
| 128 | + (into (sorted-map)) |
| 129 | + (map (fn [[k {b :BALANCE}]] [k b])) |
| 130 | + (into {}) |
| 131 | + (assoc op :type :ok, :value)) |
| 132 | + |
| 133 | + :transfer |
| 134 | + (let [{:keys [from to amount]} (:value op) |
| 135 | + from (str table-name from) |
| 136 | + to (str table-name to) |
| 137 | + con (cl/open (first (db/primaries test)) test) |
| 138 | + b1 (-> con |
| 139 | + (sql/query [(str "SELECT balance FROM " from)]) |
| 140 | + first |
| 141 | + :BALANCE |
| 142 | + (- amount)) |
| 143 | + b2 (-> con |
| 144 | + (sql/query [(str "SELECT balance FROM " to)]) |
| 145 | + first |
| 146 | + :BALANCE |
| 147 | + (+ amount))] |
| 148 | + (cond (neg? b1) |
| 149 | + (assoc op :type :fail, :error [:negative from b1]) |
| 150 | + (neg? b2) |
| 151 | + (assoc op :type :fail, :error [:negative to b2]) |
| 152 | + true |
| 153 | + ; TODO: with-transaction is not supported due to |
| 154 | + ; https://github.com/tarantool/tarantool/issues/2016 |
| 155 | + ;(cl/with-txn op [c con] |
| 156 | + (do (j/execute! con [(str "UPDATE " from " SET balance = balance - ? WHERE id = 0") amount]) |
| 157 | + (j/execute! con [(str "UPDATE " to " SET balance = balance + ? WHERE id = 0") amount]) |
| 158 | + (assoc op :type :ok))))))) |
| 159 | + |
| 160 | + (teardown! [_ test] |
| 161 | + (when-not (:leave-db-running? test) |
| 162 | + (cl/with-conn-failure-retry conn |
| 163 | + (doseq [a (:accounts test)] |
| 164 | + (info "Drop table" table-name a) |
| 165 | + (j/execute! conn [(str "DROP TABLE IF EXISTS " table-name a)]))))) |
| 166 | + |
| 167 | + (close! [_ test])) |
| 168 | + |
| 169 | +(defn multitable-workload |
| 170 | + [opts] |
| 171 | + (assoc (workload opts) |
| 172 | + :client (MultiBankClient. nil (atom false)))) |
0 commit comments