Skip to main content

feldera_types/
transaction.rs

1use std::fmt::{Display, Formatter};
2
3use serde::{Deserialize, Serialize};
4use utoipa::ToSchema;
5use uuid::Uuid;
6
7pub type TransactionId = i64;
8
9/// Response to a `/start_transaction` request.
10#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
11pub struct StartTransactionResponse {
12    pub transaction_id: i64,
13
14    /// Pass back to `/commit_transaction` to detect pipeline restarts.
15    ///
16    /// Only `None` if sent by an older pipeline.
17    #[serde(default)]
18    pub incarnation_uuid: Option<Uuid>,
19}
20
21impl StartTransactionResponse {
22    pub fn new(transaction_id: TransactionId) -> Self {
23        Self {
24            transaction_id,
25            incarnation_uuid: None,
26        }
27    }
28}
29
30/// Summary of transaction commit progress.
31#[derive(Clone, Debug, Default, Serialize, Deserialize, ToSchema)]
32pub struct CommitProgressSummary {
33    /// Number of operators that have been fully flushed.
34    pub completed: u64,
35
36    /// Number of operators that are currently being flushed.
37    pub in_progress: u64,
38
39    /// Number of operators that haven't started flushing.
40    pub remaining: u64,
41
42    /// Number of records processed by operators that are currently being flushed.
43    pub in_progress_processed_records: u64,
44
45    /// Total number of records that operators that are currently being flushed need to process.
46    pub in_progress_total_records: u64,
47}
48
49impl CommitProgressSummary {
50    pub fn new() -> Self {
51        Self::default()
52    }
53
54    pub fn merge(&mut self, other: &CommitProgressSummary) {
55        self.completed += other.completed;
56        self.in_progress += other.in_progress;
57        self.remaining += other.remaining;
58        self.in_progress_processed_records += other.in_progress_processed_records;
59        self.in_progress_total_records += other.in_progress_total_records;
60    }
61}
62
63impl Display for CommitProgressSummary {
64    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
65        write!(
66            f,
67            "completed: {} operators, evaluating: {} operators [{}/{} changes processed], remaining: {} operators",
68            self.completed,
69            self.in_progress,
70            self.in_progress_processed_records,
71            self.in_progress_total_records,
72            self.remaining
73        )
74    }
75}
76
77/// Phase of a concurrent bootstrap.
78///
79/// `#[repr(u8)]` + `NoUninit` so it can be stored in a lock-free `Atomic`.
80#[derive(
81    Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize, ToSchema, bytemuck::NoUninit,
82)]
83#[repr(u8)]
84pub enum ConcurrentBootstrapPhase {
85    /// No concurrent bootstrap is in progress.
86    #[default]
87    Inactive,
88
89    /// New and modified views are backfilling in the background while the
90    /// pre-existing views stay live and serving.
91    ConcurrentBootstrapping,
92
93    /// The cutover window: inputs are paused while recorded changes are
94    /// synchronized into the new views.
95    Synchronizing,
96}
97
98impl Display for ConcurrentBootstrapPhase {
99    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
100        match self {
101            ConcurrentBootstrapPhase::Inactive => write!(f, "inactive"),
102            ConcurrentBootstrapPhase::ConcurrentBootstrapping => {
103                write!(f, "concurrent bootstrapping")
104            }
105            ConcurrentBootstrapPhase::Synchronizing => write!(f, "synchronizing"),
106        }
107    }
108}