Skip to content
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

Catch an exception in get-all #127

Merged
merged 2 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
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
42 changes: 19 additions & 23 deletions scalardb/src/scalardb/transfer.clj
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
Result)
(com.scalar.db.io IntValue
Key)
(com.scalar.db.exception.storage ExecutionException)
(com.scalar.db.exception.transaction CrudException
UnknownTransactionStatusException)))
(com.scalar.db.exception.transaction UnknownTransactionStatusException)))

(def ^:private ^:const TABLE "transfer")
(def ^:private ^:const ACCOUNT_ID "account_id")
Expand Down Expand Up @@ -127,19 +125,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 +145,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
36 changes: 17 additions & 19 deletions scalardb/src/scalardb/transfer_append.clj
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
Scan$Ordering
Scan$Ordering$Order
Result)
(com.scalar.db.exception.transaction CrudException
UnknownTransactionStatusException)
(com.scalar.db.exception.transaction UnknownTransactionStatusException)
(com.scalar.db.io IntValue
Key)))

Expand Down Expand Up @@ -129,30 +128,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
4 changes: 3 additions & 1 deletion scalardb/test/scalardb/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
(condp = column
"tx_id" (Optional/of (TextValue. column id))
"tx_created_at" (Optional/of (BigIntValue. column (long 1566376246)))
"tx_state" (Optional/of (IntValue. column (Integer/parseInt id)))))))
"tx_state" (Optional/of (IntValue. column (Integer/parseInt id)))
;; for the coordinator table
Copy link
Member Author

@yito88 yito88 Jun 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unrelated to the bug fix.
For The group commit feature in the latest ScalarDB.

"tx_child_ids" (Optional/empty)))))

(def mock-db
(reify
Expand Down
Loading