/// A two-node cluster (ADR 0072; README.md runs it). One build runs as /// both nodes: its first argument says which role, its environment which /// node (`dist.from_env`). /// /// cluster store node b: keeps a key-value store, named "store" /// cluster client NODE node a: finds NODE's store, calls it, starts /// a worker on NODE, and watches NODE go import kv import words.{Counted, Job} import std/actor import std/dist. {type DistError, type Name, type Peer, NoName, NodeDown, NodeUp, Peer} import std/io import std/list import std/os import std/process.{type Subject, type proc, Down} import std/result /// The store's name, the same on every node. It carries its message type: /// `whereis` refuses a subject registered for another (`WrongType`). fn store_name :: -> Name { dist.name "store" } /// Node b: start, register the store, and serve until stopped. fn store :: -> Result<(), String> / { use config <- result.try (dist.from_env ()) use port <- result.try (dist.start config |> result.map_error describe) let store = kv.start () use _ <- result.try (dist.register (store_name ()) store |> result.map_error describe) io.println "store: listening on port {port}" // The program ends when the store does: never, unless it crashes, or the // program is stopped (Ctrl-C). let Down(_, why) = process.receive_forever (process.monitor (process.owner store)) Error("the store stopped: {why}") } /// Node a: reach `peer`, use its store, start a worker there, and watch /// it until it goes. fn client :: peer: String -> Result<(), String> / { use config <- result.try (dist.from_env ()) use _ <- result.try (dist.start config |> result.map_error describe) // Node events: `peer`'s state now (down, until the seed's connection is // up), then each change. let events = dist.monitor_node peer use _ <- result.try (up peer events 10000) io.println "connected to {peer}" use store <- result.try (find peer 20) use_store store use _ <- result.try (count_on peer) match peer_of peer { Some(p) -> io.println "peer: {p.state:?}, {p.sent} messages sent, {p.received} received" , None -> io.println "peer: unknown" } watch peer store events Ok(()) } /// Calls across nodes: each request carries its reply subject. fn use_store :: store: Subject -> () / { let put = actor.call store fn r { kv.Put("language", "louise", r) } 5000 io.println "put: {put:?}" let get = actor.call store fn r { kv.Get("language", r) } 5000 io.println "get: {get:?}" io.println "size: {actor.call store kv.Size 5000}" } /// Run `words.count` on `peer`: it replies on a subject of this node. fn count_on :: peer: String -> Result<(), String> / { let counted = process.subject () let job = Job("a cluster of two nodes", counted) use _ <- result.try (dist.spawn_on peer words.count job 5000 |> result.map_error describe) match process.receive counted 5000 { Some(Counted(n, node)) -> Ok(io.println "counted {n} words on {node}") , None -> Error("no count from {peer}") } } /// Monitor the store across nodes until it goes: its exit, or /// `noconnection` if its node is lost first. The node's `NodeDown` comes /// after its ties' `noconnection`. fn watch :: peer: String store: Subject events: Subject -> () / { let down = process.monitor (process.owner store) io.println "watching {peer}: stop it to see it go" let Down(_, why) = process.receive_forever down io.println "store: {why}" match process.receive_forever events { NodeDown(node, _) -> io.println "{node} is down" , NodeUp(node) -> io.println "{node} is up again" } } /// Wait up to `timeout` milliseconds for `peer`'s `NodeUp`, past the /// `NodeDown`s of a node not connected yet. A failed connection is /// redialed and leaves the node down: `dist.peers` says why it failed. fn up :: peer: String events: Subject timeout: Int -> Result<(), String> / { match process.receive events timeout { Some(NodeUp(_)) -> Ok(()) , Some(NodeDown(_, _)) -> up peer events timeout , None -> { let why = match peer_of peer { Some(Peer(state: dist.Down(why), ..)) -> why, _ -> "" } Error("cannot reach {peer} within {timeout} ms: {why}") } } } /// What `dist.peers` says of `node`. fn peer_of :: node: String -> Option / { list.find (dist.peers ()) fn p { p.node == node } } /// `whereis` the store on `peer`, `tries` times while it is not registered /// yet. fn find :: peer: String tries: Int -> Result, String> / { match dist.whereis (store_name ()) peer 5000 { Ok(store) -> Ok(store) , Error(NoName(_, _)) if tries > 1 -> { process.sleep 100 find peer (tries - 1) } , Error(e) -> Error(describe e) } } fn describe :: e: DistError -> String { "{e:?}" } pub fn main :: -> () / { let done = match os.args () { [_, "store"] -> store () , [_, "client", peer] -> client peer , _ -> Error("usage: cluster store | cluster client NODE") } match done { Ok(()) -> () , Error(why) -> { io.eprintln why os.exit 1 } } }