When devices fail
Let failing actors restart with backoff, and stop them when they keep failing.
Sensors are flaky. Sometimes a device reports a reading that is not a number at all. In this step such a reading makes the device fail, and tellus restarts it.
Failing
impl Actor for Device {
type Message = DeviceMessage;
type Timers = Untimed;
type Stash = Unstashed;
type State = Option<f64>;
type Error = InvalidReading;
fn init(&self, _: &ActorContext<Self::Message>) -> Result<Self::State, Self::Error> {
Ok(None)
}
fn receive(
&self,
_: &ActorContext<Self::Message>,
incoming: Incoming<Self::Message>,
last_reading: Self::State,
) -> Result<Control<Self::State>, Self::Error> {
let Incoming::Message(message) = incoming else {
unreachable!("the device watches no actor, hence never gets a terminated signal")
};
match message {
DeviceMessage::Record(degrees) if degrees.is_nan() => Err(InvalidReading),
DeviceMessage::Record(degrees) => Ok(Control::Continue(Some(degrees))),
DeviceMessage::Read(reply_to) => {
reply_to.reply(last_reading);
Ok(Control::Continue(last_reading))
}
DeviceMessage::Retire => Ok(Control::Stop),
}
}
}The device's Error type is now InvalidReading instead of !, a unit struct deriving thiserror::Error. For a reading that is not a number, receive returns Err(InvalidReading). You can also use ? to escalate a failure the same way. A panic in receive counts as a failure, too.
What happens next is up to the device's supervision strategy.
Restarting
let device = devices.entry(device_name).or_insert_with(|| {
let config = ActorConfig::default().with_supervision_strategy(
SupervisionStrategy::Restart(RestartPolicy::new(MAX_RESTARTS)),
);
let device = context.spawn_with_config(Device, config);
context.watch(&device);
device
});The default strategy is Stop: a failing actor stops for good. So the group spawns every device with spawn_with_config and an ActorConfig whose strategy is Restart. When a device fails, tellus runs init again for a fresh state. The device keeps its ActorRef and its mailbox, so messages that are already queued are still handled after the restart. Only the state is lost: a restarted device has no last reading.
Restarts back off exponentially, starting at 250 ms by default. The number of restarts is limited:
const MAX_RESTARTS: NonZeroU32 = NonZeroU32::new(3).expect("3 is not zero");A device which fails more than three times stops for good. The count starts over once a device has run for 30 seconds without failing, the default of RestartPolicy. Since the group watches its devices, it gets Terminated and prunes the device, just as for a retired one.
Talking to the group
#[tokio::main]
async fn main() -> anyhow::Result<()> {
init_tracing();
let system = ActorSystem::new(Group);
let group = system.root();
let readings = [
("left", 21.5),
("middle", 20.4),
("right", 19.1),
("left", f64::NAN),
("broken", f64::NAN),
("broken", f64::NAN),
("broken", f64::NAN),
("broken", f64::NAN),
];
for (device_name, degrees) in readings {
group.tell(GroupMessage::Record {
device_name,
degrees,
});
}
group.tell(GroupMessage::Retire {
device_name: "right",
});
let average = group
.ask(Duration::from_secs(5), GroupMessage::Average)
.await
.context("asking for the average temperature")?;
match average {
Some(degrees) => println!("Average temperature: {degrees:.2}"),
None => println!("No readings"),
}
system.stop();
system
.terminated()
.await
.context("awaiting actor system termination")
}main records readings for four devices, among them an invalid one for left and four invalid ones for broken, retires right and asks for the average. The query waits until broken has used up its restarts and stopped, which takes about two seconds. So main asks with a timeout of five seconds here.
Run it
From a clone of the tellus repository:
RUST_LOG=tellus=debug cargo run --quiet -p tellus --example getting_started_06_supervisionOn stdout it prints:
Average temperature: 20.40left failed and restarted, so it lost its 21.5 and has no reading. broken failed four times and stopped. right retired. Only middle still has a reading.
On stderr tellus logs every failure and restart, as JSON because the example configures the tracing subscriber that way.
What you learned
An actor fails by returning an error or panicking. Its supervision strategy decides whether it restarts or stops. A restart rebuilds the state but keeps the address and the mailbox. Together with watch, the group copes with devices failing for good. Next, you put a manager on top of the groups.