Add files for Paxos decision system.
This commit is contained in:
@@ -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)
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
class Learner():
|
||||||
|
pass
|
||||||
14
DecisionSystem/PaxosDecision/paxos_instance.py
Normal file
14
DecisionSystem/PaxosDecision/paxos_instance.py
Normal file
@@ -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
|
||||||
37
DecisionSystem/PaxosDecision/paxosmessenger.py
Normal file
37
DecisionSystem/PaxosDecision/paxosmessenger.py
Normal file
@@ -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
|
||||||
32
DecisionSystem/PaxosDecision/proposal.py
Normal file
32
DecisionSystem/PaxosDecision/proposal.py
Normal file
@@ -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")
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
class Proposer():
|
||||||
|
def propose(self):
|
||||||
|
pass
|
||||||
Reference in New Issue
Block a user