Files
gitks/actor/message.rs
T
zhenyi 8c95eb230d refactor(actor): implement replica sync and ref update notification system
- Add is_write parameter to remote clients for read/write routing distinction
- Introduce RepoEntry struct with role tracking (primary/replica) for repositories
- Replace HashSet with HashMap for repository storage with role metadata
- Add ROLE_PRIMARY and ROLE_REPLICA constants for node role identification
- Implement FindPrimary and FindReplica RPC methods for role-based routing
- Add RefUpdateEvent message type for propagating reference updates
- Create sync module with BundleApplicator for handling replica synchronization
- Implement notify_ref_update calls after branch/tag/commit operations
- Add broadcast_ref_update function to propagate events across cluster nodes
- Modify route_repository to prioritize primary for writes and replicas for reads
- Update actor message handling to support role-based repository discovery
- Implement sync_from_primary function using pack protocol for incremental updates
2026-06-08 01:54:08 +08:00

173 lines
4.7 KiB
Rust

use ractor::RpcReplyPort;
use ractor_cluster::BytesConvertable;
use ractor_cluster::RactorClusterMessage;
use crate::pb::RepositoryHeader;
impl BytesConvertable for RepositoryHeader {
fn into_bytes(self) -> Vec<u8> {
prost::Message::encode_to_vec(&self)
}
fn from_bytes(bytes: Vec<u8>) -> Self {
prost::Message::decode(bytes.as_slice()).unwrap_or_default()
}
}
pub const ROLE_PRIMARY: &str = "primary";
pub const ROLE_REPLICA: &str = "replica";
#[derive(Debug, Clone)]
pub struct RouteDecision {
pub found: bool,
pub storage_name: String,
pub relative_path: String,
pub actor_name: String,
pub grpc_addr: String,
pub role: String,
}
impl BytesConvertable for RouteDecision {
fn into_bytes(self) -> Vec<u8> {
encode_strings(&[
if self.found { "1" } else { "0" }.to_string(),
self.storage_name,
self.relative_path,
self.actor_name,
self.grpc_addr,
self.role,
])
}
fn from_bytes(bytes: Vec<u8>) -> Self {
let values = decode_strings(bytes);
Self {
found: values.first().is_some_and(|v| v == "1"),
storage_name: values.get(1).cloned().unwrap_or_default(),
relative_path: values.get(2).cloned().unwrap_or_default(),
actor_name: values.get(3).cloned().unwrap_or_default(),
grpc_addr: values.get(4).cloned().unwrap_or_default(),
role: values.get(5).cloned().unwrap_or_default(),
}
}
}
#[derive(Debug, Clone)]
pub struct NodeHealth {
pub storage_name: String,
pub repo_count: u64,
pub healthy: bool,
pub version: String,
}
impl BytesConvertable for NodeHealth {
fn into_bytes(self) -> Vec<u8> {
encode_strings(&[
self.storage_name,
self.repo_count.to_string(),
if self.healthy { "1" } else { "0" }.to_string(),
self.version,
])
}
fn from_bytes(bytes: Vec<u8>) -> Self {
let values = decode_strings(bytes);
Self {
storage_name: values.first().cloned().unwrap_or_default(),
repo_count: values.get(1).and_then(|v| v.parse().ok()).unwrap_or_default(),
healthy: values.get(2).is_some_and(|v| v == "1"),
version: values.get(3).cloned().unwrap_or_default(),
}
}
}
#[derive(Debug, Clone)]
pub struct RefUpdateEvent {
pub relative_path: String,
pub ref_name: String,
pub old_oid: String,
pub new_oid: String,
pub primary_grpc_addr: String,
pub primary_storage_name: String,
}
impl BytesConvertable for RefUpdateEvent {
fn into_bytes(self) -> Vec<u8> {
encode_strings(&[
self.relative_path,
self.ref_name,
self.old_oid,
self.new_oid,
self.primary_grpc_addr,
self.primary_storage_name,
])
}
fn from_bytes(bytes: Vec<u8>) -> Self {
let values = decode_strings(bytes);
Self {
relative_path: values.first().cloned().unwrap_or_default(),
ref_name: values.get(1).cloned().unwrap_or_default(),
old_oid: values.get(2).cloned().unwrap_or_default(),
new_oid: values.get(3).cloned().unwrap_or_default(),
primary_grpc_addr: values.get(4).cloned().unwrap_or_default(),
primary_storage_name: values.get(5).cloned().unwrap_or_default(),
}
}
}
#[derive(RactorClusterMessage)]
pub enum GitNodeMessage {
ScanAndRegister,
RegisterRepository(RepositoryHeader),
RemoveRepository(RepositoryHeader),
RefUpdated(RefUpdateEvent),
#[rpc]
FindPrimary(RepositoryHeader, RpcReplyPort<RouteDecision>),
#[rpc]
FindReplica(RepositoryHeader, RpcReplyPort<RouteDecision>),
#[rpc]
ListRepositoryPaths(RpcReplyPort<String>),
#[rpc]
RepositoryExists(RepositoryHeader, RpcReplyPort<bool>),
#[rpc]
GetNodeHealth(RpcReplyPort<NodeHealth>),
}
#[derive(ractor_cluster::RactorMessage)]
pub enum RepoActorMessage {
UpdateMetadata(RepositoryHeader),
}
fn encode_strings(values: &[String]) -> Vec<u8> {
let mut buf = Vec::new();
for value in values {
let bytes = value.as_bytes();
buf.extend((bytes.len() as u64).to_be_bytes());
buf.extend(bytes);
}
buf
}
fn decode_strings(bytes: Vec<u8>) -> Vec<String> {
let mut values = Vec::new();
let mut offset = 0;
while offset + 8 <= bytes.len() {
let len = u64::from_be_bytes(bytes[offset..offset + 8].try_into().unwrap()) as usize;
offset += 8;
if offset + len > bytes.len() {
break;
}
values.push(String::from_utf8_lossy(&bytes[offset..offset + len]).into_owned());
offset += len;
}
values
}