use serde::Deserialize;
use serde::Serialize;
use crate::StoreContractViolation;
pub const CHECKPOINT_SCHEMA_VERSION: u16 = 1;
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum CheckpointBackend {
Local,
RocksDb,
}
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields, rename_all = "camelCase")]
pub struct CheckpointOffsets {
pub appended_offset: i64,
pub durable_offset: i64,
pub consume_queue_offset: i64,
pub index_offset: i64,
}
impl CheckpointOffsets {
pub fn validate(self) -> Result<(), StoreContractViolation> {
if self.appended_offset < 0 || self.durable_offset < 0 || self.consume_queue_offset < 0 || self.index_offset < 0
{
return Err(StoreContractViolation::CheckpointInvalidOffsets(
"checkpoint offsets cannot be negative".to_string(),
));
}
if self.durable_offset > self.appended_offset {
return Err(StoreContractViolation::CheckpointInvalidOffsets(
"durable_offset cannot exceed appended_offset".to_string(),
));
}
if self.consume_queue_offset > self.durable_offset || self.index_offset > self.durable_offset {
return Err(StoreContractViolation::CheckpointInvalidOffsets(
"derived offsets cannot exceed durable_offset".to_string(),
));
}
Ok(())
}
}
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields, rename_all = "camelCase")]
pub struct CheckpointStorageIdentity {
pub volume_id: String,
pub wal_generation: u64,
}
impl CheckpointStorageIdentity {
fn validate(&self) -> Result<(), StoreContractViolation> {
require_identifier("storageIdentity.volumeId", &self.volume_id)?;
if self.wal_generation == 0 {
return Err(StoreContractViolation::CheckpointInvalidField {
field: "storageIdentity.walGeneration",
reason: "must be greater than zero".to_string(),
});
}
Ok(())
}
}
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields, rename_all = "camelCase")]
pub struct CheckpointArtifact {
pub schema_version: u16,
pub checkpoint_id: String,
pub checkpoint_set_id: String,
pub generation: u64,
pub barrier_id: String,
pub created_at_unix_millis: u64,
pub length_bytes: u64,
pub sha256: String,
pub uri: String,
}
impl CheckpointArtifact {
pub fn validate(&self) -> Result<(), StoreContractViolation> {
if self.schema_version != CHECKPOINT_SCHEMA_VERSION {
return Err(StoreContractViolation::CheckpointSchemaVersion {
expected: CHECKPOINT_SCHEMA_VERSION,
actual: self.schema_version,
});
}
require_identifier("checkpointId", &self.checkpoint_id)?;
require_identifier("checkpointSetId", &self.checkpoint_set_id)?;
require_identifier("barrierId", &self.barrier_id)?;
if self.generation == 0 {
return invalid_field("generation", "must be greater than zero");
}
if self.created_at_unix_millis == 0 {
return invalid_field("createdAtUnixMillis", "must be greater than zero");
}
if self.length_bytes == 0 {
return invalid_field("lengthBytes", "must be greater than zero");
}
require_sha256(&self.sha256)?;
if self.uri.trim().is_empty() || self.uri.bytes().any(|byte| matches!(byte, b'\r' | b'\n')) {
return invalid_field("uri", "must be a non-empty single-line URI");
}
Ok(())
}
}
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields, rename_all = "camelCase")]
pub struct CheckpointRequest {
pub checkpoint_id: String,
pub checkpoint_set_id: String,
pub generation: u64,
pub barrier_id: String,
pub member_id: String,
pub offsets: CheckpointOffsets,
pub storage_identity: CheckpointStorageIdentity,
}
impl CheckpointRequest {
pub fn validate(&self) -> Result<(), StoreContractViolation> {
require_identifier("checkpointId", &self.checkpoint_id)?;
require_identifier("checkpointSetId", &self.checkpoint_set_id)?;
require_identifier("barrierId", &self.barrier_id)?;
require_identifier("memberId", &self.member_id)?;
if self.generation == 0 {
return invalid_field("generation", "must be greater than zero");
}
self.offsets.validate()?;
self.storage_identity.validate()
}
}
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields, rename_all = "camelCase")]
pub struct CheckpointManifest {
pub artifact: CheckpointArtifact,
pub member_id: String,
pub backend: CheckpointBackend,
pub offsets: CheckpointOffsets,
pub storage_identity: CheckpointStorageIdentity,
pub wal_retained: bool,
pub persistent_volume_retained: bool,
}
impl CheckpointManifest {
pub fn validate(&self) -> Result<(), StoreContractViolation> {
self.artifact.validate()?;
require_identifier("memberId", &self.member_id)?;
self.offsets.validate()?;
self.storage_identity.validate()?;
if !self.wal_retained || !self.persistent_volume_retained {
return Err(StoreContractViolation::CheckpointDestructiveRollback);
}
Ok(())
}
}
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields, rename_all = "camelCase")]
pub struct CheckpointRestoreVerification {
pub checkpoint_id: String,
pub generation: u64,
pub verified_at_unix_millis: u64,
pub checksum_verified: bool,
pub offsets_verified: bool,
pub storage_identity_verified: bool,
pub wal_retained: bool,
pub persistent_volume_retained: bool,
}
impl CheckpointRestoreVerification {
pub fn validate(&self) -> Result<(), StoreContractViolation> {
require_identifier("checkpointId", &self.checkpoint_id)?;
if self.generation == 0 || self.verified_at_unix_millis == 0 {
return invalid_field("generation/verifiedAtUnixMillis", "must be greater than zero");
}
if !self.checksum_verified
|| !self.offsets_verified
|| !self.storage_identity_verified
|| !self.wal_retained
|| !self.persistent_volume_retained
{
return Err(StoreContractViolation::CheckpointRestoreVerificationIncomplete);
}
Ok(())
}
}
fn require_identifier(field: &'static str, value: &str) -> Result<(), StoreContractViolation> {
if value.is_empty()
|| value.len() > 256
|| !value
.bytes()
.all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'.' | b'_' | b'-' | b':' | b'/' | b'@'))
{
return invalid_field(field, "must be a canonical identifier");
}
Ok(())
}
fn require_sha256(value: &str) -> Result<(), StoreContractViolation> {
if value.len() != 64
|| !value
.bytes()
.all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte))
{
return invalid_field("sha256", "must be 64 lowercase hexadecimal characters");
}
Ok(())
}
fn invalid_field<T>(field: &'static str, reason: impl Into<String>) -> Result<T, StoreContractViolation> {
Err(StoreContractViolation::CheckpointInvalidField {
field,
reason: reason.into(),
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn domain_json_preserves_the_stable_wire_field_names() {
let request = CheckpointRequest {
checkpoint_id: "checkpoint-7".to_string(),
checkpoint_set_id: "set-7".to_string(),
generation: 7,
barrier_id: "barrier-42".to_string(),
member_id: "broker-a".to_string(),
offsets: CheckpointOffsets {
appended_offset: 120,
durable_offset: 120,
consume_queue_offset: 100,
index_offset: 100,
},
storage_identity: CheckpointStorageIdentity {
volume_id: "pvc-a".to_string(),
wal_generation: 7,
},
};
request.validate().expect("valid checkpoint request");
let value = serde_json::to_value(request).expect("serialize checkpoint request");
assert!(value.get("checkpointId").is_some());
assert!(value.get("storageIdentity").is_some());
assert!(value.get("checkpoint_id").is_none());
}
#[test]
fn destructive_or_unordered_checkpoint_state_is_rejected() {
let offsets = CheckpointOffsets {
appended_offset: 100,
durable_offset: 101,
consume_queue_offset: 90,
index_offset: 90,
};
assert!(matches!(
offsets.validate(),
Err(StoreContractViolation::CheckpointInvalidOffsets(_))
));
}
}