Skip to content

bank*: distribute initial balances uniformly #99

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 24, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 42 additions & 19 deletions src/tarantool/bank.clj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
[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
Expand All @@ -27,7 +30,12 @@

(setup! [this test node]
(locking BankClientWithLua
(let [conn (cl/open node test)]
(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
Expand All @@ -37,10 +45,10 @@
balance INT NOT NULL)")])
(doseq [a (:accounts test)]
(info "Populating account")
(sql/insert! conn table-name {:id a
:balance (if (= a (first (:accounts test)))
(:total-amount test)
0)}))))
(sql/insert! conn table-name
{:id a
:balance initial-balance-per-account}))))

(assoc this :conn conn :node node))))

(invoke! [this test op]
Expand Down Expand Up @@ -79,7 +87,12 @@

(setup! [this test node]
(locking tbl-created?
(let [conn (cl/open node test)]
(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)
Expand All @@ -92,9 +105,10 @@
"balance INT NOT NULL)")])
(info "Populating account" a)
(sql/insert! conn (str table-name a)
{:id 0
:account_id a
:balance 10})))))
{:id 0
:account_id a
:balance initial-balance-per-account})))))

(assoc this :conn conn :node node))))

(invoke! [this test op]
Expand Down Expand Up @@ -147,7 +161,12 @@

(setup! [this test node]
(locking BankClient
(let [conn (cl/open node test)]
(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
Expand All @@ -157,10 +176,10 @@
balance INT NOT NULL)")])
(doseq [a (:accounts test)]
(info "Populating account")
(sql/insert! conn table-name {:id a
:balance (if (= a (first (:accounts test)))
(:total-amount test)
0)}))))
(sql/insert! conn table-name
{:id a
:balance initial-balance-per-account}))))

(assoc this :conn conn :node node))))

(invoke! [this test op]
Expand Down Expand Up @@ -209,7 +228,12 @@

(setup! [this test node]
(locking tbl-created?
(let [conn (cl/open node test)]
(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)
Expand All @@ -221,10 +245,9 @@
"balance INT NOT NULL)")])
(info "Populating account" a)
(sql/insert! conn (str table-name a)
{:id 0
:balance (if (= a (first (:accounts test)))
(:total-amount test)
0)})))))
{:id 0
:balance initial-balance-per-account})))))

(assoc this :conn conn :node node))))

(invoke! [this test op]
Expand Down