Triển khai toàn bộ service bằng một docker compose

chandra + kie (vLLM, weights mount từ CHANDRA_MODEL_DIR/KIE_MODEL_DIR) và
api + worker (image build từ Dockerfile, network_mode host, đọc .env) thay cho
hai tmux run-api.sh/run-worker.sh và docker run chandra riêng. Worker mount
native-hdfs của gói portable, đợi hai model healthy mới nhận trang. GPU chọn
bằng GPU_DEVICE (driver nvidia) thay cho CDI GPU_UUID.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-09-28 13:40:05 +00:00
parent 3cbba8bd79
commit 0ac9a5c0fb
6 changed files with 181 additions and 82 deletions

5
.dockerignore Normal file
View File

@@ -0,0 +1,5 @@
*
!requirements-runtime.txt
!pyproject.toml
!idp_service
idp_service/__pycache__

41
.env.example Normal file
View File

@@ -0,0 +1,41 @@
# docker compose đọc file này (đổi tên thành .env) cho cả biến của compose lẫn env của api/worker.
# --- Model (compose) ---
GPU_DEVICE=0 # index hoặc UUID card, như `docker run --gpus device=0`
CHANDRA_MODEL_DIR=/path/to/chandra-dla-v2 # thư mục weights trên host
KIE_MODEL_DIR=/path/to/Qwen3.5-4B # snapshot HF đã bung (không để symlink trỏ ra ngoài)
GPU_UTIL=0.55 # chandra; GPU_UTIL + KIE_GPU_UTIL < phần card còn trống
KIE_GPU_UTIL=0.3
PORT=9222
KIE_PORT=9224
# IMAGE_TAG=chandra-dla-api:v2-20260906-024126
# KIE_IMAGE=vllm/vllm-openai:v0.23.0
# APP_IMAGE=idp-parsing-service:0.2.0
# --- HDFS native (worker) ---
HDFS_NATIVE_DIR=/root/idp-portable/native-hdfs # bin/hdfs, conf/krb5.conf, conf/*.keytab
IDP_STORAGE=hdfs_cli
IDP_HDFS_NATIVE_URI=hdfs://vptwd
HDFS_PRINCIPAL=searchengine@DATALAKE.DCS.VN
# --- Service ---
IDP_HOST=0.0.0.0
IDP_PORT=8001
IDP_API_KEY= # bắt buộc đặt khi API mở ra mạng
IDP_CALLBACK_HOSTS=10.1.18.104
NO_PROXY=127.0.0.1,localhost,10.1.18.54,10.1.18.104
IDP_CALLBACK_BASIC_USER=callback_handler
IDP_CALLBACK_BASIC_PASSWORD=change-me
IDP_DATA_DIR=/data/idp-runtime # SQLite + file làm việc; đĩa local, không NFS
IDP_OCR_URL=http://127.0.0.1:9222/v1/chat/completions
IDP_OCR_MODEL=chandra
IDP_KIE_URL=http://127.0.0.1:9224/v1/chat/completions
IDP_KIE_MODEL=qwen3.5-4b
IDP_OCR_WORKERS=64
IDP_OCR_TIMEOUT=180
IDP_LEASE_SECONDS=1800
IDP_VOFFICE_DECRYPT_MODE=auto
IDP_VOFFICE_DES_KEY_HEX=a1e3c21919adeeab

1
.gitignore vendored
View File

@@ -9,3 +9,4 @@ build/
chandra-dla-api-v2-20260906-024126.tar
hdfs-kbr.zip
.env
native-hdfs/

15
Dockerfile Normal file
View File

@@ -0,0 +1,15 @@
FROM python:3.11-slim
# kinit cho client HDFS native (Kerberos) mount từ gói portable vào /opt/native-hdfs.
RUN apt-get update \
&& DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends krb5-user \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /app
COPY requirements-runtime.txt .
RUN pip install --no-cache-dir -r requirements-runtime.txt
COPY pyproject.toml .
COPY idp_service idp_service
RUN pip install --no-cache-dir --no-deps .
ENV PYTHONUNBUFFERED=1

View File

@@ -114,43 +114,6 @@ curl -s http://127.0.0.1:8080/jobs/JOB_ID/result > out.json
`infer.py` là công cụ thử nghiệm độc lập và cần Pillow khi xử lý ảnh. Pillow
không nằm trong runtime của service PDF.
### Cài offline trên máy khác
Nếu máy đích không có Internet và cần mang cả Python lẫn virtualenv, dùng gói
`export/idp-portable-linux-x86_64-py311.tar.gz`:
```bash
tar -xzf idp-portable-linux-x86_64-py311.tar.gz
cd idp-portable
cp .env.example .env
# Sửa các hostname, đường dẫn và khóa trong .env
./run-api.sh
# Terminal/process khác:
./run-worker.sh
```
Gói tự chứa CPython 3.11 và toàn bộ runtime, không cần chạy `pip install`.
Hai script tự đọc file `.env` cạnh chúng. Có thể dùng file ở vị trí khác bằng
`IDP_ENV_FILE=/etc/idp/service.env ./run-api.sh`. File dùng cú pháp biến môi
trường của shell; giá trị có khoảng trắng phải đặt trong dấu nháy. Chỉ cấp quyền
đọc file cho tài khoản chạy service vì file chứa API key và khóa giải mã.
Nó chỉ dùng được trên Linux x86_64 có glibc tương thích (PyMuPDF yêu cầu
manylinux 2.28). Có thể chuyển thư mục đã giải nén tới vị trí khác.
Nếu máy đích đã có Python 3.11, bản wheel nhỏ hơn nằm ở
`export/idp-runtime-linux-x86_64-py311.tar.gz`:
Thư mục `export/runtime-wheels` chứa wheel của service và toàn bộ dependency cho
Linux x86_64, CPython 3.11. Chép cả thư mục sang máy đích rồi chạy:
```bash
python3.11 -m pip install --no-index --find-links runtime-wheels \
idp-parsing-service[hdfs]==0.1.0
```
Wheel có native code (`PyMuPDF`, `PyCryptodome`, `pydantic-core`) nên phải tạo
lại bộ export nếu máy đích khác kiến trúc hoặc phiên bản Python.
Gửi job từ đường dẫn HDFS sau khi PDF đã ghi xong:
```bash
@@ -163,20 +126,50 @@ curl http://127.0.0.1:8080/jobs/JOB_ID/pages
Khi đặt `IDP_API_KEY`, mọi endpoint job cần header `X-API-Key`.
## Dựng model
## Triển khai bằng docker compose
`docker-compose.yaml` dựng cả hai model trên **cùng một card** (`GPU_UUID` trong
`.env`): `chandra` (image `IMAGE_TAG`, mặc định `chandra-dla-api:v2-20260906-024126`
đã đóng sẵn weights v2) cổng `PORT` (9222) và `kie` (Qwen3.5-4B, image
`vllm/vllm-openai:v0.23.0`) cổng `KIE_PORT` (9224). Bộ nhớ chia bằng `GPU_UTIL`
(0.55) và `KIE_GPU_UTIL` (0.3), thực đo ~80% card H200; `kie` chỉ khởi động sau
khi `chandra` healthy. Weights Qwen nằm trong docker volume `qwen35_4b`, nạp một
lần theo lệnh ở đầu file compose.
Một file `docker-compose.yaml` dựng đủ 4 container trên **một host, một card GPU**:
| Service | Container | Việc | Cổng |
|---|---|---|---|
| `chandra` | `chandra-ocr` | DLA/OCR từng trang (vLLM, image `IMAGE_TAG`) | `PORT` (9222) |
| `kie` | `qwen-kie` | Bóc trường Qwen3.5-4B (vLLM, image `KIE_IMAGE`) | `KIE_PORT` (9224) |
| `api` | `idp-api` | HTTP API (image build từ `Dockerfile`) | `IDP_PORT` (8001) |
| `worker` | `idp-worker` | Hàng đợi trang, KIE, finalize, callback, tải PDF từ HDFS | — |
```bash
echo GPU_UUID=GPU-xxxx > .env
cp .env.example .env # sửa đường dẫn weights, GPU, HDFS, khóa; chmod 600 .env
docker compose up -d --build
docker compose ps # chandra/kie healthy thì worker mới khởi động
docker compose logs -f worker
```
`.env` vừa cấp biến cho compose vừa là env của `api`/`worker`, nên các biến `IDP_*`
giữ nguyên như khi chạy `run-api.sh`/`run-worker.sh`. Những biến riêng của compose:
- `GPU_DEVICE`: index hoặc UUID card (như `docker run --gpus device=0`). Hai model
chia card bằng `GPU_UTIL` (0.55) và `KIE_GPU_UTIL` (0.3), thực đo ~80% card H200;
`kie` chỉ khởi động sau khi `chandra` healthy để hai vLLM không đo bộ nhớ cùng lúc.
- `CHANDRA_MODEL_DIR`, `KIE_MODEL_DIR`: thư mục weights trên host, mount chỉ đọc vào
`/model`. Weights Qwen là snapshot HF đã bung, không để symlink trỏ ra ngoài thư mục.
- `HDFS_NATIVE_DIR`: thư mục `native-hdfs` của gói portable (Java, Hadoop client,
`conf/krb5.conf`, keytab), mount chỉ đọc vào worker ở `/opt/native-hdfs`; image
đã có `kinit`. Chỉ cần khi `IDP_STORAGE=hdfs_cli`.
- `IDP_DATA_DIR`: mount cùng đường dẫn vào `api` và `worker` để hai bên dùng chung
SQLite; phải là đĩa local.
`api`/`worker` chạy `network_mode: host` nên gọi model qua `127.0.0.1:9222`/`9224`
và phân giải namenode/KDC theo `/etc/hosts` của host, y như khi chạy bằng script.
Kiểm tra HDFS trong container: `docker compose exec worker hdfs dfs -ls /`.
Máy đích không có Internet thì build rồi mang image sang:
```bash
docker compose build api
docker save idp-parsing-service:0.2.0 vllm/vllm-openai:v0.23.0 | gzip > idp-images.tar.gz
# trên máy đích, cùng image chandra-dla-api:
docker load -i idp-images.tar.gz
docker compose up -d
curl http://localhost:9222/v1/models; curl http://localhost:9224/v1/models
```
## Cấu hình
@@ -205,7 +198,7 @@ curl http://localhost:9222/v1/models; curl http://localhost:9224/v1/models
| `IDP_KIE_MAX_TOKENS` | `2048` | Giới hạn số token đầu ra của mỗi request KIE; độc lập với `--max-model-len` của vLLM |
| `IDP_MAX_KIE_ATTEMPTS` | `3` | Số lần thử bóc trường mỗi tài liệu |
| `IDP_RUN_WORKER` | `0` | `1` chạy worker ngay trong process API |
| `IDP_OCR_WORKERS` | `64` | Giới hạn tổng request tới GPU, lưu bằng SQLite |
| `IDP_OCR_WORKERS` | `64` | Giới hạn tổng request tới GPU, lưu bằng SQLite. Mỗi luồng giữ khoá ghi SQLite khi nhận việc; log nhiều `database is locked` thì hạ xuống 16–32 |
| `IDP_CALLBACK_WORKERS` | `4` | Số luồng gửi callback độc lập; callback lỗi không giữ luồng chuẩn bị/finalize job |
| `IDP_OCR_TIMEOUT` | `180` | Timeout mỗi request OCR, giây |
| `IDP_LEASE_SECONDS` | `1800` | Lease task; phải lớn hơn timeout và thời gian chuẩn bị/finalize |
@@ -221,7 +214,7 @@ native RPC, Kerberos và HA nên chọn `IDP_STORAGE=hdfs_cli`, đặt
`IDP_HDFS_NATIVE_URI=hdfs://vptwd` và
`HDFS_PRINCIPAL=searchengine@DATALAKE.DCS.VN`. Gói portable đã kèm Java, Hadoop
client, `core-site.xml`, `hdfs-site.xml`, `krb5.conf` và keytab;
`run-worker.sh` tự thêm client vào `PATH` và đăng nhập bằng keytab. Giữ quyền
container `worker` tự thêm client vào `PATH` và trỏ `KRB5_CONFIG`/`HDFS_KEYTAB` vào đó. Giữ quyền
đọc keytab ở mức `600`. API và worker phải có quyền
truy cập cùng SQLite; chỉ đặt SQLite trên **một host với đĩa bền vững**, không đặt
trên HDFS/NFS. Nếu cần chạy nhiều host, thay job store bằng PostgreSQL trước.
@@ -229,8 +222,8 @@ trên HDFS/NFS. Nếu cần chạy nhiều host, thay job store bằng PostgreSQ
Kiểm tra HDFS native ngay trên máy triển khai trước khi chạy worker:
```bash
./test-hdfs.sh /
./test-hdfs.sh /data/bronze/incoming
docker compose exec worker hdfs dfs -ls /
docker compose exec worker hdfs dfs -ls /data/bronze/incoming
```
Máy triển khai phải phân giải được hostname trong file cấu hình, kết nối được

