use super::{PartitionExecutorCfg, PartitionExecutorScope, PartitionLimits, PreparedGroup};
use crate::error::{NasaRedisError, Result};
use std::collections::HashMap;
use std::sync::atomic::{AtomicBool, AtomicUsize};
#[derive(Debug, Clone)]
pub(super) struct DomainQuota {
pub records: usize,
pub bytes: usize,
pub batches: usize,
pub deletes: usize,
}
pub(super) struct DomainSpec {
pub group: Option<String>,
pub partition: Option<u32>,
pub quota: DomainQuota,
}
pub(super) struct ExecutionPlan {
pub scope: PartitionExecutorScope,
pub specs: Vec<DomainSpec>,
pub mapping: HashMap<String, Vec<usize>>,
pub runner_config: napart::RunnerConfig,
}
impl ExecutionPlan {
pub fn build(
groups: &[PreparedGroup],
config: &PartitionExecutorCfg,
limits: &PartitionLimits,
plans: usize,
) -> Result<Self> {
let runner_config = config.runner_config(plans)?;
let mut ordered: Vec<_> = groups.iter().collect();
ordered.sort_by(|a, b| a.id.cmp(&b.id));
let count = match config.scope {
PartitionExecutorScope::Source => 1,
PartitionExecutorScope::Group => groups.len(),
PartitionExecutorScope::Stream => groups
.iter()
.try_fold(0usize, |sum, group| sum.checked_add(group.count as usize))
.ok_or_else(|| NasaRedisError::Config("execution domain count overflow".into()))?,
};
if count == 0
|| config.max_runners == 0
|| config.max_runners > 4096
|| count > config.max_runners
|| config.max_total_partitions == 0
|| count
.checked_mul(runner_config.partitions)
.is_none_or(|slots| slots > config.max_total_partitions)
{
return Err(NasaRedisError::Config(
"execution domains exceed max_runners or max_total_partitions".into(),
));
}
let mut identities = Vec::with_capacity(count);
let mut batches = Vec::with_capacity(count);
let mut mapping = HashMap::new();
if config.scope == PartitionExecutorScope::Source {
identities.push((None, None));
batches.push(
groups
.iter()
.map(|g| g.stream_cfg.batch_size)
.max()
.unwrap_or(0),
);
}
for group in ordered {
let mut ids = Vec::with_capacity(group.count as usize);
if config.scope == PartitionExecutorScope::Group {
identities.push((Some(group.id.clone()), None));
batches.push(group.stream_cfg.batch_size);
}
for partition in 0..group.count {
if config.scope == PartitionExecutorScope::Stream {
identities.push((Some(group.id.clone()), Some(partition)));
batches.push(group.stream_cfg.batch_size);
}
ids.push(identities.len() - 1);
}
mapping.insert(group.layout.prefix.clone(), ids);
}
let min_bytes = batches
.iter()
.map(|batch| batch.checked_mul(limits.max_record_bytes))
.collect::<Option<Vec<_>>>()
.ok_or_else(|| {
NasaRedisError::Config("execution domain payload minimum overflow".into())
})?;
let records = allocate(limits.record_limit(), &batches, "records")?;
let bytes = allocate(
limits.max_inflight_payload_bytes,
&min_bytes,
"payload bytes",
)?;
let read_batches = allocate(limits.max_active_batches, &vec![1; count], "active batches")?;
let deletes = allocate(
limits.max_async_delete_records,
&vec![1; count],
"async delete records",
)?;
let specs = identities
.into_iter()
.enumerate()
.map(|(id, (group, partition))| DomainSpec {
group,
partition,
quota: DomainQuota {
records: records[id],
bytes: bytes[id],
batches: read_batches[id],
deletes: deletes[id],
},
})
.collect();
Ok(Self {
scope: config.scope,
specs,
mapping,
runner_config,
})
}
}
fn allocate(total: usize, minimum: &[usize], resource: &str) -> Result<Vec<usize>> {
let required = minimum
.iter()
.try_fold(0usize, |sum, n| sum.checked_add(*n));
let Some(spare) = required.and_then(|required| total.checked_sub(required)) else {
return Err(NasaRedisError::Config(format!(
"execution domain {resource} budget cannot reserve every domain's minimum"
)));
};
if minimum.is_empty() || minimum.contains(&0) {
return Err(NasaRedisError::Config(
"execution domain minimum must be positive".into(),
));
}
let count = minimum.len();
Ok(minimum
.iter()
.enumerate()
.map(|(id, minimum)| minimum + spare / count + usize::from(id < spare % count))
.collect())
}
pub(super) struct ExecutionDomain {
pub spec: DomainSpec,
pub runner: napart::PartitionRunner,
pub degraded: AtomicBool,
pub delete_records: AtomicUsize,
}
pub(super) struct ExecutionDomains {
pub scope: PartitionExecutorScope,
pub domains: Vec<ExecutionDomain>,
mapping: HashMap<String, Vec<usize>>,
}
impl ExecutionDomains {
pub fn new(plan: ExecutionPlan) -> Result<Self> {
let registry = napart::PartitionRunnerRegistry::builder()
.max_runners(plan.specs.len())
.build()
.map_err(|e| NasaRedisError::Config(e.to_string()))?;
let mut domains = Vec::with_capacity(plan.specs.len());
for (id, spec) in plan.specs.into_iter().enumerate() {
let runner = registry
.get_or_create(format!("redis-partition-{id}"), plan.runner_config.clone())
.map_err(|e| NasaRedisError::Config(e.to_string()))?;
domains.push(ExecutionDomain {
spec,
runner,
degraded: AtomicBool::new(false),
delete_records: AtomicUsize::new(0),
});
}
Ok(Self {
scope: plan.scope,
domains,
mapping: plan.mapping,
})
}
pub fn domain(&self, group: &str, partition: u32) -> usize {
self.mapping[group][partition as usize]
}
}