
louise
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#1199 · main · 10 min 33 s
1Étoiles
0Forks
0Issues
| 1 | /// A two-node cluster (ADR 0072; README.md runs it). One build runs as |
| 2 | /// both nodes: its first argument says which role, its environment which |
| 3 | /// node (`dist.from_env`). |
| 4 | /// |
| 5 | /// cluster store node b: keeps a key-value store, named "store" |
| 6 | /// cluster client NODE node a: finds NODE's store, calls it, starts |
| 7 | /// a worker on NODE, and watches NODE go |
| 8 | |
| 9 | import kv |
| 10 | import words.{Counted, Job} |
| 11 | |
| 12 | import std/actor |
| 13 | import std/dist. |
| 14 | {type DistError, type Name, type Peer, NoName, NodeDown, NodeUp, Peer} |
| 15 | import std/io |
| 16 | import std/list |
| 17 | import std/os |
| 18 | import std/process.{type Subject, type proc, Down} |
| 19 | import std/result |
| 20 | |
| 21 | /// The store's name, the same on every node. It carries its message type: |
| 22 | /// `whereis` refuses a subject registered for another (`WrongType`). |
| 23 | fn store_name :: -> Name<kv.Request> |
| 24 | { |
| 25 | dist.name "store" |
| 26 | } |
| 27 | |
| 28 | /// Node b: start, register the store, and serve until stopped. |
| 29 | fn store :: -> Result<(), String> / <proc, io> |
| 30 | { |
| 31 | use config <- result.try (dist.from_env ()) |
| 32 | use port <- result.try (dist.start config |> result.map_error describe) |
| 33 | let store = kv.start () |
| 34 | use _ <- |
| 35 | result.try |
| 36 | (dist.register (store_name ()) store |> result.map_error describe) |
| 37 | io.println "store: listening on port {port}" |
| 38 | // The program ends when the store does: never, unless it crashes, or the |
| 39 | // program is stopped (Ctrl-C). |
| 40 | let Down(_, why) = |
| 41 | process.receive_forever (process.monitor (process.owner store)) |
| 42 | Error("the store stopped: {why}") |
| 43 | } |
| 44 | |
| 45 | /// Node a: reach `peer`, use its store, start a worker there, and watch |
| 46 | /// it until it goes. |
| 47 | fn client :: peer: String -> Result<(), String> / <proc, io> |
| 48 | { |
| 49 | use config <- result.try (dist.from_env ()) |
| 50 | use _ <- result.try (dist.start config |> result.map_error describe) |
| 51 | // Node events: `peer`'s state now (down, until the seed's connection is |
| 52 | // up), then each change. |
| 53 | let events = dist.monitor_node peer |
| 54 | use _ <- result.try (up peer events 10000) |
| 55 | io.println "connected to {peer}" |
| 56 | use store <- result.try (find peer 20) |
| 57 | use_store store |
| 58 | use _ <- result.try (count_on peer) |
| 59 | match peer_of peer |
| 60 | { Some(p) -> |
| 61 | io.println |
| 62 | "peer: {p.state:?}, {p.sent} messages sent, {p.received} received" |
| 63 | , None -> io.println "peer: unknown" |
| 64 | } |
| 65 | watch peer store events |
| 66 | Ok(()) |
| 67 | } |
| 68 | |
| 69 | /// Calls across nodes: each request carries its reply subject. |
| 70 | fn use_store :: store: Subject<kv.Request> -> () / <proc, io> |
| 71 | { |
| 72 | let put = actor.call store fn r { kv.Put("language", "louise", r) } 5000 |
| 73 | io.println "put: {put:?}" |
| 74 | let get = actor.call store fn r { kv.Get("language", r) } 5000 |
| 75 | io.println "get: {get:?}" |
| 76 | io.println "size: {actor.call store kv.Size 5000}" |
| 77 | } |
| 78 | |
| 79 | /// Run `words.count` on `peer`: it replies on a subject of this node. |
| 80 | fn count_on :: peer: String -> Result<(), String> / <proc, io> |
| 81 | { |
| 82 | let counted = process.subject () |
| 83 | let job = Job("a cluster of two nodes", counted) |
| 84 | use _ <- |
| 85 | result.try |
| 86 | (dist.spawn_on peer words.count job 5000 |> result.map_error describe) |
| 87 | match process.receive counted 5000 |
| 88 | { Some(Counted(n, node)) -> Ok(io.println "counted {n} words on {node}") |
| 89 | , None -> Error("no count from {peer}") |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | /// Monitor the store across nodes until it goes: its exit, or |
| 94 | /// `noconnection` if its node is lost first. The node's `NodeDown` comes |
| 95 | /// after its ties' `noconnection`. |
| 96 | fn watch :: |
| 97 | peer: String |
| 98 | store: Subject<kv.Request> |
| 99 | events: Subject<dist.NodeEvent> |
| 100 | -> () / <proc, io> |
| 101 | { |
| 102 | let down = process.monitor (process.owner store) |
| 103 | io.println "watching {peer}: stop it to see it go" |
| 104 | let Down(_, why) = process.receive_forever down |
| 105 | io.println "store: {why}" |
| 106 | match process.receive_forever events |
| 107 | { NodeDown(node, _) -> io.println "{node} is down" |
| 108 | , NodeUp(node) -> io.println "{node} is up again" |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | /// Wait up to `timeout` milliseconds for `peer`'s `NodeUp`, past the |
| 113 | /// `NodeDown`s of a node not connected yet. A failed connection is |
| 114 | /// redialed and leaves the node down: `dist.peers` says why it failed. |
| 115 | fn up :: |
| 116 | peer: String |
| 117 | events: Subject<dist.NodeEvent> |
| 118 | timeout: Int |
| 119 | -> Result<(), String> / <proc, io> |
| 120 | { |
| 121 | match process.receive events timeout |
| 122 | { Some(NodeUp(_)) -> Ok(()) |
| 123 | , Some(NodeDown(_, _)) -> up peer events timeout |
| 124 | , None -> |
| 125 | { |
| 126 | let why = match peer_of peer |
| 127 | { Some(Peer(state: dist.Down(why), ..)) -> why, _ -> "" } |
| 128 | Error("cannot reach {peer} within {timeout} ms: {why}") |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | /// What `dist.peers` says of `node`. |
| 134 | fn peer_of :: node: String -> Option<Peer> / <io> |
| 135 | { |
| 136 | list.find (dist.peers ()) fn p { p.node == node } |
| 137 | } |
| 138 | |
| 139 | /// `whereis` the store on `peer`, `tries` times while it is not registered |
| 140 | /// yet. |
| 141 | fn find :: |
| 142 | peer: String |
| 143 | tries: Int |
| 144 | -> Result<Subject<kv.Request>, String> / <proc, io> |
| 145 | { |
| 146 | match dist.whereis (store_name ()) peer 5000 |
| 147 | { Ok(store) -> Ok(store) |
| 148 | , Error(NoName(_, _)) if tries > 1 -> |
| 149 | { |
| 150 | process.sleep 100 |
| 151 | find peer (tries - 1) |
| 152 | } |
| 153 | , Error(e) -> Error(describe e) |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | fn describe :: e: DistError -> String |
| 158 | { |
| 159 | "{e:?}" |
| 160 | } |
| 161 | |
| 162 | pub fn main :: -> () / <proc, io> |
| 163 | { |
| 164 | let done = match os.args () |
| 165 | { [_, "store"] -> store () |
| 166 | , [_, "client", peer] -> client peer |
| 167 | , _ -> Error("usage: cluster store | cluster client NODE") |
| 168 | } |
| 169 | match done |
| 170 | { Ok(()) -> () |
| 171 | , Error(why) -> |
| 172 | { |
| 173 | io.eprintln why |
| 174 | os.exit 1 |
| 175 | } |
| 176 | } |
| 177 | } |