Files
gitks/actor/handler.rs
T
zhenyi 8f472a0443 feat(cluster): implement distributed clustering with etcd coordination
- Integrate etcd-client for distributed coordination and leader election
- Add remote client macros with proper formatting for all services
- Implement RequestMetrics for tracking RPC performance and errors
- Add rate limiting mechanism across all service endpoints
- Create ElectionRequest and ElectionResult message types for leader election
- Add role management with primary/replica switching capabilities
- Implement health checker with automatic failover detection
- Add repository count metrics for cluster monitoring
- Update Cargo.toml with etcd-client and dashmap dependencies
- Modify RepoEntry to include read_only flag for replica handling
- Implement should_accept_election logic to prevent duplicate elections
- Add RoleChangedEvent handling for cluster role updates
2026-06-08 14:31:29 +08:00

523 lines
18 KiB
Rust

use crate::actor::message::{
ElectionRequest, ElectionResult, GitNodeMessage, NodeHealth, ROLE_PRIMARY, ROLE_REPLICA,
RefUpdateEvent, RoleChangedEvent, RouteDecision,
};
use crate::server::GitksService;
use async_trait::async_trait;
use ractor::pg;
use ractor::{Actor, ActorProcessingErr, ActorRef, SupervisionEvent};
use std::collections::HashMap;
#[derive(Clone)]
pub struct GitNodeActor {
pub version: String,
pub service: GitksService,
}
impl GitNodeActor {
pub fn init(service: GitksService) -> Self {
GitNodeActor {
version: env!("CARGO_PKG_VERSION").to_string(),
service,
}
}
}
pub struct RepoEntry {
pub role: String,
pub last_commit: String,
pub read_only: bool,
}
pub struct GitNodeArgs {
pub storage_name: String,
pub grpc_addr: String,
}
pub struct GitNodeState {
storage_name: String,
actor_name: String,
grpc_addr: String,
repos: HashMap<String, RepoEntry>,
current_term: u64,
health_failures: u32,
is_primary: bool,
last_known_primary_grpc: String,
}
#[async_trait]
impl Actor for GitNodeActor {
type Msg = GitNodeMessage;
type State = GitNodeState;
type Arguments = GitNodeArgs;
async fn pre_start(
&self,
myself: ActorRef<Self::Msg>,
args: Self::Arguments,
) -> Result<Self::State, ActorProcessingErr> {
let actor_name = format!("git_node_{}", args.storage_name);
pg::join("gitks_nodes".to_string(), vec![myself.get_cell()]);
pg::join_scoped(
args.storage_name.clone(),
"node".to_string(),
vec![myself.get_cell()],
);
tracing::info!(storage_name = %args.storage_name, actor_name = %actor_name, grpc_addr = %args.grpc_addr, "GitNodeActor started");
start_health_checker(myself.clone(), 1, 10);
Ok(GitNodeState {
storage_name: args.storage_name,
actor_name,
grpc_addr: args.grpc_addr.clone(),
repos: HashMap::new(),
current_term: 0,
health_failures: 0,
is_primary: true, // Will be refined at registration
last_known_primary_grpc: args.grpc_addr.clone(),
})
}
async fn handle(
&self,
myself: ActorRef<Self::Msg>,
message: Self::Msg,
state: &mut Self::State,
) -> Result<(), ActorProcessingErr> {
match message {
GitNodeMessage::ScanAndRegister => {
let repos = self.service.scan_all_repo()?;
tracing::info!(storage_name = %state.storage_name, found = repos.len(), "scanning local repositories");
crate::metrics::set_repository_count(repos.len() as u64);
for repo_path in repos {
let relative_path = repo_path
.strip_prefix(self.service.repo_prefix.to_string_lossy().as_ref())
.unwrap_or(&repo_path)
.trim_start_matches('/')
.to_string();
register_repo(&myself, state, relative_path);
}
}
GitNodeMessage::RegisterRepository(header) => {
register_repo(&myself, state, header.relative_path);
}
GitNodeMessage::RemoveRepository(header) => {
state.repos.remove(&header.relative_path);
tracing::info!(storage_name = %state.storage_name, relative_path = %header.relative_path, "repository route removed");
}
GitNodeMessage::RefUpdated(event) => {
if let Some(entry) = state.repos.get(&event.relative_path)
&& entry.role == ROLE_REPLICA
{
let local_path = self.service.repo_prefix.join(&event.relative_path);
crate::actor::sync::sync_from_primary(event, local_path).await;
}
}
GitNodeMessage::FindPrimary(header, reply) => {
let entry = state.repos.get(&header.relative_path);
let is_primary = entry.is_some_and(|e| e.role == ROLE_PRIMARY);
reply
.send(build_decision(
state,
&header,
is_primary,
entry.map(|e| e.role.as_str()),
))
.ok();
}
GitNodeMessage::FindReplica(header, reply) => {
let entry = state.repos.get(&header.relative_path);
let has = entry.is_some();
reply
.send(build_decision(
state,
&header,
has,
entry.map(|e| e.role.as_str()),
))
.ok();
}
GitNodeMessage::ListRepositoryPaths(reply) => {
let paths: Vec<String> = state.repos.keys().cloned().collect();
reply.send(paths.join("\n")).ok();
}
GitNodeMessage::RepositoryExists(header, reply) => {
reply
.send(state.repos.contains_key(&header.relative_path))
.ok();
}
GitNodeMessage::GetNodeHealth(reply) => {
reply
.send(NodeHealth {
storage_name: state.storage_name.clone(),
repo_count: state.repos.len() as u64,
healthy: true,
version: self.version.clone(),
})
.ok();
}
// ── Election & Role Change ──────────────────────────────────
GitNodeMessage::ElectPrimary(request, reply) => {
let accepted = should_accept_election(&request, state);
tracing::info!(
candidate = %request.candidate_storage_name,
term = request.term,
current_term = state.current_term,
accepted = accepted,
"election vote"
);
if accepted {
state.current_term = request.term;
state.last_known_primary_grpc = request.candidate_grpc_addr.clone();
}
reply
.send(ElectionResult {
accepted,
current_term: state.current_term,
voter_storage_name: state.storage_name.clone(),
voter_role: if state.is_primary {
ROLE_PRIMARY
} else {
ROLE_REPLICA
}
.to_string(),
})
.ok();
}
GitNodeMessage::RoleChanged(event) => {
// Empty storage_name = self-promotion from health checker
let is_self =
event.storage_name.is_empty() || event.storage_name == state.storage_name;
if is_self && event.new_role == ROLE_PRIMARY {
tracing::info!(
storage_name = %state.storage_name,
term = event.term,
"promoted to PRIMARY"
);
state.is_primary = true;
state.current_term = event.term;
state.health_failures = 0;
for entry in state.repos.values_mut() {
entry.role = ROLE_PRIMARY.to_string();
entry.read_only = false;
}
} else if is_self && event.new_role == ROLE_REPLICA {
tracing::info!(
storage_name = %state.storage_name,
term = event.term,
"demoted to REPLICA"
);
state.is_primary = false;
state.current_term = event.term;
for entry in state.repos.values_mut() {
entry.role = ROLE_REPLICA.to_string();
}
} else {
// Another node's role changed — update routing info
tracing::info!(
storage_name = %event.storage_name,
new_role = %event.new_role,
"remote node role changed"
);
state.last_known_primary_grpc = if event.new_role == ROLE_PRIMARY {
event.grpc_addr.clone()
} else {
state.last_known_primary_grpc.clone()
};
}
}
}
Ok(())
}
async fn handle_supervisor_evt(
&self,
_myself: ActorRef<Self::Msg>,
evt: SupervisionEvent,
_state: &mut Self::State,
) -> Result<(), ActorProcessingErr> {
match evt {
SupervisionEvent::ActorStarted(who) => {
tracing::debug!(actor = ?who.get_id(), "child started")
}
SupervisionEvent::ActorTerminated(who, _, reason) => {
tracing::warn!(actor = ?who.get_id(), reason = ?reason, "child terminated")
}
SupervisionEvent::ActorFailed(who, panic_msg) => {
tracing::error!(actor = ?who.get_id(), msg = %panic_msg, "child panicked")
}
SupervisionEvent::ProcessGroupChanged(group) => {
tracing::info!(group = ?group, "PG membership changed")
}
_ => {}
}
Ok(())
}
async fn post_stop(
&self,
_myself: ActorRef<Self::Msg>,
state: &mut Self::State,
) -> Result<(), ActorProcessingErr> {
tracing::info!(storage_name = %state.storage_name, "GitNodeActor stopped");
Ok(())
}
}
/// Determine whether to accept an election request.
fn should_accept_election(request: &ElectionRequest, state: &GitNodeState) -> bool {
// Only accept if the term is greater than our current term
// (prevents old/duplicate election messages)
if request.term <= state.current_term {
tracing::warn!(
request_term = request.term,
current_term = state.current_term,
"rejecting election: term too old"
);
return false;
}
true
}
fn build_decision(
state: &GitNodeState,
header: &crate::pb::RepositoryHeader,
found: bool,
role: Option<&str>,
) -> RouteDecision {
RouteDecision {
found,
storage_name: if found {
state.storage_name.clone()
} else {
String::new()
},
relative_path: header.relative_path.clone(),
actor_name: if found {
state.actor_name.clone()
} else {
String::new()
},
grpc_addr: if found {
state.grpc_addr.clone()
} else {
String::new()
},
role: role.unwrap_or("").to_string(),
}
}
fn register_repo(
myself: &ActorRef<GitNodeMessage>,
state: &mut GitNodeState,
relative_path: String,
) {
if state.repos.contains_key(&relative_path) {
return;
}
let members = ractor::pg::get_members(&"gitks_nodes".to_string());
let my_cell = myself.get_cell();
let other_nodes_exist = members.iter().any(|m| m != &my_cell);
let role = if other_nodes_exist {
ROLE_REPLICA.to_string()
} else {
ROLE_PRIMARY.to_string()
};
if role == ROLE_PRIMARY {
state.is_primary = true;
}
let category = extract_category(&relative_path);
pg::join_scoped(
state.storage_name.clone(),
category.to_string(),
vec![myself.get_cell()],
);
state.repos.insert(
relative_path.clone(),
RepoEntry {
role: role.clone(),
last_commit: String::new(),
read_only: false,
},
);
tracing::info!(
storage_name = %state.storage_name,
category = %category,
relative_path = %relative_path,
actor_name = %state.actor_name,
role = %role,
"repository route registered"
);
}
fn extract_category(relative_path: &str) -> &str {
relative_path.split('/').next().unwrap_or("root")
}
/// Start background health checker that monitors the PRIMARY node.
/// If the PRIMARY becomes unreachable for `max_failures` consecutive checks,
/// triggers an election.
fn start_health_checker(myself: ActorRef<GitNodeMessage>, interval_secs: u64, max_failures: u32) {
tokio::spawn(async move {
let mut interval = tokio::time::interval(std::time::Duration::from_secs(interval_secs));
interval.tick().await; // First tick immediate
let mut consecutive_failures: u32 = 0;
loop {
interval.tick().await;
let members = ractor::pg::get_members(&"gitks_nodes".to_string());
let my_cell = myself.get_cell();
let other_cells: Vec<ractor::ActorCell> =
members.into_iter().filter(|m| m != &my_cell).collect();
if other_cells.is_empty() {
// No other nodes → we are the only node → ensure we are PRIMARY
consecutive_failures = 0;
continue;
}
let mut any_reachable = false;
for cell in &other_cells {
let actor_ref: ActorRef<GitNodeMessage> = cell.clone().into();
match ractor::call_t!(actor_ref, GitNodeMessage::GetNodeHealth, 2000) {
Ok(health) if health.healthy => {
any_reachable = true;
break;
}
_ => continue,
}
}
if any_reachable {
consecutive_failures = 0;
} else {
consecutive_failures += 1;
tracing::warn!(
consecutive_failures = consecutive_failures,
max_failures = max_failures,
"no other cluster nodes reachable"
);
if consecutive_failures >= max_failures {
tracing::error!(
"no other nodes reachable for {max_failures} checks, triggering self-election as PRIMARY"
);
trigger_self_election(&myself);
consecutive_failures = 0;
}
}
}
});
}
/// Trigger self-election: this node promotes itself to PRIMARY.
fn trigger_self_election(myself: &ActorRef<GitNodeMessage>) {
let members = ractor::pg::get_members(&"gitks_nodes".to_string());
let total_nodes = members.len();
tracing::warn!(
total_nodes = total_nodes,
"initiating self-election as new PRIMARY"
);
let new_term = std::time::SystemTime::now()
.duration_since(std::time::SystemTime::UNIX_EPOCH)
.unwrap_or_default()
.as_secs();
myself
.cast(GitNodeMessage::RoleChanged(RoleChangedEvent {
storage_name: String::new(), // will be filled by handler from our own state
grpc_addr: String::new(),
new_role: ROLE_PRIMARY.to_string(),
term: new_term,
relative_paths: Vec::new(), // all repos
}))
.ok();
broadcast_role_changed(
myself,
RoleChangedEvent {
storage_name: String::new(), // handler fills
grpc_addr: String::new(),
new_role: ROLE_PRIMARY.to_string(),
term: new_term,
relative_paths: Vec::new(),
},
);
}
pub async fn start_node_actor(
service: GitksService,
storage_name: String,
grpc_addr: String,
) -> Result<(ActorRef<GitNodeMessage>, tokio::task::JoinHandle<()>), ractor::SpawnErr> {
let actor = GitNodeActor::init(service);
let (actor_ref, handle) = Actor::spawn(
Some(format!("git_node_{storage_name}")),
actor,
GitNodeArgs {
storage_name,
grpc_addr,
},
)
.await?;
actor_ref.cast(GitNodeMessage::ScanAndRegister).ok();
Ok((actor_ref, handle))
}
pub fn get_cluster_nodes(storage_name: &str) -> Vec<ractor::ActorCell> {
pg::get_scoped_members(&storage_name.to_string(), &"node".to_string())
}
pub fn get_category_members(storage_name: &str, category: &str) -> Vec<ractor::ActorCell> {
pg::get_scoped_members(&storage_name.to_string(), &category.to_string())
}
pub fn route_group_for(header: &crate::pb::RepositoryHeader) -> String {
extract_category(&header.relative_path).to_string()
}
pub fn list_all_groups() -> Vec<String> {
pg::which_groups()
}
pub fn broadcast_ref_update(_node_actor: &ActorRef<GitNodeMessage>, event: RefUpdateEvent) {
let members = ractor::pg::get_members(&"gitks_nodes".to_string());
for member in members {
let actor_ref: ActorRef<GitNodeMessage> = member.into();
actor_ref
.cast(GitNodeMessage::RefUpdated(event.clone()))
.ok();
}
}
/// Broadcast a role change event to all cluster members.
pub fn broadcast_role_changed(_actor: &ActorRef<GitNodeMessage>, event: RoleChangedEvent) {
let members = ractor::pg::get_members(&"gitks_nodes".to_string());
for member in members {
let actor_ref: ActorRef<GitNodeMessage> = member.into();
actor_ref
.cast(GitNodeMessage::RoleChanged(event.clone()))
.ok();
}
}