Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
cse550
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Winston Jodjana
cse550
Commits
860c5c01
Commit
860c5c01
authored
2 years ago
by
WinJ
Browse files
Options
Downloads
Patches
Plain Diff
type declaration
parent
dcfd2278
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
assignment2/paxos.py
+26
-24
26 additions, 24 deletions
assignment2/paxos.py
with
26 additions
and
24 deletions
assignment2/paxos.py
+
26
−
24
View file @
860c5c01
# http://pymotw.com/2/SocketServer/
from
typing
import
Tuple
,
Dict
,
Set
from
functools
import
total_ordering
import
socketserver
import
pickle
# Paxos servers
class
Paxos
(
socketserver
.
TCPServer
):
def
__init__
(
self
,
address
,
servers
)
->
None
:
# address is a tuple of (ip, port)
self
.
address
=
address
# Our address (ip, port)
self
.
servers
=
servers
# All servers addresses
self
.
address
:
Tuple
[
str
,
int
]
=
address
# Our address
tuple
(ip, port)
self
.
servers
=
servers
# All servers addresses
TODO: is this a tuple or list of server addresses?
# Sets up other variables
self
.
is_leader
=
False
# Are we the leader?
self
.
ballot
=
BallotNumber
(
1
,
self
.
address
)
# Our ballot number
self
.
highestBallotSeen
=
BallotNumber
(
0
,
self
.
address
)
# Highest ballot number seen
self
.
proposals
=
dict
()
# Used by leader to send P2A to acceptors
self
.
accepted
=
dict
()
# Used by acceptors in P2A and also P1B
self
.
log
=
dict
()
# The log
self
.
leader_recent_ping
=
False
# True if we think leader is alive
self
.
is_leader
:
bool
=
False
# Are we the leader?
self
.
ballot
:
BallotNumber
=
BallotNumber
(
1
,
self
.
address
)
# Our ballot number
self
.
highestBallotSeen
:
BallotNumber
=
BallotNumber
(
0
,
self
.
address
)
# Highest ballot number seen
self
.
proposals
:
Dict
[
int
,
Proposal
]
=
dict
()
# Used by leader to send P2A to acceptors
, slot number -> proposal
self
.
accepted
:
Dict
[
int
,
Proposal
]
=
dict
()
# Used by acceptors in P2A and also P1B
, slot number -> proposal
self
.
log
:
Dict
[
int
,
Proposal
]
=
dict
()
# The log
self
.
leader_recent_ping
:
bool
=
False
# True if we think leader is alive
# for proposal phase
self
.
slot_in
=
1
# First non-executed proposal slot (+1 last executed slot)
self
.
slot_out
=
1
# First empty slot (+1 last proposed slot)
self
.
slot_to_acceptors
=
dict
()
# Used by leader to decide majority for each slot after P2B
self
.
slot_in
:
int
=
1
# First non-executed proposal slot (+1 last executed slot)
self
.
slot_out
:
int
=
1
# First empty slot (+1 last proposed slot)
self
.
slot_to_acceptors
:
Dict
[
int
,
Set
[
Tuple
[
str
,
int
]]]
=
dict
()
# Used by leader to decide majority for each slot after P2B
, Multimap<Integer, Address>
# for leader election
self
.
voters
=
set
()
# Yes votes for leader election
self
.
p1b_replies
=
dict
()
# Accepted values for each slot by acceptors that voted yes
self
.
voters
:
Set
[
Tuple
[
str
,
int
]]
=
set
()
# Yes votes for leader election
self
.
p1b_replies
:
Dict
[
int
,
Proposal
]
=
dict
()
# Accepted values for each slot by acceptors that voted yes
, slot number -> proposal
# ...
# Default leader during setup
if
self
.
address
==
servers
[
0
]:
self
.
is_leader
=
True
socketserver
.
TCPServer
.
__init__
(
self
,
address
,
PaxosClientRequestHandler
)
...
...
@@ -57,12 +59,12 @@ class PaxosClientRequestHandler(socketserver.BaseRequestHandler):
data
=
self
.
request
.
recv
(
1024
).
strip
()
# DEBUG LOG
print
(
"
{} wrote:
"
.
format
(
self
.
client_address
[
0
]))
# DEBUG LOG
print
(
data
)
# DEBUG LOG
# test deserialize
paxos_req
=
pickle
.
loads
(
data
)
print
(
paxos_req
,
"
\n
"
)
# TODO: Change data into a format known for lock or unlock
# Propose
prop
=
Proposal
(
BallotNumber
(
self
.
ballot
().
seq_num
,
self
.
address
()),
data
)
...
...
@@ -75,7 +77,7 @@ class PaxosClientRequestHandler(socketserver.BaseRequestHandler):
# Wait untl hear majority
# Send back result
self
.
request
.
sendall
(
data
.
upper
())
# TESTING CODE
self
.
request
.
sendall
(
data
.
upper
())
# TESTING CODE
@total_ordering
...
...
@@ -83,12 +85,12 @@ class Proposal:
def
__init__
(
self
,
ballot
,
value
)
->
None
:
self
.
ballot
=
ballot
self
.
value
=
value
def
__lt__
(
self
,
other
)
->
bool
:
if
not
self
.
_is_valid_operand
(
other
):
return
NotImplemented
return
self
.
ballot
<
other
.
ballot
def
__eq__
(
self
,
other
)
->
bool
:
if
not
self
.
_is_valid_operand
(
other
):
return
NotImplemented
...
...
@@ -118,12 +120,12 @@ class BallotNumber:
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
))
@total_ordering
class
BallotValuePair
:
def
__init__
(
self
,
ballot_num
,
value
)
->
None
:
...
...
@@ -138,9 +140,9 @@ class BallotValuePair:
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
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment