Skip to content
Snippets Groups Projects
Commit 588d458f authored by Mengqi Chen's avatar Mengqi Chen
Browse files

simple testing

parent 206ff405
Branches testing
No related tags found
No related merge requests found
from assignment2.paxos import Address
from paxos_utils import Address
from typing import Dict
class LockManager:
......
from typing import Dict
from assignment2.paxos import Address, BallotValuePair, BallotNumber
from paxos_utils import Address, BallotValuePair, BallotNumber
class Message:
......
# http://pymotw.com/2/SocketServer/
import sys
from functools import total_ordering
from typing import Set, Tuple, DefaultDict, List
from paxos_utils import *
from assignment2.timers import P2ATimer, setTimer, HeartBeatCheckTimer, HeartBeatTimer, LeaderElectionTimer
from lockmanager import LockManager
from timers import P2ATimer, setTimer, HeartBeatCheckTimer, HeartBeatTimer, LeaderElectionTimer
from message import *
import socketserver
import pickle
import socket
from collections import defaultdict
Address = Tuple[str, int]
# Paxos servers
class Paxos(socketserver.TCPServer):
......@@ -36,6 +36,9 @@ class Paxos(socketserver.TCPServer):
# for leader election
self.voters: Set[Address] = set() # Yes votes for leader election, set of addresses
self.p1b_replies: Dict[int, BallotValuePair] = dict() # Accepted values for each slot by acceptors that voted yes, slot number -> proposal
# lock manager app
self.lock_manager: LockManager = LockManager()
# ...
# Default leader during setup
......@@ -112,6 +115,7 @@ class PaxosHandler(socketserver.BaseRequestHandler):
print("Handling paxos request")
# TODO: add lockManager app here, translate below java code (No need to check already executed, etc, for AMO)
#if (app.alreadyExecuted(m.value())) {
#AMOResult r = app.execute(m.value());
#if (r != null) {
......@@ -146,7 +150,7 @@ class PaxosHandler(socketserver.BaseRequestHandler):
#requestInProcess.add(p2a.value()); No need, this is for AMO
# TODO: TIMERS: set(new P2ATimer(p2a), P2ATimer.P2A_RETRY_MILLIS);
setTimer(P2ATimer(p2a), P2ATimer.P2A_RETRY_MILLIS)
#setTimer(P2ATimer(p2a), P2ATimer.P2A_RETRY_MILLIS)
if len(self.server.slot_to_acceptors[p2a.slot_num]) > (len(self.server.servers)/2):
#Majority accepted, can put into log
......@@ -156,7 +160,13 @@ class PaxosHandler(socketserver.BaseRequestHandler):
# executeLog();
# Testing code to return the PaxosResult
# self.request.sendall(pickle.dumps(PaxosResult((HOST, 12345), True)))
lock_res = False
if paxos_req.lock_command == "lock":
lock_res = self.server.lock_manager.lock(self.client_address, int(paxos_req.lock_command.value))
else: # unlock
lock_res = self.server.lock_manager.unlock(self.client_address, int(paxos_req.lock_command.value))
self.request.sendall(pickle.dumps(PaxosResult((HOST, 12345), lock_res)))
def handleP1A(self, p1a, sender):
if self.server.highest_ballot_seen < p1a.ballot_num:
......@@ -405,63 +415,6 @@ class PaxosHandler(socketserver.BaseRequestHandler):
send_msg(p1a, acceptor_addr)
setTimer(LeaderElectionTimer(p1a), LeaderElectionTimer.LEADER_ELECTION_TIMER)
@total_ordering
class BallotNumber:
def __init__(self, seq_num, addr) -> None:
self.seq_num = seq_num # Sequence number of the proposal
self.addr = addr # Address of the server proposing
def increaseBallot(self):
self.seq_num += 1
def _is_valid_operand(self, other):
return (hasattr(other, "seq_num") and
hasattr(other, "addr"))
# Only need to make lt and eq, total_ordering will do the rest
def __lt__(self, other) -> bool:
if not self._is_valid_operand(other):
return NotImplemented
if self.seq_num < other.seq_num:
return True
elif self.seq_num > other.seq_num:
return False
else:
# Same seq_num, use address for break
return self.addr[1] < other.addr[1]
def __eq__(self, other) -> bool:
if not self._is_valid_operand(other):
return NotImplemented
return ((self.seq_num, self.addr) == (other.seq_num, other.addr))
def __str__(self) -> str:
return "BallotNumber(seq_num: " + str(self.seq_num) + ", addr: " + str(self.addr) + ")"
@total_ordering
class BallotValuePair:
def __init__(self, ballot_num, value) -> None:
self.ballot_num = ballot_num # ballot number
self.value = value # paxos request
def _is_valid_operand(self, other):
return (hasattr(other, "ballot_num") and
hasattr(other, "value"))
# Only need to make lt and eq, total_ordering will do the rest
def __lt__(self, other) -> bool:
if not self._is_valid_operand(other):
return NotImplemented
return self.ballot_num < other.ballot_num
def __eq__(self, other) -> bool:
if not self._is_valid_operand(other):
return NotImplemented
return self.ballot_num == other.ballot_num
def __str__(self) -> str:
return "BallotValuePair(ballot_num: " + str(self.ballot_num) + ", value: " + str(self.value) + ")"
# Serialize obj, and send the message.
# This function will not wait for reply (communication between paxos nodes)
......
from functools import total_ordering
from typing import Tuple
Address = Tuple[str, int]
@total_ordering
class BallotNumber:
def __init__(self, seq_num, addr) -> None:
self.seq_num = seq_num # Sequence number of the proposal
self.addr = addr # Address of the server proposing
def increaseBallot(self):
self.seq_num += 1
def _is_valid_operand(self, other):
return (hasattr(other, "seq_num") and
hasattr(other, "addr"))
# Only need to make lt and eq, total_ordering will do the rest
def __lt__(self, other) -> bool:
if not self._is_valid_operand(other):
return NotImplemented
if self.seq_num < other.seq_num:
return True
elif self.seq_num > other.seq_num:
return False
else:
# Same seq_num, use address for break
return self.addr[1] < other.addr[1]
def __eq__(self, other) -> bool:
if not self._is_valid_operand(other):
return NotImplemented
return ((self.seq_num, self.addr) == (other.seq_num, other.addr))
def __str__(self) -> str:
return "BallotNumber(seq_num: " + str(self.seq_num) + ", addr: " + str(self.addr) + ")"
@total_ordering
class BallotValuePair:
def __init__(self, ballot_num, value) -> None:
self.ballot_num = ballot_num # ballot number
self.value = value # paxos request
def _is_valid_operand(self, other):
return (hasattr(other, "ballot_num") and
hasattr(other, "value"))
# Only need to make lt and eq, total_ordering will do the rest
def __lt__(self, other) -> bool:
if not self._is_valid_operand(other):
return NotImplemented
return self.ballot_num < other.ballot_num
def __eq__(self, other) -> bool:
if not self._is_valid_operand(other):
return NotImplemented
return self.ballot_num == other.ballot_num
def __str__(self) -> str:
return "BallotValuePair(ballot_num: " + str(self.ballot_num) + ", value: " + str(self.value) + ")"
import sys
from typing import Callable
from assignment2.message import P1A, P2A
from assignment2.paxos import BallotNumber
from message import P1A, P2A
from paxos_utils import BallotNumber
import threading
"""
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment