Skip to content

Commit 184ae5e

Browse files
committed
Implement EndPoint.
- Process ids are now 'EndPoint's instead of int. - Implement UdpEndPoint - Update examples - Remove generated .py files from repo - Add --full option to compiler.
1 parent 8163d30 commit 184ae5e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+1094
-1073
lines changed

compiler/__main__.py

Lines changed: 57 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,31 @@
11
"""Main entry point"""
22

3-
import sys
3+
import sys,os
44
import time
55
if sys.argv[0].endswith("__main__.py"):
6-
sys.argv[0] = "python -m disalgo"
6+
sys.argv[0] = "python -m distalgo"
77

8-
args = dict()
8+
RUNTIMEPKG = "runtime"
9+
RUNTIMEFILES = ["event.py", "udp.py", "sim.py", "util.py"]
910

1011
def parseArgs(argv):
11-
if (len(argv) < 2):
12-
printUsage(argv[0])
13-
sys.exit(1)
14-
15-
global args
16-
args['printsource'] = False
17-
args['optimize'] = False
18-
args['sourcefile'] = None
19-
args['run'] = False
20-
args['full'] = False
21-
for arg in argv:
22-
if (arg == "-p"):
23-
args['printsource'] = True
24-
elif (arg == "-o"):
25-
args['optimize'] = True
26-
elif (arg == "-r"):
27-
args['run'] = True
28-
elif (arg == "-F" or arg == "--full"):
29-
args['full'] = True
30-
else:
31-
args['sourcefile'] = arg
12+
13+
import optparse
14+
p = optparse.OptionParser()
15+
16+
p.add_option("-p", action="store_true", dest='printsource')
17+
p.add_option("-F", action="store_true", dest='genfull')
18+
p.add_option("--full", action="store_true", dest='genfull')
19+
p.add_option("-O", action="store_true", dest='optimize')
20+
p.add_option("-D", action="store", dest='rootdir')
21+
22+
p.set_defaults(printsource=False,
23+
genfull=False,
24+
optimize=False,
25+
rootdir=os.getcwd())
26+
27+
return p.parse_args()
28+
3229

3330
def printUsage(name):
3431
usage = """
@@ -37,30 +34,51 @@ def printUsage(name):
3734
"""
3835
sys.stderr.write(usage % name)
3936

40-
import ast
41-
42-
from .dist import DistalgoTransformer
4337
from .codegen import to_source
4438
from .compiler import dist_compile
4539

4640
def main():
47-
parseArgs(sys.argv)
41+
opts, args = parseArgs(sys.argv)
42+
print("rootdir is %s" % opts.rootdir)
4843

4944
start = time.time()
50-
infd = open(args['sourcefile'], 'r')
51-
pytree = dist_compile(infd)
52-
infd.close()
53-
elapsed = time.time() - start
45+
runtime = []
46+
if opts.genfull:
47+
for f in RUNTIMEFILES:
48+
p = os.path.join(opts.rootdir, RUNTIMEPKG, f)
49+
if not os.path.isfile(p):
50+
sys.stderr.write("File %s not found. Please specify root directory using -D.\n"%p)
51+
sys.exit(1)
52+
else:
53+
pfd = open(p, "r")
54+
runtime.extend(pfd.readlines())
55+
pfd.close()
56+
postamble = ["\nif __name__ == \"__main__\":\n",
57+
" main()\n"]
5458

55-
pysource = to_source(pytree)
59+
for f in args:
60+
infd = open(f, 'r')
61+
pytree = dist_compile(infd)
62+
infd.close()
5663

57-
outfile = args['sourcefile'][:-4] + ".py"
58-
outfd = open(outfile, 'w')
59-
outfd.write(pysource)
60-
outfd.close()
64+
pysource = to_source(pytree)
6165

62-
print("Total compilation time: %f second(s)." % elapsed)
63-
sys.exit(0)
66+
if opts.printsource:
67+
sys.stdout.write(pysource)
68+
else:
69+
outfile = f[:-4] + ".py"
70+
outfd = open(outfile, 'w')
71+
if opts.genfull:
72+
outfd.writelines(runtime)
73+
outfd.write(pysource)
74+
if opts.genfull:
75+
outfd.writelines(postamble)
76+
outfd.close()
77+
sys.stderr.write("Written %s.\n"%outfile)
78+
79+
elapsed = time.time() - start
80+
sys.stderr.write("\nTotal compilation time: %f second(s).\n" % elapsed)
81+
return 0
6482

6583
if __name__ == '__main__':
6684
main()

compiler/consts.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
SENT_PATTERN_VARNAME = "_sent_patterns"
33
EVENT_PATTERN_VARNAME = "_event_patterns"
44
LABEL_EVENTS_VARNAME = "_label_events"
5-
EVENT_PROC_FUNNAME = "_process_event_"
5+
EVENT_PROC_FUNNAME = "_process_event"
66

77
TIMER_VARNAME = "__await_timer_"
88
TIMEOUT_VARNAME = "_timeout"

compiler/dist.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,16 +96,15 @@ def genInitFunc(self, inf):
9696
body = []
9797
body.append(Call(Attribute(Name(DISTALGO_BASE_CLASSNAME, Load()),
9898
"__init__", Load()),
99-
[Name("self", Load()), Name("pid", Load()),
100-
Name("pipe", Load()), Name("perf_pipe", Load())],
99+
[Name("self", Load()), Name("parent", Load()),
100+
Name("initq", Load())],
101101
[], None, None))
102102
body.append(inf.genEventPatternStmt())
103103
body.append(inf.genSentPatternStmt())
104104
body.append(inf.genLabelEventsStmt())
105105
body.extend(inf.newstmts)
106106

