use crate::error::{NasaRedisError, Result};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct PartitionLimits {
pub max_inflight_publishes: usize,
pub max_inflight_publish_bytes: usize,
pub max_inflight_records: usize,
pub max_inflight_payload_bytes: usize,
pub max_active_batches: usize,
pub max_read_waiters: usize,
pub max_record_bytes: usize,
pub max_inflight_tasks: usize,
pub max_continuations: usize,
pub max_pending_commits: usize,
pub max_commit_records: usize,
pub max_async_delete_records: usize,
pub max_retry_tickets: usize,
pub max_ordered_keys: usize,
pub max_blocked_keys_per_source: usize,
pub max_deferred_records_per_key: usize,
pub max_deferred_records: usize,
pub max_unroutable_records: usize,
}
impl Default for PartitionLimits {
fn default() -> Self {
Self {
max_inflight_publishes: 128,
max_inflight_publish_bytes: 64 << 20,
max_inflight_records: 4096,
max_inflight_payload_bytes: 256 << 20,
max_active_batches: 256,
max_read_waiters: 4096,
max_record_bytes: 1 << 20,
max_inflight_tasks: 4096,
max_continuations: 4096,
max_pending_commits: 4096,
max_commit_records: 4096,
max_async_delete_records: 4096,
max_retry_tickets: 4096,
max_ordered_keys: 4096,
max_blocked_keys_per_source: 4096,
max_deferred_records_per_key: 4096,
max_deferred_records: 4096,
max_unroutable_records: 4096,
}
}
}
impl PartitionLimits {
pub(crate) fn record_limit(&self) -> usize {
[
self.max_inflight_records,
self.max_inflight_tasks,
self.max_continuations,
self.max_pending_commits,
self.max_commit_records,
self.max_retry_tickets,
self.max_ordered_keys,
self.max_deferred_records,
self.max_deferred_records_per_key,
self.max_unroutable_records,
]
.into_iter()
.min()
.unwrap_or(0)
}
pub(crate) fn validate(&self, batch: usize) -> Result<()> {
let bytes = batch.checked_mul(self.max_record_bytes);
if batch == 0
|| self.record_limit() < batch
|| self.max_record_bytes == 0
|| self.max_active_batches == 0
|| self.max_read_waiters == 0
|| self.max_inflight_publishes == 0
|| self.max_async_delete_records == 0
|| self.max_blocked_keys_per_source < batch
|| self.max_deferred_records_per_key < batch
|| bytes.is_none_or(|n| n > self.max_inflight_payload_bytes)
|| self.max_record_bytes > self.max_inflight_publish_bytes
{
return Err(NasaRedisError::Config(
"partition limits cannot reserve one complete batch and its continuations".into(),
));
}
Ok(())
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum PartitionExecutorScope {
#[default]
Source,
Group,
Stream,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct PartitionExecutorCfg {
pub scope: PartitionExecutorScope,
pub max_runners: usize,
pub max_total_partitions: usize,
pub partitions: usize,
pub queue_capacity_per_type: usize,
pub global_inflight: usize,
pub max_type_states: usize,
}
impl Default for PartitionExecutorCfg {
fn default() -> Self {
Self {
scope: PartitionExecutorScope::Source,
max_runners: 256,
max_total_partitions: 4096,
partitions: 8,
queue_capacity_per_type: 256,
global_inflight: 4096,
max_type_states: 4096,
}
}
}
impl PartitionExecutorCfg {
pub(crate) fn runner_config(&self, plans: usize) -> Result<napart::RunnerConfig> {
let config = napart::RunnerConfig {
partitions: self.partitions,
queue_capacity_per_type: self.queue_capacity_per_type,
global_inflight: self.global_inflight,
max_type_states: self.max_type_states,
..Default::default()
}
.validated()
.map_err(|e| NasaRedisError::Config(e.to_string()))?;
if plans == 0
|| config
.partitions
.checked_mul(plans)
.and_then(|n| n.checked_mul(2))
.is_none_or(|n| n > config.max_type_states)
{
return Err(NasaRedisError::Config(
"partition executor max_type_states cannot cover all consumer plans".into(),
));
}
Ok(config)
}
}