use crate::config::com::{read_config_limited_preserve_empty, save_config_with_opts};
use crate::error::{Error, Result};
use crate::object_api::ObjectOptions;
use crate::store::ECStore;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use uuid::Uuid;
pub(crate) struct MarkerRetirementContext<'a> {
pub store: Option<Arc<ECStore>>,
pub current_incarnation: Option<Uuid>,
pub lifecycle_guard: &'a rustfs_lock::NamespaceLockGuard,
}
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
struct RetirementRecord {
version: u8,
deployment_id: Uuid,
bucket: String,
incarnation_id: Uuid,
}
fn expected_record(store: &ECStore, bucket: &str, incarnation_id: Uuid) -> Result<RetirementRecord> {
let deployment_id = store
.pools
.first()
.ok_or_else(|| Error::other("missing retirement deployment"))?
.format
.id;
if deployment_id.is_nil() || incarnation_id.is_nil() {
return Err(Error::other("retirement requires non-nil deployment and incarnation IDs"));
}
Ok(RetirementRecord {
version: 1,
deployment_id,
bucket: bucket.to_owned(),
incarnation_id,
})
}
fn record_path(bucket: &str, incarnation_id: Uuid) -> String {
format!("bucket-retirements/{bucket}/{incarnation_id}.json")
}
pub(crate) async fn commit_retirement(
store: Arc<ECStore>,
bucket: &str,
incarnation_id: Uuid,
opts: &ObjectOptions,
) -> Result<()> {
let record = expected_record(&store, bucket, incarnation_id)?;
save_config_with_opts(store, &record_path(bucket, incarnation_id), serde_json::to_vec(&record)?, opts).await
}
pub(crate) async fn is_retired(store: Arc<ECStore>, bucket: &str, incarnation_id: Uuid) -> Result<bool> {
let expected = expected_record(&store, bucket, incarnation_id)?;
let bytes = match read_config_limited_preserve_empty(store, &record_path(bucket, incarnation_id), 4096).await {
Ok(bytes) => bytes,
Err(Error::ConfigNotFound) => return Ok(false),
Err(error) => return Err(error),
};
let record: RetirementRecord = serde_json::from_slice(&bytes)?;
if record != expected {
return Err(Error::other("bucket retirement identity mismatch"));
}
Ok(true)
}