refactor(actor): implement Raft consensus algorithm for cluster leader election

- Add voting mechanism with term tracking and vote persistence
- Implement election triggering logic with majority vote counting
- Add primary/replica role transition handling with state management
- Integrate health check failure detection for automatic elections
- Refactor actor messaging system for distributed coordination
- Update repository registration to query cluster for existing primary
- Add broadcast mechanism for role change notifications
- Implement proper term comparison and duplicate request filtering
- Upgrade dependency versions including tokio-util for async utilities
- Optimize code formatting and line wrapping for improved readability
- Remove redundant blank lines and improve code structure consistency
- Enhance error logging and trace information for debugging purposes
This commit is contained in:
zhenyi
2026-06-10 12:35:10 +08:00
parent ab32e8826e
commit 9a0c26e5f6
40 changed files with 1184 additions and 449 deletions
+56 -17
View File
@@ -4,8 +4,12 @@ use crate::pb::*;
impl GitBare {
/// Run heuristic optimization based on repo state.
pub fn optimize_repository(&self, request: OptimizeRepositoryRequest) -> GitResult<OptimizeRepositoryResponse> {
let strategy = OptimizeStrategy::try_from(request.strategy).unwrap_or(OptimizeStrategy::Heuristic);
pub fn optimize_repository(
&self,
request: OptimizeRepositoryRequest,
) -> GitResult<OptimizeRepositoryResponse> {
let strategy =
OptimizeStrategy::try_from(request.strategy).unwrap_or(OptimizeStrategy::Heuristic);
let mut stdout_all = String::new();
let mut stderr_all = String::new();
@@ -17,7 +21,9 @@ impl GitBare {
// Run commit-graph write if needed
if stats.commit_graph_size_bytes == 0 || strategy == OptimizeStrategy::Aggressive {
if let Ok(resp) = write_commit_graph(self, false, false) {
if !resp.ok { stderr_all.push_str(&resp.stderr); }
if !resp.ok {
stderr_all.push_str(&resp.stderr);
}
stdout_all.push_str(&resp.stdout);
}
}
@@ -28,7 +34,9 @@ impl GitBare {
if repack_needed || strategy == OptimizeStrategy::Aggressive {
let full = strategy == OptimizeStrategy::Aggressive;
if let Ok(resp) = run_repack(self, full, true, true) {
if !resp.ok { stderr_all.push_str(&resp.stderr); }
if !resp.ok {
stderr_all.push_str(&resp.stderr);
}
stdout_all.push_str(&resp.stdout);
}
}
@@ -36,7 +44,9 @@ impl GitBare {
// Prune if aggressive
if strategy == OptimizeStrategy::Aggressive {
if let Ok(resp) = run_gc(self, true, true) {
if !resp.ok { stderr_all.push_str(&resp.stderr); }
if !resp.ok {
stderr_all.push_str(&resp.stderr);
}
stdout_all.push_str(&resp.stdout);
}
}
@@ -44,7 +54,9 @@ impl GitBare {
OptimizeStrategy::Incremental => {
// Just run commit-graph write incrementally
if let Ok(resp) = write_commit_graph(self, false, false) {
if !resp.ok { stderr_all.push_str(&resp.stderr); }
if !resp.ok {
stderr_all.push_str(&resp.stderr);
}
stdout_all.push_str(&resp.stdout);
}
}
@@ -79,7 +91,10 @@ impl GitBare {
// Check commit-graph
let cg_size = std::fs::metadata(
self.bare_dir.join("objects").join("info").join("commit-graph")
self.bare_dir
.join("objects")
.join("info")
.join("commit-graph"),
)
.map(|m| m.len())
.unwrap_or(0);
@@ -96,11 +111,18 @@ impl GitBare {
}
}
fn write_commit_graph(gb: &GitBare, _split: bool, _replace: bool) -> GitResult<RepositoryMaintenanceResponse> {
fn write_commit_graph(
gb: &GitBare,
_split: bool,
_replace: bool,
) -> GitResult<RepositoryMaintenanceResponse> {
let out = std::process::Command::new("git")
.args([
"--git-dir", &gb.bare_dir.to_string_lossy(),
"commit-graph", "write", "--reachable",
"--git-dir",
&gb.bare_dir.to_string_lossy(),
"commit-graph",
"write",
"--reachable",
])
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped())
@@ -117,13 +139,25 @@ fn write_commit_graph(gb: &GitBare, _split: bool, _replace: bool) -> GitResult<R
})
}
fn run_repack(gb: &GitBare, full: bool, bitmaps: bool, _midx: bool) -> GitResult<RepositoryMaintenanceResponse> {
fn run_repack(
gb: &GitBare,
full: bool,
bitmaps: bool,
_midx: bool,
) -> GitResult<RepositoryMaintenanceResponse> {
let mut args = vec![
"--git-dir".to_string(), gb.bare_dir.to_string_lossy().into_owned(),
"--git-dir".to_string(),
gb.bare_dir.to_string_lossy().into_owned(),
"repack".to_string(),
];
if full { args.push("-ad".to_string()); } else { args.push("-d".to_string()); }
if bitmaps { args.push("--write-bitmap-index".to_string()); }
if full {
args.push("-ad".to_string());
} else {
args.push("-d".to_string());
}
if bitmaps {
args.push("--write-bitmap-index".to_string());
}
let out = std::process::Command::new("git")
.args(&args)
@@ -144,11 +178,16 @@ fn run_repack(gb: &GitBare, full: bool, bitmaps: bool, _midx: bool) -> GitResult
fn run_gc(gb: &GitBare, prune: bool, aggressive: bool) -> GitResult<RepositoryMaintenanceResponse> {
let mut args = vec![
"--git-dir".to_string(), gb.bare_dir.to_string_lossy().into_owned(),
"--git-dir".to_string(),
gb.bare_dir.to_string_lossy().into_owned(),
"gc".to_string(),
];
if prune { args.push("--prune=now".to_string()); }
if aggressive { args.push("--aggressive".to_string()); }
if prune {
args.push("--prune=now".to_string());
}
if aggressive {
args.push("--aggressive".to_string());
}
let out = std::process::Command::new("git")
.args(&args)