Skip to content

Commit

Permalink
Fix wrong message handling in cluster pool
Browse files Browse the repository at this point in the history
Closes #323.
  • Loading branch information
whatyouhide committed Aug 17, 2023
1 parent de34b69 commit ac66313
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
26 changes: 15 additions & 11 deletions lib/xandra/cluster/pool.ex
Original file line number Diff line number Diff line change
Expand Up @@ -279,24 +279,28 @@ defmodule Xandra.Cluster.Pool do
{:keep_state_and_data, {:reply, from, connected_hosts}}
end

def handle_event(:info, {:host_up, %Host{} = host}, _state, %__MODULE__{} = data) do
data = update_in(data.load_balancing_state, &data.load_balancing_module.host_up(&1, host))

data =
update_in(data.peers[Host.to_peername(host)], fn
%{status: :down} = peer -> %{peer | status: :up}
peer -> peer
def handle_event(:info, {:host_up, address, port}, _state, %__MODULE__{} = data) do
# Set the host's status as :up if its state had been previously marked as :down.
{%Host{} = host, data} =
get_and_update_in(data.peers[{address, port}], fn
%{status: :down, host: host} = peer -> {host, %{peer | status: :up}}
%{host: host} = peer -> {host, peer}
end)

data = update_in(data.load_balancing_state, &data.load_balancing_module.host_up(&1, host))

data = maybe_start_pools(data)
{:keep_state, data}
end

def handle_event(:info, {:host_down, %Host{} = host}, _state, %__MODULE__{} = data) do
data =
update_in(data.load_balancing_state, &data.load_balancing_module.host_down(&1, host))
def handle_event(:info, {:host_down, address, port}, _state, %__MODULE__{} = data) do
# Set the host's status as :down, regardless of its current state.
{%Host{} = host, data} =
get_and_update_in(data.peers[{address, port}], fn %{host: host} = peer ->
{host, %{peer | status: :down}}
end)

data = put_in(data.peers[Host.to_peername(host)].status, :down)
data = update_in(data.load_balancing_state, &data.load_balancing_module.host_down(&1, host))

data = stop_pool(data, host)
data = maybe_start_pools(data)
Expand Down
4 changes: 2 additions & 2 deletions test/xandra/cluster/pool_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ defmodule Xandra.Cluster.PoolTest do
assert_receive {[:xandra, :cluster, :pool, :started], ^telemetry_ref, %{}, %{}}

# Send the DOWN event.
send(pid, {:host_down, host})
send(pid, {:host_down, host.address, host.port})

assert_receive {[:xandra, :cluster, :pool, :stopped], ^telemetry_ref, %{}, meta}
assert %Host{address: {127, 0, 0, 1}, port: @port} = meta.host
Expand All @@ -205,7 +205,7 @@ defmodule Xandra.Cluster.PoolTest do
get_state(pid).peers[Host.to_peername(host)]

# Send the UP event.
send(pid, {:host_up, host})
send(pid, {:host_up, host.address, host.port})

assert_receive {[:xandra, :cluster, :pool, :restarted], ^telemetry_ref, %{}, meta}
assert %Host{address: {127, 0, 0, 1}, port: @port} = meta.host
Expand Down

0 comments on commit ac66313

Please sign in to comment.