use std::sync::Arc;
use kafka_client_core::{
DeliveryStatus as CoreDeliveryStatus, GroupOffsetCommitBatch as CoreBatch,
GroupOffsetCommitFailureKind as CoreFailureKind,
GroupOffsetCommitPartitionResult as CorePartitionResult,
GroupOffsetCommitTerminal as CoreTerminal,
};
use super::GroupConsumerCommitObserverError;
use crate::consumer::group_batch::GroupConsumerCheckpointObservation;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum GroupConsumerCommitDeliveryStatus {
NotSent,
PossiblySent,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct GroupConsumerCommitBrokerError {
code: i16,
}
impl GroupConsumerCommitBrokerError {
pub const fn code(self) -> i16 {
self.code
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum GroupConsumerCommitPartitionResult {
Committed,
Rejected(GroupConsumerCommitBrokerError),
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct GroupConsumerCommitPartitionOutcome {
topic: Arc<str>,
partition: i32,
result: GroupConsumerCommitPartitionResult,
}
impl GroupConsumerCommitPartitionOutcome {
pub fn topic(&self) -> &str {
&self.topic
}
pub const fn partition(&self) -> i32 {
self.partition
}
pub const fn result(&self) -> GroupConsumerCommitPartitionResult {
self.result
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct GroupConsumerCommitBatch {
throttle_time_ms: u32,
outcomes: Vec<GroupConsumerCommitPartitionOutcome>,
}
impl GroupConsumerCommitBatch {
pub const fn throttle_time_ms(&self) -> u32 {
self.throttle_time_ms
}
pub fn outcomes(&self) -> &[GroupConsumerCommitPartitionOutcome] {
&self.outcomes
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum GroupConsumerCommitFailureKind {
DeadlineElapsed,
DriverRejected,
ExecutionUnavailable,
Transport,
Compatibility,
InvalidResponse,
ResponseTooLarge,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct GroupConsumerCommitFailure {
kind: GroupConsumerCommitFailureKind,
delivery: GroupConsumerCommitDeliveryStatus,
}
impl GroupConsumerCommitFailure {
pub const fn kind(self) -> GroupConsumerCommitFailureKind {
self.kind
}
pub const fn delivery(self) -> GroupConsumerCommitDeliveryStatus {
self.delivery
}
}
#[derive(Debug)]
pub enum GroupConsumerCommitOutcome {
Committed(GroupConsumerCommitBatch),
BrokerRejected(
GroupConsumerCommitBatch,
crate::consumer::GroupConsumerCheckpoint,
),
Failed(
GroupConsumerCommitFailure,
crate::consumer::GroupConsumerCheckpoint,
),
}
pub(super) fn translate_terminal(
terminal: CoreTerminal,
observation: GroupConsumerCheckpointObservation,
) -> Result<GroupConsumerCommitOutcome, GroupConsumerCommitObserverError> {
match terminal {
CoreTerminal::Committed(batch) => {
translate_batch(batch, &observation).map(GroupConsumerCommitOutcome::Committed)
}
CoreTerminal::BrokerRejected(rejection) => {
let batch = translate_batch(rejection.into_batch(), &observation)?;
Ok(GroupConsumerCommitOutcome::BrokerRejected(
batch,
observation.into_checkpoint(),
))
}
CoreTerminal::Failed(failure) => Ok(GroupConsumerCommitOutcome::Failed(
GroupConsumerCommitFailure {
kind: failure_kind(failure.kind()),
delivery: delivery(failure.delivery()),
},
observation.into_checkpoint(),
)),
}
}
fn translate_batch(
batch: CoreBatch,
observation: &GroupConsumerCheckpointObservation,
) -> Result<GroupConsumerCommitBatch, GroupConsumerCommitObserverError> {
let (throttle_time_ms, outcomes) = batch.into_parts();
let [outcome] = outcomes.as_slice() else {
return Err(GroupConsumerCommitObserverError::InternalInvariant);
};
if outcome.topic_id() != observation.topic_id || outcome.partition() != observation.partition_id
{
return Err(GroupConsumerCommitObserverError::InternalInvariant);
}
let result = match outcome.result() {
CorePartitionResult::Committed => GroupConsumerCommitPartitionResult::Committed,
CorePartitionResult::Rejected(error) => {
GroupConsumerCommitPartitionResult::Rejected(GroupConsumerCommitBrokerError {
code: error.code(),
})
}
};
Ok(GroupConsumerCommitBatch {
throttle_time_ms,
outcomes: vec![GroupConsumerCommitPartitionOutcome {
topic: Arc::clone(observation.topic()),
partition: observation.partition(),
result,
}],
})
}
const fn failure_kind(kind: CoreFailureKind) -> GroupConsumerCommitFailureKind {
match kind {
CoreFailureKind::DeadlineElapsed => GroupConsumerCommitFailureKind::DeadlineElapsed,
CoreFailureKind::DriverRejected => GroupConsumerCommitFailureKind::DriverRejected,
CoreFailureKind::ExecutionUnavailable => {
GroupConsumerCommitFailureKind::ExecutionUnavailable
}
CoreFailureKind::Transport => GroupConsumerCommitFailureKind::Transport,
CoreFailureKind::Compatibility => GroupConsumerCommitFailureKind::Compatibility,
CoreFailureKind::InvalidResponse => GroupConsumerCommitFailureKind::InvalidResponse,
CoreFailureKind::ResponseTooLarge => GroupConsumerCommitFailureKind::ResponseTooLarge,
}
}
const fn delivery(status: CoreDeliveryStatus) -> GroupConsumerCommitDeliveryStatus {
match status {
CoreDeliveryStatus::NotSent => GroupConsumerCommitDeliveryStatus::NotSent,
CoreDeliveryStatus::PossiblySent => GroupConsumerCommitDeliveryStatus::PossiblySent,
}
}