-
Notifications
You must be signed in to change notification settings - Fork 191
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
Protect datalog cache from early cancellation #657
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d71c7f3
don't cancel if someone else is waiting
dwwoelfel c770d78
lint
dwwoelfel da7a771
cleanup
dwwoelfel 2c4ba19
timeouts in tests
dwwoelfel 5e31a24
fix issue with future that never got started
dwwoelfel b19de70
cleanup
dwwoelfel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,10 +17,15 @@ | |
(:require | ||
[clojure.string :as string] | ||
[datascript.core :as d] | ||
[instant.util.coll :as ucoll] | ||
[instant.jdbc.sql :as sql] | ||
[instant.lib.ring.websocket :as ws] | ||
[instant.util.tracer :as tracer] | ||
[instant.util.exception :as ex])) | ||
[instant.util.async :as ua] | ||
[instant.util.coll :as ucoll] | ||
[instant.util.exception :as ex] | ||
[instant.util.tracer :as tracer]) | ||
(:import | ||
(java.lang InterruptedException) | ||
(java.util.concurrent CancellationException))) | ||
|
||
(declare store-conn) | ||
|
||
|
@@ -342,23 +347,110 @@ | |
;; ------ | ||
;; datalog cache | ||
|
||
(defn swap-datalog-cache-delay! [conn app-id datalog-query delayed-call] | ||
(defn swap-datalog-cache! [conn app-id datalog-query-fn ctx datalog-query] | ||
(let [lookup-ref [:datalog-query/app-id+query [app-id datalog-query]] | ||
|
||
watcher-id (Object.) | ||
this-result-delay (atom { ;; Promise holds the result of the query | ||
:promise (promise) | ||
;; Watchers keep track of who started listening | ||
;; while the query was running, so that we can | ||
;; safely cancel the query if all listeners cancel | ||
:watchers #{watcher-id} | ||
:cancel-signal (promise) | ||
:aborted? false}) | ||
{:keys [db-after]} | ||
(transact! "store/swap-datalog-cache-delay!" | ||
(transact! "store/swap-datalog-cache!" | ||
conn | ||
[[:db.fn/call (fn [db] | ||
(if-let [existing (d/entity db lookup-ref)] | ||
(when-not (:datalog-query/delayed-call existing) | ||
[[:db/add | ||
(:db/id existing) | ||
:datalog-query/delayed-call delayed-call]]) | ||
[{:datalog-query/app-id app-id | ||
:datalog-query/query datalog-query | ||
:datalog-query/delayed-call delayed-call}]))]])] | ||
|
||
(:datalog-query/delayed-call (d/entity db-after lookup-ref)))) | ||
[[:db.fn/call | ||
(fn [db] | ||
(if-let [existing (d/entity db lookup-ref)] | ||
(if (not (:datalog-query/delayed-call existing)) | ||
[[:db/add | ||
(:db/id existing) | ||
:datalog-query/delayed-call this-result-delay]] | ||
(let [{:keys [watchers]} | ||
(swap! (:datalog-query/delayed-call existing) | ||
(fn [state] | ||
(if (:aborted? state) | ||
state | ||
(update state :watchers conj watcher-id))))] | ||
(when-not (contains? watchers watcher-id) | ||
[[:db/add | ||
(:db/id existing) | ||
:datalog-query/delayed-call this-result-delay]]))) | ||
[{:datalog-query/app-id app-id | ||
:datalog-query/query datalog-query | ||
:datalog-query/delayed-call this-result-delay}]))]]) | ||
result-delay (:datalog-query/delayed-call (d/entity db-after lookup-ref)) | ||
unwrap-result (fn [] | ||
(let [res @(:promise @result-delay)] | ||
(assert (:result res) "Missing result") | ||
(if (:ok res) | ||
(:result res) | ||
(throw (:result res))))) | ||
|
||
cancel! (fn [] | ||
(tracer/with-span! {:name "store/datalog-query-cancel!"} | ||
(deliver (:cancel-signal @result-delay) true)))] | ||
|
||
(tracer/add-data! {:attributes {:cache-hit (not= this-result-delay result-delay) | ||
:realized (realized? (:promise @result-delay))}}) | ||
|
||
(when (= this-result-delay result-delay) | ||
;; We added it, so we must execute it | ||
(let [stmt-tracker (sql/make-top-level-statement-tracker) | ||
result-promise (:promise @result-delay) | ||
work-fut (binding [ua/*child-vfutures* nil ;; Move future to a new "call-stack" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Man, bind + statement-tracker makes for a really powerful abstraction. |
||
;; Don't let our statements get canceled | ||
sql/*in-progress-stmts* stmt-tracker] | ||
(ua/vfuture | ||
(try | ||
(deliver result-promise | ||
{:ok true | ||
:result (datalog-query-fn ctx | ||
datalog-query)}) | ||
(catch Throwable t | ||
(deliver result-promise | ||
{:ok false | ||
:result t})) | ||
(finally | ||
;; noop if we already delivered | ||
(deliver result-promise | ||
{:ok false | ||
:result | ||
(Exception. "Did not deliver promise!")}) | ||
(deliver (:cancel-signal @result-delay) | ||
false))))) | ||
_cancel-fut (binding [ua/*child-vfutures* nil] | ||
(ua/vfuture | ||
(when @(:cancel-signal @result-delay) | ||
(sql/cancel-in-progress stmt-tracker) | ||
(future-cancel work-fut))))])) | ||
(try | ||
(if (realized? (:promise @result-delay)) | ||
;; The work is already done, so we don't need to listen for cancellation | ||
(unwrap-result) | ||
;; Start a tracked future to watch for cancelation | ||
(let [wait-fut (ua/vfuture (unwrap-result))] | ||
(try | ||
@wait-fut | ||
(catch Throwable t | ||
(when (and (not (realized? (:promise @result-delay))) | ||
(or (instance? InterruptedException t) | ||
(instance? CancellationException t))) | ||
(let [{:keys [aborted?]} | ||
(swap! result-delay | ||
(fn [{:keys [watchers] :as state}] | ||
(let [new-watchers (disj watchers watcher-id)] | ||
(cond-> state | ||
true (assoc :watchers new-watchers) | ||
(empty? new-watchers) (assoc :aborted? true)))))] | ||
|
||
(when aborted? | ||
(cancel!)))) | ||
(throw t))))) | ||
(finally | ||
(swap! result-delay update :watchers disj watcher-id))))) | ||
|
||
;; -------------- | ||
;; datalog loader | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤯 I salute you sir.