Skip to content

Commit

Permalink
add was_leader? metadata to oban.peer.election event
Browse files Browse the repository at this point in the history
  • Loading branch information
notslang committed Sep 16, 2024
1 parent 83d2fc1 commit e6cf9c1
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
4 changes: 2 additions & 2 deletions lib/oban/peers/global.ex
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,13 @@ defmodule Oban.Peers.Global do

@impl GenServer
def handle_info(:election, %State{} = state) do
meta = %{conf: state.conf, leader: state.leader?, peer: __MODULE__}
meta = %{conf: state.conf, leader: state.leader?, peer: __MODULE__, was_leader?: nil}

locked? =
:telemetry.span([:oban, :peer, :election], meta, fn ->
locked? = :global.set_lock(key(state), nodes(), 0)

{locked?, %{meta | leader: locked?}}
{locked?, %{meta | leader: locked?, was_leader?: meta.leader}}
end)

if locked?, do: notify_lock(state.conf)
Expand Down
6 changes: 3 additions & 3 deletions lib/oban/peers/postgres.ex
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ defmodule Oban.Peers.Postgres do

@impl GenServer
def handle_info(:election, %State{} = state) do
meta = %{conf: state.conf, leader: state.leader?, peer: __MODULE__}
meta = %{conf: state.conf, leader: state.leader?, peer: __MODULE__, was_leader?: nil}

state =
:telemetry.span([:oban, :peer, :election], meta, fn ->
Expand All @@ -106,14 +106,14 @@ defmodule Oban.Peers.Postgres do

case Repo.transaction(state.conf, fun, retry: 1) do
{:ok, state} ->
{state, %{meta | leader: state.leader?}}
{state, %{meta | leader: state.leader?, was_leader?: meta.leader}}

{:error, :rollback} ->
# The peer maintains its current `leader?` status on rollback—this may cause
# inconsistency if the leader encounters an error and multiple rollbacks happen in
# sequence. That tradeoff is acceptable because the situation is unlikely and less of
# an issue than crashing the peer.
{state, meta}
{state, %{meta | was_leader?: meta.leader}}
end
end)

Expand Down
3 changes: 2 additions & 1 deletion lib/oban/telemetry.ex
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,14 @@ defmodule Oban.Telemetry do
| event | measures | metadata |
| ------------ | -------------- | -------------------------------------------------------------- |
| `:start` | `:system_time` | `:conf`, `:leader`, `:peer`, |
| `:stop` | `:duration` | `:conf`, `:leader`, `:peer`, |
| `:stop` | `:duration` | `:conf`, `:leader`, `:peer`, `:was_leader`, |
| `:exception` | `:duration` | `:conf`, `:leader`, `:peer`, `:kind`, `:reason`, `:stacktrace` |
#### Metadata
* `:conf`, `:kind`, `:reason`, `:stacktrace` — see the explanation in notifier metadata above
* `:leader` — whether the peer is the current leader
* `:was_leader` — whether the peer was the leader before the election occurred
* `:peer` — the module used for peering
## Queue Shutdown Events
Expand Down

0 comments on commit e6cf9c1

Please sign in to comment.