Skip to content

Commit

Permalink
fix to catch exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
yito88 committed Jun 23, 2024
1 parent b3d1e66 commit 94a74ed
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 37 deletions.
38 changes: 18 additions & 20 deletions scalardb/src/scalardb/transfer.clj
Original file line number Diff line number Diff line change
Expand Up @@ -127,19 +127,16 @@
(assoc op :type :fail :error {:results results})))))

(defn- read-record
"Read a record with a transaction. If read fails, this function returns nil."
"Read a record with a transaction. If read fails, an exception is thrown."
[tx storage i]
(try
;; Need Storage API to read the transaction metadata
(let [tx-result (.get tx (prepare-get i))
result (.get storage (prepare-get i))]
;; Put the same balance to check conflicts with in-flight transactions
(->> (calc-new-balance tx-result 0)
(prepare-put i)
(.put tx))
result)
(catch CrudException _ nil)
(catch ExecutionException _ nil)))
;; Need Storage API to read the transaction metadata
(let [tx-result (.get tx (prepare-get i))
result (.get storage (prepare-get i))]
;; Put the same balance to check conflicts with in-flight transactions
(->> (calc-new-balance tx-result 0)
(prepare-put i)
(.put tx))
result))

(defn read-all-with-retry
[test n]
Expand All @@ -150,15 +147,16 @@
(scalar/prepare-transaction-service! test)
(scalar/prepare-storage-service! test))
test
(let [tx (scalar/start-transaction test)
results (doall (map #(read-record tx @(:storage test) %) (range n)))]
(try
(try
(let [tx (scalar/start-transaction test)
results (doall (map #(read-record tx @(:storage test) %)
(range n)))]
(.commit tx)
(if (some nil? results) nil results)
(catch Exception e
;; The transaction conflicted
(warn (.getMessage e))
nil)))))
results)
(catch Exception e
;; The transaction conflicted
(warn (.getMessage e))
nil))))

(defrecord TransferClient [initialized? n initial-balance max-txs]
client/Client
Expand Down
33 changes: 16 additions & 17 deletions scalardb/src/scalardb/transfer_append.clj
Original file line number Diff line number Diff line change
Expand Up @@ -129,30 +129,29 @@
:start-fail))

(defn- scan-records
"Scan records with a transaction. If the scan fails, an exception is thrown."
[tx id]
(try
(let [results (.scan tx (prepare-scan id))]
;; Put the same balance to check conflicts with in-flight transactions
(->> (prepare-put id
(-> results first calc-new-age)
(-> results first (calc-new-balance 0)))
(.put tx))
results)
(catch CrudException _ nil)))
(let [results (.scan tx (prepare-scan id))]
;; Put the same balance to check conflicts with in-flight transactions
(->> (prepare-put id
(-> results first calc-new-age)
(-> results first (calc-new-balance 0)))
(.put tx))
results))

(defn scan-all-records-with-retry
[test n]
(scalar/check-transaction-connection! test)
(scalar/with-retry scalar/prepare-transaction-service! test
(let [tx (scalar/start-transaction test)
results (doall (map #(scan-records tx %) (range n)))]
(try
(try
(let [tx (scalar/start-transaction test)
results (doall (map #(scan-records tx %) (range n)))]
(.commit tx)
(if (some nil? results) nil results)
(catch Exception e
;; The transaction conflicted
(warn (.getMessage e))
nil)))))
results)
(catch Exception e
;; The transaction conflicted
(warn (.getMessage e))
nil))))

(defrecord TransferClient [initialized? n initial-balance max-txs]
client/Client
Expand Down

0 comments on commit 94a74ed

Please sign in to comment.