use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::{Arc, OnceLock};
use std::time::SystemTime;
use arc_swap::ArcSwap;
use async_trait::async_trait;
use bytes::Bytes;
use claviron_runtime::congif::{
self, ConfigFile, ConfigObserver, Configurable,
};
use claviron_runtime::registry::{self, Bootable, Reloadable};
use claviron_runtime::{AppState, Component};
use orbitive::Fleet;
use tokio::sync::Notify;
use super::config::{ChannelConfig, ConfigError, LoggingConfig};
use super::record::{self, ChannelId, Record};
use super::ring::{
FRAME_KIND_PART, FRAME_KIND_RECORD, LogFrame, MAX_FRAMES, PART_HEADER,
PAYLOAD_CAPACITY,
};
pub struct LogChannels {
config_path: PathBuf,
table: ArcSwap<ChannelTable>,
changed: Notify,
fleet: OnceLock<Arc<Fleet>>,
}
#[derive(Default)]
pub(crate) struct ChannelTable {
ids: HashMap<String, ChannelId>,
access: Option<ChannelId>,
pub(crate) channels: Vec<Option<(String, ChannelConfig)>>,
}
impl ChannelTable {
fn with(
&self,
config: &LoggingConfig,
path: &Path,
) -> Result<Self, ConfigError> {
let mut channels: Vec<Option<(String, ChannelConfig)>> =
self.channels.iter().map(|_| None).collect();
let mut ids = HashMap::with_capacity(config.channels.len());
for (name, channel) in &config.channels {
let id = match self.ids.get(name) {
Some(id) => *id,
None => {
let next = channels.len();
if next > usize::from(u16::MAX) {
return Err(ConfigError::TooManyChannels {
path: path.to_owned(),
count: next + 1,
});
}
channels.push(None);
ChannelId(next as u16)
}
};
channels[usize::from(id.0)] = Some((name.clone(), channel.clone()));
ids.insert(name.clone(), id);
}
let access =
config.access_log.as_ref().and_then(|name| ids.get(name).copied());
Ok(Self { ids, access, channels })
}
}
impl std::fmt::Debug for LogChannels {
fn fmt(
&self,
f: &mut std::fmt::Formatter<'_>,
) -> std::fmt::Result {
f.debug_struct("LogChannels")
.field("channels", &self.table.load().ids.len())
.finish_non_exhaustive()
}
}
#[derive(Debug, thiserror::Error)]
pub enum EmitError {
#[error("the log pipeline has not booted")]
NotBooted,
#[error("no declared log channel has id {0}")]
UnknownChannel(u16),
#[error("record cannot fit the log frame budget")]
RecordCapacity,
#[error("log ring write failed: {0}")]
Ring(#[from] std::io::Error),
}
impl LogChannels {
pub(crate) fn new(config_path: PathBuf) -> Self {
Self {
config_path,
table: ArcSwap::from_pointee(ChannelTable::default()),
changed: Notify::new(),
fleet: OnceLock::new(),
}
}
pub(crate) fn table(&self) -> Arc<ChannelTable> {
self.table.load_full()
}
pub(crate) async fn changed(&self) {
self.changed.notified().await;
}
pub(super) fn apply(
&self,
config: &LoggingConfig,
) -> Result<(), ConfigError> {
let table = self.table.load().with(config, &self.config_path)?;
self.table.store(Arc::new(table));
self.changed.notify_one();
Ok(())
}
pub fn access(&self) -> Option<ChannelId> {
self.table.load().access
}
pub fn channel(
&self,
name: &str,
) -> Option<ChannelId> {
self.table.load().ids.get(name).copied()
}
pub fn emit(
&self,
channel: ChannelId,
record: &Record<'_>,
) -> Result<(), EmitError> {
if self
.table
.load()
.channels
.get(usize::from(channel.0))
.is_none_or(Option::is_none)
{
return Err(EmitError::UnknownChannel(channel.0));
}
let fleet = self.fleet.get().ok_or(EmitError::NotBooted)?;
let part = PAYLOAD_CAPACITY - PART_HEADER;
let encoded = record::encode(
channel,
record,
SystemTime::now(),
MAX_FRAMES * part,
)
.ok_or(EmitError::RecordCapacity)?;
if encoded.len() > MAX_FRAMES * part {
return Err(EmitError::RecordCapacity);
}
if encoded.len() <= PAYLOAD_CAPACITY {
fleet.publish_notified::<LogFrame>(
FRAME_KIND_RECORD,
0,
Bytes::from(encoded),
)?;
return Ok(());
}
let count = encoded.len().div_ceil(part);
if count > MAX_FRAMES {
return Err(EmitError::RecordCapacity);
}
let count =
u8::try_from(count).map_err(|_| EmitError::RecordCapacity)?;
let parts = encoded
.chunks(part)
.enumerate()
.map(|(index, bytes)| {
let mut frame = Vec::with_capacity(PART_HEADER + bytes.len());
frame.push(index as u8);
frame.push(count);
frame.extend_from_slice(bytes);
Bytes::from(frame)
})
.collect();
fleet.publish_batch_notified::<LogFrame>(FRAME_KIND_PART, 0, parts)?;
Ok(())
}
}
impl Component<AppState> for LogChannels {
type Deps = (Arc<claviron_orbit::FleetProvider>,);
fn name(&self) -> &'static str {
"LogChannels"
}
fn as_configurable(&self) -> Option<&dyn congif::ConfigRegistration> {
(!self.config_path.as_os_str().is_empty()).then_some(self)
}
fn as_reloadable(&self) -> Option<&dyn Reloadable<AppState>> {
(!self.config_path.as_os_str().is_empty()).then_some(self)
}
fn as_bootable(&self) -> Option<&dyn Bootable<AppState, Self::Deps>> {
Some(self)
}
}
#[async_trait]
impl Bootable<AppState, (Arc<claviron_orbit::FleetProvider>,)> for LogChannels {
async fn boot(
&self,
(fleet,): (Arc<claviron_orbit::FleetProvider>,),
_state: &AppState,
) -> registry::Result<()> {
let fleet = super::shared_fleet(&fleet)?;
let _ = self.fleet.set(fleet);
Ok(())
}
}
#[async_trait]
impl Configurable for LogChannels {
type Config = LoggingConfig;
fn source(&self) -> ConfigFile {
ConfigFile::yaml(&self.config_path)
}
fn absent_source(&self) -> Option<LoggingConfig> {
Some(LoggingConfig::default())
}
fn validate_config(
&self,
config: &LoggingConfig,
) -> congif::Result<()> {
config.validate(&self.config_path).map_err(congif::Error::validation)
}
async fn configure(
&self,
config: &LoggingConfig,
) -> congif::Result<()> {
self.apply(config).map_err(congif::Error::validation)
}
fn as_config_observer(
&self
) -> Option<&dyn ConfigObserver<Config = Self::Config>> {
Some(self)
}
}
#[async_trait]
impl ConfigObserver for LogChannels {
async fn config_changed(
&self,
config: &LoggingConfig,
) -> congif::Result<()> {
self.apply(config).map_err(congif::Error::validation)
}
}
#[async_trait]
impl Reloadable<AppState> for LogChannels {
async fn reload(
&self,
state: &AppState,
) -> registry::Result<()> {
let config = state.config_reader().snapshot::<Self>()?;
self.apply(&config)
.map_err(|error| registry::Error::msg(error.to_string()))
}
}