forked from AlloyTools/org.alloytools.alloy
-
Notifications
You must be signed in to change notification settings - Fork 9
Leader election
Alcino Cunha edited this page Oct 10, 2019
·
10 revisions
Consider the following partial model of the ring leader election protocol.
open util/ordering[Id]
sig Node {
id : one Id,
succ : one Node,
var inbox : set Id,
var outbox : set Id
}
sig Id {}
fact ring {
all i : Id | lone id.i
all n : Node | Node in n.^succ
}
fun elected : set Node {
{n : Node | once (n.id in n.inbox)}
}
pred send [n : Node] { ... }
pred compute [n : Node] {
some i : n.inbox {
n.inbox' = n.inbox - i
n.outbox' = n.outbox + (i - n.id.*(~next))
}
all m : Node - n | m.inbox' = m.inbox
all m : Node - n | m.outbox' = m.outbox
}
pred skip {
inbox' = inbox
outbox' = outbox
}
fact init {
no inbox
outbox = id
}
fact transitions {
always (skip or some n : Node | send[n] or compute[n])
}
assert safety {
always lone elected
}
check safety
pred sendEnabled [n : Node] { some n.outbox }
pred computeEnabled [n : Node] { some n.inbox }
pred fairness {
all n : Node {
(eventually always sendEnabled[n] implies (always eventually send[n]))
(eventually always computeEnabled[n] implies (always eventually compute[n]))
}
}
assert liveness {
fairness and some Node implies eventually some elected
}
check liveness
- Finish the model by defining the predicate
send
. - Using the simulator and an empty
run
command, show that the following scenarios are possible:- Starting with a 3 node ring with ids in increasing order, it is possible to reach a state where one node of them is elected in 5 steps.
- Starting with a 2 node ring, it is possible to reach a state where one of them is elected and all the mailboxes are empty.
- Show how the previous scenarios can be directly generated with a
run
command. - Fix the specification of the
safety
assertion to ensure that it is not possible for two different processes to be elected at different times. - This model can be simplified by removing the
Id
signature and directly impose the total order over the signatureNode
. Change the model to reflect this simplification. - Use auxiliary functions to visualise which events are occurring at each time.
- The model can be made more abstract by removing the
outbox
and by merging thecompute
andsend
operations. Change the model to reflect this simplification.