From d7698b8c5afb69663b72624654165481b5f23ba2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E7=82=8E=E6=B3=BC?= Date: Fri, 10 Jan 2025 11:04:34 +0800 Subject: [PATCH] Chore: replace usage of `LogId.leader_id` with `LogId.leader_id()` --- cluster_benchmark/tests/benchmark/store.rs | 2 +- examples/raft-kv-memstore-grpc/src/lib.rs | 2 +- .../raft-kv-memstore-grpc/src/store/mod.rs | 2 +- .../raft-kv-memstore-network-v2/src/store.rs | 2 +- .../src/store.rs | 2 +- .../src/store.rs | 2 +- examples/raft-kv-memstore/src/store/mod.rs | 2 +- examples/raft-kv-rocksdb/src/store.rs | 2 +- examples/rocksstore/src/lib.rs | 2 +- openraft/src/core/raft_core.rs | 2 +- openraft/src/engine/log_id_list.rs | 28 +++++++++---------- openraft/src/log_id/mod.rs | 2 +- stores/memstore/src/lib.rs | 2 +- .../t11_append_inconsistent_log.rs | 2 +- tests/tests/fixtures/mod.rs | 2 +- 15 files changed, 28 insertions(+), 28 deletions(-) diff --git a/cluster_benchmark/tests/benchmark/store.rs b/cluster_benchmark/tests/benchmark/store.rs index 025ed23b7..a9e949c37 100644 --- a/cluster_benchmark/tests/benchmark/store.rs +++ b/cluster_benchmark/tests/benchmark/store.rs @@ -151,7 +151,7 @@ impl RaftSnapshotBuilder for Arc { let snapshot_idx = self.snapshot_idx.fetch_add(1, Ordering::Relaxed); let snapshot_id = if let Some(last) = last_applied_log { - format!("{}-{}-{}", last.leader_id, last.index(), snapshot_idx) + format!("{}-{}-{}", last.leader_id(), last.index(), snapshot_idx) } else { format!("--{}", snapshot_idx) }; diff --git a/examples/raft-kv-memstore-grpc/src/lib.rs b/examples/raft-kv-memstore-grpc/src/lib.rs index 69f1a2cdf..c9a822d3d 100644 --- a/examples/raft-kv-memstore-grpc/src/lib.rs +++ b/examples/raft-kv-memstore-grpc/src/lib.rs @@ -60,7 +60,7 @@ impl From for VoteResponse { impl From for pb::LogId { fn from(log_id: LogId) -> Self { pb::LogId { - term: log_id.leader_id, + term: *log_id.leader_id(), index: log_id.index(), } } diff --git a/examples/raft-kv-memstore-grpc/src/store/mod.rs b/examples/raft-kv-memstore-grpc/src/store/mod.rs index b0b237129..fb22df446 100644 --- a/examples/raft-kv-memstore-grpc/src/store/mod.rs +++ b/examples/raft-kv-memstore-grpc/src/store/mod.rs @@ -85,7 +85,7 @@ impl RaftSnapshotBuilder for Arc { }; let snapshot_id = if let Some(last) = last_applied_log { - format!("{}-{}-{}", last.leader_id, last.index(), snapshot_idx) + format!("{}-{}-{}", last.leader_id(), last.index(), snapshot_idx) } else { format!("--{}", snapshot_idx) }; diff --git a/examples/raft-kv-memstore-network-v2/src/store.rs b/examples/raft-kv-memstore-network-v2/src/store.rs index f31d84b04..c1dc202d5 100644 --- a/examples/raft-kv-memstore-network-v2/src/store.rs +++ b/examples/raft-kv-memstore-network-v2/src/store.rs @@ -92,7 +92,7 @@ impl RaftSnapshotBuilder for Arc { }; let snapshot_id = if let Some(last) = last_applied_log { - format!("{}-{}-{}", last.leader_id, last.index(), snapshot_idx) + format!("{}-{}-{}", last.leader_id(), last.index(), snapshot_idx) } else { format!("--{}", snapshot_idx) }; diff --git a/examples/raft-kv-memstore-opendal-snapshot-data/src/store.rs b/examples/raft-kv-memstore-opendal-snapshot-data/src/store.rs index 1c3ae5266..c6241ea26 100644 --- a/examples/raft-kv-memstore-opendal-snapshot-data/src/store.rs +++ b/examples/raft-kv-memstore-opendal-snapshot-data/src/store.rs @@ -106,7 +106,7 @@ impl RaftSnapshotBuilder for Arc { }; let snapshot_id = if let Some(last) = last_applied_log { - format!("{}-{}-{}", last.leader_id, last.index(), snapshot_idx) + format!("{}-{}-{}", last.leader_id(), last.index(), snapshot_idx) } else { format!("--{}", snapshot_idx) }; diff --git a/examples/raft-kv-memstore-singlethreaded/src/store.rs b/examples/raft-kv-memstore-singlethreaded/src/store.rs index bf630ac55..d690416dc 100644 --- a/examples/raft-kv-memstore-singlethreaded/src/store.rs +++ b/examples/raft-kv-memstore-singlethreaded/src/store.rs @@ -146,7 +146,7 @@ impl RaftSnapshotBuilder for Rc { }; let snapshot_id = if let Some(last) = last_applied_log { - format!("{}-{}-{}", last.leader_id, last.index(), snapshot_idx) + format!("{}-{}-{}", last.leader_id(), last.index(), snapshot_idx) } else { format!("--{}", snapshot_idx) }; diff --git a/examples/raft-kv-memstore/src/store/mod.rs b/examples/raft-kv-memstore/src/store/mod.rs index de46ff281..5bf5af5ca 100644 --- a/examples/raft-kv-memstore/src/store/mod.rs +++ b/examples/raft-kv-memstore/src/store/mod.rs @@ -105,7 +105,7 @@ impl RaftSnapshotBuilder for Arc { let snapshot_idx = self.snapshot_idx.fetch_add(1, Ordering::Relaxed) + 1; let snapshot_id = if let Some(last) = last_applied_log { - format!("{}-{}-{}", last.leader_id, last.index(), snapshot_idx) + format!("{}-{}-{}", last.leader_id(), last.index(), snapshot_idx) } else { format!("--{}", snapshot_idx) }; diff --git a/examples/raft-kv-rocksdb/src/store.rs b/examples/raft-kv-rocksdb/src/store.rs index e9aebab60..141d877b6 100644 --- a/examples/raft-kv-rocksdb/src/store.rs +++ b/examples/raft-kv-rocksdb/src/store.rs @@ -89,7 +89,7 @@ impl RaftSnapshotBuilder for StateMachineStore { }; let snapshot_id = if let Some(last) = last_applied_log { - format!("{}-{}-{}", last.leader_id, last.index(), self.snapshot_idx) + format!("{}-{}-{}", last.leader_id(), last.index(), self.snapshot_idx) } else { format!("--{}", self.snapshot_idx) }; diff --git a/examples/rocksstore/src/lib.rs b/examples/rocksstore/src/lib.rs index 5169a4770..186543c19 100644 --- a/examples/rocksstore/src/lib.rs +++ b/examples/rocksstore/src/lib.rs @@ -130,7 +130,7 @@ impl RaftSnapshotBuilder for RocksStateMachine { let snapshot_idx: u64 = rand::thread_rng().gen_range(0..1000); let snapshot_id = if let Some(last) = last_applied_log { - format!("{}-{}-{}", last.leader_id, last.index(), snapshot_idx) + format!("{}-{}-{}", last.leader_id(), last.index(), snapshot_idx) } else { format!("--{}", snapshot_idx) }; diff --git a/openraft/src/core/raft_core.rs b/openraft/src/core/raft_core.rs index 3cc9c8f75..f873b6616 100644 --- a/openraft/src/core/raft_core.rs +++ b/openraft/src/core/raft_core.rs @@ -586,7 +586,7 @@ where id: self.id.clone(), // --- data --- - current_term: st.vote_ref().to_leader_id().term(), + current_term: st.vote_ref().term(), vote: st.io_state().io_progress.flushed().map(|io_id| io_id.to_vote()).unwrap_or_default(), last_log_index: st.last_log_id().index(), last_applied: st.io_applied().cloned(), diff --git a/openraft/src/engine/log_id_list.rs b/openraft/src/engine/log_id_list.rs index 24024eeb0..7019992fe 100644 --- a/openraft/src/engine/log_id_list.rs +++ b/openraft/src/engine/log_id_list.rs @@ -71,8 +71,8 @@ where C: RaftTypeConfig }; // Case AA - if first.leader_id == last.leader_id { - if res.last().map(|x| &x.leader_id) < Some(&first.leader_id) { + if first.leader_id() == last.leader_id() { + if res.last().map(|x| x.leader_id()) < Some(first.leader_id()) { res.push(first); } continue; @@ -80,7 +80,7 @@ where C: RaftTypeConfig // Two adjacent logs with different leader_id, no need to binary search if first.index() + 1 == last.index() { - if res.last().map(|x| &x.leader_id) < Some(&first.leader_id) { + if res.last().map(|x| x.leader_id()) < Some(first.leader_id()) { res.push(first); } res.push(last); @@ -89,13 +89,13 @@ where C: RaftTypeConfig let mid = sto.get_log_id((first.index() + last.index()) / 2).await?; - if first.leader_id == mid.leader_id { + if first.leader_id() == mid.leader_id() { // Case AAC - if res.last().map(|x| &x.leader_id) < Some(&first.leader_id) { + if res.last().map(|x| x.leader_id()) < Some(first.leader_id()) { res.push(first); } stack.push((mid, last)); - } else if mid.leader_id == last.leader_id { + } else if mid.leader_id() == last.leader_id() { // Case ACC stack.push((first, mid)); } else { @@ -137,7 +137,7 @@ where C: RaftTypeConfig if let Some(last) = new_ids.last() { let last_id = last.get_log_id(); - assert_eq!(last_id.leader_id, first_id.leader_id); + assert_eq!(last_id.leader_id(), first_id.leader_id()); if last_id != first_id { self.append(last_id.clone()); @@ -150,15 +150,15 @@ where C: RaftTypeConfig // leader_id: Copy is feature gated #[allow(clippy::clone_on_copy)] pub(crate) fn extend<'a, LID: RaftLogId + 'a>(&mut self, new_ids: &[LID]) { - let mut prev = self.last().map(|x| x.leader_id.clone()); + let mut prev = self.last().map(|x| x.leader_id().clone()); for x in new_ids.iter() { let log_id = x.get_log_id(); - if prev.as_ref() != Some(&log_id.leader_id) { + if prev.as_ref() != Some(log_id.leader_id()) { self.append(log_id.clone()); - prev = Some(log_id.leader_id.clone()); + prev = Some(log_id.leader_id().clone()); } } @@ -204,7 +204,7 @@ where C: RaftTypeConfig let last = &self.key_log_ids[l - 1]; - if self.key_log_ids.get(l - 2).map(|x| &x.leader_id) == Some(&last.leader_id) { + if self.key_log_ids.get(l - 2).map(|x| x.leader_id()) == Some(last.leader_id()) { // Replace the **last log id**. self.key_log_ids[l - 1] = new_log_id; return; @@ -237,7 +237,7 @@ where C: RaftTypeConfig // Add key log id if there is a gap between last.index and at - 1. let last = self.key_log_ids.last(); if let Some(last) = last { - let (last_leader_id, last_index) = (last.leader_id.clone(), last.index()); + let (last_leader_id, last_index) = (last.leader_id().clone(), last.index()); if last_index < at - 1 { self.append(LogIdOf::::new(last_leader_id, at - 1)); } @@ -284,12 +284,12 @@ where C: RaftTypeConfig let res = self.key_log_ids.binary_search_by(|log_id| log_id.index().cmp(&index)); match res { - Ok(i) => Some(LogIdOf::::new(self.key_log_ids[i].leader_id.clone(), index)), + Ok(i) => Some(LogIdOf::::new(self.key_log_ids[i].leader_id().clone(), index)), Err(i) => { if i == 0 || i == self.key_log_ids.len() { None } else { - Some(LogIdOf::::new(self.key_log_ids[i - 1].leader_id.clone(), index)) + Some(LogIdOf::::new(self.key_log_ids[i - 1].leader_id().clone(), index)) } } } diff --git a/openraft/src/log_id/mod.rs b/openraft/src/log_id/mod.rs index 9eba4e996..253704cf5 100644 --- a/openraft/src/log_id/mod.rs +++ b/openraft/src/log_id/mod.rs @@ -55,7 +55,7 @@ impl Display for LogId where C: RaftTypeConfig { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - write!(f, "{}.{}", self.leader_id, self.index()) + write!(f, "{}.{}", self.leader_id(), self.index()) } } diff --git a/stores/memstore/src/lib.rs b/stores/memstore/src/lib.rs index a3983d26c..074bbb7dd 100644 --- a/stores/memstore/src/lib.rs +++ b/stores/memstore/src/lib.rs @@ -294,7 +294,7 @@ impl RaftSnapshotBuilder for Arc { }; let snapshot_id = if let Some(last) = last_applied_log { - format!("{}-{}-{}", last.leader_id, last.index(), snapshot_idx) + format!("{}-{}-{}", last.leader_id(), last.index(), snapshot_idx) } else { format!("--{}", snapshot_idx) }; diff --git a/tests/tests/append_entries/t11_append_inconsistent_log.rs b/tests/tests/append_entries/t11_append_inconsistent_log.rs index 9ff32005c..1dd282800 100644 --- a/tests/tests/append_entries/t11_append_inconsistent_log.rs +++ b/tests/tests/append_entries/t11_append_inconsistent_log.rs @@ -115,7 +115,7 @@ async fn append_inconsistent_log() -> Result<()> { let logs = sto0.try_get_log_entries(60..=60).await?; assert_eq!( 3, - logs.first().unwrap().log_id.leader_id.term, + logs.first().unwrap().log_id.leader_id().term, "log is overridden by leader logs" ); diff --git a/tests/tests/fixtures/mod.rs b/tests/tests/fixtures/mod.rs index b10f671b4..35e046a23 100644 --- a/tests/tests/fixtures/mod.rs +++ b/tests/tests/fixtures/mod.rs @@ -930,7 +930,7 @@ impl TypedRaftRouter { } assert_eq!( - &snap.meta.last_log_id.unwrap_or_default().leader_id.term, + &snap.meta.last_log_id.unwrap_or_default().leader_id().term, term, "expected node {} to have snapshot with term {}, got {:?}", id,