|
12 | 12 |
|
13 | 13 | (defn- tx-transfer |
14 | 14 | [tx1 tx2 from to amount] |
15 | | - (try |
16 | | - (infof "Transferring %d from %d to %d (tx: %s)" amount from to (.getId tx1)) |
17 | | - (let [^Result from-result |
18 | | - (t-append/scan-for-latest tx1 (t-append/prepare-scan-for-latest from)) |
19 | | - ^Result to-result |
20 | | - (t-append/scan-for-latest tx2 (t-append/prepare-scan-for-latest to))] |
21 | | - (infof "fromID: %d, the latest balance: %d, the latest age: %d (tx: %s)" from (t-append/get-balance from-result) (t-append/get-age from-result) (.getId tx1)) |
22 | | - (->> (t-append/prepare-put from |
23 | | - (t-append/calc-new-age from-result) |
24 | | - (t-append/calc-new-balance from-result |
25 | | - (- amount))) |
26 | | - (.put tx1)) |
27 | | - (infof "toID: %d, the latest balance: %d, the latest age: %d (tx: %s)" to (t-append/get-balance to-result) (t-append/get-age to-result) (.getId tx1)) |
28 | | - (->> (t-append/prepare-put to |
29 | | - (t-append/calc-new-age to-result) |
30 | | - (t-append/calc-new-balance to-result amount)) |
31 | | - (.put tx2))) |
32 | | - (scalar/prepare-validate-commit-txs [tx1 tx2]) |
33 | | - (catch UnknownTransactionStatusException e |
34 | | - (throw e)) |
35 | | - (catch Exception e |
36 | | - (scalar/rollback-txs [tx1 tx2]) |
37 | | - (throw e)))) |
| 15 | + (infof "Transferring %d from %d to %d (tx: %s)" amount from to (.getId tx1)) |
| 16 | + (let [^Result from-result |
| 17 | + (t-append/scan-for-latest tx1 (t-append/prepare-scan-for-latest from)) |
| 18 | + ^Result to-result |
| 19 | + (t-append/scan-for-latest tx2 (t-append/prepare-scan-for-latest to))] |
| 20 | + (infof "fromID: %d, the latest balance: %d, the latest age: %d (tx: %s)" from (t-append/get-balance from-result) (t-append/get-age from-result) (.getId tx1)) |
| 21 | + (->> (t-append/prepare-put from |
| 22 | + (t-append/calc-new-age from-result) |
| 23 | + (t-append/calc-new-balance from-result |
| 24 | + (- amount))) |
| 25 | + (.put tx1)) |
| 26 | + (infof "toID: %d, the latest balance: %d, the latest age: %d (tx: %s)" to (t-append/get-balance to-result) (t-append/get-age to-result) (.getId tx1)) |
| 27 | + (->> (t-append/prepare-put to |
| 28 | + (t-append/calc-new-age to-result) |
| 29 | + (t-append/calc-new-balance to-result amount)) |
| 30 | + (.put tx2))) |
| 31 | + (scalar/prepare-validate-commit-txs [tx1 tx2])) |
38 | 32 |
|
39 | 33 | (defn- try-tx-transfer |
40 | 34 | [test {:keys [from to amount]}] |
41 | 35 | (let [tx1 (try (scalar/start-2pc test) |
42 | 36 | (catch Exception e |
43 | 37 | (warn (.getMessage e)))) |
44 | | - tx2 (try (scalar/join-2pc test (.getId tx1)) |
45 | | - (catch Exception e |
46 | | - (warn (.getMessage e))))] |
47 | | - (try |
48 | | - (tx-transfer tx1 tx2 from to amount) |
49 | | - :commit |
50 | | - (catch UnknownTransactionStatusException _ |
51 | | - (swap! (:unknown-tx test) conj (.getId tx1)) |
52 | | - (warn "Unknown transaction: " (.getId tx1)) |
53 | | - :unknown-tx-status) |
54 | | - (catch Exception e |
55 | | - (warn (.getMessage e)) |
56 | | - :fail)))) |
| 38 | + tx2 (if tx1 |
| 39 | + (try (scalar/join-2pc test (.getId tx1)) |
| 40 | + (catch Exception e |
| 41 | + (warn (.getMessage e)))) |
| 42 | + nil)] |
| 43 | + (if (and tx1 tx2) |
| 44 | + (try |
| 45 | + (tx-transfer tx1 tx2 from to amount) |
| 46 | + :commit |
| 47 | + (catch UnknownTransactionStatusException _ |
| 48 | + (swap! (:unknown-tx test) conj (.getId tx1)) |
| 49 | + (warn "Unknown transaction: " (.getId tx1)) |
| 50 | + :unknown-tx-status) |
| 51 | + (catch Exception e |
| 52 | + (warn (.getMessage e)) |
| 53 | + (scalar/rollback-txs [tx1 tx2]) |
| 54 | + :fail)) |
| 55 | + (do |
| 56 | + (when tx1 (scalar/rollback-txs [tx1])) |
| 57 | + :start-fail)))) |
57 | 58 |
|
58 | 59 | (defrecord TransferClient [initialized? n initial-balance max-txs] |
59 | 60 | client/Client |
|
0 commit comments