Last active
September 20, 2015 21:14
-
-
Save rystsov/44b25528e74bb617726d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Accepted = namedtuple('Accepted', ['n', 'state']) | |
class Acceptor: | |
def __init__(self, node_id, hdd): | |
self.node_id = node_id | |
if hdd.is_empty(): | |
hdd.write(promise=0, accepted=Accepted(0,None)) | |
# emit_* functions are used to record a event and capture a bit of context | |
# The proof is based on reasoning on the structure of the sequence of the event | |
# that the program can generate | |
emit_accepted(0, node_id=self.node_id, ts=now(), accepted_n=0, state=self.hdd.accepted.state) | |
self.hdd = hdd | |
@endpoint | |
@synchronized | |
def prepare(self, ballot_n): | |
if self.hdd.promise < ballot_n: | |
self.hdd.write(promise=ballot_n) | |
emit_promised(ballot_n, node_id=self.node_id, ts=now(), accepted_n=self.hdd.accepted.n) | |
return self.hdd.accepted | |
return Conflict(promise=self.hdd.promise) | |
@endpoint | |
@synchronized | |
def accept(self, ballot_n, state): | |
if self.hdd.promise == ballot_n: | |
# always true, but it simplifies the proof | |
# the ballot number monotonicity assert: | |
assert self.hdd.accepted.n < ballot_n | |
self.hdd.write(accepted=Accepted(ballot_n, state)) | |
emit_accepted(ballot_n, node_id=self.node_id, ts=now(), accepted_n=ballot_n, state=self.hdd.accepted.state) | |
return OK() | |
return Conflict(promise=self.hdd.promise) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment