
Amazon S3 (Simple Storage Service) lưu file dưới dạng object, mỗi object được định danh bằng một key duy nhất thay vì một đường dẫn trong hệ thống file truyền thống. Thiết kế này mở rộng tốt cho lưu trữ, nhưng có thể gây trở ngại khi dùng S3 như một hệ thống file thông thường, đặc biệt trong các workflow bioinformatics. Khi Nextflow dùng S3 làm backend input/output, có những đánh đổi cần cân nhắc, nhất là với số lượng lớn file nhỏ. Trong những trường hợp đó, Nextflow có thể dành đáng kể thời gian cho việc tải lên và tải xuống qua AWS CLI v2, ảnh hưởng đến hiệu năng tổng thể của workflow. Bài này bắt đầu từ phía input.
AWS CLI là công cụ dòng lệnh để làm việc với các dịch vụ AWS. Trong Nextflow, nó có thể dùng để tải input xuống và tải output lên. Để làm theo hướng dẫn, hãy clone repository. Repository dùng pixi và Docker để thiết lập môi trường nhanh.
git clone https://github.com/nttg8100/nextflow-cost-optimization.gitcd nextflow-cost-optimizationpixi shellwhich awsMakefile dưới đây khởi động dịch vụ Docker, tải các file lên, và tạo một tar archive gồm 10.000 file nhỏ để dùng cho benchmark về sau.
aws-config: start-minio export AWS_ACCESS_KEY_ID="minioadmin"; \ export AWS_SECRET_ACCESS_KEY="minioadmin"; \ export AWS_DEFAULT_REGION="us-east-1"; \ export AWS_ENDPOINT_URL="http://localhost:9000" ; \ sleep 10 && aws s3 mb s3://io-benchmark --endpoint-url http://localhost:9000
results/tarball.tar: @mkdir -p results/tarball @count=10000; size=1M; index=1; \ for k in $$(seq $$count); do \ dd if=/dev/zero of=results/tarball/$${size}-$${index}-$$k.data bs=1 count=0 seek=$$size; \ done tar -cvf results/tarball.tar -C results/tarball .
upload-tar: aws s3 cp results/tarball.tar s3://io-benchmark/ --endpoint-url http://localhost:9000
upload-10k-files: aws s3 cp results/tarball s3://io-benchmark/tarball --endpoint-url http://localhost:9000 --recursiveĐến đây bạn có thể làm việc với S3 object storage. Lệnh này khởi chạy MinIO, một dịch vụ tương thích S3, giúp giảm lỗi phát sinh từ khác biệt giữa máy cục bộ và bucket từ xa. Nó tạo bucket tên io-benchmark và đồng thời sinh ra nhiều file nhỏ dùng về sau để minh hoạ vấn đề.
make aws-configĐể kiểm tra dịch vụ S3:
# export envexport AWS_ACCESS_KEY_ID="minioadmin"export AWS_SECRET_ACCESS_KEY="minioadmin"export AWS_DEFAULT_REGION="us-east-1"export AWS_ENDPOINT_URL="http://localhost:9000"# configure threadsaws configure set default.s3.max_concurrent_requests 8# test bucketaws s3 ls --endpoint-url http://localhost:9000# 1026-01-27 10:29:58 io-benchmarkMakefile ở trên tạo một thư mục gồm 10.000 file, mỗi file 1 MB, cùng một tar archive của chính các file đó. Nextflow có thể chậm với input vì hai lý do chính:
Makefile cũng tạo một file tar của 10.000 file này. Benchmark dưới đây cho thấy vì sao điều này quan trọng và nó có thể giúp workflow dễ hơn như thế nào.
Trước khi liên quan đến Nextflow, hãy đo thời gian thao tác này chỉ với AWS CLI v2.
Chạy lệnh sau để tải xuống:
make upload-10k-filesfor i in {1..3}; do /usr/bin/time -f "%e" aws s3 cp s3://io-benchmark/tarball ./tarball --endpoint-url http://localhost:9000 --recursive 2>&1 | tail -n 1; rm -rf ./tarball; donestderr cho thấy việc tải các file mất khoảng 110 giây:
110.62109.62110.59Chạy thao tác tương tự trên một file tar duy nhất nhanh hơn nhiều:
make upload-tarfor i in {1..3}; do /usr/bin/time -f "%e" aws s3 cp s3://io-benchmark/tarball.tar . --endpoint-url http://localhost:9000 2>&1 | tail -n 1; rm -rf ./tarball.tar; donestderr cho thấy mất chưa đến 30 giây để tải file tar:
27.5026.9625.35Tuy nhiên, vẫn cần các file riêng lẻ bên trong. Có thể dùng pipe (|) để vừa tải vừa giải nén:
for i in {1..3}; do /usr/bin/time -f "%e" bash -c 'mkdir -p tarball && aws s3 cp s3://io-benchmark/tarball.tar - --endpoint-url http://localhost:9000 | tar -xvf - -C tarball' 2>&1 | tail -n 1; rm -rf ./tarball; donestderr cho thấy mất chưa đến 35 giây để tải và giải nén, tạo ra toàn bộ file nhỏ:
33.4331.8033.20Ngoài việc tải từng file hoặc file tar bằng lệnh aws, một hệ thống dựa trên FUSE là cách tiếp cận thay thế.
Cách này hữu ích cho những ứng dụng làm việc với một file lớn, hoặc một thư mục chứa nhiều file nhỏ, nhưng không nạp toàn bộ. Ví dụ, khi annotate biến thể cho dữ liệu whole-exome, cơ sở dữ liệu annotation được xây cho toàn bộ hệ gen; một FUSE mount cho phép công cụ chỉ đọc các vùng cần cho những biến thể được annotate. Các engine phân tán dùng hệ thống dựa trên FUSE cũng có thể hưởng lợi từ việc nạp dữ liệu theo nhu cầu.
Makefile dưới đây cài mount-s3 qua pixi. Nó mount toàn bộ bucket tại /mnt/vep_cache. Điểm mount được đặt tên theo vep vì nó được dùng ở ví dụ annotate VEP về sau.
mount-s3-vep-cache: ${HOME}/.pixi/bin/pixi mkdir -p ./mnt/vep_cache mkdir -p ./mnt/tmp ${HOME}/.pixi/bin/pixi run -e mount mount-s3 --endpoint-url http://localhost:9000 --region us-east-1 --force-path-style io-benchmark ./mnt/vep_cache --read-only --cache ./mnt/tmp --max-threads 8Chạy lệnh này để mount bucket S3 từ xa:
make mount-s3-vep-cache# checkdf# Filesystem 1K-blocks Used Available Use% Mounted on# io-benchmark 1099511627776 0 1099511627776 0% /scratch/data/nextflow-cost-optimization/mnt/vep_cacheKhi đã mount, có thể copy 10.000 file về cục bộ:
for i in {1..3}; do /usr/bin/time -f "%e" bash -c 'cp -r ./mnt/vep_cache/tarball tarball ' 2>&1 | tail -n 1; rm -rf ./tarball; donestderr cho thấy mất chưa đến 45 giây:
44.9446.0645.46Cũng có thể đọc một file tar duy nhất qua mount:
for i in {1..3}; do /usr/bin/time -f "%e" bash -c 'mkdir -p tarball && cp ./mnt/vep_cache/tarball.tar tarball' 2>&1 | tail -n 1; rm -rf ./tarball; donestderr cho thấy mất chưa đến 20 giây:
15.6015.3715.74Kết hợp FUSE với stream giải nén cũng hoạt động:
for i in {1..3}; do /usr/bin/time -f "%e" bash -c 'mkdir -p tarball && cat ./mnt/vep_cache/tarball.tar|tar -xvf - -C tarball' 2>&1 | tail -n 1; rm -rf ./tarball; donestderr cho thấy mất khoảng 20 giây:
19.6821.8620.31Bảng dưới đây tóm tắt các cách tải 10.000 file nhỏ từ S3, với hiệu năng quan sát được và trường hợp sử dụng điển hình:
| Method | Download Time (s) | Pros | Cons | When to Use |
|---|---|---|---|---|
| AWS CLI (recursive, 10k files) | ~110 | Simple, no extra setup | Very slow for many small files | Rarely; only for small numbers of files |
| AWS CLI (tarball + untar via pipe) | ~33 | Fast, single download + extraction | Needs tar/untar logic | When workflow can handle tar extraction |
| FUSE (cp -r 10k files) | ~45 | Transparent, works like local FS | Needs FUSE setup, not always fastest | When random access or partial reads are needed |
| FUSE (cat tarball | tar -xvf -) | ~20 | Fast, combines FUSE and streaming untar | Needs FUSE setup, tar logic | For large archives with extraction |
Khuyến nghị:
Workflow dưới đây nhận input từ S3, tải về để dùng trong process, và đếm số file bên trong.
include { COUNT_FILES } from './modules/count_files_tar.nf'include { COUNT_FILES_TAR } from './modules/count_files_tar.nf'
workflow { main: // benchmark files input // normal files if (params.benchmark_input){ ch_files = COUNT_FILES(Channel.fromPath(params.inputs).collect()) }
// tarball and untar if (params.benchmark_input_tar){ ch_files = COUNT_FILES_TAR(Channel.fromPath(params.inputs).collect()) }}Nó nhận danh sách file trực tiếp từ S3.
process COUNT_FILES { cpus 2
input: path(file_path)
script: """ ls -lah tarball/**.data|wc -l > num_files.txt """}Nó tải file tar trước, rồi giải nén bên trong process.
Việc tải file và dùng pipe để giải nén nhanh được áp dụng về sau.
process COUNT_FILES_ { cpus 2
input: path(file_path)
script: """ tar -xf ${file_path} ls -lah tarball/**.data|wc -l > num_files.txt """}Các phần còn lại gồm những cấu hình Nextflow:
nextflow.config: cấu hình chuẩn để chạy với Docker, Singularity và các nền tảng khácnextflow_s3.config: thông tin xác thực S3 cho MinIO storagenextflow_tar.config: cấu hình chạy với file cục bộ về sauĐây là những thiết lập chuẩn nên không giải thích chi tiết ở đây. Cấu trúc được trình bày dưới đây.
├── benchmark_computing_resource.nf├── benchmark_input.nf├── inputs│ ├── 1_samplesheet.csv│ └── full_samplsheet.csv├── LICENSE├── Makefile├── modules│ ├── count_files.nf│ ├── count_files_tar.nf│ ├── fastp.nf│ ├── fastqc.nf│ └── vep.nf├── nextflow.config├── nextflow_s3.config├── nextflow_tar.config├── pixi.lock├── pixi.toml├── README.mdMakefile chạy workflow với các cấu hình khác nhau. Với mục đích proof of concept, chạy một lần là đủ.
test-input-standard: ${HOME}/.pixi/bin/pixi ${HOME}/.pixi/bin/pixi run -e standard nextflow run benchmark_input.nf \ -c nextflow_s3.config \ --benchmark_input \ --inputs="s3://io-benchmark/tarball"
test-input-tar: ${HOME}/.pixi/bin/pixi ${HOME}/.pixi/bin/pixi run -e standard nextflow run benchmark_input.nf \ -c nextflow_s3.config \ --benchmark_input_tar \ --inputs="s3://io-benchmark/tmp.txt"time make test-input-standard# 42,34s user 7,04s system 77% cpu 1:03,97 totaltime make test-input-tar# 11,80s user 3,42s system 66% cpu 22,965 totalChúng tôi đã cho thấy có thể dùng một file tar lớn làm input và giải nén bên trong process. Điều đó đòi hỏi phải thay đổi module. Vậy có cấu hình nào tránh được việc đó không? Tiếp nối cách dùng pipe ở trên, cấu hình dưới đây giữ nguyên module trong khi các file được nạp bằng beforeScript.
process{ withName:COUNT_FILES{ beforeScript = "mkdir -p tarball && aws s3 cp s3://io-benchmark/tarball.tar - --endpoint-url http://localhost:9000 | tar -xvf - -C tarball" }}Để chạy workflow, có thể dùng một file làm placeholder cho Nextflow nhận input, và thêm cấu hình nextflow_tar.config:
test-input-tar-pipe: ${HOME}/.pixi/bin/pixi ${HOME}/.pixi/bin/pixi run -e standard nextflow run benchmark_input.nf \ -c nextflow_s3.config -c nextflow_tar.config \ --benchmark_input \ --inputs="s3://io-benchmark/tmp.txt"Nhớ tạo file tạm và tải lên bucket:
touch tmp.txtaws s3 cp tmp.txt s3://io-benchmark --endpoint-url http://localhost:9000Chạy thiết lập mới:
time make test-input-tar-pipe# 11,33s user 2,85s system 82% cpu 17,166 totalMô hình tương tự cũng xuất hiện trong workload thực tế. Bài viết trên blog AWS HPC mô tả cách Genomics England xử lý một vấn đề tương tự khi dùng VEP để annotate biến thể. Cơ sở dữ liệu họ dùng chứa nhiều file nhỏ, và việc tải xuống mất nhiều thời gian hơn cả bước annotation, vốn chỉ chạy trong vài phút.
Pseudo code:
process VEP {
input: path(vcf) val(vep_cache)
script: """ vep \ --input_file $vcf \ --fasta $params.human_reference_fasta \ --dir_cache $vep_cache """
}Workflow chỉ dùng module VEP. Để tái lập nhanh, mọi tham số đều được hard code.
include { ENSEMBLVEP_VEP } from './modules/vep.nf'
workflow { main: ENSEMBLVEP_VEP( Channel.of(tuple([id: 'HCC1395N'], file("inputs/vep_test_data.vcf.gz"), file("inputs/vep_test_data.vcf.gz.tbi"))), // tuple val(meta), path(vcf), path(custom_extra_files) Channel.value("GRCh38"), // val genome Channel.value("homo_sapiens"), // val species Channel.value(114), // val cache_version Channel.fromPath("s3://io-benchmark/vep_cache"), // path cache Channel.of(tuple([:], [])), // tuple val(meta2), path(fasta) Channel.of([]) // path extra_files )}Module VEP được lấy từ nf-core.
process ENSEMBLVEP_VEP { tag "${meta.id}" label 'process_medium'
conda "${moduleDir}/environment.yml" container "${workflow.containerEngine == 'singularity' && !task.ext.singularity_pull_docker_container ? 'https://community-cr-prod.seqera.io/docker/registry/v2/blobs/sha256/4b/4b5a8c173dc9beaa93effec76b99687fc926b1bd7be47df5d6ce19d7d6b4d6b7/data' : 'community.wave.seqera.io/library/ensembl-vep:115.2--90ec797ecb088e9a'}"
input: tuple val(meta), path(vcf), path(custom_extra_files) val genome val species val cache_version path cache tuple val(meta2), path(fasta) path extra_files
output: tuple val(meta), path("*.vcf.gz"), emit: vcf, optional: true tuple val(meta), path("*.vcf.gz.tbi"), emit: tbi, optional: true tuple val(meta), path("*.tab.gz"), emit: tab, optional: true tuple val(meta), path("*.json.gz"), emit: json, optional: true path "*.html", emit: report, optional: true path "versions.yml", emit: versions
when: task.ext.when == null || task.ext.when
script: def args = task.ext.args ?: '' def args2 = task.ext.args2 ?: '' def file_extension = args.contains("--vcf") ? 'vcf' : args.contains("--json") ? 'json' : args.contains("--tab") ? 'tab' : 'vcf' def compress_cmd = args.contains("--compress_output") ? '' : '--compress_output bgzip' def prefix = task.ext.prefix ?: "${meta.id}" def dir_cache = cache ? "\${PWD}/${cache}" : "/.vep" def reference = fasta ? "--fasta ${fasta}" : "" def create_index = file_extension == "vcf" ? "tabix ${args2} ${prefix}.${file_extension}.gz" : "" """ vep \\ -i ${vcf} \\ -o ${prefix}.${file_extension}.gz \\ ${args} \\ ${compress_cmd} \\ ${reference} \\ --assembly ${genome} \\ --species ${species} \\ --cache \\ --cache_version ${cache_version} \\ --dir_cache ${dir_cache} \\ --fork ${task.cpus}
${create_index}
cat <<-END_VERSIONS > versions.yml "${task.process}": ensemblvep: \$( echo \$(vep --help 2>&1) | sed 's/^.*Versions:.*ensembl-vep : //;s/ .*\$//') tabix: \$(echo \$(tabix -h 2>&1) | sed 's/^.*Version: //; s/ .*\$//') END_VERSIONS """
stub: def prefix = task.ext.prefix ?: "${meta.id}" def file_extension = args.contains("--vcf") ? 'vcf' : args.contains("--json") ? 'json' : args.contains("--tab") ? 'tab' : 'vcf' def create_index = file_extension == "vcf" ? "touch ${prefix}.${file_extension}.gz.tbi" : "" """ echo "" | gzip > ${prefix}.${file_extension}.gz ${create_index} touch ${prefix}_summary.html
cat <<-END_VERSIONS > versions.yml "${task.process}": ensemblvep: \$( echo \$(vep --help 2>&1) | sed 's/^.*Versions:.*ensembl-vep : //;s/ .*\$//') tabix: \$(echo \$(tabix -h 2>&1) | sed 's/^.*Version: //; s/ .*\$//') END_VERSIONS """}Giải pháp của họ được trình bày dưới đây.
File VCF được chuẩn bị trong thư mục inputs, còn cơ sở dữ liệu cache VEP có thể được chuẩn bị qua Makefile dưới đây.
vep/114_GRCh38: ${HOME}/.pixi/bin/pixi mkdir -p vep touch vep/vep_cache cd vep && ${HOME}/.pixi/bin/pixi run -e core aws s3 --no-sign-request cp s3://annotation-cache/vep_cache/114_GRCh38 114_GRCh38 --recursive
vep/vep_cache.tar: vep/114_GRCh38 tar -cvf vep/vep_cache.tar -C vep/114_GRCh38 .
upload-vep-cache: vep/vep_cache.tar ${HOME}/.pixi/bin/pixi run -e core aws s3 cp vep/114_GRCh38 s3://io-benchmark/ --endpoint-url http://localhost:9000 --recursive ${HOME}/.pixi/bin/pixi run -e core aws s3 cp vep/vep_cache.tar s3://io-benchmark/ --endpoint-url http://localhost:9000
mount-s3-vep-cache: ${HOME}/.pixi/bin/pixi mkdir -p ./mnt/vep_cache mkdir -p ./mnt/tmp ${HOME}/.pixi/bin/pixi run -e mount mount-s3 --endpoint-url http://localhost:9000 --region us-east-1 --force-path-style io-benchmark ./mnt/vep_cache --read-only --cache ./mnt/tmp --max-threads 8Sau đó chạy:
# download and upload to local s3make upload-vep-cache
# mount to local folder from local s3 via mountpoint-s3make mount-s3-vep-cacheCác lệnh Makefile dưới đây benchmark nhanh các phương án:
test-vep-local: ${HOME}/.pixi/bin/pixi ${HOME}/.pixi/bin/pixi run -e core nextflow run benchmark_vep.nf -profile docker --vep_cache "./vep/114_GRCh38"
test-vep-mount-cache: ${HOME}/.pixi/bin/pixi ${HOME}/.pixi/bin/pixi run -e core nextflow run benchmark_vep.nf \ -c nextflow_s3.config -c nextflow_vep_mount.config --vep_cache "./mnt/vep_cache/114_GRCh38" -profile docker
test-vep-direct-s3: ${HOME}/.pixi/bin/pixi ${HOME}/.pixi/bin/pixi run -e core nextflow run benchmark_vep.nf \ -c nextflow_s3.config --vep_cache "s3://io-benchmark/114_GRCh38" -profile docker
test-vep-tar-cache: ${HOME}/.pixi/bin/pixi ${HOME}/.pixi/bin/pixi run -e core nextflow run benchmark_vep.nf \ -c nextflow_s3.config -c nextflow_vep_tar.config --vep_cache "s3://io-benchmark/vep_cache" -profile docker
test-vep-mount-tar: ${HOME}/.pixi/bin/pixi ${HOME}/.pixi/bin/pixi run -e core nextflow run benchmark_vep.nf \ -c nextflow_s3.config -c nextflow_vep_mount_tar.config --vep_cache "s3://io-benchmark/vep_cache" -profile dockerSau đó chạy ví dụ cần kiểm tra:
make <test-vep-benchmark>Bảng dưới đây tóm tắt kết quả benchmark của các cách truy cập cơ sở dữ liệu cache VEP trong quá trình phân tích:
| Method | Command | Time (min:sec) |
|---|---|---|
| Local cache | make test-vep-local |
2:26.20 |
| S3 mount (FUSE) | make test-vep-mount-cache |
2:31.08 |
| S3 mount tar (FUSE) | make test-vep-mount-tar |
5:57.18 |
| Direct S3 access | make test-vep-direct-s3 |
6:32.63 |
| S3 tar cache | make test-vep-tar-cache |
6:02.52 |
cp \$(readlink $cache) tmp -r trong module cho thấy mất 4:16.09 để đọc từ điểm mount và copy toàn bộ thư mục sang một thư mục tạm mới.Dù dữ liệu là hàng nghìn file nhỏ hay hàng terabyte dữ liệu tham chiếu, những mô hình này giúp thiết kế workflow dùng input hiệu quả hơn.



