concept takeover in category elixir

This is an excerpt from Manning's book The Little Elixir & OTP Guidebook.
This chapter and the next will be the most fun chapters (I say that about every chapter). In this chapter, we’ll explore the distribution capabilities of the Erlang VM. You’ll learn about the distribution primitives that let you create a cluster of nodes and spawn processes remotely. The next chapter will explore failover and takeover in a distributed system.
The next concept we’ll explore is fault tolerance with respect to distribution. For this, you’ll build an application that will demonstrate how a cluster handles failures by having another node automatically stepping up to take the place of a downed node. To take things further, it will also demonstrate how a node yields control when a previously downed node of higher priority rejoins the cluster. In other words, you’ll build an application that demonstrates the failover and takeover capabilities of distributed Elixir.
Listing 9.2. Implementing the Application behavior (lib/chucky.ex)
defmodule Chucky do use Application require Logger def start(type, _args) do import Supervisor.Spec children = [ worker(Chucky.Server, []) ] case type do :normal -> Logger.info("Application is started on #{node}") {:takeover, old_node} -> Logger.info("#{node} is taking over #{old_node}") {:failover, old_node} -> Logger.info("#{old_node} is failing over to #{node}") end opts = [strategy: :one_for_one, name: {:global, Chucky.Supervisor}] Supervisor.start_link(children, opts) end def fact do Chucky.Server.fact end end
So far, these have been failover situations. Now, let’s consider what happens when a restarts. Because a is the master node, it has the highest priority among all the nodes. Therefore, it initiates a takeover (see figure 9.4).
Whichever slave node is running, the application exits and yields control to the master node. How awesome is that? Next, let’s walk through the steps to configure your distributed application for failover and takeover.