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
+55 -24
View File
@@ -13,9 +13,9 @@ use std::sync::atomic::{AtomicU64, Ordering};
use ractor_cluster::BytesConvertable;
use crate::error::{GitError, GitResult};
use crate::actor::snapshot::{RaftSnapshot, SnapshotStorage};
use crate::actor::handler::RepoEntry;
use crate::actor::snapshot::{RaftSnapshot, SnapshotStorage};
use crate::error::{GitError, GitResult};
use std::collections::HashMap;
/// Protocol version for forward/backward compatibility.
@@ -56,11 +56,19 @@ impl Command {
pub fn encode(&self) -> Vec<u8> {
let mut buf = Vec::new();
match self {
Command::RefUpdate { relative_path, ref_name, old_oid, new_oid } => {
Command::RefUpdate {
relative_path,
ref_name,
old_oid,
new_oid,
} => {
buf.push(0); // tag
encode_strings(&mut buf, &[relative_path, ref_name, old_oid, new_oid]);
}
Command::RegisterRepo { relative_path, storage_name } => {
Command::RegisterRepo {
relative_path,
storage_name,
} => {
buf.push(1);
encode_strings(&mut buf, &[relative_path, storage_name]);
}
@@ -68,7 +76,10 @@ impl Command {
buf.push(2);
encode_strings(&mut buf, &[relative_path]);
}
Command::SetPrimary { storage_name, relative_paths } => {
Command::SetPrimary {
storage_name,
relative_paths,
} => {
buf.push(3);
encode_string(&mut buf, storage_name);
buf.extend((relative_paths.len() as u32).to_be_bytes());
@@ -192,7 +203,12 @@ pub struct LogEntry {
impl LogEntry {
pub fn new(term: u64, index: u64, command: Command) -> Self {
let checksum = Self::compute_checksum(term, index, &command);
Self { term, index, command, checksum }
Self {
term,
index,
command,
checksum,
}
}
fn compute_checksum(term: u64, index: u64, command: &Command) -> u32 {
@@ -243,7 +259,12 @@ impl LogEntry {
return None;
}
Some(LogEntry { term, index, command, checksum })
Some(LogEntry {
term,
index,
command,
checksum,
})
}
}
@@ -320,7 +341,9 @@ impl RaftStorage {
.append(true)
.open(&self.index_path)
.map_err(GitError::Io)?;
index_file.write_all(&index_entry.encode()).map_err(GitError::Io)?;
index_file
.write_all(&index_entry.encode())
.map_err(GitError::Io)?;
index_file.flush().map_err(GitError::Io)?;
Ok(entry.index)
@@ -383,7 +406,12 @@ impl RaftStorage {
);
break;
}
entries.push(LogEntry { term, index, command, checksum });
entries.push(LogEntry {
term,
index,
command,
checksum,
});
}
None => {
tracing::warn!(index, "failed to decode command during recovery, stopping");
@@ -478,7 +506,10 @@ impl RaftLog {
let entries = storage.load_all()?;
let next_index = entries.last().map(|e| e.index + 1).unwrap_or(snapshot_index + 1);
let next_index = entries
.last()
.map(|e| e.index + 1)
.unwrap_or(snapshot_index + 1);
let last_applied = entries.last().map(|e| e.index).unwrap_or(snapshot_index);
Ok(Self {
@@ -610,7 +641,9 @@ impl RaftLog {
return Ok(()); // Nothing to compact
}
let keep: Vec<LogEntry> = self.entries.iter()
let keep: Vec<LogEntry> = self
.entries
.iter()
.filter(|e| e.index >= from_index)
.cloned()
.collect();
@@ -622,11 +655,7 @@ impl RaftLog {
self.storage.truncate_and_rebuild(&keep)?;
self.entries = keep;
tracing::info!(
from_index,
kept = self.entries.len(),
"raft log compacted"
);
tracing::info!(from_index, kept = self.entries.len(), "raft log compacted");
Ok(())
}
@@ -635,7 +664,9 @@ impl RaftLog {
/// when a follower detects a term mismatch, it must delete the conflicting entry
/// and all entries that follow.
pub fn truncate_from(&mut self, from_index: u64) -> GitResult<()> {
let keep: Vec<LogEntry> = self.entries.iter()
let keep: Vec<LogEntry> = self
.entries
.iter()
.filter(|e| e.index < from_index)
.cloned()
.collect();
@@ -677,11 +708,7 @@ impl RaftLog {
/// Create a snapshot of the current state and compact the log.
pub fn create_snapshot(&mut self, repos: HashMap<String, RepoEntry>) -> GitResult<()> {
let snapshot = RaftSnapshot::new(
self.last_applied,
self.term_at(self.last_applied),
repos,
);
let snapshot = RaftSnapshot::new(self.last_applied, self.term_at(self.last_applied), repos);
self.snapshot_storage.save(&snapshot)?;
self.snapshot_index = snapshot.last_included_index;
@@ -701,12 +728,16 @@ impl RaftLog {
}
/// Restore state from a snapshot.
pub fn restore_snapshot(&mut self, snapshot: RaftSnapshot) -> GitResult<HashMap<String, RepoEntry>> {
pub fn restore_snapshot(
&mut self,
snapshot: RaftSnapshot,
) -> GitResult<HashMap<String, RepoEntry>> {
self.snapshot_index = snapshot.last_included_index;
self.snapshot_term = snapshot.last_included_term;
self.commit_index = snapshot.last_included_index;
self.last_applied = snapshot.last_included_index;
self.next_index.store(snapshot.last_included_index + 1, Ordering::SeqCst);
self.next_index
.store(snapshot.last_included_index + 1, Ordering::SeqCst);
// Clear all entries (they're covered by the snapshot)
self.entries.clear();