Skip to content

Instantly share code, notes, and snippets.

@ukazap
Last active September 27, 2022 06:28
Show Gist options
  • Save ukazap/eeb89b989e62909086e276c7c47bdf3b to your computer and use it in GitHub Desktop.
Save ukazap/eeb89b989e62909086e276c7c47bdf3b to your computer and use it in GitHub Desktop.
riak_core cluster formation

riak_core cluster formation

Set up libcluster

Use libcluster to set up cluster automatically.

Add dependency in mix.exs and run mix deps.get:

defp deps do
  [
    # ...
    {:libcluster, "~> 3.3"}
    # ...
  ]
end

Add Cluster.Supervisor process in the supervision tree of the app (usually in lib/appname/application.ex):

defmodule MyApp.App do
  use Application

  def start(_type, _args) do
    topologies = [
      local_udp_gossip_aja: [
        strategy: Cluster.Strategy.Gossip,
        config: [
          secret: "this_is_a_shared_secret"
        ]
      ]
    ]

    children = [
      {Cluster.Supervisor, [topologies, [name: MyApp.ClusterSupervisor]]},
      # ..other children..
    ]
    Supervisor.start_link(children, strategy: :one_for_one, name: MyApp.Supervisor)
  end
end

Running

Run 3 nodes:

MIX_ENV=node1 iex --name [email protected] --cookie this_is_a_shared_secret -S mix
MIX_ENV=node2 iex --name [email protected] --cookie this_is_a_shared_secret -S mix
MIX_ENV=node3 iex --name [email protected] --cookie this_is_a_shared_secret -S mix

On node2 and node3 run:

:riak_core.join('[email protected]')

On node1 run:

# To see the planned changes in the ring:
:riak_core_claimant.plan()

# Now we can commit the plan:
:riak_core_claimant.commit()

# Periodically run some of these:

:riak_core_console.member_status([])

:riak_core_handoff_manager.status()

:riak_core_console.transfers([])

{:ok, ring} = :riak_core_ring_manager.get_my_ring()
:riak_core_ring.pretty_print(ring, [:legend])

:riak_core_status.ringready()
@ukazap
Copy link
Author

ukazap commented Sep 27, 2022

Todo: automatically admit new node to riak core cluster using something like Horde's NodeListener

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment