# A cluster in one process: a master that owns all state, workers that pull# tasks from it, a worker whose first tasks fail, and a result that must equal# a run without any of this. Run: jennifer run cluster.jusechannel;
use io;
use lists;
usetask;
use time;
defconstTASKSasintinit20;
defconstWORKERSasintinit3;
defconstMAX_LEASESasintinit3;
defconstDONEasintinit -1;
defconstLATERasintinit -2;
/**
* A worker's message to the master.
* @field what {string} "lease", "result" or "failed"
* @field worker {int} who sends it
* @field index {int} the task, for "result" and "failed"
* @field value {int} the task's result, for "result"
* @field reply {channel} where a "lease" gets its answer
*/defstruct Msg {
what asstring,
worker asint,
index asint,
value asint,
reply aschannelofint
};
# The work of one task: pure, so it can run twice on any worker.funcwork(index asint) {
time.sleep(time.fromMilliseconds(10));
return$index * $index;
}
funcmessage(what asstring, id asint, index asint, value asint, reply aschannelofint) {
return Msg{what: $what, worker: $id, index: $index, value: $value, reply: $reply};
}
# A worker: ask for a task, run it, report, until the master says DONE.funcworker(id asint, inbox aschannelof Msg) {
def reply aschannelofintinitchannel.make(1);
def failures asintinit0;
while (true) { # lint-disable: L105channel.send($inbox, message("lease", $id, 0, 0, $reply));
def index asintinitchannel.recv($reply);
if ($index == DONE) {
return$id;
}
if ($index == LATER) {
time.sleep(time.fromMilliseconds(5));
continue;
}
if ($id == 0and$failures < 2) {
# worker 0 is unreliable: its first two tasks fail$failures = $failures + 1;
channel.send($inbox, message("failed", $id, $index, 0, $reply));
continue;
}
channel.send($inbox, message("result", $id, $index, work($index), $reply));
}
}
# The master: the one owner of the queue, the leases and the result.funcmaster(inbox aschannelof Msg) {
def queue aslistofintinitlists.range(0, TASKS, 1);
def leases aslistofintinitlists.replicate([0], TASKS);
def finished aslistofboolinitlists.replicate([false], TASKS);
def done asintinit0;
def total asintinit0;
def stopped asintinit0;
def served aslistofintinitlists.replicate([0], WORKERS);
while ($stopped < WORKERS) {
def m as Msg initchannel.recv($inbox);
if ($m.what == "lease") {
if ($done == TASKS) {
channel.send($m.reply, DONE);
$stopped = $stopped + 1;
} elseif (len($queue) == 0) {
channel.send($m.reply, LATER);
} else {
def index asintinit$queue[0];
$queue = $queue[1..len($queue)];
$leases[$index] = $leases[$index] + 1;
channel.send($m.reply, $index);
}
} elseif ($m.what == "failed") {
if ($leases[$m.index] >= MAX_LEASES) {
throw Error{kind: "value", message: "task {$m.index} failed {MAX_LEASES} times"};
}
io.printf("task %d failed on worker %d, queued again\n", $m.index, $m.worker);
$queue[] = $m.index;
} elseif (not$finished[$m.index]) {
# the first result wins; the merge (a sum) does not care about order$finished[$m.index] = true;
$total = $total + $m.value;
$done = $done + 1;
$served[$m.worker] = $served[$m.worker] + 1;
}
}
io.printf("results per worker: %v\n", $served);
return$total;
}
def mailbox aschannelof Msg initchannel.make(16);
def crew aslistoftaskofint;
for (def n in0..WORKERS) {
$crew[] = spawn {
returnworker($n, $mailbox);
};
}
def sum asintinitmaster($mailbox);
task.waitAll($crew);
def local asintinit0;
for (def k in0..TASKS) {
$local = $local + work($k);
}
if ($sum != $local) {
throw Error{kind: "value", message: "distributed {$sum}, local {$local}"};
}
io.printf("distributed %d, local %d: equal\n", $sum, $local);