mod.rs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. pub(crate) use internal::SharedLogPersister;
  2. use crate::{Index, Term};
  3. mod decode_and_encode;
  4. mod internal;
  5. /// A reference type that points to a Raft log entry. Used as input parameters
  6. /// in the storage interface `RaftStoragePersisterTrait`.
  7. /// This is to keep the implementation details of Raft log array separated from
  8. /// the public storage interface.
  9. pub trait RaftLogEntryRef {
  10. fn index(&self) -> Index;
  11. fn term(&self) -> Term;
  12. fn command_bytes(&self) -> Vec<u8>;
  13. }
  14. /// An object that writes data to the underlying storage. A typical disk-based
  15. /// implementation can be implemented as follows:
  16. /// 1. A file large enough to store a few integers: term, ID of voted for peer,
  17. /// and a pair of disk offsets of valid log entries.
  18. /// 2. A list of continuous disk blocks used to store an array of
  19. /// `RaftStoredLogEntry` bytes.
  20. /// 3. Another list of continuous disk blocks that stores the application
  21. /// snapshot.
  22. ///
  23. /// TODO: Add default index range check implementation to `append_one_entry()`
  24. /// and `append_entries()`.
  25. pub trait RaftStoragePersisterTrait<LogEntry: RaftLogEntryRef>:
  26. Send + Sync + 'static
  27. {
  28. /// Save the term and vote to storage.
  29. fn save_term_vote(&self, term: Term, voted_for: String);
  30. /// Append one entry to the saved log, overriding the existing entry at the
  31. /// same index if it is previously appended. Any existing entries after the
  32. /// give index are discarded.
  33. fn append_one_entry(&self, entry: &LogEntry);
  34. /// Append a list of entries to the saved log, overriding existing entries
  35. /// at the same indexes if they are previously appended. The indexes
  36. /// specified in the input list must be in order and consecutive. Existing
  37. /// entries at indexes after the input index range are discarded.
  38. fn append_entries<'a, LogEntryList>(&self, entries: LogEntryList)
  39. where
  40. LogEntry: 'a,
  41. LogEntryList: IntoIterator<Item = &'a LogEntry>;
  42. /// Store the application snapshot. The snapshot is computed from all log
  43. /// entries at and before `index`. After the snapshot is saved, any log
  44. /// entry on or before index can be discarded.
  45. fn update_snapshot(&self, index: Index, term: Term, snapshot: &[u8]);
  46. }
  47. /// An object that watches the underlying storage system and help Raft decide
  48. /// if a log compaction, i.e. taking a snapshot, is needed.
  49. pub trait RaftStorageMonitorTrait: Send + 'static {
  50. /// Returns true when the storage system requires a log compaction.
  51. fn should_compact_log_now(&self) -> bool;
  52. }
  53. /// A concrete type that holds one log entry read from the storage.
  54. #[derive(Clone)]
  55. pub struct RaftStoredLogEntry {
  56. pub index: Index,
  57. pub term: Term,
  58. pub command: Vec<u8>,
  59. }
  60. /// A concrete type that holds all information that is needed to restore the
  61. /// Raft log array and application state right after the instance starts.
  62. #[derive(Clone)]
  63. pub struct RaftStoredState {
  64. pub current_term: Term,
  65. pub voted_for: String,
  66. pub log: Vec<RaftStoredLogEntry>,
  67. pub snapshot_index: Index,
  68. pub snapshot_term: Term,
  69. pub snapshot: Vec<u8>,
  70. }
  71. /// An object that has everything Raft needs from a storage system.
  72. pub trait RaftStorageTrait {
  73. type RaftStoragePersister<LogEntry: RaftLogEntryRef>: RaftStoragePersisterTrait<LogEntry>;
  74. type RaftStorageMonitor: RaftStorageMonitorTrait;
  75. /// Returns a persister that writes data to the underlying storage.
  76. ///
  77. /// `LogEntry` is not a trait generic parameter, but a method generic parameter,
  78. /// because the implementation of this trait must accept any `RaftLogEntryRef`,
  79. /// even though it is guaranteed that at runtime only one concrete subtype of
  80. /// `RaftLogEntryRef` will be passed to the implementation.
  81. fn persister<LogEntry: RaftLogEntryRef>(
  82. self,
  83. ) -> std::sync::Arc<Self::RaftStoragePersister<LogEntry>>;
  84. /// Reads out the entire saved state, including term, vote, Raft logs and
  85. /// the application snapshot.
  86. fn read_state(&self) -> std::io::Result<RaftStoredState>;
  87. /// Whether or not this storage system requires log compaction. Any
  88. /// non-experimental storage must require log compaction.
  89. fn log_compaction_required(&self) -> bool {
  90. true
  91. }
  92. /// Returns a monitor that helps Raft decide when a compaction should happen
  93. /// during the lifetime of the application.
  94. fn monitor(&self) -> Self::RaftStorageMonitor;
  95. }