From ba86d91ccb217cd585a8b00ab5b115b0ab9f706f Mon Sep 17 00:00:00 2001 From: Alexander Turenko Date: Sun, 14 Nov 2021 15:31:44 +0300 Subject: [PATCH] bank*: distribute initial balances uniformly Before this commit the initial distribution was the following: first account has `total-amount` balances, other 9 ones have zero balance. From time to time is appears that there are no attempts to transfer from the first account. It means that all transfers are marked as failed and the history analysis reports fail. Even when there are some successful transfers, there are many attempts to transfer from an account with zero balance: those transfers are useless in terms of tarantool testing. This commit changes the initial distribution to the uniform one and resolves the problems, which are described above. See the linked issue for details. Aside of the problematic bank-lua I changed other banking tests in the same way to unify the approach. How each test is changed: * bank-lua: [100 0 0 0 0 0 0 0 0 0] -> [10 10 10 10 10 10 10 10 10 10]. * bank-multitable-lua: the test already distributes initial balances uniformly, but hardcodes balance value. Now it is deduced from the `total-amount` parameter. * bank: same as bank-lua. * bank-multitable: same as bank-lua. 'bank' and 'bank-multitable' fixes are blind, because they're disabled. See #83. 'bank-lua' and 'bank-multitable-lua' are passed in a manual run. 'bank-lua' gives 2-3 failed transfers of ~40 ones. Before it was like 5-15 *successful* ones of 30-40. (I have no representative statistics, just looked over several runs in CI, but the numbers looks expected.) Fixes #94 --- src/tarantool/bank.clj | 61 +++++++++++++++++++++++++++++------------- 1 file changed, 42 insertions(+), 19 deletions(-) diff --git a/src/tarantool/bank.clj b/src/tarantool/bank.clj index 9257cb6..835ced2 100644 --- a/src/tarantool/bank.clj +++ b/src/tarantool/bank.clj @@ -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 @@ -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 @@ -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] @@ -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) @@ -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] @@ -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 @@ -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] @@ -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) @@ -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]