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

feat(primer-api): run evaluation requests outside STM #1244

Merged
merged 1 commit into from
Apr 18, 2024
Merged

Conversation

dhess
Copy link
Member

@dhess dhess commented Apr 18, 2024

Prior to this change, we ran API-requested evaluations inside a transaction in STM (via QueryApp inside withSession'). However, in retrospect, this is (as far as I can determine) completely unnecessary:

  • Evaluations can take a relatively long time, and could theoretically be restarted if their transaction is aborted; e.g., due to a concurrent edit.

  • But we don't really care if an edit is made during an evaluation. All we need to do is guarantee that the App the evaluation is using is not modified during the evaluation.

We can guard against concurrent edits on the App used by an evaluation by atomically getting the App via STM, returning that app to the API handler for the evaluation request, and then simply running the evaluation on that (immutable) copy of the App outside of STM. (Note that we already do something similar in the availableActions and actionOptions API handlers, for example.)

Not only does this change avoid unnecessary evaluation restarts in the event of an aborted transaction, it also means that logging during evaluation is less likely to accumulate large amounts of memory due to the need to use runPureLog in STM, since we cannot run IO actions (i.e., log messages) in STM.

Speaking of which, the primary motivation for this change is to pave the way for using our new interpreter via the API. Due to its lazy implementation, we're forced to throw exceptions from IO when reporting errors or timeouts in the interpreter, and this would be incompatible with STM.

Prior to this change, we ran API-requested evaluations inside a
transaction in STM (via `QueryApp` inside `withSession'`). However, in
retrospect, this is (as far as I can determine) completely
unnecessary:

* Evaluations can take a relatively long time, and could theoretically
be restarted if their transaction is aborted; e.g., due to a
concurrent edit.

* But we don't really care if an edit is made during an evaluation.
All we need to do is guarantee that the `App` the evaluation is using
is not modified during the evaluation.

We can guard against concurrent edits on the `App` used by an
evaluation by atomically getting the `App` via STM, returning that app
to the API handler for the evaluation request, and then simply running
the evaluation on that (immutable) copy of the `App` outside of STM.
(Note that we already do something similar in the `availableActions`
and `actionOptions` API handlers, for example.)

Not only does this change avoid unnecessary evaluation restarts in the
event of an aborted transaction, it also means that logging during
evaluation is less likely to accumulate large amounts of memory due to
the need to use `runPureLog` in STM, since we cannot run IO
actions (i.e., log messages) in STM.

Speaking of which, the primary motivation for this change is to pave
the way for using our new interpreter via the API. Due to its lazy
implementation, we're forced to throw exceptions from IO when
reporting errors or timeouts in the interpreter, and this would be
incompatible with STM.

Signed-off-by: Drew Hess <src@drewhess.com>
@dhess dhess enabled auto-merge April 18, 2024 01:41
@dhess dhess added this pull request to the merge queue Apr 18, 2024
Merged via the queue into main with commit 17171c7 Apr 18, 2024
72 checks passed
@dhess dhess deleted the dhess/eval-opt branch April 18, 2024 02:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant