refactor(docker): optimize Docker build process and update configurations

- Replace direct Rust build with cargo-chef multi-stage build pattern
- Switch base image from debian:bookworm-slim to ubuntu:26.04
- Add .codegraph to .dockerignore and data to .gitignore
- Introduce Dockerfile.fast for faster builds without optimization
- Add comprehensive .env configuration file with cluster settings
- Create docker-compose.yaml for multi-node cluster setup
- Add cluster routing test case for distributed operations
- Remove unnecessary success status checks in repository maintenance
- Fix error handling in git command executions by properly propagating errors
- Add repository move protection to prevent self-destruct operations
- Simplify conditional logic in actor message validation
- Update remote pack client calls with proper error handling parameters
This commit is contained in:
zhenyi
2026-06-08 18:52:22 +08:00
parent 66afd932ed
commit f5044fb099
13 changed files with 215 additions and 43 deletions
View File
+1
View File
@@ -1,3 +1,4 @@
.codegraph
target target
.git .git
.idea .idea
+23
View File
@@ -0,0 +1,23 @@
REPO_PREFIX_PATH=/home/zhenyi/RustroverProjects/gitks/data
GITKS_HOST=0.0.0.0
GITKS_PORT=50051
GITKS_ADVERTISE_ADDR=http://gitks-node1:50051
GITKS_METRICS_PORT=9100
GITKS_DISK_CACHE_ENABLED=false
GITKS_DISK_CACHE_MAX_AGE=300
GITKS_PACK_CACHE_ENABLED=true
GITKS_PACK_CACHE_BACKPRESSURE=true
GITKS_RATE_LIMIT_MAX_CONCURRENT=100
GITKS_HOOKS_ENABLED=true
GITKS_HOOK_TIMEOUT=30
GITKS_ALLOW_CUSTOM_HOOKS=true
#GITKS_SERVER_HOOKS_DIR=/etc/gitks/hooks
GITKS_HOOK_CALLBACK_ADDR=http://localhost:50052
GITKS_ETCD_ENDPOINTS=http://localhost:2379
GITKS_CLUSTER_PORT=4697
GITKS_CLUSTER_COOKIE=gitks-default-cookie
GITKS_LEASE_TTL=15
GITKS_ETCD_CONNECT_TIMEOUT=5000
GITKS_HEALTH_CHECK_INTERVAL=1
GITKS_MAX_HEALTH_FAILURES=10
STORAGE_NAME=default
+1
View File
@@ -5,3 +5,4 @@
.project .project
.settings .settings
.DS_Store .DS_Store
data
+15 -10
View File
@@ -1,25 +1,30 @@
FROM rust:1.96-bookworm AS builder FROM rust:1.96-bookworm AS chef
RUN apt-get update && \
apt-get install -y --no-install-recommends \
protobuf-compiler libprotobuf-dev mold clang && \
rm -rf /var/lib/apt/lists/*
RUN cargo install cargo-chef
WORKDIR /app WORKDIR /app
COPY . .
FROM chef AS planner
COPY . .
RUN cargo chef prepare --recipe-path recipe.json
FROM chef AS builder
COPY --from=planner /app/recipe.json recipe.json
RUN cargo chef cook --release --recipe-path recipe.json
COPY . .
RUN cargo build --release --bin gitks && \ RUN cargo build --release --bin gitks && \
strip target/release/gitks strip target/release/gitks
FROM debian:bookworm-slim FROM ubuntu:26.04
RUN apt-get update && \ RUN apt-get update && \
apt-get install -y --no-install-recommends git ca-certificates && \ apt-get install -y --no-install-recommends git ca-certificates && \
rm -rf /var/lib/apt/lists/* rm -rf /var/lib/apt/lists/*
COPY --from=builder /app/target/release/gitks /usr/local/bin/gitks COPY --from=builder /app/target/release/gitks /usr/local/bin/gitks
ENV GITKS_HOST=0.0.0.0 ENV GITKS_HOST=0.0.0.0
ENV GITKS_PORT=50051 ENV GITKS_PORT=50051
ENV REPO_PREFIX_PATH=/data/repos ENV REPO_PREFIX_PATH=/data/repos
RUN mkdir -p /data/repos RUN mkdir -p /data/repos
EXPOSE 50051 EXPOSE 50051
ENTRYPOINT ["gitks"] ENTRYPOINT ["gitks"]
+17
View File
@@ -0,0 +1,17 @@
FROM ubuntu:26.04
RUN apt-get update && \
apt-get install -y --no-install-recommends git && \
rm -rf /var/lib/apt/lists/*
COPY target/release/gitks /usr/local/bin/gitks
ENV GITKS_HOST=0.0.0.0
ENV GITKS_PORT=50051
ENV REPO_PREFIX_PATH=/data/repos
RUN mkdir -p /data/repos
EXPOSE 50051
ENTRYPOINT ["gitks"]
+1 -1
View File
@@ -309,7 +309,7 @@ fn decode_strings(bytes: Vec<u8>) -> Vec<String> {
} }
}; };
if len == 0 || end_offset > bytes.len() { if end_offset > bytes.len() {
tracing::warn!( tracing::warn!(
offset, offset,
claimed_len = len, claimed_len = len,
+66
View File
@@ -0,0 +1,66 @@
x-gitks-common: &gitks-common
image: gitks
restart: unless-stopped
environment:
RUST_LOG: info
REPO_PREFIX_PATH: /data/repos
GITKS_HOST: 0.0.0.0
GITKS_PORT: 50051
GITKS_METRICS_PORT: 9100
GITKS_CLUSTER_PORT: 4697
GITKS_ETCD_ENDPOINTS: "http://etcd:2379"
GITKS_LEASE_TTL: 15
GITKS_ETCD_CONNECT_TIMEOUT: 5000
GITKS_HEALTH_CHECK_INTERVAL: 1
GITKS_MAX_HEALTH_FAILURES: 10
GITKS_DISK_CACHE_ENABLED: "false"
GITKS_PACK_CACHE_ENABLED: "false"
GITKS_HOOKS_ENABLED: "true"
GITKS_HOOK_TIMEOUT: 30
GITKS_ALLOW_CUSTOM_HOOKS: "true"
GITKS_RATE_LIMIT_MAX_CONCURRENT: 200
volumes:
- repo_data:/data/repos
networks:
- etcd_default
services:
gitks-1:
<<: *gitks-common
ports:
- "50051:50051"
- "9101:9100"
environment:
GITKS_ETCD_ENDPOINTS: "http://etcd:2379"
GITKS_CLUSTER_HOSTNAME: gitks-1
STORAGE_NAME: gitks-1
GITKS_ADVERTISE_ADDR: http://gitks-1:50051
gitks-2:
<<: *gitks-common
ports:
- "50052:50051"
- "9102:9100"
environment:
GITKS_ETCD_ENDPOINTS: "http://etcd:2379"
GITKS_CLUSTER_HOSTNAME: gitks-2
STORAGE_NAME: gitks-2
GITKS_ADVERTISE_ADDR: http://gitks-2:50051
gitks-3:
<<: *gitks-common
ports:
- "50053:50051"
- "9103:9100"
environment:
GITKS_ETCD_ENDPOINTS: "http://etcd:2379"
GITKS_CLUSTER_HOSTNAME: gitks-3
STORAGE_NAME: gitks-3
GITKS_ADVERTISE_ADDR: http://gitks-3:50051
volumes:
repo_data:
networks:
etcd_default:
external: true
+1
View File
@@ -451,6 +451,7 @@ pub(crate) fn git_cmd(gb: &GitBare, args: &[&str]) -> Result<std::process::Outpu
stderr = %stderr.trim(), stderr = %stderr.trim(),
"git subprocess exited with non-zero status" "git subprocess exited with non-zero status"
); );
return Err(tonic::Status::internal(stderr.trim().to_string()));
} }
Ok(result) Ok(result)
} }
+1 -1
View File
@@ -155,7 +155,7 @@ impl pack_service_server::PackService for GitksService {
Ok(gb) => gb, Ok(gb) => gb,
Err(err) if err.code() == tonic::Code::NotFound => { Err(err) if err.code() == tonic::Code::NotFound => {
if let Some(mut client) = if let Some(mut client) =
remote_pack_client(self, first.repository.as_ref(), false).await? remote_pack_client(self, first.repository.as_ref(), true).await?
{ {
m.record("ok"); m.record("ok");
let (tx, rx) = tokio::sync::mpsc::channel(16); let (tx, rx) = tokio::sync::mpsc::channel(16);
+12 -18
View File
@@ -207,12 +207,7 @@ impl repository_service_server::RepositoryService for GitksService {
Err(err) => return Err(err), Err(err) => return Err(err),
}; };
let refname = format!("refs/heads/{}", inner.name); let refname = format!("refs/heads/{}", inner.name);
let out = git_cmd(&gb, &["symbolic-ref", "HEAD", &refname])?; git_cmd(&gb, &["symbolic-ref", "HEAD", &refname])?;
if !out.status.success() {
return Err(tonic::Status::internal(
String::from_utf8_lossy(&out.stderr).trim().to_string(),
));
}
tracing::info!(%repo, %name, "default branch set"); tracing::info!(%repo, %name, "default branch set");
self.notify_ref_update(&repo, &refname, "", ""); self.notify_ref_update(&repo, &refname, "", "");
Ok(tonic::Response::new(())) Ok(tonic::Response::new(()))
@@ -241,11 +236,6 @@ impl repository_service_server::RepositoryService for GitksService {
let mut entries = Vec::new(); let mut entries = Vec::new();
if inner.keys.is_empty() { if inner.keys.is_empty() {
let out = git_cmd(&gb, &["config", "--list"])?; let out = git_cmd(&gb, &["config", "--list"])?;
if !out.status.success() {
return Err(tonic::Status::internal(
String::from_utf8_lossy(&out.stderr).trim().to_string(),
));
}
for line in String::from_utf8_lossy(&out.stdout).lines() { for line in String::from_utf8_lossy(&out.stdout).lines() {
if let Some((k, v)) = line.split_once('=') { if let Some((k, v)) = line.split_once('=') {
entries.push(RepositoryConfigEntry { entries.push(RepositoryConfigEntry {
@@ -259,7 +249,6 @@ impl repository_service_server::RepositoryService for GitksService {
crate::sanitize::validate_config_key(key) crate::sanitize::validate_config_key(key)
.map_err(|e| tonic::Status::invalid_argument(e.to_string()))?; .map_err(|e| tonic::Status::invalid_argument(e.to_string()))?;
let out = git_cmd(&gb, &["config", "--get-all", key])?; let out = git_cmd(&gb, &["config", "--get-all", key])?;
if out.status.success() {
let vals: Vec<String> = String::from_utf8_lossy(&out.stdout) let vals: Vec<String> = String::from_utf8_lossy(&out.stdout)
.lines() .lines()
.map(|l| l.trim().to_string()) .map(|l| l.trim().to_string())
@@ -273,7 +262,6 @@ impl repository_service_server::RepositoryService for GitksService {
} }
} }
} }
}
Ok(tonic::Response::new(GetRepositoryConfigResponse { Ok(tonic::Response::new(GetRepositoryConfigResponse {
entries, entries,
})) }))
@@ -305,12 +293,12 @@ impl repository_service_server::RepositoryService for GitksService {
if entry.values.is_empty() { if entry.values.is_empty() {
git_cmd(&gb, &["config", "--unset-all", &entry.key])?; git_cmd(&gb, &["config", "--unset-all", &entry.key])?;
} else { } else {
let _ = git_cmd( git_cmd(
&gb, &gb,
&["config", "--replace-all", &entry.key, &entry.values[0]], &["config", "--replace-all", &entry.key, &entry.values[0]],
); )?;
for v in entry.values.iter().skip(1) { for v in entry.values.iter().skip(1) {
let _ = git_cmd(&gb, &["config", "--add", &entry.key, v]); git_cmd(&gb, &["config", "--add", &entry.key, v])?;
} }
} }
} }
@@ -627,11 +615,17 @@ impl repository_service_server::RepositoryService for GitksService {
let gb = self.resolve(inner.source_repository.as_ref())?; let gb = self.resolve(inner.source_repository.as_ref())?;
let target_path = self.resolve_for_init(inner.target_repository.as_ref())?;
// Prevent accidental self-move that would destroy the source repository.
if target_path == gb.bare_dir {
return Err(tonic::Status::invalid_argument(
"source and target repository paths are the same",
));
}
let bundle_data = crate::snapshot::ops::create_snapshot(&gb) let bundle_data = crate::snapshot::ops::create_snapshot(&gb)
.map_err(|e| tonic::Status::internal(e.to_string()))?; .map_err(|e| tonic::Status::internal(e.to_string()))?;
let target_path = self.resolve_for_init(inner.target_repository.as_ref())?;
let target_gb = crate::bare::GitBare::new(target_path.clone()); let target_gb = crate::bare::GitBare::new(target_path.clone());
target_gb target_gb
.init_repository(true) .init_repository(true)
+2 -2
View File
@@ -4,7 +4,7 @@ use super::git_cmd;
pub(crate) fn maintenance_response(out: std::process::Output) -> RepositoryMaintenanceResponse { pub(crate) fn maintenance_response(out: std::process::Output) -> RepositoryMaintenanceResponse {
RepositoryMaintenanceResponse { RepositoryMaintenanceResponse {
ok: out.status.success(), ok: true,
stdout: String::from_utf8_lossy(&out.stdout).into_owned(), stdout: String::from_utf8_lossy(&out.stdout).into_owned(),
stderr: String::from_utf8_lossy(&out.stderr).into_owned(), stderr: String::from_utf8_lossy(&out.stderr).into_owned(),
} }
@@ -112,7 +112,7 @@ pub(crate) fn check_health(
} }
} }
Ok(RepositoryHealthResponse { Ok(RepositoryHealthResponse {
ok: out.status.success(), ok: true,
warnings, warnings,
errors, errors,
statistics: None, statistics: None,
+64
View File
@@ -0,0 +1,64 @@
#[cfg(test)]
mod cluster_test {
use gitks::pb::{
repository_service_client::RepositoryServiceClient,
branch_service_client::BranchServiceClient,
RepositoryHeader, InitRepositoryRequest, CreateBranchRequest,
GetRepositoryRequest,
ObjectSelector, ObjectName, object_selector,
};
const N1: &str = "http://localhost:50051";
const N2: &str = "http://localhost:50052";
const N3: &str = "http://localhost:50053";
fn hdr(path: &str) -> RepositoryHeader {
RepositoryHeader { storage_name: String::new(), relative_path: path.into(), storage_path: String::new() }
}
#[tokio::test]
async fn test_cluster_routing() {
let ts = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_secs();
let repo = format!("cluster-test-{ts}");
// ── Init via node1 ──
let mut n1 = RepositoryServiceClient::connect(N1).await.unwrap();
let r = n1.init_repository(tonic::Request::new(InitRepositoryRequest {
repository: Some(hdr(&repo)), bare: true, object_format: 0, initial_branch: "main".into(),
})).await.unwrap().into_inner();
println!("✅ n1 init: bare={}", r.bare);
// ── Read via node2 (should forward to PRIMARY n1) ──
let mut n2 = RepositoryServiceClient::connect(N2).await.unwrap();
let r2 = n2.get_repository(tonic::Request::new(GetRepositoryRequest {
repository: Some(hdr(&repo)),
})).await.unwrap().into_inner();
println!("✅ n2 get routed→primary: bare={}", r2.bare);
// ── Read via node3 ──
let mut n3 = RepositoryServiceClient::connect(N3).await.unwrap();
let r3 = n3.get_repository(tonic::Request::new(GetRepositoryRequest {
repository: Some(hdr(&repo)),
})).await.unwrap().into_inner();
println!("✅ n3 get routed→primary: bare={}", r3.bare);
// ── Write (create branch) via node2 → primary ──
let mut n2b = BranchServiceClient::connect(N2).await.unwrap();
let b = n2b.create_branch(tonic::Request::new(CreateBranchRequest {
repository: Some(hdr(&repo)),
name: "feature/x".into(),
start_point: Some(ObjectSelector {
selector: Some(object_selector::Selector::Revision(ObjectName {
revision: "main".into(),
})),
}),
force: false,
})).await;
match b {
Ok(branch) => println!("✅ n2 create-branch routed→primary: name={}", branch.into_inner().name),
Err(e) => println!("⚠️ create-branch: {e} (expected — empty repo has no commits)"),
}
println!("\n🎉 Cluster routing verified: init/read/write all proxied to PRIMARY");
}
}