1use 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
30pub const NULL_VERSION_ID: &str = "null";
34
35pub const REPLICATE_QUEUED: &str = "replicate:queue";
37
38pub const REPLICATE_EXISTING: &str = "replicate:existing";
40pub const REPLICATE_EXISTING_DELETE: &str = "replicate:existing:delete";
42
43pub const REPLICATE_MRF: &str = "replicate:mrf";
45pub const REPLICATE_INCOMING: &str = "replicate:incoming";
47pub const REPLICATE_INCOMING_DELETE: &str = "replicate:incoming:delete";
49
50pub const REPLICATE_HEAL: &str = "replicate:heal";
52pub const REPLICATE_HEAL_DELETE: &str = "replicate:heal:delete";
54
55#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default, Hash)]
65pub enum ReplicationStatusType {
66 Pending,
68 Completed,
70 CompletedLegacy,
72 Failed,
74 Replica,
76 #[default]
77 Empty,
78}
79
80impl ReplicationStatusType {
81 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 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 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#[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#[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 #[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 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 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 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 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 All,
387 Metadata,
389 #[default]
391 None,
392}
393
394impl ReplicationAction {
395 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#[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 #[serde(default, skip_serializing_if = "Option::is_none")]
440 pub target_delete_marker_version_id: Option<String>,
441 #[serde(default, skip_serializing_if = "Option::is_none")]
446 pub target_version_id: Option<String>,
447}
448
449impl ReplicatedTargetInfo {
450 pub fn is_empty(&self) -> bool {
452 self.arn.is_empty()
453 }
454}
455
456#[derive(Debug, Clone)]
458pub struct ReplicatedInfos {
459 pub replication_timestamp: Option<OffsetDateTime>,
460 pub targets: Vec<ReplicatedTargetInfo>,
461}
462
463impl ReplicatedInfos {
464 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 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 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 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 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 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 pub fn action(&self) -> ReplicationAction {
561 for target in &self.targets {
562 if target.is_empty() {
563 continue;
564 }
565 if target.prev_replication_status != ReplicationStatusType::Completed {
567 return target.replication_action;
568 }
569 }
570 ReplicationAction::None
571 }
572}
573
574#[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 #[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 #[serde(rename = "op", default)]
626 pub op: MrfOpKind,
627
628 #[serde(rename = "forceDelete", default)]
631 pub force_delete: bool,
632
633 #[serde(rename = "deleteMarkerVersionID", skip_serializing_if = "Option::is_none", default)]
636 pub delete_marker_version_id: Option<Uuid>,
637
638 #[serde(rename = "deleteMarker", default)]
641 pub delete_marker: bool,
642
643 #[serde(rename = "deleteMarkerMtime", skip_serializing_if = "Option::is_none", default)]
648 pub delete_marker_mtime: Option<i64>,
649
650 #[serde(rename = "targetDeleteMarkerVersionIDs", skip_serializing_if = "HashMap::is_empty", default)]
657 pub target_delete_marker_version_ids: HashMap<String, String>,
658
659 #[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 #[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 #[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#[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 pub fn replicate_any(&self) -> bool {
739 self.targets_map.values().any(|t| t.replicate)
740 }
741
742 pub fn is_synchronous(&self) -> bool {
744 self.targets_map.values().any(|t| t.synchronous)
745 }
746
747 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 pub fn set(&mut self, target: ReplicateTargetDecision) {
770 self.targets_map.insert(target.arn.clone(), target);
771 }
772
773 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
813pub 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 }
877
878#[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 #[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 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 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 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
1043pub 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
1057pub 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
1093const 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 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#[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 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}