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

Check if there is interest in using highest random weight distribution strategy #277

Open
ruslandoga opened this issue Nov 30, 2024 · 1 comment

Comments

@ruslandoga
Copy link

ruslandoga commented Nov 30, 2024

👋

I wonder if there is any interest in implementing the approach described in https://en.wikipedia.org/wiki/Rendezvous_hashing for one of the default Horde distribution strategies.

defmodule Horde.RendezvousDistribution do
  @moduledoc """
  Distributes processes to nodes uniformly using rendezvous hashing.

  Given the *same* set of members, it will always start the same process on the same node.
  """

  def choose_node(child_spec, members) do
    identifier = Map.drop(child_spec, [:id])
    
    case Enum.filter(members, &match?(%{status: :alive}, &1)) do
      [_ | _] = alive ->
        {:ok, Enum.max_by(alive, fn %{name: name} -> {:erlang.phash2({identifier, name}), name} end)}

      [] ->
        {:error, :no_alive_nodes}
    end
  end
  
  def has_quorum?(_members), do: true
end
@ruslandoga
Copy link
Author

ruslandoga commented Nov 30, 2024

I wonder if there is any interest [...]

If there is, I'd be happy to contribute! :)

Btw it's also much faster

Horde.RendezvousDistribution here is a naive implementation from the OP. It can be made much more performant.

child_spec = %{id: Worker, start: {Worker, :start_link, [[some: :options]]}}

members = [
  %{name: :alice, status: :alive},
  %{name: :bob, status: :alive},
  %{name: :eve, status: :alive}
]

:timer.tc(fn -> Enum.each(1..10000, fn _ -> Horde.RendezvousDistribution.choose_node(child_spec, members) end) end)
# {16997, :ok}

:timer.tc(fn -> Enum.each(1..10000, fn _ -> Horde.UniformDistribution.choose_node(child_spec, members) end) end)
# {829810, :ok}

@ruslandoga ruslandoga changed the title Check if there is interest in using Rendezvous distribution strategy Check if there is interest in using highest random weight distribution strategy Nov 30, 2024
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

No branches or pull requests

1 participant