1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
//! Communication between components.
//!
//! The main purpose of communication is for a unit is to announce updates to
//! its data set and operational state to all other components that are
//! interested. It also takes care of managing these communication lines.
//!
//! There are three main types here: Each unit has a single [`Gate`] to
//! which it hands its updates. The opposite end is called a [`Link`] and
//! is held by any interested component. A [`GateAgent`] is a reference to a
//! gate that can be used to create new links.
//!
//! The type [`GateMetrics`] can be used by units to provide some obvious
//! metrics such as the number of payload units in the data set or the time
//! of last update based on the updates sent to the gate.
use std::fmt;
use std::sync::atomic;
use std::sync::Arc;
use std::sync::atomic::{AtomicU32, AtomicUsize};
use chrono::{DateTime, Utc};
use crossbeam_utils::atomic::AtomicCell;
use futures::pin_mut;
use futures::future::{select, Either, Future};
use slab::Slab;
use serde::Deserialize;
use tokio::sync::{mpsc, oneshot};
use crate::{manager, metrics, payload};
use crate::config::Marked;
use crate::metrics::{Metric, MetricType, MetricUnit};
//------------ Configuration -------------------------------------------------
/// The queue length of an update channel.
const UPDATE_QUEUE_LEN: usize = 8;
/// The queue length of a command channel.
const COMMAND_QUEUE_LEN: usize = 16;
//------------ Gate ----------------------------------------------------------
/// A communication gate representing the source of data.
///
/// Each unit receives exactly one gate. Whenever it has new data or its
/// status changes, it sends these to (through?) the gate which takes care
/// of distributing the information to whomever is interested.
///
/// A gate may be active or dormant. It is active if there is at least one
/// party interested in receiving data updates. Otherwise it is dormant.
/// Obviously, there is no need for a unit with a dormant gate to produce
/// any updates. Units are, in fact, encouraged to suspend their operation
/// until their gate becomes active again.
///
/// In order for the gate to maintain its own state, the unit needs to
/// regularly run the [`process`](Self::process) method. In return,
/// the unit will receive an update to the gate’s state as soon as it
/// becomes available.
///
/// Sending of updates happens via the [`update_data`](Self::update_data) and
/// [`update_status`](Self::update_status) methods.
#[derive(Debug)]
pub struct Gate {
/// Receiver for commands sent in by the links.
commands: mpsc::Receiver<GateCommand>,
/// Senders to all links.
updates: Slab<UpdateSender>,
/// Number of suspended senders.
suspended: usize,
/// The current unit status.
unit_status: UnitStatus,
/// The gate metrics.
metrics: Arc<GateMetrics>,
}
impl Gate {
/// Creates a new gate.
///
/// The function returns a gate and a gate agent that allows creating new
/// links. Typically, you would pass the gate to a subsequently created
/// unit and keep the agent around for future use.
pub fn new() -> (Gate, GateAgent) {
let (tx, rx) = mpsc::channel(COMMAND_QUEUE_LEN);
let gate = Gate {
commands: rx,
updates: Slab::new(),
suspended: 0,
unit_status: UnitStatus::default(),
metrics: Default::default(),
};
let agent = GateAgent { commands: tx };
(gate, agent)
}
/// Returns a shareable reference to the gate metrics.
pub fn metrics(&self) -> Arc<GateMetrics> {
self.metrics.clone()
}
/// Runs the gate’s internal machine.
///
/// This method returns a future that runs the gate’s internal machine.
/// It resolves once the gate’s status changes. It can be dropped at any
/// time. In this case, the gate will pick up where it left off when the
/// method is called again.
///
/// The method will resolve into an error if the unit should terminate.
/// This is the case if all links and gate agents referring to the gate
/// have been dropped.
pub async fn process(&mut self) -> Result<GateStatus, Terminated> {
let status = self.get_gate_status();
loop {
let command = match self.commands.recv().await {
Some(command) => command,
None => return Err(Terminated)
};
match command {
GateCommand::Suspension { slot, suspend } => {
self.suspension(slot, suspend)
}
GateCommand::Subscribe { suspended, response } => {
self.subscribe(suspended, response)
}
}
let new_status = self.get_gate_status();
if new_status != status {
return Ok(new_status)
}
}
}
/// Runs the gate’s internal machine until a future resolves.
///
/// Ignores any gate status changes.
pub async fn process_until<Fut: Future>(
&mut self,
fut: Fut
) -> Result<Fut::Output, Terminated> {
pin_mut!(fut);
loop {
let process = self.process();
pin_mut!(process);
match select(process, fut).await {
Either::Left((Err(_), _)) => return Err(Terminated),
Either::Left((Ok(_), next_fut)) => {
fut = next_fut;
}
Either::Right((res, _)) => return Ok(res)
}
}
}
/// Updates the data set of the unit.
///
/// This method will send out the update to all active links. It will
/// also update the gate metrics based on the update.
pub async fn update_data(&mut self, update: payload::Update) {
for (_, item) in &mut self.updates {
if item.suspended {
continue
}
match item.sender.as_mut() {
Some(sender) => {
if sender.send(Ok(update.clone())).await.is_ok() {
continue
}
}
None => continue
}
item.sender = None
}
self.updates.retain(|_, item| item.sender.is_some());
self.metrics.update(&update);
}
/// Updates the unit status.
///
/// The method sends out the new status to all links.
pub async fn update_status(&mut self, update: UnitStatus) {
self.unit_status = update;
for (_, item) in &mut self.updates {
match item.sender.as_mut() {
Some(sender) => {
if sender.send(Err(update)).await.is_ok() {
continue
}
}
None => continue
}
item.sender = None
}
self.updates.retain(|_, item| item.sender.is_some());
self.metrics.update_status(update);
}
/// Returns the current gate status.
pub fn get_gate_status(&self) -> GateStatus {
if self.suspended == self.updates.len() {
GateStatus::Dormant
}
else {
GateStatus::Active
}
}
/// Processes a suspension command.
fn suspension(&mut self, slot: usize, suspend: bool) {
if let Some(item) = self.updates.get_mut(slot) {
item.suspended = suspend
}
}
/// Processes a subscribe command.
fn subscribe(
&mut self,
suspended: bool,
response: oneshot::Sender<SubscribeResponse>
) {
let (tx, receiver) = mpsc::channel(UPDATE_QUEUE_LEN);
let slot = self.updates.insert(UpdateSender {
sender: Some(tx),
suspended,
});
let subscription = SubscribeResponse {
slot,
receiver,
unit_status: self.unit_status
};
if let Err(subscription) = response.send(subscription) {
self.updates.remove(subscription.slot);
}
}
}
//------------ GateAgent -----------------------------------------------------
/// A representative of a gate allowing creation of new links for it.
///
/// The agent can be cloned and passed along. The method
/// [`create_link`](Self::create_link) can be used to create a new link.
///
/// Yes, the name is a bit of a mixed analogy.
#[derive(Clone, Debug)]
pub struct GateAgent {
commands: mpsc::Sender<GateCommand>,
}
impl GateAgent {
/// Creates a new link to the gate.
pub fn create_link(&mut self) -> Link {
Link::new(self.commands.clone())
}
}
//------------ GateMetrics ---------------------------------------------------
/// Metrics about the updates distributed via the gate.
///
/// This type is a [`metrics::Source`](crate::metrics::Source) that provides a
/// number of metrics for a unit that can be derived from the updates sent by
/// the unit and thus are common to all units.
///
/// Gates provide access to values of this type via the [`Gate::metrics`]
/// method. When stored behind an arc t can be kept and passed around freely.
#[derive(Debug, Default)]
pub struct GateMetrics {
/// The current unit status.
status: AtomicCell<UnitStatus>,
/// The serial number of the last update.
serial: AtomicU32,
/// The number of payload items in the last update.
count: AtomicUsize,
/// The date and time of the last update.
///
/// If there has never been an update, this will be `None`.
update: AtomicCell<Option<DateTime<Utc>>>,
}
impl GateMetrics {
/// Updates the metrics to match the given update.
fn update(&self, update: &payload::Update) {
self.serial.store(update.serial().into(), atomic::Ordering::Relaxed);
self.count.store(update.set().len(), atomic::Ordering::Relaxed);
self.update.store(Some(Utc::now()));
}
/// Updates the metrics to match the given unit status.
fn update_status(&self, status: UnitStatus) {
self.status.store(status)
}
}
impl GateMetrics {
const STATUS_METRIC: Metric = Metric::new(
"unit_status", "the operational status of the unit",
MetricType::Text, MetricUnit::Info
);
const SERIAL_METRIC: Metric = Metric::new(
"gate_serial", "the serial number of the unit's updates",
MetricType::Counter, MetricUnit::Info
);
const COUNT_METRIC: Metric = Metric::new(
"vrps", "the number of VRPs in the last update",
MetricType::Gauge, MetricUnit::Total
);
const UPDATE_METRIC: Metric = Metric::new(
"last_update", "the date and time of the last update",
MetricType::Text, MetricUnit::Info
);
const UPDATE_AGO_METRIC: Metric = Metric::new(
"since_last_update", "the number of seconds since the last update",
MetricType::Gauge, MetricUnit::Second
);
}
impl metrics::Source for GateMetrics {
/// Appends the current gate metrics to a target.
///
/// The name of the unit these metrics are associated with is given via
/// `unit_name`.
fn append(&self, unit_name: &str, target: &mut metrics::Target) {
target.append_simple(
&Self::STATUS_METRIC, Some(unit_name), self.status.load()
);
target.append_simple(
&Self::SERIAL_METRIC, Some(unit_name),
self.serial.load(atomic::Ordering::Relaxed)
);
target.append_simple(
&Self::COUNT_METRIC, Some(unit_name),
self.count.load(atomic::Ordering::Relaxed)
);
match self.update.load() {
Some(update) => {
target.append_simple(
&Self::UPDATE_METRIC, Some(unit_name),
update
);
let ago = Utc::now().signed_duration_since(update);
let ago = (ago.num_milliseconds() as f64) / 1000.;
target.append_simple(
&Self::UPDATE_AGO_METRIC, Some(unit_name), ago
);
}
None => {
target.append_simple(
&Self::UPDATE_METRIC, Some(unit_name),
"N/A"
);
target.append_simple(
&Self::UPDATE_AGO_METRIC, Some(unit_name), -1
);
}
}
}
}
//------------ Link ----------------------------------------------------------
/// A link to another unit.
///
/// The link allows tracking of updates of that other unit. This happens via
/// the [`query`](Self::query) method. A link’s owner can signal that they
/// are currently not interested in receiving updates via the
/// [`suspend`](Self::suspend) method. This suspension will automatically be
/// lifted the next time `query` is called.
///
/// Links can be created from the name of the unit they should be linking to
/// via [manager::load_link](crate::manager::load_link). This function is
/// also called implicitly through the impls for `Deserialize` and `From`.
/// Note, however, that the function only adds the link to a list of links
/// to be properly connected by the manager later.
#[derive(Debug, Deserialize)]
#[serde(from = "String")]
pub struct Link {
/// A sender of commands to the gate.
commands: mpsc::Sender<GateCommand>,
/// The connection to the unit.
///
/// If this is `None`, the link has not been connected yet.
connection: Option<LinkConnection>,
/// The current unit status.
unit_status: UnitStatus,
/// Are we currently suspended?
suspended: bool,
}
#[derive(Debug)]
struct LinkConnection {
/// The slot number at the gate.
slot: usize,
/// The update receiver.
updates: UpdateReceiver,
}
impl Link {
/// Creates a new, unconnected link.
fn new(commands: mpsc::Sender<GateCommand>) -> Self {
Link {
commands,
connection: None,
unit_status: UnitStatus::Healthy,
suspended: false,
}
}
/// Query for the next update.
///
/// The method returns a future that resolves into the next update. The
/// future can be dropped safely at any time.
///
/// The future either resolves into a payload update or the connected
/// unit’s new status as the error variant. The current status is also
/// available via the `get_status` method.
///
/// If the link is currently suspended, calling this method will lift the
/// suspension.
pub async fn query(&mut self) -> Result<payload::Update, UnitStatus> {
self.connect(false).await?;
let conn = self.connection.as_mut().unwrap();
match conn.updates.recv().await {
Some(Ok(update)) => Ok(update),
Some(Err(status)) => {
self.unit_status = status;
Err(status)
}
None => {
self.unit_status = UnitStatus::Gone;
Err(UnitStatus::Gone)
}
}
}
/// Query a suspended link.
///
/// When a link is suspended, it still received updates to the unit’s
/// status. These updates can also be queried for explicitly via this
/// method.
///
/// Much like `query`, the future returned by this method can safely be
/// dropped at any time.
pub async fn query_suspended(&mut self) -> UnitStatus {
if let Err(err) = self.connect(true).await {
return err
}
let conn = self.connection.as_mut().unwrap();
loop {
match conn.updates.recv().await {
Some(Ok(_)) => continue,
Some(Err(status)) => return status,
None => {
self.unit_status = UnitStatus::Gone;
return UnitStatus::Gone
}
}
}
}
/// Suspends the link.
///
/// A suspended link will not receive any payload updates from the
/// connected unit. It will, however, still receive status updates.
///
/// The suspension is lifted automatically the next time `query` is
/// called.
///
/// Note that this is an async method that needs to be awaited in order
/// to do anything.
pub async fn suspend(&mut self) {
if !self.suspended {
self.request_suspend(true).await
}
}
/// Request suspension from the gate.
async fn request_suspend(&mut self, suspend: bool) {
if self.connection.is_none() {
return
}
let conn = self.connection.as_mut().unwrap();
if self.commands.send(GateCommand::Suspension {
slot: conn.slot,
suspend
}).await.is_err() {
self.unit_status = UnitStatus::Gone
}
else {
self.suspended = suspend
}
}
/// Returns the current status of the connected unit.
pub fn get_status(&self) -> UnitStatus {
self.unit_status
}
/// Connects the link to the gate.
async fn connect(&mut self, suspended: bool) -> Result<(), UnitStatus> {
if self.connection.is_some() {
return Ok(())
}
if let UnitStatus::Gone = self.unit_status {
return Err(UnitStatus::Gone)
}
let (tx, rx) = oneshot::channel();
if self.commands.send(
GateCommand::Subscribe { suspended, response: tx }
).await.is_err() {
self.unit_status = UnitStatus::Gone;
return Err(UnitStatus::Gone)
}
let sub = match rx.await {
Ok(sub) => sub,
Err(_) => {
self.unit_status = UnitStatus::Gone;
return Err(UnitStatus::Gone)
}
};
self.connection = Some(LinkConnection {
slot: sub.slot,
updates: sub.receiver,
});
self.unit_status = sub.unit_status;
self.suspended = suspended;
if self.unit_status == UnitStatus::Gone {
Err(UnitStatus::Gone)
}
else {
Ok(())
}
}
}
impl From<Marked<String>> for Link {
fn from(name: Marked<String>) -> Self {
manager::load_link(name)
}
}
impl From<String> for Link {
fn from(name: String) -> Self {
manager::load_link(name.into())
}
}
//------------ GateStatus ----------------------------------------------------
/// The status of a gate.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum GateStatus {
/// The gate is connected to at least one active link.
///
/// The unit owning this gate should produce updates.
Active,
/// The gate is not connected to any active links.
///
/// This doesn’t necessarily mean that there are no links at all, only
/// that currently none of the links is interested in receiving updates
/// from this unit.
Dormant,
}
impl Default for GateStatus {
fn default() -> GateStatus {
GateStatus::Active
}
}
//------------ UnitStatus ----------------------------------------------------
/// The operational status of a unit.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum UnitStatus {
/// The unit is ready to produce data updates.
///
/// Note that this status does not necessarily mean that the unit is
/// actually producing updates, only that it could. That is, if a unit’s
/// gate is dormant and the unit ceases operation because nobody cares,
/// it is still in healthy status.
Healthy,
/// The unit had to temporarily suspend operation.
///
/// If it sets this status, the unit will try to become healthy again
/// later. The status is typically used if a server has become
/// unavailable.
Stalled,
/// The unit had to permanently suspend operation.
///
/// This status indicates that the unit will not become healthy ever
/// again. Links to the unit can safely be dropped.
Gone,
}
impl Default for UnitStatus {
fn default() -> Self {
UnitStatus::Healthy
}
}
impl fmt::Display for UnitStatus {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(match *self {
UnitStatus::Healthy => "healthy",
UnitStatus::Stalled => "stalled",
UnitStatus::Gone => "gone",
})
}
}
//------------ Terminated ----------------------------------------------------
/// An error signalling that a unit has been terminated.
///
/// In response to this error, a unit’s run function should return.
#[derive(Clone, Copy, Debug)]
pub struct Terminated;
//------------ GateCommand ---------------------------------------------------
/// A command sent by a link to a gate.
#[derive(Debug)]
enum GateCommand {
/// Change the suspension state of a link.
Suspension {
/// The slot number of the link to be manipulated.
slot: usize,
/// Suspend the link?
suspend: bool,
},
/// Subscribe to the gate.
Subscribe {
/// Should the subscription start in suspended state?
suspended: bool,
/// The sender for the response.
///
/// The response payload is the slot number of the subscription.
response: oneshot::Sender<SubscribeResponse>,
}
}
//------------ UpdateSender --------------------------------------------------
/// The gate side of sending updates.
#[derive(Debug)]
struct UpdateSender {
/// The actual sender.
///
/// This is an option to facilitate deleted dropped links. When sending
/// fails, we swap this to `None` and then go over the slab again and
/// drop anything that is `None`. We need to do this because
/// `Slab::retain` isn’t async but `mpsc::Sender::send` is.
sender: Option<mpsc::Sender<Result<payload::Update, UnitStatus>>>,
/// Are we currently suspended?
suspended: bool
}
//------------ UpdateReceiver ------------------------------------------------
/// The link side of receiving updates.
type UpdateReceiver = mpsc::Receiver<Result<payload::Update, UnitStatus>>;
//------------ SubscribeResponse ---------------------------------------------
/// The response to a subscribe request.
#[derive(Debug)]
struct SubscribeResponse {
/// The slot number of this subscription in the gate.
slot: usize,
/// The update receiver for this subscription.
receiver: UpdateReceiver,
/// The current unit status.
unit_status: UnitStatus,
}