Skip to main content

rustfs_replication/
operation.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 std::collections::HashMap;
16
17use crate::http::{
18    AMZ_BUCKET_REPLICATION_STATUS, AMZ_OBJECT_TAGGING, SSEC_ALGORITHM_HEADER, SSEC_KEY_HEADER, SSEC_KEY_MD5_HEADER,
19    SUFFIX_REPLICATION_RESET_STATUS, SUFFIX_REPLICATION_STATUS, get_consistent_internal_metadata, get_header_metadata,
20};
21use s3s::dto::ReplicationConfiguration;
22use time::OffsetDateTime;
23use uuid::Uuid;
24
25use crate::storage_api::ObjectToDelete;
26use crate::{
27    ObjectOpts, ReplicateDecision, ReplicateTargetDecision, ReplicationConfigurationExt as _, ReplicationState,
28    ReplicationStatusType, ReplicationType, ResyncTargetDecision, VersionPurgeStatusType, replication_statuses_map,
29    target_reset_header, version_purge_statuses_map,
30};
31
32#[derive(Debug, Clone, Default)]
33pub struct MustReplicateOptions {
34    meta: HashMap<String, String>,
35    status: ReplicationStatusType,
36    op_type: ReplicationType,
37    replication_request: bool,
38}
39
40impl MustReplicateOptions {
41    pub fn new(meta: &HashMap<String, String>, user_tags: String, op_type: ReplicationType, replication_request: bool) -> Self {
42        let mut meta = meta.clone();
43        if !user_tags.is_empty() {
44            meta.insert(AMZ_OBJECT_TAGGING.to_string(), user_tags);
45        }
46
47        Self {
48            meta,
49            status: ReplicationStatusType::default(),
50            op_type,
51            replication_request,
52        }
53    }
54
55    pub fn with_replication_status(mut self, status: ReplicationStatusType) -> Self {
56        self.status = status;
57        self
58    }
59
60    pub fn replication_status(&self) -> ReplicationStatusType {
61        if let Some(rs) = self.meta.get(AMZ_BUCKET_REPLICATION_STATUS) {
62            let status = ReplicationStatusType::from(rs.as_str());
63            if !status.is_empty() {
64                return status;
65            }
66        }
67        self.status.clone()
68    }
69
70    pub fn is_existing_object_replication(&self) -> bool {
71        self.op_type == ReplicationType::ExistingObject
72    }
73
74    pub fn is_metadata_replication(&self) -> bool {
75        self.op_type == ReplicationType::Metadata
76    }
77
78    pub fn is_replication_request(&self) -> bool {
79        self.replication_request
80    }
81
82    pub fn user_tags(&self) -> &str {
83        self.meta.get(AMZ_OBJECT_TAGGING).map(String::as_str).unwrap_or_default()
84    }
85
86    pub fn metadata_target_is_eligible(&self, arn: &str) -> bool {
87        if !self.is_metadata_replication() {
88            return true;
89        }
90
91        // A REPLICA version was delivered by a peer and carries no per-target
92        // internal status of its own; whether its local metadata edits flow
93        // back is the replication rule's ReplicaModifications decision
94        // (`ReplicationConfig::replicate` with `replica = true`, MinIO
95        // mustReplicate parity). Gating it on a COMPLETED target state would
96        // silently drop every replica-side tag / retention / legal-hold edit in
97        // an active-active topology (rustfs/backlog#1953).
98        if self.replication_status() == ReplicationStatusType::Replica {
99            return true;
100        }
101
102        let Some(statuses) = get_consistent_internal_metadata(&self.meta, SUFFIX_REPLICATION_STATUS) else {
103            return false;
104        };
105        let mut admitted = None;
106        for entry in statuses.split(';').filter(|entry| !entry.is_empty()) {
107            let Some((target_arn, status)) = entry.split_once('=') else {
108                continue;
109            };
110            if target_arn != arn {
111                continue;
112            }
113            // Per-target admission is authoritative only when the target has
114            // exactly one unambiguous historical status record.
115            if admitted.is_some() {
116                return false;
117            }
118            admitted = Some(ReplicationStatusType::from(status));
119        }
120
121        matches!(
122            admitted,
123            Some(
124                ReplicationStatusType::Pending
125                    | ReplicationStatusType::Failed
126                    | ReplicationStatusType::Completed
127                    | ReplicationStatusType::CompletedLegacy
128            )
129        )
130    }
131}
132
133pub fn is_ssec_encrypted(user_defined: &HashMap<String, String>) -> bool {
134    user_defined.contains_key(SSEC_ALGORITHM_HEADER)
135        || user_defined.contains_key(SSEC_KEY_HEADER)
136        || user_defined.contains_key(SSEC_KEY_MD5_HEADER)
137}
138
139#[derive(Debug, Clone)]
140pub struct ReplicationDeleteSource<'a> {
141    pub user_defined: &'a HashMap<String, String>,
142    pub user_tags: &'a str,
143    pub delete_marker: bool,
144    pub replication_status: ReplicationStatusType,
145}
146
147pub fn delete_replication_object_opts(dobj: &ObjectToDelete, source: &ReplicationDeleteSource<'_>) -> ObjectOpts {
148    ObjectOpts {
149        name: dobj.object_name.clone(),
150        ssec: is_ssec_encrypted(source.user_defined),
151        user_tags: source.user_tags.to_string(),
152        delete_marker: source.delete_marker,
153        version_id: dobj.version_id,
154        op_type: ReplicationType::Delete,
155        replica: source.replication_status == ReplicationStatusType::Replica,
156        ..Default::default()
157    }
158}
159
160#[derive(Debug, Clone)]
161pub struct ReplicationDeleteStateSource {
162    pub name: String,
163    pub user_tags: String,
164    pub version_id: Option<Uuid>,
165    pub delete_marker: bool,
166    pub replica: bool,
167}
168
169pub fn delete_replication_state_from_config(
170    config: &ReplicationConfiguration,
171    source: &ReplicationDeleteStateSource,
172) -> Option<ReplicationState> {
173    let opts = ObjectOpts {
174        name: source.name.clone(),
175        user_tags: source.user_tags.clone(),
176        version_id: source.version_id,
177        delete_marker: source.delete_marker,
178        op_type: ReplicationType::Delete,
179        replica: source.replica,
180        ..Default::default()
181    };
182    let target_decisions = config.filter_target_replication_decisions(&opts);
183    if target_decisions.is_empty() {
184        return None;
185    }
186
187    let mut decision = ReplicateDecision::new();
188    for (target_arn, replicate) in target_decisions {
189        decision.set(ReplicateTargetDecision::new(target_arn, replicate, false));
190    }
191    if !decision.replicate_any() {
192        return None;
193    }
194
195    let pending_status = decision.pending_status();
196    let mut state = ReplicationState {
197        replicate_decision_str: decision.to_string(),
198        ..Default::default()
199    };
200    if source.version_id.is_some() {
201        state.version_purge_status_internal = pending_status.clone();
202        state.purge_targets = version_purge_statuses_map(pending_status.as_deref().unwrap_or_default());
203    } else {
204        state.replication_status_internal = pending_status.clone();
205        state.targets = replication_statuses_map(pending_status.as_deref().unwrap_or_default());
206    }
207    Some(state)
208}
209
210#[derive(Debug, Clone, Copy)]
211pub struct ReplicationDeleteScheduleInput<'a> {
212    pub replication_request: bool,
213    pub version_id_requested: bool,
214    pub source_delete_marker: bool,
215    pub source_replication_status: &'a ReplicationStatusType,
216    pub source_version_purge_status: &'a VersionPurgeStatusType,
217    pub deleted_delete_marker_version: bool,
218}
219
220pub fn should_schedule_delete_replication(input: ReplicationDeleteScheduleInput<'_>) -> bool {
221    if input.replication_request {
222        return false;
223    }
224
225    if input.version_id_requested {
226        return input.source_version_purge_status == &VersionPurgeStatusType::Pending;
227    }
228
229    input.source_replication_status == &ReplicationStatusType::Replica
230        || input.source_replication_status == &ReplicationStatusType::Pending
231        || input.source_version_purge_status == &VersionPurgeStatusType::Pending
232        || (input.deleted_delete_marker_version && input.source_replication_status == &ReplicationStatusType::Completed)
233}
234
235pub fn delete_replication_version_id(
236    source_delete_marker: bool,
237    source_version_id: Option<Uuid>,
238    deleted_delete_marker_version: bool,
239) -> Option<Uuid> {
240    if source_delete_marker && !deleted_delete_marker_version {
241        None
242    } else {
243        source_version_id
244    }
245}
246
247pub fn should_use_existing_delete_replication_source(
248    replication_request: bool,
249    deleted_delete_marker: bool,
250    has_existing_source: bool,
251) -> bool {
252    replication_request && deleted_delete_marker && has_existing_source
253}
254
255pub fn should_use_existing_delete_replication_info(version_id_requested: bool, delete_marker_request: bool) -> bool {
256    version_id_requested && !delete_marker_request
257}
258
259pub fn heal_uses_delete_replication_path(delete_marker: bool, version_purge_status: &VersionPurgeStatusType) -> bool {
260    delete_marker || !version_purge_status.is_empty()
261}
262
263pub fn delete_replication_missing_source_decision(
264    delete_marker: bool,
265    target_status: ReplicationStatusType,
266    rule_replicates: bool,
267    version_purge_status: &VersionPurgeStatusType,
268) -> Option<bool> {
269    let valid_replication_status = matches!(
270        target_status,
271        ReplicationStatusType::Pending | ReplicationStatusType::Completed | ReplicationStatusType::Failed
272    );
273
274    if delete_marker && (valid_replication_status || rule_replicates) {
275        return Some(rule_replicates);
276    }
277
278    if !version_purge_status.is_empty() {
279        return Some(matches!(
280            version_purge_status,
281            &VersionPurgeStatusType::Pending | &VersionPurgeStatusType::Failed
282        ));
283    }
284
285    None
286}
287
288#[derive(Debug, Clone)]
289pub struct ReplicationResyncTargetObject<'a> {
290    pub mod_time: Option<OffsetDateTime>,
291    pub user_defined: &'a HashMap<String, String>,
292}
293
294pub fn resync_target_for_object(
295    object: &ReplicationResyncTargetObject<'_>,
296    arn: &str,
297    reset_id: &str,
298    reset_before_date: Option<OffsetDateTime>,
299    status: ReplicationStatusType,
300) -> ResyncTargetDecision {
301    let rs = object
302        .user_defined
303        .get(target_reset_header(arn).as_str())
304        .cloned()
305        .or_else(|| get_header_metadata(object.user_defined, SUFFIX_REPLICATION_RESET_STATUS));
306
307    let mut dec = ResyncTargetDecision::default();
308
309    let mod_time = object.mod_time.unwrap_or(OffsetDateTime::UNIX_EPOCH);
310
311    let Some(rs) = rs else {
312        let reset_before_date = reset_before_date.unwrap_or(OffsetDateTime::UNIX_EPOCH);
313        if !reset_id.is_empty() && mod_time <= reset_before_date {
314            dec.replicate = true;
315            return dec;
316        }
317
318        dec.replicate = status == ReplicationStatusType::Empty;
319
320        return dec;
321    };
322
323    let Some(reset_before_date) = reset_before_date else {
324        return dec;
325    };
326
327    if reset_id.is_empty() {
328        return dec;
329    }
330
331    let parts: Vec<&str> = rs.splitn(2, ';').collect();
332
333    if parts.len() != 2 {
334        return dec;
335    }
336
337    let new_reset = parts[1] != reset_id;
338
339    if !new_reset && status == ReplicationStatusType::Completed {
340        return dec;
341    }
342
343    dec.replicate = new_reset && mod_time <= reset_before_date;
344
345    dec
346}
347
348#[cfg(test)]
349mod tests {
350    use super::{
351        MustReplicateOptions, ReplicationDeleteScheduleInput, ReplicationDeleteSource, ReplicationDeleteStateSource,
352        ReplicationResyncTargetObject, delete_replication_missing_source_decision, delete_replication_object_opts,
353        delete_replication_state_from_config, delete_replication_version_id, heal_uses_delete_replication_path,
354        is_ssec_encrypted, resync_target_for_object, should_schedule_delete_replication,
355        should_use_existing_delete_replication_info, should_use_existing_delete_replication_source,
356    };
357    use crate::http::{
358        AMZ_BUCKET_REPLICATION_STATUS, SSEC_ALGORITHM_HEADER, SUFFIX_REPLICATION_STATUS, insert_internal_metadata,
359    };
360    use crate::storage_api::ObjectToDelete;
361    use crate::{ReplicationStatusType, ReplicationType, VersionPurgeStatusType, target_reset_header};
362    use s3s::dto::{
363        DeleteMarkerReplication, DeleteMarkerReplicationStatus, DeleteReplication, DeleteReplicationStatus, Destination,
364        ExistingObjectReplication, ExistingObjectReplicationStatus, ReplicaModifications, ReplicaModificationsStatus,
365        ReplicationConfiguration, ReplicationRule, ReplicationRuleStatus, SourceSelectionCriteria,
366    };
367    use std::collections::HashMap;
368    use time::{Duration, OffsetDateTime};
369    use uuid::Uuid;
370
371    #[test]
372    fn must_replicate_options_preserves_user_tags_and_operation_type() {
373        let options = MustReplicateOptions::new(&HashMap::new(), "env=prod".to_string(), ReplicationType::ExistingObject, true);
374
375        assert_eq!(options.user_tags(), "env=prod");
376        assert!(options.is_existing_object_replication());
377        assert!(options.is_replication_request());
378    }
379
380    #[test]
381    fn must_replicate_options_reads_replication_status_header() {
382        let meta = HashMap::from([(AMZ_BUCKET_REPLICATION_STATUS.to_string(), "COMPLETED".to_string())]);
383        let options = MustReplicateOptions::new(&meta, String::new(), ReplicationType::Object, false)
384            .with_replication_status(ReplicationStatusType::Replica);
385
386        assert_eq!(options.replication_status(), ReplicationStatusType::Completed);
387    }
388
389    #[test]
390    fn must_replicate_options_falls_back_to_explicit_replication_status() {
391        let options = MustReplicateOptions::new(&HashMap::new(), String::new(), ReplicationType::Delete, false)
392            .with_replication_status(ReplicationStatusType::Replica);
393
394        assert_eq!(options.replication_status(), ReplicationStatusType::Replica);
395    }
396
397    #[test]
398    fn metadata_replication_requires_prior_target_admission() {
399        let arn = "arn:rustfs:replication:target";
400        for status in ["PENDING", "FAILED", "COMPLETED", "COMPLETE"] {
401            let mut meta = HashMap::new();
402            insert_internal_metadata(&mut meta, SUFFIX_REPLICATION_STATUS, format!("{arn}={status};"));
403            let options = MustReplicateOptions::new(&meta, String::new(), ReplicationType::Metadata, false);
404
405            assert!(
406                options.metadata_target_is_eligible(arn),
407                "known target state {status} must remain eligible"
408            );
409            assert!(!options.metadata_target_is_eligible("arn:rustfs:replication:missing"));
410        }
411
412        for status in ["", "REPLICA", "UNKNOWN"] {
413            let mut meta = HashMap::new();
414            insert_internal_metadata(&mut meta, SUFFIX_REPLICATION_STATUS, format!("{arn}={status};"));
415            let options = MustReplicateOptions::new(&meta, String::new(), ReplicationType::Metadata, false);
416            assert!(
417                !options.metadata_target_is_eligible(arn),
418                "unsupported target state {status:?} must fail closed"
419            );
420        }
421
422        for key in ["x-rustfs-internal-replication-status", "X-Minio-Internal-Replication-Status"] {
423            let meta = HashMap::from([(key.to_string(), format!("{arn}=PENDING;"))]);
424            assert!(
425                MustReplicateOptions::new(&meta, String::new(), ReplicationType::Metadata, false)
426                    .metadata_target_is_eligible(arn),
427                "a single compatibility alias must preserve rolling-upgrade admission"
428            );
429        }
430
431        let matching_aliases = HashMap::from([
432            ("x-rustfs-internal-replication-status".to_string(), format!("{arn}=COMPLETED;")),
433            ("X-Minio-Internal-Replication-Status".to_string(), format!("{arn}=COMPLETED;")),
434        ]);
435        assert!(
436            MustReplicateOptions::new(&matching_aliases, String::new(), ReplicationType::Metadata, false)
437                .metadata_target_is_eligible(arn)
438        );
439
440        for conflicting in [
441            HashMap::from([
442                ("x-rustfs-internal-replication-status".to_string(), format!("{arn}=PENDING;")),
443                ("x-minio-internal-replication-status".to_string(), format!("{arn}=REPLICA;")),
444            ]),
445            HashMap::from([
446                ("x-rustfs-internal-replication-status".to_string(), format!("{arn}=PENDING;")),
447                ("x-minio-internal-replication-status".to_string(), String::new()),
448            ]),
449            HashMap::from([(
450                "x-rustfs-internal-replication-status".to_string(),
451                format!("{arn}=PENDING;{arn}=REPLICA;"),
452            )]),
453            HashMap::from([(
454                "x-rustfs-internal-replication-status".to_string(),
455                format!("{arn}=PENDING;{arn}=PENDING;"),
456            )]),
457        ] {
458            assert!(
459                !MustReplicateOptions::new(&conflicting, String::new(), ReplicationType::Metadata, false)
460                    .metadata_target_is_eligible(arn),
461                "conflicting, empty, or duplicate admission evidence must fail closed"
462            );
463        }
464
465        // A replica-side metadata edit (active-active) has no per-target
466        // internal status; eligibility is left to the ReplicaModifications rule.
467        let replica = MustReplicateOptions::new(&HashMap::new(), String::new(), ReplicationType::Metadata, false)
468            .with_replication_status(ReplicationStatusType::Replica);
469        assert!(replica.metadata_target_is_eligible(arn));
470        assert!(replica.metadata_target_is_eligible("arn:rustfs:replication:missing"));
471        assert!(
472            MustReplicateOptions::new(&HashMap::new(), String::new(), ReplicationType::Object, false)
473                .metadata_target_is_eligible(arn)
474        );
475    }
476
477    #[test]
478    fn ssec_detection_uses_existing_metadata_headers() {
479        let meta = HashMap::from([(SSEC_ALGORITHM_HEADER.to_string(), "AES256".to_string())]);
480
481        assert!(is_ssec_encrypted(&meta));
482        assert!(!is_ssec_encrypted(&HashMap::new()));
483    }
484
485    #[test]
486    fn heal_delete_path_is_limited_to_delete_markers_and_version_purges() {
487        assert!(heal_uses_delete_replication_path(true, &VersionPurgeStatusType::Empty));
488        assert!(heal_uses_delete_replication_path(false, &VersionPurgeStatusType::Pending));
489        assert!(!heal_uses_delete_replication_path(false, &VersionPurgeStatusType::Empty));
490    }
491
492    #[test]
493    fn missing_source_delete_decision_keeps_delete_marker_and_purge_rules() {
494        assert_eq!(
495            delete_replication_missing_source_decision(
496                true,
497                ReplicationStatusType::Completed,
498                false,
499                &VersionPurgeStatusType::Empty,
500            ),
501            Some(false)
502        );
503        assert_eq!(
504            delete_replication_missing_source_decision(
505                false,
506                ReplicationStatusType::Empty,
507                false,
508                &VersionPurgeStatusType::Pending,
509            ),
510            Some(true)
511        );
512        assert_eq!(
513            delete_replication_missing_source_decision(
514                false,
515                ReplicationStatusType::Empty,
516                false,
517                &VersionPurgeStatusType::Empty
518            ),
519            None
520        );
521    }
522
523    #[test]
524    fn delete_replication_object_opts_marks_replica_deletes() {
525        let version_id = Uuid::new_v4();
526        let user_defined = HashMap::from([(SSEC_ALGORITHM_HEADER.to_string(), "AES256".to_string())]);
527        let dobj = ObjectToDelete {
528            object_name: "obj".to_string(),
529            version_id: Some(version_id),
530            ..Default::default()
531        };
532        let source = ReplicationDeleteSource {
533            user_defined: &user_defined,
534            user_tags: "env=prod",
535            delete_marker: true,
536            replication_status: ReplicationStatusType::Replica,
537        };
538
539        let opts = delete_replication_object_opts(&dobj, &source);
540
541        assert!(opts.replica);
542        assert!(opts.ssec);
543        assert_eq!(opts.user_tags, "env=prod");
544        assert_eq!(opts.version_id, Some(version_id));
545        assert_eq!(opts.name, "obj");
546        assert_eq!(opts.op_type, ReplicationType::Delete);
547    }
548
549    fn delete_replication_rule(arn: &str, replica_modifications: bool) -> ReplicationRule {
550        ReplicationRule {
551            delete_marker_replication: Some(DeleteMarkerReplication {
552                status: Some(DeleteMarkerReplicationStatus::from_static(DeleteMarkerReplicationStatus::ENABLED)),
553            }),
554            delete_replication: None,
555            destination: Destination {
556                bucket: arn.to_string(),
557                ..Default::default()
558            },
559            existing_object_replication: Some(ExistingObjectReplication {
560                status: ExistingObjectReplicationStatus::from_static(ExistingObjectReplicationStatus::ENABLED),
561            }),
562            filter: None,
563            id: Some("rule-1".to_string()),
564            prefix: Some("test/".to_string()),
565            priority: Some(1),
566            source_selection_criteria: replica_modifications.then_some(SourceSelectionCriteria {
567                replica_modifications: Some(ReplicaModifications {
568                    status: ReplicaModificationsStatus::from_static(ReplicaModificationsStatus::ENABLED),
569                }),
570                sse_kms_encrypted_objects: None,
571            }),
572            status: ReplicationRuleStatus::from_static(ReplicationRuleStatus::ENABLED),
573        }
574    }
575
576    #[test]
577    fn delete_replication_state_tracks_downstream_delete_marker_targets() {
578        let arn = "arn:aws:s3:::target-bucket";
579        let config = ReplicationConfiguration {
580            role: arn.to_string(),
581            rules: vec![delete_replication_rule(arn, true)],
582        };
583        let source = ReplicationDeleteStateSource {
584            name: "test/object.txt".to_string(),
585            user_tags: String::new(),
586            version_id: None,
587            delete_marker: true,
588            replica: true,
589        };
590
591        let state = delete_replication_state_from_config(&config, &source)
592            .expect("replica delete marker should be forwarded to downstream targets");
593        let pending = format!("{arn}=PENDING;");
594
595        assert_eq!(state.replication_status_internal.as_deref(), Some(pending.as_str()));
596        assert_eq!(state.replicate_decision_str, format!("{arn}=true;false;{arn};"));
597        assert!(state.targets.contains_key(arn));
598    }
599
600    #[test]
601    fn delete_replication_state_skips_replica_delete_without_replica_modifications() {
602        let arn = "arn:aws:s3:::target-bucket";
603        let config = ReplicationConfiguration {
604            role: arn.to_string(),
605            rules: vec![delete_replication_rule(arn, false)],
606        };
607        let source = ReplicationDeleteStateSource {
608            name: "test/object.txt".to_string(),
609            user_tags: String::new(),
610            version_id: None,
611            delete_marker: true,
612            replica: true,
613        };
614
615        assert!(delete_replication_state_from_config(&config, &source).is_none());
616    }
617
618    #[test]
619    fn delete_replication_state_uses_delete_switch_for_marker_version_purges() {
620        let arn = "arn:aws:s3:::target-bucket";
621        let mut rule = delete_replication_rule(arn, false);
622        rule.delete_replication = Some(DeleteReplication {
623            status: DeleteReplicationStatus::from_static(DeleteReplicationStatus::DISABLED),
624        });
625        let mut config = ReplicationConfiguration {
626            role: arn.to_string(),
627            rules: vec![rule],
628        };
629        let source = ReplicationDeleteStateSource {
630            name: "test/object.txt".to_string(),
631            user_tags: String::new(),
632            version_id: Some(Uuid::new_v4()),
633            delete_marker: true,
634            replica: false,
635        };
636
637        assert!(
638            delete_replication_state_from_config(&config, &source).is_none(),
639            "delete-marker version purge must not use the enabled marker-creation switch"
640        );
641
642        config.rules[0].delete_replication = Some(DeleteReplication {
643            status: DeleteReplicationStatus::from_static(DeleteReplicationStatus::ENABLED),
644        });
645        let state = delete_replication_state_from_config(&config, &source)
646            .expect("delete-marker version purge should honor the enabled permanent-delete switch");
647        let pending = format!("{arn}=PENDING;");
648
649        assert_eq!(state.version_purge_status_internal.as_deref(), Some(pending.as_str()));
650        assert_eq!(state.replicate_decision_str, format!("{arn}=true;false;{arn};"));
651        assert!(state.purge_targets.contains_key(arn));
652    }
653
654    #[test]
655    fn delete_replication_schedule_skips_replica_requests() {
656        assert!(!should_schedule_delete_replication(ReplicationDeleteScheduleInput {
657            replication_request: true,
658            version_id_requested: true,
659            source_delete_marker: true,
660            source_replication_status: &ReplicationStatusType::Completed,
661            source_version_purge_status: &VersionPurgeStatusType::Empty,
662            deleted_delete_marker_version: true,
663        }));
664    }
665
666    #[test]
667    fn delete_replication_schedule_requires_current_admission_for_version_purges() {
668        assert!(!should_schedule_delete_replication(ReplicationDeleteScheduleInput {
669            replication_request: false,
670            version_id_requested: true,
671            source_delete_marker: true,
672            source_replication_status: &ReplicationStatusType::Completed,
673            source_version_purge_status: &VersionPurgeStatusType::Empty,
674            deleted_delete_marker_version: true,
675        }));
676        assert!(should_schedule_delete_replication(ReplicationDeleteScheduleInput {
677            replication_request: false,
678            version_id_requested: true,
679            source_delete_marker: false,
680            source_replication_status: &ReplicationStatusType::Empty,
681            source_version_purge_status: &VersionPurgeStatusType::Pending,
682            deleted_delete_marker_version: true,
683        }));
684        assert!(should_schedule_delete_replication(ReplicationDeleteScheduleInput {
685            replication_request: false,
686            version_id_requested: true,
687            source_delete_marker: false,
688            source_replication_status: &ReplicationStatusType::Empty,
689            source_version_purge_status: &VersionPurgeStatusType::Pending,
690            deleted_delete_marker_version: false,
691        }));
692        assert!(should_schedule_delete_replication(ReplicationDeleteScheduleInput {
693            replication_request: false,
694            version_id_requested: false,
695            source_delete_marker: false,
696            source_replication_status: &ReplicationStatusType::Empty,
697            source_version_purge_status: &VersionPurgeStatusType::Pending,
698            deleted_delete_marker_version: false,
699        }));
700    }
701
702    #[test]
703    fn delete_replication_version_id_splits_marker_creation_and_purge() {
704        let version_id = Uuid::new_v4();
705
706        assert_eq!(delete_replication_version_id(true, Some(version_id), false), None);
707        assert_eq!(delete_replication_version_id(true, Some(version_id), true), Some(version_id));
708    }
709
710    #[test]
711    fn delete_replication_source_selection_prefers_existing_marker_source_only_for_replica_requests() {
712        assert!(should_use_existing_delete_replication_source(true, true, true));
713        assert!(!should_use_existing_delete_replication_source(false, true, true));
714        assert!(!should_use_existing_delete_replication_source(true, false, true));
715        assert!(!should_use_existing_delete_replication_source(true, true, false));
716    }
717
718    #[test]
719    fn existing_delete_replication_info_is_limited_to_version_delete_requests() {
720        assert!(should_use_existing_delete_replication_info(true, false));
721        assert!(!should_use_existing_delete_replication_info(true, true));
722        assert!(!should_use_existing_delete_replication_info(false, false));
723    }
724
725    /// P1-20 truth-table pin (rustfs/backlog#1675): without any reset in play
726    /// (no per-target reset header on the object, empty reset id on the
727    /// target) the existing-object resync decision compensates exactly the
728    /// never-replicated objects — Empty replicates, any recorded status does
729    /// not.
730    #[test]
731    fn resync_target_without_reset_replicates_only_empty_status() {
732        let user_defined = HashMap::new();
733        let object = ReplicationResyncTargetObject {
734            mod_time: Some(OffsetDateTime::UNIX_EPOCH + Duration::seconds(10)),
735            user_defined: &user_defined,
736        };
737
738        for (status, expected) in [
739            (ReplicationStatusType::Empty, true),
740            (ReplicationStatusType::Completed, false),
741            // "COMPLETE" on disk parses to this legacy variant, so objects
742            // written by older versions reach the decision through it.
743            (ReplicationStatusType::CompletedLegacy, false),
744            (ReplicationStatusType::Pending, false),
745            (ReplicationStatusType::Failed, false),
746            (ReplicationStatusType::Replica, false),
747        ] {
748            let label = format!("{status:?}");
749            let decision = resync_target_for_object(&object, "arn:target", "", None, status);
750            assert_eq!(
751                decision.replicate, expected,
752                "existing-object resync without a reset must replicate only never-replicated objects (status {label})"
753            );
754        }
755    }
756
757    #[test]
758    fn resync_target_includes_object_at_reset_before_boundary() {
759        let reset_before = OffsetDateTime::UNIX_EPOCH + Duration::seconds(30);
760        let user_defined = HashMap::new();
761        let object = ReplicationResyncTargetObject {
762            mod_time: Some(reset_before),
763            user_defined: &user_defined,
764        };
765
766        let decision =
767            resync_target_for_object(&object, "arn:target", "reset-1", Some(reset_before), ReplicationStatusType::Completed);
768
769        assert!(decision.replicate);
770    }
771
772    #[test]
773    fn resync_target_replicates_when_reset_id_changes() {
774        let reset_before = OffsetDateTime::UNIX_EPOCH + Duration::seconds(30);
775        let user_defined = HashMap::from([(target_reset_header("arn:target"), "1970-01-01T00:00:20Z;old-reset".to_string())]);
776        let object = ReplicationResyncTargetObject {
777            mod_time: Some(OffsetDateTime::UNIX_EPOCH + Duration::seconds(10)),
778            user_defined: &user_defined,
779        };
780
781        let decision =
782            resync_target_for_object(&object, "arn:target", "new-reset", Some(reset_before), ReplicationStatusType::Completed);
783
784        assert!(decision.replicate);
785    }
786
787    #[test]
788    fn resync_target_skips_completed_object_for_same_reset_id() {
789        let reset_before = OffsetDateTime::UNIX_EPOCH + Duration::seconds(30);
790        let user_defined = HashMap::from([(target_reset_header("arn:target"), "1970-01-01T00:00:20Z;same-reset".to_string())]);
791        let object = ReplicationResyncTargetObject {
792            mod_time: Some(OffsetDateTime::UNIX_EPOCH + Duration::seconds(10)),
793            user_defined: &user_defined,
794        };
795
796        let decision =
797            resync_target_for_object(&object, "arn:target", "same-reset", Some(reset_before), ReplicationStatusType::Completed);
798
799        assert!(!decision.replicate);
800    }
801}