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

Rename "replication lag" to "retained WAL size" #2056

Merged
merged 1 commit into from
Nov 29, 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
34 changes: 20 additions & 14 deletions packages/sync-service/lib/electric/connection/manager.ex
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ defmodule Electric.Connection.Manager do
GenServer.cast(server, {:pg_info_looked_up, pg_info})
end

def query_replication_lag(server) do
GenServer.call(server, :query_replication_lag)
def report_retained_wal_size(server) do
GenServer.call(server, :report_retained_wal_size)
end

@impl true
Expand Down Expand Up @@ -231,8 +231,12 @@ defmodule Electric.Connection.Manager do
{:reply, :ok, %{state | drop_slot_requested: true}}
end

def handle_call(:query_replication_lag, _from, state) do
report_replication_lag(state)
def handle_call(:report_retained_wal_size, _from, state) do
if state.monitoring_started? do
slot_name = Keyword.fetch!(state.replication_opts, :slot_name)
query_and_report_retained_wal_size(state.pool_pid, slot_name)
end

{:reply, :ok, state}
end

Expand Down Expand Up @@ -650,20 +654,22 @@ defmodule Electric.Connection.Manager do
end
end

defp report_replication_lag(%{monitoring_started?: false}), do: :ok

defp report_replication_lag(%{pool_pid: pool} = state) do
slot_name = Keyword.fetch!(state.replication_opts, :slot_name)

query =
"SELECT pg_wal_lsn_diff(pg_current_wal_lsn(), confirmed_flush_lsn) AS replication_lag_size FROM pg_replication_slots WHERE slot_name = $1;"
defp query_and_report_retained_wal_size(pool, slot_name) do
query = """
SELECT
pg_wal_lsn_diff(pg_current_wal_lsn(), confirmed_flush_lsn)
FROM
pg_replication_slots
WHERE
slot_name = $1
"""

case Postgrex.query(pool, query, [slot_name]) do
{:ok, %Postgrex.Result{rows: [[lag]]}} ->
:telemetry.execute([:electric, :postgres, :replication], %{lag: lag})
{:ok, %Postgrex.Result{rows: [[wal_size]]}} ->
:telemetry.execute([:electric, :postgres, :replication], %{wal_size: wal_size})

{:error, error} ->
Logger.warning("Failed to query replication lag\nError: #{inspect(error)}")
Logger.warning("Failed to query retained WAL size\nError: #{inspect(error)}")
end

:ok
Expand Down
2 changes: 1 addition & 1 deletion packages/sync-service/lib/electric/telemetry.ex
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ defmodule Electric.Telemetry do
[
# A module, function and arguments to be invoked periodically.
{__MODULE__, :uptime_event, []},
{Electric.Connection.Manager, :query_replication_lag,
{Electric.Connection.Manager, :report_retained_wal_size,
[Electric.Connection.Manager.name(stack_id)]}
]
end
Expand Down
Loading