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
+10 -5
View File
@@ -6,9 +6,15 @@ impl GitBare {
/// Detect license by reading LICENSE/COPYING files and doing basic matching.
pub fn find_license(&self) -> GitResult<FindLicenseResponse> {
let possible_paths = [
"LICENSE", "LICENSE.md", "LICENSE.txt",
"LICENCE", "LICENCE.md", "LICENCE.txt",
"COPYING", "COPYING.md", "COPYING.txt",
"LICENSE",
"LICENSE.md",
"LICENSE.txt",
"LICENCE",
"LICENCE.md",
"LICENCE.txt",
"COPYING",
"COPYING.md",
"COPYING.txt",
"UNLICENSE",
];
@@ -102,8 +108,7 @@ fn detect_license(content: &str) -> (&'static str, &'static str, f64) {
}
// ISC
if lower.contains("permission to use, copy, modify, and/or distribute")
&& lower.contains("isc")
if lower.contains("permission to use, copy, modify, and/or distribute") && lower.contains("isc")
{
return ("ISC", "ISC License", 0.80);
}
+11 -3
View File
@@ -4,7 +4,10 @@ use crate::pb::*;
impl GitBare {
/// Find the best merge base for a set of revisions (OIDs).
pub fn find_merge_base(&self, request: FindMergeBaseRequest) -> GitResult<FindMergeBaseResponse> {
pub fn find_merge_base(
&self,
request: FindMergeBaseRequest,
) -> GitResult<FindMergeBaseResponse> {
if request.revisions.is_empty() {
return Ok(FindMergeBaseResponse::default());
}
@@ -49,7 +52,10 @@ impl GitBare {
}
/// Check if one commit is an ancestor of another.
pub fn commit_is_ancestor(&self, request: CommitIsAncestorRequest) -> GitResult<CommitIsAncestorResponse> {
pub fn commit_is_ancestor(
&self,
request: CommitIsAncestorRequest,
) -> GitResult<CommitIsAncestorResponse> {
crate::sanitize::validate_revision(&request.ancestor_oid)?;
crate::sanitize::validate_revision(&request.descendant_oid)?;
@@ -68,6 +74,8 @@ impl GitBare {
.map(|s| s.success())
.unwrap_or(false);
Ok(CommitIsAncestorResponse { is_ancestor: result })
Ok(CommitIsAncestorResponse {
is_ancestor: result,
})
}
}
+7 -6
View File
@@ -42,12 +42,13 @@ impl GitBare {
})?;
}
let output = child.wait_with_output().map_err(|e| {
crate::error::GitError::CommandFailed {
status_code: None,
stderr: e.to_string(),
}
})?;
let output =
child
.wait_with_output()
.map_err(|e| crate::error::GitError::CommandFailed {
status_code: None,
stderr: e.to_string(),
})?;
let stdout = String::from_utf8_lossy(&output.stdout);
let mut sizes = Vec::new();
+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)
+18 -8
View File
@@ -4,7 +4,10 @@ use crate::pb::*;
impl GitBare {
/// Get raw changes between two revisions (file-level changes only, no diff content).
pub fn get_raw_changes(&self, request: GetRawChangesRequest) -> GitResult<GetRawChangesResponse> {
pub fn get_raw_changes(
&self,
request: GetRawChangesRequest,
) -> GitResult<GetRawChangesResponse> {
crate::sanitize::validate_revision(&request.base)?;
crate::sanitize::validate_revision(&request.head)?;
@@ -32,11 +35,15 @@ impl GitBare {
for line in stdout.lines() {
let line = line.trim();
if !line.starts_with(':') { continue; }
if !line.starts_with(':') {
continue;
}
let line = &line[1..];
let parts: Vec<&str> = line.split_whitespace().collect();
if parts.len() < 5 { continue; }
if parts.len() < 5 {
continue;
}
let old_mode = u32::from_str_radix(parts[0], 8).unwrap_or(0);
let new_mode = u32::from_str_radix(parts[1], 8).unwrap_or(0);
@@ -55,11 +62,14 @@ impl GitBare {
};
let (old_path, new_path) = if parts.len() >= 6 {
(parts[5].to_string(), if status_letter == 'R' || status_letter == 'C' {
parts.get(6).map(|s| s.to_string()).unwrap_or_default()
} else {
String::new()
})
(
parts[5].to_string(),
if status_letter == 'R' || status_letter == 'C' {
parts.get(6).map(|s| s.to_string()).unwrap_or_default()
} else {
String::new()
},
)
} else {
(String::new(), String::new())
};
+29 -7
View File
@@ -4,17 +4,28 @@ use crate::pb::*;
impl GitBare {
/// Search file contents with a regex pattern.
pub fn search_files_by_content(&self, request: SearchFilesByContentRequest) -> GitResult<SearchFilesByContentResponse> {
pub fn search_files_by_content(
&self,
request: SearchFilesByContentRequest,
) -> GitResult<SearchFilesByContentResponse> {
crate::sanitize::validate_revision(&request.revision)?;
let revision = if request.revision.is_empty() { "HEAD" } else { &request.revision };
let max_results = if request.max_results == 0 { 100 } else { request.max_results };
let revision = if request.revision.is_empty() {
"HEAD"
} else {
&request.revision
};
let max_results = if request.max_results == 0 {
100
} else {
request.max_results
};
let mut args = vec![
"--git-dir".to_string(),
self.bare_dir.to_string_lossy().into_owned(),
"grep".to_string(),
"-I".to_string(), // don't match binary files
"-I".to_string(), // don't match binary files
"--line-number".to_string(),
"--column".to_string(),
];
@@ -62,11 +73,22 @@ impl GitBare {
}
/// Search file names matching a pattern.
pub fn search_files_by_name(&self, request: SearchFilesByNameRequest) -> GitResult<SearchFilesByNameResponse> {
let revision = if request.revision.is_empty() { "HEAD" } else { &request.revision };
pub fn search_files_by_name(
&self,
request: SearchFilesByNameRequest,
) -> GitResult<SearchFilesByNameResponse> {
let revision = if request.revision.is_empty() {
"HEAD"
} else {
&request.revision
};
crate::sanitize::validate_revision(revision)?;
let max_results = if request.max_results == 0 { 100 } else { request.max_results };
let max_results = if request.max_results == 0 {
100
} else {
request.max_results
};
let mut args = vec![
"--git-dir".to_string(),