From 2abdf7bc1ebb0f943b3e74fd9eab5011e0e3af6a Mon Sep 17 00:00:00 2001 From: "DSTO\\pivatom" Date: Thu, 10 Jan 2019 14:54:53 +1030 Subject: [PATCH] Add files for Paxos decision system. --- DecisionSystem/PaxosDecision/acceptor.py | 26 +++++++++++++ DecisionSystem/PaxosDecision/learner.py | 2 + .../PaxosDecision/paxos_instance.py | 14 +++++++ .../PaxosDecision/paxosmessenger.py | 37 +++++++++++++++++++ DecisionSystem/PaxosDecision/proposal.py | 32 ++++++++++++++++ DecisionSystem/PaxosDecision/proposer.py | 3 ++ 6 files changed, 114 insertions(+) create mode 100644 DecisionSystem/PaxosDecision/paxos_instance.py create mode 100644 DecisionSystem/PaxosDecision/paxosmessenger.py create mode 100644 DecisionSystem/PaxosDecision/proposal.py diff --git a/DecisionSystem/PaxosDecision/acceptor.py b/DecisionSystem/PaxosDecision/acceptor.py index e69de29..a9cbe0e 100644 --- a/DecisionSystem/PaxosDecision/acceptor.py +++ b/DecisionSystem/PaxosDecision/acceptor.py @@ -0,0 +1,26 @@ +from proposer import Proposer +from paxosmessenger import Messenger + +class Acceptor(): + highest_proposal = None + highest_promise = None + messenger = None + + def __init__(self, messenger: Messenger): + self.messenger = messenger + + def on_message(self, client, userdata, message): + pass + + def prepare(self): + pass + + def accept_request(self, fromt_uid, proposal_id, value): + ''' + Called when an accept message is received from a proposer + ''' + if proposal_id >= self.highest_promise: + self.highest_promise = proposal_id + self.accepted_id = proposal_id + self.accepted_value = value + self.messenger.send_accepted(proposal_id, self.accepted_value) \ No newline at end of file diff --git a/DecisionSystem/PaxosDecision/learner.py b/DecisionSystem/PaxosDecision/learner.py index e69de29..37955b4 100644 --- a/DecisionSystem/PaxosDecision/learner.py +++ b/DecisionSystem/PaxosDecision/learner.py @@ -0,0 +1,2 @@ +class Learner(): + pass \ No newline at end of file diff --git a/DecisionSystem/PaxosDecision/paxos_instance.py b/DecisionSystem/PaxosDecision/paxos_instance.py new file mode 100644 index 0000000..41d8609 --- /dev/null +++ b/DecisionSystem/PaxosDecision/paxos_instance.py @@ -0,0 +1,14 @@ +''' +@author Michael Pivato + +Much thanks to Tom Cocagne by providing basic paxos code +which was the basis of this module and algorithm. +Check out the original at: https://github.com/cocagne/paxos/blob/master/paxos/essential.py +''' + +from learner import Learner +from acceptor import Acceptor +from proposer import Proposer + +class PaxosInstance(): + pass \ No newline at end of file diff --git a/DecisionSystem/PaxosDecision/paxosmessenger.py b/DecisionSystem/PaxosDecision/paxosmessenger.py new file mode 100644 index 0000000..47e38a6 --- /dev/null +++ b/DecisionSystem/PaxosDecision/paxosmessenger.py @@ -0,0 +1,37 @@ +from proposal import Proposal + +class Messenger(): + def send_prepare(self, proposal_id): + ''' + Broadcasts a Prepare message to all Acceptors + ''' + + NotImplementedError + + def send_promise(self, proposer_id, proposal_id, previous_proposal): + ''' + Sends a Promise message to the specified Proposer + ''' + + NotImplementedError + + def send_accept(self, proposal): + ''' + Broadcasts an Accept message to all Acceptors + ''' + + NotImplementedError + + def send_accepted(self, proposal): + ''' + Broadcasts an Accepted message to all Learners + ''' + + NotImplementedError + + def on_resolution(self, proposal_id, value): + ''' + Called when a resolution is reached + ''' + + NotImplementedError \ No newline at end of file diff --git a/DecisionSystem/PaxosDecision/proposal.py b/DecisionSystem/PaxosDecision/proposal.py new file mode 100644 index 0000000..ef1424a --- /dev/null +++ b/DecisionSystem/PaxosDecision/proposal.py @@ -0,0 +1,32 @@ +from Messaging.messaginginterface import Message +import umsgpack + +# Uses MessagePack for message transfer. + +class Proposal(Message): + ballot_number = None + vote = None + + def __init__(self): + pass + + def serialise(self) -> str: + return umsgpack.packb({"ballot": self.ballot_number, "vote": self.vote}) + + def deserialise(self, binary: bytearray): + ''' + Deserialises a given proposal message into a proposal object. + ''' + obj = umsgpack.unpackb(binary) + old_ballot = self.ballot_number + old_vote = self.vote + try: + self.ballot_number = obj['ballot'] + self.vote = obj['vote'] + except: + # Reset the values in case they have been changed. + self.ballot_number = old_ballot + self.vote = old_vote + print("Error: binary object is not a proposal") + + \ No newline at end of file diff --git a/DecisionSystem/PaxosDecision/proposer.py b/DecisionSystem/PaxosDecision/proposer.py index e69de29..2bb19b8 100644 --- a/DecisionSystem/PaxosDecision/proposer.py +++ b/DecisionSystem/PaxosDecision/proposer.py @@ -0,0 +1,3 @@ +class Proposer(): + def propose(self): + pass \ No newline at end of file