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
This commit is contained in:
zhenyi
2026-06-08 01:54:08 +08:00
parent 5c99b27421
commit 8c95eb230d
16 changed files with 518 additions and 105 deletions
+15 -9
View File
@@ -6,12 +6,13 @@ use super::{GitksService, into_status};
async fn remote_branch_client(
svc: &GitksService,
header: Option<&RepositoryHeader>,
is_write: bool,
) -> Result<Option<BranchServiceClient<tonic::transport::Channel>>, tonic::Status> {
let header = match header {
Some(h) => h,
None => return Ok(None),
};
let Some(route) = svc.route_repository(header).await? else {
let Some(route) = svc.route_repository(header, is_write).await? else {
return Ok(None);
};
tracing::info!(storage_name = %route.storage_name, relative_path = %route.relative_path, actor_name = %route.actor_name, grpc_addr = %route.grpc_addr, "forwarding branch rpc");
@@ -35,7 +36,7 @@ impl branch_service_server::BranchService for GitksService {
let gb = match self.resolve(inner.repository.as_ref()) {
Ok(gb) => gb,
Err(err) if err.code() == tonic::Code::NotFound => {
if let Some(mut client) = remote_branch_client(self, inner.repository.as_ref()).await? {
if let Some(mut client) = remote_branch_client(self, inner.repository.as_ref(), false).await? {
return client.list_branches(inner).await;
}
return Err(err);
@@ -59,7 +60,7 @@ impl branch_service_server::BranchService for GitksService {
let gb = match self.resolve(inner.repository.as_ref()) {
Ok(gb) => gb,
Err(err) if err.code() == tonic::Code::NotFound => {
if let Some(mut client) = remote_branch_client(self, inner.repository.as_ref()).await? {
if let Some(mut client) = remote_branch_client(self, inner.repository.as_ref(), false).await? {
return client.get_branch(inner).await;
}
return Err(err);
@@ -82,7 +83,7 @@ impl branch_service_server::BranchService for GitksService {
let gb = match self.resolve(inner.repository.as_ref()) {
Ok(gb) => gb,
Err(err) if err.code() == tonic::Code::NotFound => {
if let Some(mut client) = remote_branch_client(self, inner.repository.as_ref()).await? {
if let Some(mut client) = remote_branch_client(self, inner.repository.as_ref(), true).await? {
return client.create_branch(inner).await;
}
return Err(err);
@@ -91,6 +92,7 @@ impl branch_service_server::BranchService for GitksService {
};
let resp = gb.create_branch(inner).map_err(into_status)?;
tracing::info!(%repo, %name, "branch created");
self.notify_ref_update(&repo, &format!("refs/heads/{}", name), "", "");
Ok(tonic::Response::new(resp))
}
@@ -106,7 +108,7 @@ impl branch_service_server::BranchService for GitksService {
let gb = match self.resolve(inner.repository.as_ref()) {
Ok(gb) => gb,
Err(err) if err.code() == tonic::Code::NotFound => {
if let Some(mut client) = remote_branch_client(self, inner.repository.as_ref()).await? {
if let Some(mut client) = remote_branch_client(self, inner.repository.as_ref(), true).await? {
return client.delete_branch(inner).await;
}
return Err(err);
@@ -115,6 +117,7 @@ impl branch_service_server::BranchService for GitksService {
};
gb.delete_branch(inner).map_err(into_status)?;
tracing::info!(%repo, %name, "branch deleted");
self.notify_ref_update(&repo, &format!("refs/heads/{}", name), "", "");
Ok(tonic::Response::new(()))
}
@@ -131,7 +134,7 @@ impl branch_service_server::BranchService for GitksService {
let gb = match self.resolve(inner.repository.as_ref()) {
Ok(gb) => gb,
Err(err) if err.code() == tonic::Code::NotFound => {
if let Some(mut client) = remote_branch_client(self, inner.repository.as_ref()).await? {
if let Some(mut client) = remote_branch_client(self, inner.repository.as_ref(), true).await? {
return client.rename_branch(inner).await;
}
return Err(err);
@@ -140,6 +143,7 @@ impl branch_service_server::BranchService for GitksService {
};
let resp = gb.rename_branch(inner).map_err(into_status)?;
tracing::info!(%repo, old = %old, new = %new, "branch renamed");
self.notify_ref_update(&repo, &format!("refs/heads/{}", new), "", "");
Ok(tonic::Response::new(resp))
}
@@ -155,7 +159,7 @@ impl branch_service_server::BranchService for GitksService {
let gb = match self.resolve(inner.repository.as_ref()) {
Ok(gb) => gb,
Err(err) if err.code() == tonic::Code::NotFound => {
if let Some(mut client) = remote_branch_client(self, inner.repository.as_ref()).await? {
if let Some(mut client) = remote_branch_client(self, inner.repository.as_ref(), true).await? {
return client.update_branch_target(inner).await;
}
return Err(err);
@@ -164,6 +168,7 @@ impl branch_service_server::BranchService for GitksService {
};
let resp = gb.update_branch_target(inner).map_err(into_status)?;
tracing::info!(%repo, %name, "branch target updated");
self.notify_ref_update(&repo, &format!("refs/heads/{}", name), "", "");
Ok(tonic::Response::new(resp))
}
@@ -179,7 +184,7 @@ impl branch_service_server::BranchService for GitksService {
let gb = match self.resolve(inner.repository.as_ref()) {
Ok(gb) => gb,
Err(err) if err.code() == tonic::Code::NotFound => {
if let Some(mut client) = remote_branch_client(self, inner.repository.as_ref()).await? {
if let Some(mut client) = remote_branch_client(self, inner.repository.as_ref(), true).await? {
return client.set_branch_upstream(inner).await;
}
return Err(err);
@@ -188,6 +193,7 @@ impl branch_service_server::BranchService for GitksService {
};
let resp = gb.set_branch_upstream(inner).map_err(into_status)?;
tracing::info!(%repo, %name, "branch upstream set");
self.notify_ref_update(&repo, &format!("refs/heads/{}", name), "", "");
Ok(tonic::Response::new(resp))
}
@@ -204,7 +210,7 @@ impl branch_service_server::BranchService for GitksService {
let gb = match self.resolve(inner.repository.as_ref()) {
Ok(gb) => gb,
Err(err) if err.code() == tonic::Code::NotFound => {
if let Some(mut client) = remote_branch_client(self, inner.repository.as_ref()).await? {
if let Some(mut client) = remote_branch_client(self, inner.repository.as_ref(), false).await? {
return client.compare_branch(inner).await;
}
return Err(err);