Skip to main content

rustfs_replication/
filemeta.rs

1// Copyright 2024 RustFS Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use crate::http::internal_key_rustfs;
16use bytes::Bytes;
17use core::fmt;
18use regex::Regex;
19use serde::{Deserialize, Serialize};
20use std::any::Any;
21use std::collections::HashMap;
22use std::sync::LazyLock;
23use std::time::Duration;
24use time::OffsetDateTime;
25use uuid::Uuid;
26
27pub const REPLICATION_RESET: &str = "replication-reset";
28pub const REPLICATION_STATUS: &str = "replication-status";
29
30/// The S3 wire spelling of the unversioned ("null") version id. Owned here as
31/// part of the replication wire contracts; `rustfs-filemeta` keeps its own
32/// copy of the same literal (the crates are intentionally independent).
33pub const NULL_VERSION_ID: &str = "null";
34
35// ReplicateQueued - replication being queued trail
36pub const REPLICATE_QUEUED: &str = "replicate:queue";
37
38// ReplicateExisting - audit trail for existing objects replication
39pub const REPLICATE_EXISTING: &str = "replicate:existing";
40// ReplicateExistingDelete - audit trail for delete replication triggered for existing delete markers
41pub const REPLICATE_EXISTING_DELETE: &str = "replicate:existing:delete";
42
43// ReplicateMRF - audit trail for replication from Most Recent Failures (MRF) queue
44pub const REPLICATE_MRF: &str = "replicate:mrf";
45// ReplicateIncoming - audit trail of inline replication
46pub const REPLICATE_INCOMING: &str = "replicate:incoming";
47// ReplicateIncomingDelete - audit trail of inline replication of deletes.
48pub const REPLICATE_INCOMING_DELETE: &str = "replicate:incoming:delete";
49
50// ReplicateHeal - audit trail for healing of failed/pending replications
51pub const REPLICATE_HEAL: &str = "replicate:heal";
52// ReplicateHealDelete - audit trail of healing of failed/pending delete replications.
53pub const REPLICATE_HEAL_DELETE: &str = "replicate:heal:delete";
54
55/// StatusType of Replication for x-amz-replication-status header
56///
57/// NOTE: `rustfs-filemeta` owns a sibling copy of this enum (plus
58/// `VersionPurgeStatusType` and `ReplicationState`) bound to the xl.meta disk
59/// format, while this copy is bound to the MRF/resync persistence format.
60/// When adding or renaming a variant here, reconcile the sibling and the
61/// conversion layer — the reconciliation tests in
62/// `crates/ecstore/src/bucket/replication/replication_filemeta_boundary.rs`
63/// fail to compile until both sides agree.
64#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default, Hash)]
65pub enum ReplicationStatusType {
66    /// Pending - replication is pending.
67    Pending,
68    /// Completed - replication completed ok.
69    Completed,
70    /// CompletedLegacy was called "COMPLETE" incorrectly.
71    CompletedLegacy,
72    /// Failed - replication failed.
73    Failed,
74    /// Replica - this is a replica.
75    Replica,
76    #[default]
77    Empty,
78}
79
80impl ReplicationStatusType {
81    /// Returns string representation of status
82    pub fn as_str(&self) -> &'static str {
83        match self {
84            ReplicationStatusType::Pending => "PENDING",
85            ReplicationStatusType::Completed => "COMPLETED",
86            ReplicationStatusType::CompletedLegacy => "COMPLETE",
87            ReplicationStatusType::Failed => "FAILED",
88            ReplicationStatusType::Replica => "REPLICA",
89            ReplicationStatusType::Empty => "",
90        }
91    }
92    pub fn is_empty(&self) -> bool {
93        matches!(self, ReplicationStatusType::Empty)
94    }
95}
96
97impl fmt::Display for ReplicationStatusType {
98    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
99        write!(f, "{}", self.as_str())
100    }
101}
102
103impl From<&str> for ReplicationStatusType {
104    fn from(s: &str) -> Self {
105        match s {
106            "PENDING" => ReplicationStatusType::Pending,
107            "COMPLETED" => ReplicationStatusType::Completed,
108            "COMPLETE" => ReplicationStatusType::CompletedLegacy,
109            "FAILED" => ReplicationStatusType::Failed,
110            "REPLICA" => ReplicationStatusType::Replica,
111            _ => ReplicationStatusType::Empty,
112        }
113    }
114}
115
116impl From<VersionPurgeStatusType> for ReplicationStatusType {
117    fn from(status: VersionPurgeStatusType) -> Self {
118        match status {
119            VersionPurgeStatusType::Pending => ReplicationStatusType::Pending,
120            VersionPurgeStatusType::Complete => ReplicationStatusType::Completed,
121            VersionPurgeStatusType::Failed => ReplicationStatusType::Failed,
122            VersionPurgeStatusType::Empty => ReplicationStatusType::Empty,
123        }
124    }
125}
126
127#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
128pub enum VersionPurgeStatusType {
129    Pending,
130    Complete,
131    Failed,
132    #[default]
133    Empty,
134}
135
136impl VersionPurgeStatusType {
137    /// Returns string representation of version purge status
138    pub fn as_str(&self) -> &'static str {
139        match self {
140            VersionPurgeStatusType::Pending => "PENDING",
141            VersionPurgeStatusType::Complete => "COMPLETE",
142            VersionPurgeStatusType::Failed => "FAILED",
143            VersionPurgeStatusType::Empty => "",
144        }
145    }
146
147    /// Returns true if the version is pending purge.
148    pub fn is_pending(&self) -> bool {
149        matches!(self, VersionPurgeStatusType::Pending | VersionPurgeStatusType::Failed)
150    }
151
152    pub fn is_empty(&self) -> bool {
153        matches!(self, VersionPurgeStatusType::Empty)
154    }
155}
156
157impl fmt::Display for VersionPurgeStatusType {
158    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
159        write!(f, "{}", self.as_str())
160    }
161}
162
163impl From<&str> for VersionPurgeStatusType {
164    fn from(s: &str) -> Self {
165        match s {
166            "PENDING" => VersionPurgeStatusType::Pending,
167            "COMPLETE" => VersionPurgeStatusType::Complete,
168            "FAILED" => VersionPurgeStatusType::Failed,
169            _ => VersionPurgeStatusType::Empty,
170        }
171    }
172}
173
174/// Type - replication type enum
175#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
176pub enum ReplicationType {
177    #[default]
178    Unset,
179    Object,
180    Delete,
181    Metadata,
182    Heal,
183    ExistingObject,
184    Resync,
185    All,
186}
187
188impl ReplicationType {
189    pub fn as_str(&self) -> &'static str {
190        match self {
191            ReplicationType::Unset => "",
192            ReplicationType::Object => "OBJECT",
193            ReplicationType::Delete => "DELETE",
194            ReplicationType::Metadata => "METADATA",
195            ReplicationType::Heal => "HEAL",
196            ReplicationType::ExistingObject => "EXISTING_OBJECT",
197            ReplicationType::Resync => "RESYNC",
198            ReplicationType::All => "ALL",
199        }
200    }
201
202    pub fn is_valid(&self) -> bool {
203        matches!(
204            self,
205            ReplicationType::Object
206                | ReplicationType::Delete
207                | ReplicationType::Metadata
208                | ReplicationType::Heal
209                | ReplicationType::ExistingObject
210                | ReplicationType::Resync
211                | ReplicationType::All
212        )
213    }
214
215    pub fn is_data_replication(&self) -> bool {
216        matches!(self, ReplicationType::Object | ReplicationType::Delete | ReplicationType::Heal)
217    }
218}
219
220impl fmt::Display for ReplicationType {
221    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
222        write!(f, "{}", self.as_str())
223    }
224}
225
226impl From<&str> for ReplicationType {
227    fn from(s: &str) -> Self {
228        match s {
229            "UNSET" => ReplicationType::Unset,
230            "OBJECT" => ReplicationType::Object,
231            "DELETE" => ReplicationType::Delete,
232            "METADATA" => ReplicationType::Metadata,
233            "HEAL" => ReplicationType::Heal,
234            "EXISTING_OBJECT" => ReplicationType::ExistingObject,
235            "RESYNC" => ReplicationType::Resync,
236            "ALL" => ReplicationType::All,
237            _ => ReplicationType::Unset,
238        }
239    }
240}
241
242/// ReplicationState represents internal replication state
243#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
244pub struct ReplicationState {
245    pub replica_timestamp: Option<OffsetDateTime>,
246    pub replica_status: ReplicationStatusType,
247    pub delete_marker: bool,
248    pub replication_timestamp: Option<OffsetDateTime>,
249    pub replication_status_internal: Option<String>,
250    pub version_purge_status_internal: Option<String>,
251    pub replicate_decision_str: String,
252    pub targets: HashMap<String, ReplicationStatusType>,
253    pub purge_targets: HashMap<String, VersionPurgeStatusType>,
254    pub reset_statuses_map: HashMap<String, String>,
255    /// Skipped by serde: this state has a positional wire form, so the map
256    /// travels in the object's internal metadata and is re-derived on read.
257    /// Kept in step with the filemeta crate's copy of the same state.
258    #[serde(skip)]
259    pub target_delete_marker_version_ids: HashMap<String, String>,
260    #[serde(skip)]
261    pub target_delete_marker_version_ids_corrupt: bool,
262}
263
264impl ReplicationState {
265    pub fn new() -> Self {
266        Self::default()
267    }
268
269    /// Returns true if replication state is identical for version purge statuses and replication statuses
270    pub fn equal(&self, other: &ReplicationState) -> bool {
271        self.replica_status == other.replica_status
272            && self.replication_status_internal == other.replication_status_internal
273            && self.version_purge_status_internal == other.version_purge_status_internal
274    }
275
276    /// Returns overall replication status for the object version being replicated
277    pub fn composite_replication_status(&self) -> ReplicationStatusType {
278        if let Some(replication_status_internal) = &self.replication_status_internal {
279            match ReplicationStatusType::from(replication_status_internal.as_str()) {
280                ReplicationStatusType::Pending
281                | ReplicationStatusType::Completed
282                | ReplicationStatusType::Failed
283                | ReplicationStatusType::Replica => {
284                    return ReplicationStatusType::from(replication_status_internal.as_str());
285                }
286                _ => {
287                    let repl_status = get_composite_replication_status(&self.targets);
288
289                    if self.replica_timestamp.is_none() {
290                        return repl_status;
291                    }
292
293                    if repl_status == ReplicationStatusType::Completed
294                        && let (Some(replica_timestamp), Some(replication_timestamp)) =
295                            (self.replica_timestamp, self.replication_timestamp)
296                        && replica_timestamp > replication_timestamp
297                    {
298                        return self.replica_status.clone();
299                    }
300
301                    return repl_status;
302                }
303            }
304        } else if !self.replica_status.is_empty() {
305            return self.replica_status.clone();
306        }
307
308        ReplicationStatusType::default()
309    }
310
311    /// Returns overall replication purge status for the permanent delete being replicated
312    pub fn composite_version_purge_status(&self) -> VersionPurgeStatusType {
313        match VersionPurgeStatusType::from(self.version_purge_status_internal.clone().unwrap_or_default().as_str()) {
314            VersionPurgeStatusType::Pending | VersionPurgeStatusType::Complete | VersionPurgeStatusType::Failed => {
315                VersionPurgeStatusType::from(self.version_purge_status_internal.clone().unwrap_or_default().as_str())
316            }
317            _ => get_composite_version_purge_status(&self.purge_targets),
318        }
319    }
320
321    /// Returns replicatedInfos struct initialized with the previous state of replication
322    pub fn target_state(&self, arn: &str) -> ReplicatedTargetInfo {
323        let resync_timestamp = self
324            .reset_statuses_map
325            .get(&target_reset_header(arn))
326            .or_else(|| self.reset_statuses_map.get(arn))
327            .cloned()
328            .unwrap_or_default();
329
330        ReplicatedTargetInfo {
331            arn: arn.to_string(),
332            prev_replication_status: self.targets.get(arn).cloned().unwrap_or_default(),
333            version_purge_status: self.purge_targets.get(arn).cloned().unwrap_or_default(),
334            target_delete_marker_version_id: self.target_delete_marker_version_ids.get(arn).cloned(),
335            resync_timestamp,
336            ..Default::default()
337        }
338    }
339}
340
341pub fn get_composite_replication_status(targets: &HashMap<String, ReplicationStatusType>) -> ReplicationStatusType {
342    if targets.is_empty() {
343        return ReplicationStatusType::Empty;
344    }
345
346    let mut completed = 0;
347    for status in targets.values() {
348        match status {
349            ReplicationStatusType::Failed => return ReplicationStatusType::Failed,
350            ReplicationStatusType::Completed => completed += 1,
351            _ => {}
352        }
353    }
354
355    if completed == targets.len() {
356        ReplicationStatusType::Completed
357    } else {
358        ReplicationStatusType::Pending
359    }
360}
361
362pub fn get_composite_version_purge_status(targets: &HashMap<String, VersionPurgeStatusType>) -> VersionPurgeStatusType {
363    if targets.is_empty() {
364        return VersionPurgeStatusType::default();
365    }
366
367    let mut completed = 0;
368    for status in targets.values() {
369        match status {
370            VersionPurgeStatusType::Failed => return VersionPurgeStatusType::Failed,
371            VersionPurgeStatusType::Complete => completed += 1,
372            _ => {}
373        }
374    }
375
376    if completed == targets.len() {
377        VersionPurgeStatusType::Complete
378    } else {
379        VersionPurgeStatusType::Pending
380    }
381}
382
383#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
384pub enum ReplicationAction {
385    /// Replicate all data
386    All,
387    /// Replicate only metadata
388    Metadata,
389    /// Do not replicate
390    #[default]
391    None,
392}
393
394impl ReplicationAction {
395    /// Returns string representation of replication action
396    pub fn as_str(&self) -> &'static str {
397        match self {
398            ReplicationAction::All => "all",
399            ReplicationAction::Metadata => "metadata",
400            ReplicationAction::None => "none",
401        }
402    }
403}
404
405impl fmt::Display for ReplicationAction {
406    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
407        write!(f, "{}", self.as_str())
408    }
409}
410
411impl From<&str> for ReplicationAction {
412    fn from(s: &str) -> Self {
413        match s {
414            "all" => ReplicationAction::All,
415            "metadata" => ReplicationAction::Metadata,
416            "none" => ReplicationAction::None,
417            _ => ReplicationAction::None,
418        }
419    }
420}
421
422/// ReplicatedTargetInfo struct represents replication info on a target
423#[derive(Debug, Clone, Serialize, Deserialize, Default)]
424pub struct ReplicatedTargetInfo {
425    pub arn: String,
426    pub size: i64,
427    pub duration: Duration,
428    pub replication_action: ReplicationAction,
429    pub op_type: ReplicationType,
430    pub replication_status: ReplicationStatusType,
431    pub prev_replication_status: ReplicationStatusType,
432    pub version_purge_status: VersionPurgeStatusType,
433    pub resync_timestamp: String,
434    pub replication_resynced: bool,
435    pub endpoint: String,
436    pub secure: bool,
437    pub error: Option<String>,
438    /// Version the target assigned to the delete marker it just created.
439    #[serde(default, skip_serializing_if = "Option::is_none")]
440    pub target_delete_marker_version_id: Option<String>,
441    /// Version the target assigned to this object version when it differs
442    /// from the source id (a target that mints its own ids). Persisted as the
443    /// per-target ledger every later version-addressed mutation resolves
444    /// through; `None` on targets that adopt the source id.
445    #[serde(default, skip_serializing_if = "Option::is_none")]
446    pub target_version_id: Option<String>,
447}
448
449impl ReplicatedTargetInfo {
450    /// Returns true for a target if arn is empty
451    pub fn is_empty(&self) -> bool {
452        self.arn.is_empty()
453    }
454}
455
456/// ReplicatedInfos struct contains replication information for multiple targets
457#[derive(Debug, Clone)]
458pub struct ReplicatedInfos {
459    pub replication_timestamp: Option<OffsetDateTime>,
460    pub targets: Vec<ReplicatedTargetInfo>,
461}
462
463impl ReplicatedInfos {
464    /// Returns the total size of completed replications
465    pub fn completed_size(&self) -> i64 {
466        let mut sz = 0i64;
467        for target in &self.targets {
468            if target.is_empty() {
469                continue;
470            }
471            if target.replication_status == ReplicationStatusType::Completed
472                && target.prev_replication_status != ReplicationStatusType::Completed
473            {
474                sz += target.size;
475            }
476        }
477        sz
478    }
479
480    /// Returns true if replication was attempted on any of the targets for the object version queued
481    pub fn replication_resynced(&self) -> bool {
482        for target in &self.targets {
483            if target.is_empty() || !target.replication_resynced {
484                continue;
485            }
486            return true;
487        }
488        false
489    }
490
491    /// Returns internal representation of replication status for all targets
492    pub fn replication_status_internal(&self) -> Option<String> {
493        let mut result = String::new();
494        for target in &self.targets {
495            if target.is_empty() {
496                continue;
497            }
498            result.push_str(&format!("{}={};", target.arn, target.replication_status));
499        }
500        if result.is_empty() { None } else { Some(result) }
501    }
502
503    /// Returns overall replication status across all targets
504    pub fn replication_status(&self) -> ReplicationStatusType {
505        if self.targets.is_empty() {
506            return ReplicationStatusType::Empty;
507        }
508
509        let mut completed = 0;
510        for target in &self.targets {
511            match target.replication_status {
512                ReplicationStatusType::Failed => return ReplicationStatusType::Failed,
513                ReplicationStatusType::Completed => completed += 1,
514                _ => {}
515            }
516        }
517
518        if completed == self.targets.len() {
519            ReplicationStatusType::Completed
520        } else {
521            ReplicationStatusType::Pending
522        }
523    }
524
525    /// Returns overall version purge status across all targets
526    pub fn version_purge_status(&self) -> VersionPurgeStatusType {
527        if self.targets.is_empty() {
528            return VersionPurgeStatusType::Empty;
529        }
530
531        let mut completed = 0;
532        for target in &self.targets {
533            match target.version_purge_status {
534                VersionPurgeStatusType::Failed => return VersionPurgeStatusType::Failed,
535                VersionPurgeStatusType::Complete => completed += 1,
536                _ => {}
537            }
538        }
539
540        if completed == self.targets.len() {
541            VersionPurgeStatusType::Complete
542        } else {
543            VersionPurgeStatusType::Pending
544        }
545    }
546
547    /// Returns internal representation of version purge status for all targets
548    pub fn version_purge_status_internal(&self) -> Option<String> {
549        let mut result = String::new();
550        for target in &self.targets {
551            if target.is_empty() || target.version_purge_status.is_empty() {
552                continue;
553            }
554            result.push_str(&format!("{}={};", target.arn, target.version_purge_status));
555        }
556        if result.is_empty() { None } else { Some(result) }
557    }
558
559    /// Returns replication action based on target that actually performed replication
560    pub fn action(&self) -> ReplicationAction {
561        for target in &self.targets {
562            if target.is_empty() {
563                continue;
564            }
565            // rely on replication action from target that actually performed replication now.
566            if target.prev_replication_status != ReplicationStatusType::Completed {
567                return target.replication_action;
568            }
569        }
570        ReplicationAction::None
571    }
572}
573
574/// Distinguishes the kind of operation stored in [`MrfReplicateEntry`].
575///
576/// Old serialized files lack the `op` key; `default` maps to `Object`, which preserves
577/// the pre-existing replay behaviour for entries written before this field existed.
578#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, Default)]
579pub enum MrfOpKind {
580    #[default]
581    #[serde(rename = "object")]
582    Object,
583    #[serde(rename = "metadata")]
584    Metadata,
585    #[serde(rename = "heal")]
586    Heal,
587    #[serde(rename = "existingObject")]
588    ExistingObject,
589    #[serde(rename = "delete")]
590    Delete,
591}
592
593impl MrfOpKind {
594    pub fn replication_type(self) -> ReplicationType {
595        match self {
596            Self::Object => ReplicationType::Object,
597            Self::Metadata => ReplicationType::Metadata,
598            Self::Heal => ReplicationType::Heal,
599            Self::ExistingObject => ReplicationType::ExistingObject,
600            Self::Delete => ReplicationType::Delete,
601        }
602    }
603}
604
605#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq, Eq)]
606pub struct MrfReplicateEntry {
607    #[serde(rename = "bucket")]
608    pub bucket: String,
609
610    #[serde(rename = "object")]
611    pub object: String,
612
613    // Persisted so recovery after restart can replay the exact version.
614    // Old serialized files lack this key; `default` fills in None safely.
615    #[serde(rename = "versionID", skip_serializing_if = "Option::is_none", default)]
616    pub version_id: Option<Uuid>,
617
618    #[serde(rename = "retryCount")]
619    pub retry_count: i32,
620
621    #[serde(rename = "size", default)]
622    pub size: i64,
623
624    // Operation kind. Old files lack this key; default=Object preserves existing behaviour.
625    #[serde(rename = "op", default)]
626    pub op: MrfOpKind,
627
628    // For delete entries: whether the source operation was a force-delete. Old files lack
629    // this key; default=false preserves the pre-existing replay contract.
630    #[serde(rename = "forceDelete", default)]
631    pub force_delete: bool,
632
633    // For delete entries: the delete-marker version id (distinct from version_id, which is
634    // the version being purged). Old files lack this; default=None is correct.
635    #[serde(rename = "deleteMarkerVersionID", skip_serializing_if = "Option::is_none", default)]
636    pub delete_marker_version_id: Option<Uuid>,
637
638    // For delete entries: whether this is a delete-marker vs a versioned-object delete.
639    // Old files lack this; default=false is correct.
640    #[serde(rename = "deleteMarker", default)]
641    pub delete_marker: bool,
642
643    // For delete entries: the original delete-marker mtime, persisted as Unix nanoseconds so
644    // replay stamps replicas with the source timestamp instead of the replay time. Old files
645    // lack this key; default=None means "unknown", and replay falls back to the current time
646    // to preserve pre-existing behaviour (backlog#867).
647    #[serde(rename = "deleteMarkerMtime", skip_serializing_if = "Option::is_none", default)]
648    pub delete_marker_mtime: Option<i64>,
649
650    // For delete-marker purge intents: the exact version id each target assigned to the
651    // replicated marker, keyed by target ARN. A generic S3 target mints its own version ids
652    // and answers a DELETE of an unknown id with 204, so a replay that fell back to the source
653    // marker id would be acknowledged while the real marker stayed behind (backlog#2290).
654    // Old files lack this key; default=empty means "unknown" and replay keeps the source-id
655    // fallback it always had.
656    #[serde(rename = "targetDeleteMarkerVersionIDs", skip_serializing_if = "HashMap::is_empty", default)]
657    pub target_delete_marker_version_ids: HashMap<String, String>,
658
659    // Companion to the map above: the source metadata disagreed about the recorded ids when
660    // the intent was journaled, so the live path refused to guess and reported the target as
661    // failed. Replay must keep refusing instead of falling back to the source id. Old files
662    // lack this key; default=false.
663    #[serde(
664        rename = "targetDeleteMarkerVersionIDsCorrupt",
665        skip_serializing_if = "std::ops::Not::not",
666        default
667    )]
668    pub target_delete_marker_version_ids_corrupt: bool,
669
670    #[serde(rename = "targetARNs", skip_serializing_if = "Vec::is_empty", default)]
671    pub target_arns: Vec<String>,
672
673    // Force-delete entries use the target ARN list above as their immutable target set.
674    // The id distinguishes a durable intent from legacy MRF delete entries.
675    #[serde(rename = "forceDeleteID", skip_serializing_if = "Option::is_none", default)]
676    pub force_delete_id: Option<Uuid>,
677    #[serde(rename = "forceDeleteGeneration", skip_serializing_if = "Option::is_none", default)]
678    pub force_delete_generation: Option<i64>,
679
680    // Replay is allowed only after the source-side recursive delete has committed.
681    #[serde(rename = "forceDeleteLocalCommit", default)]
682    pub force_delete_local_commit: bool,
683}
684
685fn retry_count_to_mrf(retry_count: u32) -> i32 {
686    i32::try_from(retry_count).unwrap_or(i32::MAX)
687}
688
689pub trait ReplicationWorkerOperation: Any + Send + Sync {
690    fn to_mrf_entry(&self) -> MrfReplicateEntry;
691    fn as_any(&self) -> &dyn Any;
692    fn get_bucket(&self) -> &str;
693    fn get_object(&self) -> &str;
694    fn get_size(&self) -> i64;
695    fn is_delete_marker(&self) -> bool;
696    fn get_op_type(&self) -> ReplicationType;
697}
698
699#[derive(Debug, Clone, Serialize, Deserialize, Default)]
700pub struct ReplicateTargetDecision {
701    pub replicate: bool,
702    pub synchronous: bool,
703    pub arn: String,
704    pub id: String,
705}
706
707impl ReplicateTargetDecision {
708    pub fn new(arn: String, replicate: bool, sync: bool) -> Self {
709        Self {
710            replicate,
711            synchronous: sync,
712            arn,
713            id: String::new(),
714        }
715    }
716}
717
718impl fmt::Display for ReplicateTargetDecision {
719    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
720        write!(f, "{};{};{};{}", self.replicate, self.synchronous, self.arn, self.id)
721    }
722}
723
724/// ReplicateDecision represents replication decision for each target
725#[derive(Debug, Clone, Serialize, Deserialize)]
726pub struct ReplicateDecision {
727    pub targets_map: HashMap<String, ReplicateTargetDecision>,
728}
729
730impl ReplicateDecision {
731    pub fn new() -> Self {
732        Self {
733            targets_map: HashMap::new(),
734        }
735    }
736
737    /// Returns true if at least one target qualifies for replication
738    pub fn replicate_any(&self) -> bool {
739        self.targets_map.values().any(|t| t.replicate)
740    }
741
742    /// Returns true if at least one target qualifies for synchronous replication
743    pub fn is_synchronous(&self) -> bool {
744        self.targets_map.values().any(|t| t.synchronous)
745    }
746
747    /// Split admitted targets by their configured delivery mode.
748    ///
749    /// Non-replicating entries are intentionally omitted from both decisions.
750    /// Callers must not promote an async target merely because another target is
751    /// synchronous, and unsupported operation paths can keep both partitions
752    /// empty or explicitly async.
753    pub fn partition_by_sync(&self) -> (Self, Self) {
754        let mut synchronous = Self::new();
755        let mut asynchronous = Self::new();
756
757        for target in self.targets_map.values().filter(|target| target.replicate) {
758            if target.synchronous {
759                synchronous.set(target.clone());
760            } else {
761                asynchronous.set(target.clone());
762            }
763        }
764
765        (synchronous, asynchronous)
766    }
767
768    /// Updates ReplicateDecision with target's replication decision
769    pub fn set(&mut self, target: ReplicateTargetDecision) {
770        self.targets_map.insert(target.arn.clone(), target);
771    }
772
773    /// Returns a stringified representation of internal replication status with all targets marked as `PENDING`
774    pub fn pending_status(&self) -> Option<String> {
775        let mut result = String::new();
776        for target in self.targets_map.values() {
777            if target.replicate {
778                result.push_str(&format!("{}={};", target.arn, ReplicationStatusType::Pending.as_str()));
779            }
780        }
781        if result.is_empty() { None } else { Some(result) }
782    }
783
784    pub fn replicate_target_arns(&self) -> Vec<String> {
785        let mut arns = self
786            .targets_map
787            .values()
788            .filter(|target| target.replicate && !target.arn.is_empty())
789            .map(|target| target.arn.clone())
790            .collect::<Vec<_>>();
791        arns.sort();
792        arns.dedup();
793        arns
794    }
795}
796
797impl fmt::Display for ReplicateDecision {
798    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
799        let mut result = String::new();
800        for (key, value) in &self.targets_map {
801            result.push_str(&format!("{key}={value},"));
802        }
803        write!(f, "{}", result.trim_end_matches(','))
804    }
805}
806
807impl Default for ReplicateDecision {
808    fn default() -> Self {
809        Self::new()
810    }
811}
812
813// parse k-v pairs of target ARN to stringified ReplicateTargetDecision delimited by ',' into a
814// ReplicateDecision struct
815pub fn parse_replicate_decision(_bucket: &str, s: &str) -> std::io::Result<ReplicateDecision> {
816    let mut decision = ReplicateDecision::new();
817
818    if s.is_empty() {
819        return Ok(decision);
820    }
821
822    for p in s.split(',') {
823        if p.is_empty() {
824            continue;
825        }
826
827        let slc = p.split('=').collect::<Vec<&str>>();
828        if slc.len() != 2 {
829            return Err(std::io::Error::new(
830                std::io::ErrorKind::InvalidInput,
831                format!("invalid replicate decision format: {s}"),
832            ));
833        }
834
835        let tgt_str = slc[1].trim_matches('"');
836        let tgt = tgt_str.split(';').collect::<Vec<&str>>();
837        if tgt.len() != 4 {
838            return Err(std::io::Error::new(
839                std::io::ErrorKind::InvalidInput,
840                format!("invalid replicate decision format: {s}"),
841            ));
842        }
843
844        let tgt = ReplicateTargetDecision {
845            replicate: tgt[0] == "true",
846            synchronous: tgt[1] == "true",
847            arn: tgt[2].to_string(),
848            id: tgt[3].to_string(),
849        };
850        decision.targets_map.insert(slc[0].to_string(), tgt);
851    }
852
853    Ok(decision)
854
855    // r = ReplicateDecision{
856    // 	targetsMap: make(map[string]replicateTargetDecision),
857    // }
858    // if len(s) == 0 {
859    // 	return
860    // }
861    // for _, p := range strings.Split(s, ",") {
862    // 	if p == "" {
863    // 		continue
864    // 	}
865    // 	slc := strings.Split(p, "=")
866    // 	if len(slc) != 2 {
867    // 		return r, errInvalidReplicateDecisionFormat
868    // 	}
869    // 	tgtStr := strings.TrimSuffix(strings.TrimPrefix(slc[1], `"`), `"`)
870    // 	tgt := strings.Split(tgtStr, ";")
871    // 	if len(tgt) != 4 {
872    // 		return r, errInvalidReplicateDecisionFormat
873    // 	}
874    // 	r.targetsMap[slc[0]] = replicateTargetDecision{Replicate: tgt[0] == "true", Synchronous: tgt[1] == "true", Arn: tgt[2], ID: tgt[3]}
875    // }
876}
877
878/// Source snapshot used to fence replication terminal-status publication.
879///
880/// Timestamp and mutation id remain opaque because supported RustFS/MinIO
881/// writers may use different textual representations. `invalid` preserves the
882/// distinction between truly absent legacy metadata and corrupt/conflicting
883/// compatibility aliases.
884#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
885pub struct ReplicationGenerationSnapshot {
886    pub timestamp: Option<String>,
887    pub mutation_id: Option<String>,
888    pub payload_fingerprint: Option<[u8; 32]>,
889    pub invalid: bool,
890}
891
892#[derive(Debug, Clone, Default, Serialize, Deserialize)]
893pub struct ReplicateObjectInfo {
894    pub name: String,
895    pub size: i64,
896    pub actual_size: i64,
897    pub bucket: String,
898    pub version_id: Option<Uuid>,
899    pub etag: Option<String>,
900    pub mod_time: Option<OffsetDateTime>,
901    pub replication_status: ReplicationStatusType,
902    pub replication_status_internal: Option<String>,
903    pub delete_marker: bool,
904    pub version_purge_status_internal: Option<String>,
905    pub version_purge_status: VersionPurgeStatusType,
906    pub replication_state: Option<ReplicationState>,
907    pub op_type: ReplicationType,
908    pub event_type: String,
909    pub dsc: ReplicateDecision,
910    pub existing_obj_resync: ResyncDecision,
911    pub target_statuses: HashMap<String, ReplicationStatusType>,
912    pub target_purge_statuses: HashMap<String, VersionPurgeStatusType>,
913    pub replication_timestamp: Option<OffsetDateTime>,
914    /// Exact persisted source snapshot used to fence terminal status
915    /// write-back after remote I/O.
916    #[serde(default)]
917    pub replication_generation: ReplicationGenerationSnapshot,
918    pub ssec: bool,
919    pub user_tags: String,
920    pub checksum: Option<Bytes>,
921    pub retry_count: u32,
922}
923
924impl ReplicationWorkerOperation for ReplicateObjectInfo {
925    fn as_any(&self) -> &dyn Any {
926        self
927    }
928
929    fn to_mrf_entry(&self) -> MrfReplicateEntry {
930        MrfReplicateEntry {
931            bucket: self.bucket.clone(),
932            object: self.name.clone(),
933            version_id: self.version_id,
934            retry_count: retry_count_to_mrf(self.retry_count),
935            size: self.size,
936            op: match self.op_type {
937                ReplicationType::Metadata => MrfOpKind::Metadata,
938                ReplicationType::Heal => MrfOpKind::Heal,
939                ReplicationType::ExistingObject => MrfOpKind::ExistingObject,
940                _ => MrfOpKind::Object,
941            },
942            force_delete: false,
943            delete_marker_version_id: None,
944            delete_marker: false,
945            delete_marker_mtime: None,
946            target_arns: self.admitted_target_arns(),
947            ..Default::default()
948        }
949    }
950
951    fn get_bucket(&self) -> &str {
952        &self.bucket
953    }
954
955    fn get_object(&self) -> &str {
956        &self.name
957    }
958
959    fn get_size(&self) -> i64 {
960        self.size
961    }
962
963    fn is_delete_marker(&self) -> bool {
964        self.delete_marker
965    }
966
967    fn get_op_type(&self) -> ReplicationType {
968        self.op_type
969    }
970}
971
972static REPL_STATUS_REGEX: LazyLock<Regex> = LazyLock::new(|| match Regex::new(r"([^=].*?)=([^,].*?);") {
973    Ok(regex) => regex,
974    Err(err) => panic!("replication status regex must compile: {err}"),
975});
976
977impl ReplicateObjectInfo {
978    /// Returns the target set captured when this queued operation was admitted.
979    /// Resync decisions are more specific than the general heal decision and must
980    /// win for ExistingObject work.
981    pub fn admitted_target_arns(&self) -> Vec<String> {
982        if self.op_type == ReplicationType::ExistingObject && !self.existing_obj_resync.is_empty() {
983            let mut arns = self
984                .existing_obj_resync
985                .targets
986                .iter()
987                .filter(|(_, decision)| decision.replicate)
988                .map(|(arn, _)| arn.clone())
989                .collect::<Vec<_>>();
990            arns.sort();
991            arns.dedup();
992            return arns;
993        }
994        self.dsc.replicate_target_arns()
995    }
996
997    /// Returns replication status of a target
998    pub fn target_replication_status(&self, arn: &str) -> ReplicationStatusType {
999        let binding = self.replication_status_internal.clone().unwrap_or_default();
1000        let captures = REPL_STATUS_REGEX.captures_iter(&binding);
1001        for cap in captures {
1002            if cap.len() == 3 && &cap[1] == arn {
1003                return ReplicationStatusType::from(&cap[2]);
1004            }
1005        }
1006        ReplicationStatusType::default()
1007    }
1008
1009    /// Returns the relevant info needed by MRF
1010    pub fn to_mrf_entry(&self) -> MrfReplicateEntry {
1011        MrfReplicateEntry {
1012            bucket: self.bucket.clone(),
1013            object: self.name.clone(),
1014            version_id: self.version_id,
1015            retry_count: retry_count_to_mrf(self.retry_count),
1016            size: self.size,
1017            op: match self.op_type {
1018                ReplicationType::Metadata => MrfOpKind::Metadata,
1019                ReplicationType::Heal => MrfOpKind::Heal,
1020                ReplicationType::ExistingObject => MrfOpKind::ExistingObject,
1021                _ => MrfOpKind::Object,
1022            },
1023            force_delete: false,
1024            delete_marker_version_id: None,
1025            delete_marker: false,
1026            delete_marker_mtime: None,
1027            target_arns: self.admitted_target_arns(),
1028            ..Default::default()
1029        }
1030    }
1031}
1032
1033pub fn replicate_decision_for_admitted_targets(target_arns: &[String]) -> ReplicateDecision {
1034    let mut decision = ReplicateDecision::new();
1035    for arn in target_arns {
1036        if !arn.is_empty() {
1037            decision.set(ReplicateTargetDecision::new(arn.clone(), true, false));
1038        }
1039    }
1040    decision
1041}
1042
1043// constructs a replication status map from string representation
1044pub fn replication_statuses_map(s: &str) -> HashMap<String, ReplicationStatusType> {
1045    let mut targets = HashMap::new();
1046    let rep_stat_matches = REPL_STATUS_REGEX.captures_iter(s).map(|c| c.extract());
1047    for (_, [arn, status]) in rep_stat_matches {
1048        if arn.is_empty() {
1049            continue;
1050        }
1051        let status = ReplicationStatusType::from(status);
1052        targets.insert(arn.to_string(), status);
1053    }
1054    targets
1055}
1056
1057// constructs a version purge status map from string representation
1058pub fn version_purge_statuses_map(s: &str) -> HashMap<String, VersionPurgeStatusType> {
1059    let mut targets = HashMap::new();
1060    let purge_status_matches = REPL_STATUS_REGEX.captures_iter(s).map(|c| c.extract());
1061    for (_, [arn, status]) in purge_status_matches {
1062        if arn.is_empty() {
1063            continue;
1064        }
1065        let status = VersionPurgeStatusType::from(status);
1066        targets.insert(arn.to_string(), status);
1067    }
1068    targets
1069}
1070
1071fn replication_statuses_string(targets: &HashMap<String, ReplicationStatusType>) -> Option<String> {
1072    let mut result = String::new();
1073    for (arn, status) in targets {
1074        if arn.is_empty() || status.is_empty() {
1075            continue;
1076        }
1077        result.push_str(&format!("{arn}={status};"));
1078    }
1079    if result.is_empty() { None } else { Some(result) }
1080}
1081
1082fn version_purge_statuses_string(targets: &HashMap<String, VersionPurgeStatusType>) -> Option<String> {
1083    let mut result = String::new();
1084    for (arn, status) in targets {
1085        if arn.is_empty() || status.is_empty() {
1086            continue;
1087        }
1088        result.push_str(&format!("{arn}={status};"));
1089    }
1090    if result.is_empty() { None } else { Some(result) }
1091}
1092
1093/// Kept in step with the bounds used by the filemeta crate's copy.
1094const MAX_REPLICATION_TARGET_VERSION_ENTRIES: usize = 1_000;
1095const MAX_REPLICATION_TARGET_ARN_LEN: usize = 1_024;
1096const MAX_REPLICATION_TARGET_VERSION_ID_LEN: usize = 1_024;
1097
1098pub fn get_replication_state(rinfos: &ReplicatedInfos, prev_state: &ReplicationState, _vid: Option<String>) -> ReplicationState {
1099    let reset_status_map: Vec<(String, String)> = rinfos
1100        .targets
1101        .iter()
1102        .filter(|v| !v.resync_timestamp.is_empty())
1103        .map(|t| (target_reset_header(t.arn.as_str()), t.resync_timestamp.clone()))
1104        .collect();
1105
1106    let mut targets = prev_state.targets.clone();
1107    for (arn, status) in replication_statuses_map(&rinfos.replication_status_internal().unwrap_or_default()) {
1108        targets.insert(arn, status);
1109    }
1110
1111    let mut purge_targets = prev_state.purge_targets.clone();
1112    for (arn, status) in version_purge_statuses_map(&rinfos.version_purge_status_internal().unwrap_or_default()) {
1113        purge_targets.insert(arn, status);
1114    }
1115
1116    let repl_statuses = replication_statuses_string(&targets);
1117    let vpurge_statuses = version_purge_statuses_string(&purge_targets);
1118
1119    let mut reset_statuses_map = prev_state.reset_statuses_map.clone();
1120    for (key, value) in reset_status_map {
1121        reset_statuses_map.insert(key, value);
1122    }
1123
1124    // Carry forward the recorded per-target delete-marker versions, dropping
1125    // anything outside the bounds, then fold in what this round's targets
1126    // reported. A map already past the cap is discarded rather than trusted.
1127    let mut target_delete_marker_version_ids = prev_state.target_delete_marker_version_ids.clone();
1128    target_delete_marker_version_ids.retain(|arn, version_id| {
1129        !arn.is_empty()
1130            && arn.len() <= MAX_REPLICATION_TARGET_ARN_LEN
1131            && !version_id.is_empty()
1132            && version_id.len() <= MAX_REPLICATION_TARGET_VERSION_ID_LEN
1133    });
1134    if target_delete_marker_version_ids.len() > MAX_REPLICATION_TARGET_VERSION_ENTRIES {
1135        target_delete_marker_version_ids.clear();
1136    }
1137    for target in &rinfos.targets {
1138        let Some(version_id) = target.target_delete_marker_version_id.as_ref() else {
1139            continue;
1140        };
1141        if (!target_delete_marker_version_ids.contains_key(&target.arn)
1142            && target_delete_marker_version_ids.len() >= MAX_REPLICATION_TARGET_VERSION_ENTRIES)
1143            || target.arn.is_empty()
1144            || target.arn.len() > MAX_REPLICATION_TARGET_ARN_LEN
1145            || version_id.is_empty()
1146            || version_id.len() > MAX_REPLICATION_TARGET_VERSION_ID_LEN
1147        {
1148            continue;
1149        }
1150        target_delete_marker_version_ids.insert(target.arn.clone(), version_id.clone());
1151    }
1152
1153    ReplicationState {
1154        replicate_decision_str: prev_state.replicate_decision_str.clone(),
1155        reset_statuses_map,
1156        replica_timestamp: prev_state.replica_timestamp,
1157        replica_status: prev_state.replica_status.clone(),
1158        targets,
1159        replication_status_internal: repl_statuses,
1160        replication_timestamp: rinfos.replication_timestamp,
1161        purge_targets,
1162        version_purge_status_internal: vpurge_statuses,
1163        target_delete_marker_version_ids,
1164        target_delete_marker_version_ids_corrupt: prev_state.target_delete_marker_version_ids_corrupt,
1165
1166        ..Default::default()
1167    }
1168}
1169
1170pub fn target_reset_header(arn: &str) -> String {
1171    internal_key_rustfs(&format!("{REPLICATION_RESET}-{arn}"))
1172}
1173
1174#[derive(Debug, Clone, Serialize, Deserialize, Default)]
1175pub struct ResyncTargetDecision {
1176    pub replicate: bool,
1177    pub reset_id: String,
1178    pub reset_before_date: Option<OffsetDateTime>,
1179}
1180
1181/// ResyncDecision is a struct representing a map with target's individual resync decisions
1182#[derive(Debug, Clone, Serialize, Deserialize)]
1183pub struct ResyncDecision {
1184    pub targets: HashMap<String, ResyncTargetDecision>,
1185}
1186
1187impl ResyncDecision {
1188    pub fn new() -> Self {
1189        Self { targets: HashMap::new() }
1190    }
1191
1192    /// Returns true if no targets with resync decision present
1193    pub fn is_empty(&self) -> bool {
1194        self.targets.is_empty()
1195    }
1196
1197    pub fn must_resync(&self) -> bool {
1198        self.targets.values().any(|v| v.replicate)
1199    }
1200
1201    pub fn must_resync_target(&self, tgt_arn: &str) -> bool {
1202        self.targets.get(tgt_arn).map(|v| v.replicate).unwrap_or(false)
1203    }
1204}
1205
1206impl Default for ResyncDecision {
1207    fn default() -> Self {
1208        Self::new()
1209    }
1210}
1211
1212#[cfg(test)]
1213mod tests {
1214    use super::*;
1215
1216    #[test]
1217    fn replicate_decision_returns_sorted_unique_replicating_target_arns() {
1218        let mut decision = ReplicateDecision::new();
1219        decision.set(ReplicateTargetDecision {
1220            arn: "arn:target-b".to_string(),
1221            replicate: true,
1222            ..Default::default()
1223        });
1224        decision.set(ReplicateTargetDecision {
1225            arn: "arn:target-a".to_string(),
1226            replicate: true,
1227            ..Default::default()
1228        });
1229        decision.set(ReplicateTargetDecision {
1230            arn: "arn:target-c".to_string(),
1231            replicate: false,
1232            ..Default::default()
1233        });
1234        decision.set(ReplicateTargetDecision {
1235            arn: String::new(),
1236            replicate: true,
1237            ..Default::default()
1238        });
1239
1240        assert_eq!(
1241            decision.replicate_target_arns(),
1242            vec!["arn:target-a".to_string(), "arn:target-b".to_string()]
1243        );
1244    }
1245
1246    #[test]
1247    fn replicate_object_info_mrf_entry_carries_replicating_targets() {
1248        let mut decision = ReplicateDecision::new();
1249        decision.set(ReplicateTargetDecision {
1250            arn: "arn:target-a".to_string(),
1251            replicate: true,
1252            ..Default::default()
1253        });
1254        decision.set(ReplicateTargetDecision {
1255            arn: "arn:target-b".to_string(),
1256            replicate: false,
1257            ..Default::default()
1258        });
1259        let info = ReplicateObjectInfo {
1260            bucket: "bucket".to_string(),
1261            name: "object".to_string(),
1262            size: 42,
1263            dsc: decision,
1264            ..Default::default()
1265        };
1266
1267        let entry = info.to_mrf_entry();
1268
1269        assert_eq!(entry.target_arns, vec!["arn:target-a".to_string()]);
1270    }
1271
1272    #[test]
1273    fn partition_by_sync_keeps_mixed_targets_independent() {
1274        let mut decision = ReplicateDecision::new();
1275        decision.set(ReplicateTargetDecision::new("arn:sync".to_string(), true, true));
1276        decision.set(ReplicateTargetDecision::new("arn:async".to_string(), true, false));
1277        decision.set(ReplicateTargetDecision::new("arn:disabled".to_string(), false, true));
1278
1279        let (synchronous, asynchronous) = decision.partition_by_sync();
1280
1281        assert_eq!(synchronous.replicate_target_arns(), vec!["arn:sync".to_string()]);
1282        assert_eq!(asynchronous.replicate_target_arns(), vec!["arn:async".to_string()]);
1283        assert!(synchronous.is_synchronous());
1284        assert!(!asynchronous.is_synchronous());
1285    }
1286
1287    #[test]
1288    fn partition_by_sync_does_not_promote_async_targets() {
1289        let mut decision = ReplicateDecision::new();
1290        decision.set(ReplicateTargetDecision::new("arn:async".to_string(), true, false));
1291
1292        let (synchronous, asynchronous) = decision.partition_by_sync();
1293
1294        assert!(!synchronous.replicate_any());
1295        assert_eq!(asynchronous.replicate_target_arns(), vec!["arn:async".to_string()]);
1296    }
1297
1298    #[test]
1299    fn metadata_replication_mrf_entry_preserves_operation_kind() {
1300        let info = ReplicateObjectInfo {
1301            bucket: "bucket".to_string(),
1302            name: "object".to_string(),
1303            op_type: ReplicationType::Metadata,
1304            ..Default::default()
1305        };
1306
1307        assert_eq!(info.to_mrf_entry().op, MrfOpKind::Metadata);
1308    }
1309
1310    #[test]
1311    fn admission_snapshot_prefers_resync_targets_for_existing_objects() {
1312        let mut decision = ReplicateDecision::new();
1313        decision.set(ReplicateTargetDecision::new("arn:live".to_string(), true, false));
1314        let mut resync = ResyncDecision::new();
1315        resync.targets.insert(
1316            "arn:admitted".to_string(),
1317            ResyncTargetDecision {
1318                replicate: true,
1319                reset_id: "reset-1".to_string(),
1320                ..Default::default()
1321            },
1322        );
1323        let info = ReplicateObjectInfo {
1324            op_type: ReplicationType::ExistingObject,
1325            dsc: decision,
1326            existing_obj_resync: resync,
1327            ..Default::default()
1328        };
1329
1330        assert_eq!(info.admitted_target_arns(), vec!["arn:admitted".to_string()]);
1331        assert_eq!(info.to_mrf_entry().op, MrfOpKind::ExistingObject);
1332    }
1333
1334    #[test]
1335    fn mrf_operation_kind_round_trips_heal_and_existing_object_intent() {
1336        assert_eq!(MrfOpKind::Heal.replication_type(), ReplicationType::Heal);
1337        assert_eq!(MrfOpKind::ExistingObject.replication_type(), ReplicationType::ExistingObject);
1338    }
1339
1340    #[test]
1341    fn target_state_reads_resync_timestamp_from_target_reset_header_key() {
1342        let arn = "arn:rustfs:replication:us-east-1:target:bucket";
1343        let timestamp = "2026-06-30T00:00:00Z;reset-1".to_string();
1344        let mut state = ReplicationState::default();
1345        state.reset_statuses_map.insert(target_reset_header(arn), timestamp.clone());
1346
1347        let target_state = state.target_state(arn);
1348
1349        assert_eq!(target_state.resync_timestamp, timestamp);
1350    }
1351
1352    #[test]
1353    fn get_replication_state_preserves_untouched_target_statuses() {
1354        let target_a = "arn:target:a".to_string();
1355        let target_b = "arn:target:b".to_string();
1356        let mut prev_state = ReplicationState::default();
1357        prev_state.targets.insert(target_a.clone(), ReplicationStatusType::Failed);
1358        prev_state.targets.insert(target_b.clone(), ReplicationStatusType::Completed);
1359
1360        let rinfos = ReplicatedInfos {
1361            replication_timestamp: None,
1362            targets: vec![ReplicatedTargetInfo {
1363                arn: target_a.clone(),
1364                replication_status: ReplicationStatusType::Completed,
1365                ..Default::default()
1366            }],
1367        };
1368
1369        let state = get_replication_state(&rinfos, &prev_state, None);
1370
1371        assert_eq!(state.targets.get(&target_a), Some(&ReplicationStatusType::Completed));
1372        assert_eq!(state.targets.get(&target_b), Some(&ReplicationStatusType::Completed));
1373        assert_eq!(
1374            replication_statuses_map(&state.replication_status_internal.unwrap_or_default()).get(&target_b),
1375            Some(&ReplicationStatusType::Completed)
1376        );
1377    }
1378}