View File

@@ -1,32 +1,57 @@
# Hai model của service trên cùng một card GPU:
# chandra (DLA/OCR từng trang) → cổng ${PORT:-9222}, served name "chandra"
# kie (bóc trường, Qwen3.5-4B) → cổng ${KIE_PORT:-9224}, served name "qwen3.5-4b"
# Toàn bộ service trên một host, một card GPU: `cp .env.example .env`, sửa .env, rồi
# docker compose up -d --build
#
# .env cần GPU_UUID (nvidia-smi -L). Mặc định hai model dùng ~80% card (GPU_UTIL 0.55 +
# KIE_GPU_UTIL 0.3); card có tiến trình khác thì hạ tổng xuống dưới phần còn trống.
# kie chỉ khởi động sau khi chandra healthy để hai vLLM không đo bộ nhớ GPU cùng lúc.
# chandra DLA/OCR từng trang, vLLM cổng ${PORT:-9222}, served name "chandra"
# kie bóc trường Qwen3.5-4B, vLLM cổng ${KIE_PORT:-9224}, served name "qwen3.5-4b"
# api HTTP API cổng ${IDP_PORT:-8001}
# worker hàng đợi trang, KIE, finalize, callback; tải PDF từ HDFS
#
# Weights chandra đóng sẵn trong image IMAGE_TAG. Weights Qwen nằm trong volume
# ${KIE_VOLUME:-qwen35_4b}, nạp một lần từ snapshot HF (tar -h để theo symlink):
# docker volume create qwen35_4b
# tar -C <snapshot Qwen/Qwen3.5-4B> -chf - . | docker run --rm -i -v qwen35_4b:/model \
# --entrypoint tar vllm/vllm-openai:v0.23.0 -C /model -xf -
# api/worker chạy network_mode host nên .env giữ nguyên như khi chạy run-api.sh/run-worker.sh:
# IDP_OCR_URL=http://127.0.0.1:9222/..., IDP_KIE_URL mặc định http://localhost:9224/...,
# namenode/KDC phân giải theo /etc/hosts của host.
# kie chỉ khởi động sau khi chandra healthy để hai vLLM không đo bộ nhớ GPU cùng lúc;
# worker đợi cả hai model healthy mới nhận trang.
x-gpu: &gpu
resources:
reservations:
devices:
- driver: nvidia
device_ids: ["${GPU_DEVICE:-0}"] # như `docker run --gpus device=0`
capabilities: [gpu]
x-vllm-env: &vllm-env
HF_HUB_OFFLINE: "1"
TRANSFORMERS_OFFLINE: "1"
VLLM_LOGGING_LEVEL: "${VLLM_LOG_LEVEL:-INFO}"
x-health: &health
test: ["CMD-SHELL", "python3 -c \"import urllib.request; urllib.request.urlopen('http://localhost:8000/health')\""]
interval: 30s
timeout: 5s
retries: 20
start_period: 600s
x-app: &app
image: ${APP_IMAGE:-idp-parsing-service:0.2.0}
build: .
restart: unless-stopped
network_mode: host
env_file: .env
services:
chandra:
image: ${IMAGE_TAG:-chandra-dla-api:v2-20260906-024126}
container_name: chandra-ocr
restart: unless-stopped
gpus: &gpu
- driver: cdi
device_ids: ["nvidia.com/gpu=${GPU_UUID:?dat GPU_UUID trong .env}"]
deploy: *gpu
ipc: host
shm_size: "16gb"
ports:
- "${PORT:-9222}:8000"
environment: &env
HF_HUB_OFFLINE: "1"
TRANSFORMERS_OFFLINE: "1"
VLLM_LOGGING_LEVEL: "${VLLM_LOG_LEVEL:-INFO}"
environment: *vllm-env
volumes:
- ${CHANDRA_MODEL_DIR:?dat CHANDRA_MODEL_DIR trong .env}:/model:ro
command:
- --model=/model
- --served-model-name=chandra
@@ -41,12 +66,7 @@ services:
- --seed=42
# PHẢI khớp lúc train (IMAGE_MAX_TOKEN_NUM=1792) — đổi là bbox lệch.
- --mm-processor-kwargs={"min_pixels":65536,"max_pixels":1835008}
healthcheck: &health
test: ["CMD-SHELL", "python3 -c \"import urllib.request; urllib.request.urlopen('http://localhost:8000/health')\""]
interval: 30s
timeout: 5s
retries: 20
start_period: 600s
healthcheck: *health
kie:
image: ${KIE_IMAGE:-vllm/vllm-openai:v0.23.0}
@@ -55,14 +75,14 @@ services:
depends_on:
chandra:
condition: service_healthy
gpus: *gpu
deploy: *gpu
ipc: host
shm_size: "8gb"
ports:
- "${KIE_PORT:-9224}:8000"
environment: *env
environment: *vllm-env
volumes:
- kie_model:/model:ro
- ${KIE_MODEL_DIR:?dat KIE_MODEL_DIR trong .env}:/model:ro
command:
- --model=/model
- --served-model-name=qwen3.5-4b
@@ -80,7 +100,31 @@ services:
- --mm-processor-kwargs={"min_pixels":65536,"max_pixels":1835008}
healthcheck: *health
api:
<<: *app
container_name: idp-api
environment:
IDP_DATA_DIR: ${IDP_DATA_DIR:-/data/idp-runtime}
volumes:
kie_model:
external: true
name: ${KIE_VOLUME:-qwen35_4b}
# Cùng đường dẫn trong và ngoài container; SQLite phải nằm trên đĩa local, không NFS.
- ${IDP_DATA_DIR:-/data/idp-runtime}:${IDP_DATA_DIR:-/data/idp-runtime}
command: ["uvicorn", "idp_service.api:app", "--host=${IDP_HOST:-0.0.0.0}", "--port=${IDP_PORT:-8001}"]
worker:
<<: *app
container_name: idp-worker
depends_on:
chandra:
condition: service_healthy
kie:
condition: service_healthy
environment:
IDP_DATA_DIR: ${IDP_DATA_DIR:-/data/idp-runtime}
# Như run-worker.sh: client HDFS native (Java, Hadoop, conf, keytab) lấy từ gói portable.
PATH: /opt/native-hdfs/bin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
KRB5_CONFIG: /opt/native-hdfs/conf/krb5.conf
HDFS_KEYTAB: /opt/native-hdfs/conf/searchengine.headless.keytab
volumes:
- ${IDP_DATA_DIR:-/data/idp-runtime}:${IDP_DATA_DIR:-/data/idp-runtime}
- ${HDFS_NATIVE_DIR:-./native-hdfs}:/opt/native-hdfs:ro
command: ["python", "-m", "idp_service.worker"]