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
+50 -12
View File
@@ -1,4 +1,5 @@
use std::process::Stdio;
use std::time::Duration;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::process::Command;
@@ -7,6 +8,10 @@ use tokio_stream::wrappers::ReceiverStream;
use crate::bare::GitBare;
use crate::pb::ReceivePackResponse;
use super::CancellableReceiverStream;
/// Maximum time allowed for a git receive-pack process before it is killed.
const RECEIVE_PACK_TIMEOUT: Duration = Duration::from_secs(1800); // 30 minutes
impl GitBare {
/// Receive pack data using git-receive-pack with true concurrent streaming.
@@ -23,7 +28,7 @@ impl GitBare {
input: impl tokio_stream::Stream<Item = Result<crate::pb::ReceivePackRequest, tonic::Status>>
+ Send
+ 'static,
) -> Result<ReceiverStream<Result<ReceivePackResponse, tonic::Status>>, tonic::Status> {
) -> Result<CancellableReceiverStream<Result<ReceivePackResponse, tonic::Status>>, tonic::Status> {
let bare_dir = self.bare_dir.to_string_lossy().into_owned();
tracing::info!(
repo = %bare_dir,
@@ -33,6 +38,10 @@ impl GitBare {
let (tx, rx) = tokio::sync::mpsc::channel(16);
// Use a cancellation token to track client disconnect
let cancel_token = tokio_util::sync::CancellationToken::new();
let cancel_token_clone = cancel_token.clone();
let stream = Box::pin(input);
tokio::spawn(async move {
let stream = stream;
@@ -59,15 +68,20 @@ impl GitBare {
}
};
let child_id = child.id();
let mut stdin = child.stdin.take();
let mut stdout = child.stdout.take();
let mut stderr = child.stderr.take();
let stdin_task = {
let mut stream = stream;
let cancel = cancel_token.clone();
async move {
if let Some(mut stdin) = stdin.take() {
while let Some(result) = stream.next().await {
if cancel.is_cancelled() {
break;
}
match result {
Ok(req) => {
if stdin.write_all(&req.packet).await.is_err() {
@@ -87,10 +101,14 @@ impl GitBare {
let stdout_task = {
let tx = tx.clone();
let cancel = cancel_token.clone();
async move {
if let Some(mut stdout) = stdout.take() {
let mut buf = vec![0u8; 65536];
loop {
if cancel.is_cancelled() {
break;
}
match stdout.read(&mut buf).await {
Ok(0) => break,
Ok(n) => {
@@ -129,25 +147,45 @@ impl GitBare {
}
};
tokio::join!(stdin_task, stdout_task, stderr_task);
// Run all three concurrently with timeout
let _process_future = tokio::join!(stdin_task, stdout_task, stderr_task);
match child.wait().await {
Ok(status) if !status.success() => {
let _ = tx
.send(Err(tonic::Status::internal(
"git receive-pack exited with error",
)))
.await;
match tokio::time::timeout(RECEIVE_PACK_TIMEOUT, child.wait()).await {
Ok(Ok(status)) => {
if !status.success() {
let _ = tx
.send(Err(tonic::Status::internal(
"git receive-pack exited with error",
)))
.await;
}
}
Err(e) => {
Ok(Err(e)) => {
let _ = tx
.send(Err(tonic::Status::internal(format!("wait error: {e}"))))
.await;
}
_ => {}
Err(_timeout) => {
tracing::warn!(
repo = %bare_dir,
pid = ?child_id,
timeout_secs = RECEIVE_PACK_TIMEOUT.as_secs(),
"git receive-pack timed out, killing"
);
let _ = child.kill().await;
let _ = tx
.send(Err(tonic::Status::deadline_exceeded(
"git receive-pack timed out",
)))
.await;
}
}
});
Ok(ReceiverStream::new(rx))
// When the ReceiverStream is dropped (client disconnect), cancel the background task
let rx_stream = ReceiverStream::new(rx);
let cancel_guard = cancel_token_clone.clone().drop_guard();
Ok(super::CancellableReceiverStream::new(rx_stream, cancel_guard))
}
}