feat(gateway): implement remote service forwarding for distributed git operations
- Add remote client functions for archive, blame, and branch services - Implement fallback logic to forward requests to remote storage nodes - Add logging for forwarding operations with route details - Update Cargo.lock with new dependencies including ractor cluster libraries - Extend .gitignore with IDE and build system files - Remove outdated comments from bare repository implementation
This commit is contained in:
@@ -0,0 +1,200 @@
|
||||
use std::collections::HashSet;
|
||||
use async_trait::async_trait;
|
||||
use ractor::pg;
|
||||
use ractor::{Actor, ActorProcessingErr, ActorRef, SupervisionEvent};
|
||||
use crate::actor::message::{GitNodeMessage, NodeHealth, RouteDecision};
|
||||
use crate::pb::RepositoryHeader;
|
||||
use crate::server::GitksService;
|
||||
|
||||
#[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 GitNodeArgs {
|
||||
pub storage_name: String,
|
||||
pub grpc_addr: String,
|
||||
}
|
||||
|
||||
pub struct GitNodeState {
|
||||
storage_name: String,
|
||||
actor_name: String,
|
||||
grpc_addr: String,
|
||||
registered_repos: HashSet<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");
|
||||
Ok(GitNodeState {
|
||||
storage_name: args.storage_name,
|
||||
actor_name,
|
||||
grpc_addr: args.grpc_addr,
|
||||
registered_repos: HashSet::new(),
|
||||
})
|
||||
}
|
||||
|
||||
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");
|
||||
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.registered_repos.remove(&header.relative_path);
|
||||
tracing::info!(
|
||||
storage_name = %state.storage_name,
|
||||
relative_path = %header.relative_path,
|
||||
"repository route removed"
|
||||
);
|
||||
}
|
||||
|
||||
GitNodeMessage::RouteRepository(header, reply) => {
|
||||
let found = state.registered_repos.contains(&header.relative_path);
|
||||
reply.send(RouteDecision {
|
||||
found,
|
||||
storage_name: state.storage_name.clone(),
|
||||
relative_path: header.relative_path,
|
||||
actor_name: if found { state.actor_name.clone() } else { String::new() },
|
||||
grpc_addr: if found { state.grpc_addr.clone() } else { String::new() },
|
||||
}).ok();
|
||||
}
|
||||
|
||||
GitNodeMessage::ListRepositoryPaths(reply) => {
|
||||
let paths: Vec<String> = state.registered_repos.iter().cloned().collect();
|
||||
reply.send(paths.join("\n")).ok();
|
||||
}
|
||||
|
||||
GitNodeMessage::RepositoryExists(header, reply) => {
|
||||
reply.send(state.registered_repos.contains(&header.relative_path)).ok();
|
||||
}
|
||||
|
||||
GitNodeMessage::GetNodeHealth(reply) => {
|
||||
reply.send(NodeHealth {
|
||||
storage_name: state.storage_name.clone(),
|
||||
repo_count: state.registered_repos.len() as u64,
|
||||
healthy: true,
|
||||
version: self.version.clone(),
|
||||
}).ok();
|
||||
}
|
||||
}
|
||||
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(())
|
||||
}
|
||||
}
|
||||
|
||||
fn register_repo(myself: &ActorRef<GitNodeMessage>, state: &mut GitNodeState, relative_path: String) {
|
||||
let category = extract_category(&relative_path);
|
||||
pg::join_scoped(state.storage_name.clone(), category.to_string(), vec![myself.get_cell()]);
|
||||
state.registered_repos.insert(relative_path.clone());
|
||||
tracing::info!(
|
||||
storage_name = %state.storage_name,
|
||||
category = %category,
|
||||
relative_path = %relative_path,
|
||||
actor_name = %state.actor_name,
|
||||
"repository route registered"
|
||||
);
|
||||
}
|
||||
|
||||
fn extract_category(relative_path: &str) -> &str {
|
||||
relative_path.split('/').next().unwrap_or("root")
|
||||
}
|
||||
|
||||
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: &RepositoryHeader) -> String {
|
||||
extract_category(&header.relative_path).to_string()
|
||||
}
|
||||
|
||||
pub fn list_all_groups() -> Vec<String> {
|
||||
pg::which_groups()
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
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()
|
||||
}
|
||||
}
|
||||
|
||||
#[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,
|
||||
}
|
||||
|
||||
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,
|
||||
])
|
||||
}
|
||||
|
||||
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(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[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(RactorClusterMessage)]
|
||||
pub enum GitNodeMessage {
|
||||
ScanAndRegister,
|
||||
|
||||
RegisterRepository(RepositoryHeader),
|
||||
|
||||
RemoveRepository(RepositoryHeader),
|
||||
|
||||
#[rpc]
|
||||
RouteRepository(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
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
pub mod message;
|
||||
pub mod handler;
|
||||
pub mod server;
|
||||
|
||||
pub use handler::{GitNodeActor, GitNodeArgs, start_node_actor, get_cluster_nodes, get_category_members, route_group_for, list_all_groups};
|
||||
pub use server::init_actor_cluster;
|
||||
pub use message::{GitNodeMessage, NodeHealth, RepoActorMessage, RouteDecision};
|
||||
@@ -0,0 +1,15 @@
|
||||
use ractor::ActorRef;
|
||||
use crate::actor::handler::start_node_actor;
|
||||
use crate::actor::message::GitNodeMessage;
|
||||
use crate::server::GitksService;
|
||||
|
||||
pub async fn init_actor_cluster(
|
||||
service: GitksService,
|
||||
storage_name: String,
|
||||
grpc_addr: String,
|
||||
) -> Result<(ActorRef<GitNodeMessage>, tokio::task::JoinHandle<()>), ractor::SpawnErr> {
|
||||
tracing::info!(storage_name = %storage_name, grpc_addr = %grpc_addr, "initializing actor cluster");
|
||||
let result = start_node_actor(service, storage_name.clone(), grpc_addr).await?;
|
||||
tracing::info!(storage_name = %storage_name, "actor cluster ready");
|
||||
Ok(result)
|
||||
}
|
||||
Reference in New Issue
Block a user