8c95eb230d
- 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
85 lines
3.5 KiB
Rust
85 lines
3.5 KiB
Rust
use crate::pb::*;
|
|
use crate::pb::archive_service_client::ArchiveServiceClient;
|
|
|
|
use super::{GitksService, cache, into_status};
|
|
|
|
async fn remote_archive_client(
|
|
svc: &GitksService,
|
|
header: Option<&RepositoryHeader>,
|
|
is_write: bool,
|
|
) -> Result<Option<ArchiveServiceClient<tonic::transport::Channel>>, tonic::Status> {
|
|
let header = match header {
|
|
Some(h) => h,
|
|
None => return Ok(None),
|
|
};
|
|
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 archive rpc");
|
|
let endpoint = super::remote_endpoint(&route.grpc_addr).await?;
|
|
let client = ArchiveServiceClient::connect(endpoint)
|
|
.await
|
|
.map_err(|e| tonic::Status::unavailable(e.to_string()))?;
|
|
Ok(Some(client))
|
|
}
|
|
|
|
#[tonic::async_trait]
|
|
impl archive_service_server::ArchiveService for GitksService {
|
|
type GetArchiveStream =
|
|
tokio_stream::wrappers::ReceiverStream<Result<ArchiveChunk, tonic::Status>>;
|
|
|
|
async fn get_archive(
|
|
&self,
|
|
request: tonic::Request<ArchiveRequest>,
|
|
) -> Result<tonic::Response<Self::GetArchiveStream>, tonic::Status> {
|
|
let inner = request.into_inner();
|
|
let repo = self.repo_label(inner.repository.as_ref());
|
|
let span = tracing::info_span!("archive.get_archive", %repo);
|
|
let _enter = span.enter();
|
|
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_archive_client(self, inner.repository.as_ref(), false).await? {
|
|
let resp = client.get_archive(inner).await?;
|
|
let stream = super::bridge_server_stream(resp.into_inner());
|
|
return Ok(tonic::Response::new(stream));
|
|
}
|
|
return Err(err);
|
|
}
|
|
Err(err) => return Err(err),
|
|
};
|
|
let stream = gb.get_archive_stream(inner)?;
|
|
tracing::info!(%repo, "archive streaming started");
|
|
Ok(tonic::Response::new(stream))
|
|
}
|
|
|
|
async fn list_archive_entries(
|
|
&self,
|
|
request: tonic::Request<ListArchiveEntriesRequest>,
|
|
) -> Result<tonic::Response<ListArchiveEntriesResponse>, tonic::Status> {
|
|
let inner = request.into_inner();
|
|
let repo = self.repo_label(inner.repository.as_ref());
|
|
let span = tracing::info_span!("archive.list_archive_entries", %repo);
|
|
let _enter = span.enter();
|
|
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_archive_client(self, inner.repository.as_ref(), false).await? {
|
|
return client.list_archive_entries(inner).await;
|
|
}
|
|
return Err(err);
|
|
}
|
|
Err(err) => return Err(err),
|
|
};
|
|
let resp = if cache::selector_is_oid(&inner.treeish) {
|
|
cache::cached_response("archive.list_archive_entries", &inner, || {
|
|
gb.list_archive_entries(inner.clone()).map_err(into_status)
|
|
})?
|
|
} else {
|
|
gb.list_archive_entries(inner).map_err(into_status)?
|
|
};
|
|
tracing::info!(%repo, count = resp.entries.len(), "list_archive_entries done");
|
|
Ok(tonic::Response::new(resp))
|
|
}
|
|
}
|