-
Notifications
You must be signed in to change notification settings - Fork 2
/
jobs.scala
301 lines (235 loc) · 7.89 KB
/
jobs.scala
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package KeYmaeraD
import scala.actors.Actor
import scala.actors.TIMEOUT
import scala.actors.AbstractActor
import scala.actors.Actor._
import scala.actors.Futures._
import scala.actors.remote.RemoteActor
import scala.actors.remote.RemoteActor._
import scala.actors.remote._
import Procedures._
object Jobs {
type JobID = Nodes.NodeID
/*
private var nextID = 0
def nextJobID : JobID = {
val res = nextID;
nextID += 1;
res
}
*/
class SyncMap[A,B]
extends scala.collection.mutable.HashMap[A, B]
with scala.collection.mutable.SynchronizedMap[A,B]
case class JobData( jid: JobID,
s: scala.actors.OutputChannel[Any],
p: String, // the proc
sq: Sequent,
t: Long // start time
)
class JobMaster(myPort: Int) extends Actor {
// the jobs we've sent out to workers
val jobs =
new scala.collection.mutable.HashMap[JobID,
(JobData, scala.actors.OutputChannel[Any])]()
/* Keep a queue of unstarted jobs and idling workers.
* At each loop, match them up and dispatch.
*/
val newjobs = new scala.collection.mutable.Queue[JobData]()
val idleworkers =
new scala.collection.mutable.Queue[scala.actors.OutputChannel[Any]]()
// val localworker = new JobWorker()
// localworker.start()
def act(): Unit = {
println("jobmaster acting")
RemoteActor.classLoader = getClass().getClassLoader()
alive(myPort)
register('master, self)
while(true){
// Assign jobs if we can.
while((! idleworkers.isEmpty) && (! newjobs.isEmpty) ){
val iw = idleworkers.dequeue()
val jd@JobData(jid, s, p, sq, t) = newjobs.dequeue()
println("Assigning work!")
iw ! ( ('job, p, sq, jid) )
jobs.put(jid, (jd, iw))
()
};
receive {
case 'quit =>
println("jobmaster quitting, notifying workers")
// localworker !? 'quit
for (w <- idleworkers) {
println("notifying worker: " + w)
w ! ('quit)
}
println("jobmaster quits")
sender ! ()
exit
case ('idling) =>
println("A worker idles.")
sender ! 'ack
idleworkers.enqueue(sender)
case ('job, p: String, sq: Sequent, jid: JobID) =>
println("jobmaster got a job")
val t = System.currentTimeMillis
// PERF: insert easy filter here.
newjobs += JobData(jid, sender, p, sq, t)
case 'list =>
println(jobs)
case ('jobdone, jid: JobID, res: Sequent) =>
jobs.get(jid) match {
case Some((JobData(_,s,p,sq,t),w)) =>
s ! ('jobdone, jid, res)
case None =>
throw new Error("invalid jobID")
}
jobs.remove(jid)
case ('jobdone, jid: JobID) =>
jobs.get(jid) match {
case Some((JobData(_,s,p,sq,t),w)) =>
s ! ('jobdone, jid)
case None =>
throw new Error("invalid jobID")
}
jobs.remove(jid)
case ('abort, jid: JobID) =>
val njs = newjobs.dequeueAll( {case JobData(x,_,_,_,_) => x == jid})
println("jobmaster: cancelling "+ njs.length + " job(s) ")
for(JobData(jid1,s,p,sq,t) <- njs){
s ! ('jobdone, jid1)
}
jobs.get(jid) match {
case Some((JobData(_,s,p,sq,t), w)) =>
println("jobmaster: aborting job " + jid)
w ! 'abort
case None =>
if (njs.length == 0)
println("jobmaster: could not find job " + jid)
}
case msg =>
println ("jobserver got message: " + msg)
}
}
}
}
class JobWorker(masterNode: Node) extends Actor {
case class JobData(proc:String,
sq:Sequent,
jid: JobID,
sender:scala.actors.OutputChannel[Any])
var working: Option[Procedure] = None
val procqueue =
new scala.collection.mutable.Queue[JobData]()
val lock = new Object()
def tryworking : Unit = {
if (working == None && ! procqueue.isEmpty) {
println("procqueue has length " + procqueue.length)
val jd@JobData(p,sq,jid,sender) = procqueue.dequeue
procs.get(p) match {
case Some(pr) =>
val sf = self
if(pr.applies(sq)) {
println("working on jid " + jid)
lock.synchronized{
working = Some(pr)
}
future {
val res = pr.proceed(sq)
res match {
case Some(r) =>
sf ! ('done, jd, r)
case None =>
sf ! ('done, jd)
}
}
()
} else sf ! ('doesnotapply, jid) // should not happen
case None =>
// should not happen
}
}
}
private def abort() = {
lock.synchronized{ working } match {
case Some(pr) =>
pr.abort
case None => // XXX need to look through the queue
println("got abort when nothing was running")
}
}
private def getack = receiveWithin (5000) {
case 'ack =>
println("got an ack")
case TIMEOUT =>
println("no ack. quitting")
exit
}
def act(): Unit = {
println("jobworker acting")
val master = select(masterNode, 'master)
master ! ('idling)
getack
while(true) {
tryworking
receive {
case 'quit =>
println("jobmaster quitting, worker aborts jobs")
abort()
println("jobmaster quitting, worker exits")
exit
case ('job, p: String, sq: Sequent , jid: JobID) =>
procqueue.enqueue(JobData(p, sq, jid, sender))
case ('done, JobData(p,sq,jid,jobsender), res: Sequent) =>
lock.synchronized {working = None}
jobsender ! ('jobdone, jid, res)
master ! ('idling)
getack
case ('done, JobData(p,sq,jid,jobsender)) =>
lock.synchronized { working = None }
jobsender ! ('jobdone, jid)
master ! ('idling)
getack
case 'abort =>
abort()
case msg =>
println("jobworker got unknown message: " + msg)
}
}
}
}
}
// Code entry point for workers.
object WorkerMain {
import Jobs._
import org.apache.commons.cli.Options
import org.apache.commons.cli.CommandLine
import java.net.InetAddress
def parse(args: Array[String]) : CommandLine = {
//use CLI to parse command line options
var opts = new org.apache.commons.cli.Options();
opts.addOption("c", true, "coordinator address (default = localhost)");
opts.addOption("cp", true, "coordinator port (default = 50001)");
//do parsing
var parser = new org.apache.commons.cli.GnuParser();
parser.parse(opts, args);
}
def main(args: Array[String]) : Unit = {
println("worker says: hello world.")
val cmd = parse(args)
//process options
//coordinator address and port
if (!cmd.hasOption("c")) {
println("Using default coordinator address localhost. (use -c to specify).")
}
val coorHost =
new Node(cmd.getOptionValue("c","localhost"),
java.lang.Integer.parseInt(cmd.getOptionValue("cp", "50001")))
println("coordinator at " + coorHost.toString)
// Weird. If we don't do this, the worker can't find the KeYmaeraD classes.
RemoteActor.classLoader = getClass().getClassLoader()
val jobWorker = new JobWorker(coorHost)
jobWorker.start
()
}
}