-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorig.da
executable file
·212 lines (178 loc) · 7.67 KB
/
orig.da
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import sys
from random import randint
import csv
import time as t
controller = import_da('controller')
class Proposer(process, controller.Controllee):
def setup(ctl, acceptors:set, loss_rate, msg_delay, wait_delay, timeout):
super().setup(ctl)
self.pmsg_to_drop = 0
self.amsg_to_drop = 0
self.n = None # proposal number
self.majority = acceptors # majority of acceptors; all in other papers
def to_consent():
n = (0, self) if n == None else (n[0]+1, self) # pick a prop num
is_drop = handle_msg_delay_loss(loss_rate, msg_delay,'prepare')
if not is_drop:
pmsg_to_drop += 1
send(('prepare', n), to= majority)
#output('Send:: ', n, ' Prepare Request')
if await ((len(setof(a, received(('respond', _n, _), from_ =a)))
> len(acceptors)/2)):
v = anyof(setof(v, received(('respond', _n, (n2, v))),
n2==max(setof(n2, received(('respond', _n, (n2, _))))))
or {randint(1,100)}) # any value, pick in 1..100
responded = setof(a, received(('respond', _n, _), from_ =a))
is_drop = handle_msg_delay_loss(loss_rate, msg_delay,'prepare')
if not is_drop:
amsg_to_drop += 1
send(('accept', n, v), to= responded)
#output('### chose', n, v)
elif timeout(timeout):
output('failed proposal number', n)
@controller.run
def run():
while not received(('done',)):
to_consent()
t.sleep(wait_delay)
output('terminating')
def handle_msg_delay_loss(loss_rate, msg_delay, req_type):
if(msg_delay):
t.sleep(msg_delay)
if(req_type == 'prepare' and loss_rate != 0 and pmsg_to_drop == int(1/loss_rate)):
pmsg_to_drop = 0
print("************Prepare Message is dropped************")
return True;
if(req_type == 'accept' and loss_rate != 0 and amsg_to_drop == int(1/loss_rate)):
print("************Accept message is dropped************")
amsg_to_drop = 0
return True;
return False;
def anyof(s):
return next(iter(s)) if s else None
@controller.rugroup('bo_measured')
class Acceptor(process,controller.Controllee):
def setup(ctl,learners:set, loss_rate, msg_delay):
super().setup(ctl)
self.respond_to_drop = 0
self.accepted_to_drop = 0
def receive(msg= ('prepare', n), from_= p):
if each(sent(('respond', n2, _)), has= n > n2):
maxprop = anyof(setof((n, v), sent(('accepted', n, v)),
n==max(setof(n, sent(('accepted', n, _))))))
is_drop = handle_msg_delay_loss(loss_rate, msg_delay,'respond')
if not is_drop:
respond_to_drop += 1
send(('respond', n, maxprop), to =p)
def receive(msg= ('accept', n, v)):
if not some(sent(('respond', n2, _)), has= n2 > n):
is_drop = handle_msg_delay_loss(loss_rate, msg_delay,'accepted')
if not is_drop:
respond_to_drop += 1
send(('accepted', n, v), to= learners)
def handle_msg_delay_loss(loss_rate, msg_delay, req_type):
if(msg_delay):
t.sleep(msg_delay)
if(req_type == 'respond' and loss_rate != 0 and respond_to_drop == int(1/loss_rate)):
respond_to_drop = 0
print("************Respond Message is dropped************")
return True;
if(req_type == 'accepted' and loss_rate != 0 and accepted_to_drop == int(1/loss_rate)):
accepted_to_drop = 0
print("************Accepted Message is dropped***********")
return True;
return False;
@controller.run
def run():
await(received(('done',)))
output('terminating')
def anyof(s):
"""return any element of set s if s is not empty or 'None' otherwise"""
return next(iter(s)) if s else None
class Learner(process,controller.Controllee):
def setup(ctl,acceptors:set, timeout):
super().setup(ctl)
# Populate these two variables to verify correctness
self.learned_val = None
self.is_timeout = False
def learn():
if await(some(received(('accepted', n, v)),
has= len(setof(a, received(('accepted', _n, _v), from_=a)))
> len(acceptors)/2)):
output('learned', n, v)
learned_val = v
elif timeout(timeout):
is_timeout = True
output('failed learning anything')
@controller.run
def run():
learn()
output('terminating')
# Adding for collecting correctness data
send(('learned_val', (is_timeout, learned_val)), to=nodeof(self))
send(('learned',), to=nodeof(self))
def main():
nacceptors = int(sys.argv[1]) if len(sys.argv) > 1 else 3
nproposers = int(sys.argv[2]) if len(sys.argv) > 2 else 5
nlearners = int(sys.argv[3]) if len(sys.argv) > 3 else 3
num_reps = int(sys.argv[4]) if len(sys.argv) > 4 else 1
loss_rate = float(sys.argv[5]) if len(sys.argv) > 5 else 0
msg_delay = float(sys.argv[6]) if len(sys.argv) > 6 else 0
wait_delay = float(sys.argv[7]) if len(sys.argv) > 7 else 0
p_timeout = float(sys.argv[8]) if len(sys.argv) > 8 else 1
l_timeout= float(sys.argv[9]) if len(sys.argv) > 9 else 10
print("loss_rate", loss_rate)
print("msg_delay", msg_delay)
print("wait_delay", wait_delay)
result = []
itr = 0;
is_timeout = []
is_corr = []
for i in range(num_reps):
acceptors = new(Acceptor, num= nacceptors)
proposers = new(Proposer, num= nproposers)
learners = new(Learner, num= nlearners)
ctl = new(controller.Controller, num= 1)
setup(ctl, (nacceptors + nproposers + nlearners, ))
start(ctl)
for a in acceptors: setup(a, (ctl, learners, loss_rate, msg_delay ))
for p in proposers: setup(p, (ctl, acceptors,loss_rate, msg_delay, wait_delay, p_timeout))
for l in learners: setup(l, (ctl, acceptors, l_timeout))
start(acceptors | proposers | learners)
await(each(l in learners, has=received(('learned',), from_=l )))
l_val = set()
t = False;
for l in learners:
learned = setof(val, received(('learned_val',val), from_=a) , a == l )
learned = list(learned)
t = t or learned[0][0]
l_val.add(learned[0][1])
if(t):
is_timeout.append(1)
else:
is_timeout.append(0)
if(len(l_val) > 1):
is_corr.append(0)
else:
is_corr.append(1)
output('done')
send(('done',), to= (acceptors|proposers))
send(('Get_Stats', itr), to=ctl)
if await(some(received(('Stats',data, n)), has= (n == itr)) ):
temp = list(data)
temp.append(is_timeout[i])
temp.append(is_corr[i])
data = tuple(temp)
result += list((data,))
#print("Stat Data received for itr: ", itr)
#print(result[i])
with open('temp.csv', mode='a') as output_file:
writer = csv.writer(output_file)
writer.writerow(list(result[i]))
itr += 1
#send(('Stats_Read',), to=ctl)
# This is an executable specification of the algorithm described in
# Lamport, L. (2001). Paxos Made Simple. ACM SIGACT News
# (Distributed Computing Column), 32(4):51-58, December.
# This code includes setup and termination for running repeated rounds until
# the learners all terminate after learning the consent value or timeout.