-
Notifications
You must be signed in to change notification settings - Fork 0
Serialize runtime SQLite writes #16
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
368b6b6
Add serialized database writer
mjc 8abf353
Cover DbWriter execution modes
mjc 736ff59
Tune SQLite settings for serialized writes
mjc 1265549
Route service and webhook writes through DbWriter
mjc 0aef522
Serialize video mutation helpers
mjc 8f3d9e1
Serialize Media context mutations
mjc 7461a22
Tighten queue claim and preview queries
mjc 908c6b9
Cover cleanup and dashboard query regressions
mjc 9b44ed3
Stream sync file writes incrementally
mjc 976893d
Cover bounded incremental sync orchestration
mjc 45ccbca
Address PR feedback and CRF search race
mjc 1809065
Label DbWriter config and failure writes
mjc 5a322a2
Address remaining PR review comments
mjc 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| defmodule Reencodarr.DbWriter do | ||
| @moduledoc """ | ||
| Serializes runtime SQLite mutations through a single in-process writer. | ||
|
|
||
| Reads stay concurrent on the normal repo pool. Runtime writes should route | ||
| through this module so only one process is mutating SQLite at a time. | ||
| """ | ||
|
|
||
| use GenServer | ||
| require Logger | ||
|
|
||
| alias Reencodarr.Core.Retry | ||
| alias Reencodarr.Repo | ||
|
|
||
| @inline_envs [:test] | ||
| @default_call_timeout 60_000 | ||
| @default_max_attempts 3 | ||
| @default_backoff_ms 25 | ||
|
|
||
| def start_link(opts) do | ||
| GenServer.start_link(__MODULE__, opts, name: __MODULE__) | ||
| end | ||
|
|
||
| @spec run((-> result), keyword()) :: result when result: var | ||
| def run(fun, opts \\ []) when is_function(fun, 0) do | ||
| if inline_mode?(opts) or in_writer?() do | ||
| execute_inline(fun, opts) | ||
| else | ||
| __MODULE__ | ||
| |> GenServer.call( | ||
| {:run, fun, opts}, | ||
| Keyword.get(opts, :writer_timeout, @default_call_timeout) | ||
| ) | ||
| |> unwrap_result() | ||
| end | ||
| end | ||
|
|
||
| @spec transaction((-> result), keyword()) :: {:ok, result} | {:error, term()} when result: var | ||
| def transaction(fun, opts \\ []) when is_function(fun, 0) do | ||
| run(fn -> Repo.transaction(fun) end, opts) | ||
| end | ||
|
|
||
| @spec enqueue((-> any()), keyword()) :: :ok | ||
| def enqueue(fun, opts \\ []) when is_function(fun, 0) do | ||
| if inline_mode?(opts) or in_writer?() do | ||
| execute_enqueue(fun, opts) | ||
| :ok | ||
| else | ||
| GenServer.cast(__MODULE__, {:enqueue, fun, opts}) | ||
| end | ||
| end | ||
|
|
||
| @spec in_writer?() :: boolean() | ||
| def in_writer?, do: Process.get(:db_writer_active, false) == true | ||
|
|
||
| @impl true | ||
| def init(_opts) do | ||
| {:ok, :ok} | ||
| end | ||
|
|
||
| @impl true | ||
| def handle_call({:run, fun, opts}, _from, state) do | ||
| {:reply, execute(fun, opts), state} | ||
| end | ||
|
|
||
| @impl true | ||
| def handle_cast({:enqueue, fun, opts}, state) do | ||
| execute_enqueue(fun, opts) | ||
|
|
||
| {:noreply, state} | ||
| end | ||
|
mjc marked this conversation as resolved.
|
||
|
|
||
| defp inline_mode?(opts) do | ||
| Keyword.get(opts, :inline?, default_inline_mode?()) | ||
| end | ||
|
|
||
| defp default_inline_mode? do | ||
| Application.get_env(:reencodarr, :env) in @inline_envs | ||
| end | ||
|
|
||
| defp execute_inline(fun, opts) do | ||
| execute(fun, opts) | ||
| |> unwrap_result() | ||
| end | ||
|
|
||
| defp execute_enqueue(fun, opts) do | ||
| case execute(fun, opts) do | ||
| {:ok, _result} -> | ||
| :ok | ||
|
|
||
| {:raised, kind, reason, stacktrace} -> | ||
| log_async_failure(kind, reason, stacktrace, opts) | ||
| end | ||
| end | ||
|
|
||
| defp execute(fun, opts) do | ||
| retry_opts = [ | ||
| max_attempts: Keyword.get(opts, :max_attempts, @default_max_attempts), | ||
| base_backoff_ms: Keyword.get(opts, :base_backoff_ms, @default_backoff_ms), | ||
| label: Keyword.get(opts, :label) | ||
| ] | ||
|
|
||
| previous = Process.get(:db_writer_active) | ||
| Process.put(:db_writer_active, true) | ||
|
|
||
| try do | ||
| {:ok, Retry.retry_on_db_busy(fun, retry_opts)} | ||
| rescue | ||
| error -> | ||
| {:raised, :error, error, __STACKTRACE__} | ||
| catch | ||
| kind, reason -> | ||
| {:raised, kind, reason, __STACKTRACE__} | ||
| after | ||
| if previous == nil do | ||
| Process.delete(:db_writer_active) | ||
| else | ||
| Process.put(:db_writer_active, previous) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| defp unwrap_result({:ok, value}), do: value | ||
|
|
||
| defp unwrap_result({:raised, kind, reason, stacktrace}) do | ||
| :erlang.raise(kind, reason, stacktrace) | ||
| end | ||
|
|
||
| defp log_async_failure(kind, reason, stacktrace, opts) do | ||
| label = Keyword.get(opts, :label, :db_writer) | ||
|
|
||
| Logger.error(""" | ||
| DbWriter async task failed for #{inspect(label)} | ||
| #{Exception.format(kind, reason, stacktrace)} | ||
| """) | ||
| end | ||
| end | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.