107-
arglist = [arg("self", None), arg("pid", None), arg("pipe", None),
108-
arg("perf_pipe", None)]
107+
arglist = [arg("self", None), arg("parent", None), arg("initq", None)]
109108
args = arguments(arglist, None, None, [], None,
110109
None, [], None)
111110
return FunctionDef("__init__", args, body, [], None)

examples/2pc/2pc.dis

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
from random import randint
22

33
class Proposer(DistProcess):
4-
def setup(nodes, increment):
5-
ballot = self # Our starting ballot number
4+
def setup(nodes):
5+
ballot = (0, self) # Our starting ballot number
66
value = self
7-
increment = increment # Increment to ensure unique ballot number
8-
nodes = nodes # Set of all acceptors
97

108
def main():
11-
retry = 0
9+
retry = 0
1210
while True:
1311
--transact_start
1412
retry += 1
@@ -21,9 +19,9 @@ class Proposer(DistProcess):
2119
# otherwise abort:
2220
if (len(received(Agree(ballot, src))) == len(nodes)):
2321
send(Commit(ballot), nodes)
24-
output("Committed ballot %d on %dth attempt"%(ballot, retry))
22+
output("Committed ballot %r on %dth attempt"%(ballot, retry))
2523
retry = 0
26-
ballot += increment
24+
ballot = (ballot[0]+1, self)
2725
break
2826
else:
2927
send(Abort(ballot), nodes)
@@ -34,8 +32,8 @@ class Proposer(DistProcess):
3432
class Acceptor(DistProcess):
3533
def setup(rate):
3634
failure_rate = rate
37-
staging = {}
38-
localdb = {}
35+
staging = dict()
36+
localdb = dict()
3937

4038
def main():
4139
await(False)

examples/2pc/2pc.py

Lines changed: 0 additions & 96 deletions
This file was deleted.

examples/2pc/2pc.run

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
from runtime.sim import DistProcess
2-
3-
global DistProcess
4-
global TIMEOUT
51
TIMEOUT = 5
6-
72
dist_source("2pc.dis")
83

9-
def config():
4+
def main():
105
nproposers = 5
116
nacceptors = 10
12-
fail_rate = 0
7+
fail_rate = 5
8+
9+
config_endpoint("udp")
1310

1411
accpts = createprocs(Acceptor, nacceptors, [fail_rate])
15-
propsrs = createprocs(Proposer, nproposers, [accpts, nproposers])
12+
propsrs = createprocs(Proposer, nproposers, [accpts])
13+
config_max_event_timeout(accpts | propsrs, 3)
1614

17-
config_max_event_timeout(3)
18-
config_trace(True)
15+
startprocs(accpts)
16+
startprocs(propsrs)

examples/byzpaxos/byzopt.run

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
TIMEOUT = 3
2+
dist_source("opt.dis")
3+
4+
def main():
5+
nproposers = 3
6+
nacceptors = 15
7+
f = (nacceptors - 1) /3
8+
9+
acceptors = createprocs(Acceptor, nacceptors)
10+
proposers = createprocs(Proposer, nproposers)
11+
12+
setupprocs(acceptors, [(acceptors|proposers), nacceptors/2+f, f])
13+
setupprocs(proposers, [acceptors, nacceptors/2+f, f])
14+
15+
config_max_event_timeout(acceptors|proposers, 1)
16+
17+
startprocs(acceptors)
18+
startprocs(proposers)

examples/byzpaxos/byzpaxos.dis

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,17 @@ class Proposer(DistProcess):
1010
propVal = self # Current value to propose
1111

1212
def main():
13-
--start
1413
while True:
15-
--prepare
14+
--start
1615
send(Prepare(propNum, self), acpts)
1716
await(len(received(Promise(propNum, _vp, _vv, _a)))>maj, TIMEOUT)
1817
--propose
1918
if (not _timeout):
2019
safeval = -1
2120
props = [(n, v)
2221
for (n, v, _) in
23-
received(Promise(propNum, vp,vv, a))
24-
if len(received(Promise(propNum, n, v, a))) > f]
22+
received(Promise(propNum, _n, _v, _a))
23+
if len(received(Promise(propNum, n, v, _a))) > f]
2524
if (len(props) > 0):
2625
_, safeval = max(props)
2726
if (safeval >= 0):
@@ -31,7 +30,8 @@ class Proposer(DistProcess):
3130
await(len(received(TwoAv(propNum, propVal, a))) > maj, TIMEOUT)
3231
if (not _timeout): # We're done
3332
--end
34-
return
33+
output("Succeeded proposing %d" % propVal)
34+
continue
3535
--reinit
3636
output("Failed ballot %d, retrying."%(propNum))
3737
# Try again with a higher proposal number
@@ -41,9 +41,8 @@ class Proposer(DistProcess):
4141
return propNum+total_procs
4242

4343
class Acceptor(DistProcess):
44-
def setup(allprocs, quorumSize, tolerence):
44+
def setup(allprocs, tolerence):
4545
peers = allprocs
46-
maj = quorumSize
4746
f = tolerence
4847

4948
def main():
@@ -61,7 +60,7 @@ class Acceptor(DistProcess):
6160
if (n >= maxpromised() and islegal(n, v) and
6261
len(sent(TwoAv(n, _v, self))) == 0):
6362
send(TwoAv(n, v, self), peers)
64-
output("Sent 2av for %d in ballot %d"%(v, n))
63+
# output("Sent 2av for %d in ballot %d"%(v, n))
6564

6665
def OnTwoAv(propnum, propval, p):
6766
pass

0 commit comments

Comments
 (0)