refactor(build): reformat code and add tonic health dependency

- Reformatted build script with proper indentation and line breaks
- Added tonic-health dependency to Cargo.toml and updated lock file
- Improved error handling in disk cache with concurrent deletion checks
- Refactored conditional chains using && and let expressions
- Reformatted struct initialization and function parameter lists
- Added proper spacing and alignment in language stats processing
- Improved assertion formatting in test cases
- Reorganized import statements and code layout in multiple files
- Updated metrics functions with better parameter handling and formatting
This commit is contained in:
zhenyi
2026-06-11 13:56:15 +08:00
parent c32a7cad2f
commit a40da90ef9
31 changed files with 696 additions and 417 deletions
+51 -35
View File
@@ -1,7 +1,7 @@
use crate::actor::message::{
AppendEntriesRequest, AppendEntriesResponse, ElectionRequest, ElectionResult, GitNodeMessage,
NodeHealth, ReadIndexResponse, RefUpdateEvent, RoleChangedEvent, RouteDecision,
ROLE_PRIMARY, ROLE_REPLICA, RAFT_MSG_VERSION,
NodeHealth, RAFT_MSG_VERSION, ROLE_PRIMARY, ROLE_REPLICA, ReadIndexResponse, RefUpdateEvent,
RoleChangedEvent, RouteDecision,
};
use crate::actor::raft_log::RaftLog;
use crate::pb::RepositoryHeader;
@@ -94,9 +94,8 @@ impl Actor for GitNodeActor {
// Initialize Raft log with disk persistence
let raft_data_dir = args.data_dir.join("raft");
let raft_log = RaftLog::new(&raft_data_dir).map_err(|e| {
ActorProcessingErr::from(format!("failed to init raft log: {e}"))
})?;
let raft_log = RaftLog::new(&raft_data_dir)
.map_err(|e| ActorProcessingErr::from(format!("failed to init raft log: {e}")))?;
tracing::info!(
storage_name = %args.storage_name,
entries = raft_log.len(),
@@ -451,9 +450,7 @@ fn should_accept_election(request: &ElectionRequest, state: &GitNodeState) -> bo
);
return false;
}
if request.last_log_term == my_last_term
&& request.last_log_index < my_last_index
{
if request.last_log_term == my_last_term && request.last_log_index < my_last_index {
tracing::warn!(
candidate_index = request.last_log_index,
my_index = my_last_index,
@@ -796,20 +793,19 @@ fn handle_append_entries(
};
}
}
if state.raft_log.term_at(entry.index) == 0 {
if let Some(raft_entry) = entry.to_entry()
&& let Err(e) = state.raft_log.append_reserved(raft_entry)
{
tracing::error!(error = %e, "failed to append raft entry");
return AppendEntriesResponse {
version: RAFT_MSG_VERSION,
term: state.current_term,
success: false,
match_index: state.raft_log.last_index(),
conflict_index: 0,
conflict_term: 0,
};
}
if state.raft_log.term_at(entry.index) == 0
&& let Some(raft_entry) = entry.to_entry()
&& let Err(e) = state.raft_log.append_reserved(raft_entry)
{
tracing::error!(error = %e, "failed to append raft entry");
return AppendEntriesResponse {
version: RAFT_MSG_VERSION,
term: state.current_term,
success: false,
match_index: state.raft_log.last_index(),
conflict_index: 0,
conflict_term: 0,
};
}
}
@@ -858,7 +854,10 @@ fn handle_read_index(state: &GitNodeState) -> ReadIndexResponse {
ReadIndexResponse {
commit_index: state.raft_log.commit_index(),
leader_term: state.current_term,
is_leader: state.is_primary && state.leader_lease_deadline.is_some_and(|d| d > Instant::now()),
is_leader: state.is_primary
&& state
.leader_lease_deadline
.is_some_and(|d| d > Instant::now()),
}
}
@@ -902,8 +901,12 @@ pub async fn broadcast_append_entries(
match ractor::call_t!(actor_ref, GitNodeMessage::AppendEntries, 5000, request) {
Ok(response) if response.success => {
success_count += 1;
state.match_index.insert(follower_id.clone(), response.match_index);
state.next_index.insert(follower_id, response.match_index + 1);
state
.match_index
.insert(follower_id.clone(), response.match_index);
state
.next_index
.insert(follower_id, response.match_index + 1);
}
Ok(response) => {
// Follower rejected — update next_index for retry
@@ -916,7 +919,9 @@ pub async fn broadcast_append_entries(
// Decrement next_index (optimization: use conflict info)
let next = state.next_index.get(&follower_id).copied().unwrap_or(1);
if response.conflict_index > 0 && response.conflict_index < next {
state.next_index.insert(follower_id, response.conflict_index);
state
.next_index
.insert(follower_id, response.conflict_index);
} else if next > 1 {
state.next_index.insert(follower_id, next - 1);
}
@@ -933,7 +938,9 @@ pub async fn broadcast_append_entries(
/// Check if Leader lease is still valid.
pub fn is_leader_lease_valid(state: &GitNodeState) -> bool {
state.is_primary
&& state.leader_lease_deadline.is_some_and(|d| d > Instant::now())
&& state
.leader_lease_deadline
.is_some_and(|d| d > Instant::now())
}
/// Update Leader lease after successful majority replication.
@@ -1006,8 +1013,12 @@ async fn handle_raft_write(
match ractor::call_t!(actor_ref, GitNodeMessage::AppendEntries, 5000, request) {
Ok(response) if response.success => {
success_count += 1;
state.match_index.insert(follower_id.clone(), response.match_index);
state.next_index.insert(follower_id, response.match_index + 1);
state
.match_index
.insert(follower_id.clone(), response.match_index);
state
.next_index
.insert(follower_id, response.match_index + 1);
}
Ok(response) => {
tracing::debug!(
@@ -1083,11 +1094,14 @@ fn apply_raft_command(state: &mut GitNodeState, command: &crate::actor::raft_log
storage_name = %storage_name,
"applying RegisterRepo from Raft log"
);
state.repos.entry(relative_path.clone()).or_insert_with(|| RepoEntry {
role: ROLE_REPLICA.to_string(),
last_commit: String::new(),
read_only: false,
});
state
.repos
.entry(relative_path.clone())
.or_insert_with(|| RepoEntry {
role: ROLE_REPLICA.to_string(),
last_commit: String::new(),
read_only: false,
});
}
crate::actor::raft_log::Command::RemoveRepo { relative_path } => {
tracing::info!(
@@ -1121,5 +1135,7 @@ fn apply_raft_command(state: &mut GitNodeState, command: &crate::actor::raft_log
}
// Advance last_applied
state.raft_log.advance_last_applied(state.raft_log.commit_index());
state
.raft_log
.advance_last_applied(state.raft_log.commit_index());
}