Skip to content

Instantly share code, notes, and snippets.

@potatosalad
Created January 14, 2018 17:50
Show Gist options
  • Save potatosalad/a1eda4ddaa84a68f86f24d65301360b4 to your computer and use it in GitHub Desktop.
Save potatosalad/a1eda4ddaa84a68f86f24d65301360b4 to your computer and use it in GitHub Desktop.

There's something out of whack with the consistent hash algorithm used by swarm (libring):

Add 4 nodes all weighted equally (should be 4 x 25%) libring

iex> ring = HashRing.new()
iex> ring = HashRing.add_node(ring, :a@nohost)
iex> ring = HashRing.add_node(ring, :b@nohost)
iex> ring = HashRing.add_node(ring, :c@nohost)
iex> ring = HashRing.add_node(ring, :d@nohost)
iex> HashRing.balance_check(ring, 1000000)
{:ok, {1000000, 280936000, 280.936},
 [{:a@nohost, 268608, 0.268608}, {:b@nohost, 295090, 0.29509},
  {:c@nohost, 221535, 0.221535}, {:d@nohost, 214767, 0.214767}]}
Node Expected Actual Error
:a@nohost 25% 26.86% +7.44%
:b@nohost 25% 29.51% +18.04%
:c@nohost 25% 22.15% -11.39%
:d@nohost 25% 21.48% -14.09%

Add 4 nodes all weighted equally (should be 4 x 25%) ryng

iex> {:ok, _} = :ryng.new_ring([id: :test])
iex> :ryng.add_node(:test, :a@nohost)
iex> :ryng.add_node(:test, :b@nohost)
iex> :ryng.add_node(:test, :c@nohost)
iex> :ryng.add_node(:test, :d@nohost)
iex> :ryng.balance_check(:test, 1000000)
{:ok, {1000000, 1317824000, 1317.824},
 [{:a@nohost, 249886, 0.249886}, {:b@nohost, 250168, 0.250168},
  {:c@nohost, 250055, 0.250055}, {:d@nohost, 249891, 0.249891}]}
Node Expected Actual Error
:a@nohost 25% 24.99% -0.05%
:b@nohost 25% 25.02% +0.07%
:c@nohost 25% 25.01% -0.02%
:d@nohost 25% 24.99% -0.04%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment