Topology & Hosting

The same actors run in three topologies. Which one a process is comes from its configuration, not its code.

The three topologies

Topology What the process does
Standalone Hosts its actors here and answers their routes itself. No network.
Cluster A cluster node: it routes requests and makes actors on satellites, but hosts none itself.
Hybrid A cluster node, a satellite that hosts its actors, and a client, in one process.

A cluster has three roles in all:

  • cluster nodes share a route table by gossip and decide where each route runs;
  • satellites host actors by route template and register what they can run with their cluster nodes;
  • asteroids are clients: they send a request for a route to a cluster node and get the answer back.
Asteroid ──TCP──► Cluster ◄──Gossip──► Cluster
                     │                     │
                 Satellite             Satellite
                 (actors)              (actors)

Hosted classes

A class hosted by route takes the cluster’s messages: type Message C = Hosted. A hosted message is a type name, a payload (bytes) and, when it was asked, a reply:

import Cluster.Satellite as Sat
import Cluster.Class
import Payload.Codec

data Greeter = Greeter { greeted :: Int }

@route("greeter/{lang}")
instance Actor Greeter where
  type Message Greeter = Hosted
  receive ctx g msg = case msg of
    HostedMsg typeName payload reply ->
      Sat.answer reply (encodePayload stringCodec (greeting (textOf payload) (g.greeted + 1)))
        >> continue g { greeted = g.greeted + 1 }

instance Initial Greeter where
  initial params = Greeter 0

greeterOf :: ActorSystem -> Parameters -> Address Greeter
greeterOf system params = actorOf system params

hostings :: [Hosting]
hostings = [hostingOf greeterOf]

hostingOf takes a function that makes the class and hosts it by the route its @route gives. Sat.answer answers an ask and does nothing for a tell.

Starting a host

import Surround.Host
import Network.Envelope

startHost :: ActorSystem -> HostConfig -> Either Fault Host
hostConfig :: Topology -> NodeEndpoint -> [NodeEndpoint] -> [Hosting] -> HostConfig
case startHost system (hostConfig Standalone (NodeEndpoint "127.0.0.1" 5000) [] hostings) of
  Left fault -> ...
  Right host -> askRoute (hostClient host) "greeter/en" "String" payload 5000

hostConfig topology self seeds hostings: the topology, this node’s endpoint, the cluster nodes to join, and what it hosts. sealedBy seal config names how every link is sealed: Fortress, each process admitted by its identity from the cluster’s CA (FORTRESS_IDENTITY), for links that leave a cluster; or the open transport (ACTOR_SEAL=open) for the nodes of one cluster on its own network (see Deployment). A cluster topology with no seal does not start. validate config lists warnings, such as a cluster-only node given actors to host.

Configuration from the environment

Surround.Config reads what a container is given:

Variable Meaning Default
ACTOR_TOPOLOGY standalone, cluster or hybrid standalone
NODE_ID The node’s name local
NODE_HOST The name other nodes reach this one by 127.0.0.1
NODE_PORT The port it listens on (a hybrid node also the next two) 5000
SEEDS The cluster nodes, host:port;host:port none
ACTOR_SEAL fortress (Fortress, with FORTRESS_IDENTITY) or open (frames as they are, for nodes of one cluster on its own network); any other name is no seal fortress
FORTRESS_IDENTITY This process’s identity file (issued by the cluster’s CA); a cluster topology sealed by Fortress needs it none: refuses
import Surround.Config

startHost system (hostConfigFromEnv hostings)

Same code, any topology

A client asks a route the same way everywhere:

askRoute  :: ActorClient -> String -> String -> Bytes -> Int -> Either Fault Bytes
tellRoute :: ActorClient -> String -> String -> Bytes -> ()
askText :: ActorClient -> String -> String -> String
askText client route text = case encodePayload stringCodec text of
  Left f -> "failed"
  Right payload -> case askRoute client route "String" payload 5000 of
    Right answer             -> textOf answer
    Left (Fault kind detail) -> S.concatStr [kind, ": ", detail]

Standalone, askText client "greeter/en" "Ada" answers Hello, Ada! (1), the next ask Hello, Alan! (2) (the same actor), and a route nothing hosts UnknownRoute: no actor hosts nobody. In a cluster the same calls go through an asteroid to a cluster node, which has a satellite make greeter/en the first time and forwards every answer back.

Standalone

startHost system (hostConfig Standalone self [] hostings)

The host keeps the actors of the routes it has been asked, and makes a route’s actor the first time. Each ask is answered from its own fiber, so a slow actor does not hold the others.

Cluster

A cluster node routes but hosts nothing. Start nodes, satellites and clients separately:

import Cluster.Node as Node
import Cluster.Satellite as Sat
import Cluster.Asteroid as Ast

Node.startClusterNodeWith system self seal seeds          -- a cluster node joining its seeds
Sat.startSatellite system self seal clusters hostings     -- a satellite hosting for those cluster nodes
Ast.startAsteroid system self seal clusters               -- a client of those cluster nodes

A cluster node:

  • sends its peers what changed in its route table (a CRDT) every half second, the whole table to a peer newly linked, and every 5 s a digest (per writer, its live entries counted and summed); a peer whose digest differs is sent what it lacks;
  • knows its peers from its seeds and from the tables that come (every node a table names as a route’s maker); a peer silent past what its syncs’ intervals make likely (phi over 8, about 2 s) is suspected, and gone once its link dropped too or its suspicion lasted 3 s (10 s of silence at most) – a peer only paused, its link up, keeps its routes;
  • removes a route whose actor ended (its satellite says so) and drops a tombstone ten minutes after it learned it;
  • judges every age by its own monotonic clock, from when it learned an entry, never by a time another node wrote;
  • sends work for a route to the satellite that hosts it, sending it again after 3 s without the satellite’s word, at once when the link to that satellite drops, and on another satellite (at most 3 times) when one fails or goes;
  • has the least loaded satellite that can run an unknown route make its actor;
  • forwards work for a route another cluster node owns to that node.

A satellite registers with each of its nodes once per link (and when a node that does not know it asks), runs a message at most once per actor incarnation (below), and answers a new ask Overloaded while the route’s actor has more than 1,000 asks to answer. An asteroid sends its work on to the next node at once when its link to a node drops, and after 4 s without the node’s word.

The direct path

An asteroid keeps where its routes run. The first work for a route goes through a node, which places it (only the route’s owner node places it: a rendezvous hash over the nodes that can) and answers the asteroid’s Resolve with the satellite and the placement’s epoch. Later work for the route goes straight to that satellite, and the answer straight back: two frames an ask, where through a node it took six. A satellite runs a route’s actor only under the lease a placement gave it, and answers work under no lease or another Moved: the asteroid forgets the route, sends the work on through the nodes and resolves again. An ask written straight to a satellite whose link then drops is answered Gone (it may have run); a tell goes on through the nodes. A node just started holds its work for a 3 s warm-up, until its peers have seen it.

Many clusters

With CLUSTER_ID and MESH_SEEDS (see Deployment) and ACTOR_SEAL=open, links inside a cluster stay open and links to other clusters’ nodes open with Fortress; with Fortress every link is sealed, inside and across, and the clusters mesh the same way. Clusters share what they offer (the route families their satellites registered, their live nodes), never their actors or tables: among the live clusters that offer a route’s family, the one a rendezvous hash of the route picks owns it, and another cluster’s route is relayed to its nodes. A cluster is lost when it has offered nothing for ten minutes; a shorter silence, a partition, leaves its routes unavailable from here rather than made twice. Past ten minutes each side serves the routes itself (available, not single); when the partition heals, each side hears the other again as new and ends the routes that hash to it, so the two converge on one owner by themselves. A cluster that joins, or comes back, places nothing another offers until the others say they see it, having ended the routes that now hash to it. Ownership follows the offers: when a cluster starts offering a family, the routes of it that now hash to that cluster are ended where they ran, and go to it.

Delivery

At-least-once from the asteroid’s outbox (it sends a piece of work again until a node takes it); run at most once per actor incarnation (a satellite knows each message it took for 30 s by its route and its caller’s message id, so a node’s resend and a caller’s ask again with the same id are not run twice – a repeat of an ask is given the first run’s answer); a node’s record of work in flight is retry bookkeeping in memory, not a durable store. An actor made again (it passivated, or failed for good) is a new incarnation.

Hybrid

startHost system (hostConfig Hybrid self seeds hostings)

A hybrid node is a cluster node on NODE_PORT, a satellite on the next port and an asteroid on the one after. Its client asks through the cluster, so a hybrid node reaches its own actors and every other node’s the same way.

When to use each

Situation Topology
Tests, tools, a single service Standalone
A dedicated routing tier Cluster nodes, with satellites elsewhere
Most deployments Hybrid nodes, every node hosting and routing

Distributed pub/sub

Actor.Topic is a typed event stream in one process: topic system name, subscribe, unsubscribe, publish, subscribers. Surround.EventStream spreads one across nodes: publishing tells the local subscribers and sends the event to the other nodes, whose local subscribers hear it too, without echo.

case Topic.topic system "orders" of
  orders -> case Topic.subscribe orders reader of
    _ -> Topic.publish orders (Note "o-17 placed")      -- reader is told Note "o-17 placed"

distributedTopic :: ActorSystem -> String -> NodeEndpoint -> [NodeEndpoint] -> Codec e -> FrameGuard
                 -> Either Fault (DistributedTopic e)
subscribeAll :: DistributedTopic e -> ActorRef e -> ()
publishAll   :: DistributedTopic e -> e -> ()

Where next