| #!/usr/bin/env bash |
| set -euo pipefail |
|
|
| ROOT_DIR="${ROOT_DIR:-/home/sf895/SignVerse-2M}" |
| SUBMIT_SCRIPT="${SUBMIT_SCRIPT:-$ROOT_DIR/slurm/submit_upload_parallel_slurm.sh}" |
| PARALLEL_SHARDS="${PARALLEL_SHARDS:-8}" |
| ARRAY_PARALLEL="${ARRAY_PARALLEL:-8}" |
| PARTITION="${PARTITION:-main-redhat}" |
| START_STAGGER_MIN="${START_STAGGER_MIN:-1}" |
| START_STAGGER_MAX="${START_STAGGER_MAX:-3}" |
| TARGET_BYTES="${TARGET_BYTES:-10737418240}" |
| TARGET_FOLDERS="${TARGET_FOLDERS:-40}" |
| SLEEP_BETWEEN_WAVES="${SLEEP_BETWEEN_WAVES:-15}" |
| PROGRESS_JSON="${PROGRESS_JSON:-/home/sf895/SignVerse-2M-runtime/archive_upload_progress.json}" |
| STATE_ROOT="${STATE_ROOT:-/home/sf895/SignVerse-2M-runtime}" |
| HOME_DATASET_DIR="${HOME_DATASET_DIR:-/home/sf895/SignVerse-2M-runtime/dataset}" |
| SCRATCH_DATASET_DIR="${SCRATCH_DATASET_DIR:-/scratch/$USER/SignVerse-2M-runtime/dataset}" |
| HOME_RAW_VIDEO_DIR="${HOME_RAW_VIDEO_DIR:-/home/sf895/SignVerse-2M-runtime/raw_video}" |
| SCRATCH_RAW_VIDEO_DIR="${SCRATCH_RAW_VIDEO_DIR:-/scratch/$USER/SignVerse-2M-runtime/raw_video}" |
|
|
| remaining_stats() { |
| python3 - <<PY2 |
| import json |
| from pathlib import Path |
| import sys |
| sys.path.insert(0, '/cache/home/sf895/SignVerse-2M') |
| from utils.dataset_pool import list_unuploaded_folder_paths |
| progress = json.loads(Path(r"$PROGRESS_JSON").read_text()) |
| folders = list_unuploaded_folder_paths(Path(r"$HOME_DATASET_DIR"), Path(r"$SCRATCH_DATASET_DIR"), progress.get('uploaded_folders', {})) |
| size_bytes = 0 |
| for item in folders: |
| path = item[1] if isinstance(item, tuple) else item |
| try: |
| for child in path.rglob('*'): |
| if child.is_file(): |
| size_bytes += child.stat().st_size |
| except FileNotFoundError: |
| continue |
| print(f"remaining={len(folders)}") |
| print(f"bytes={size_bytes}") |
| PY2 |
| } |
|
|
| upstream_busy() { |
| local download_claims process_claims raw_count |
| download_claims=$(find "$STATE_ROOT/slurm/state/download_claims" -maxdepth 1 -type f -name '*.claim' 2>/dev/null | wc -l) |
| process_claims=$(find "$STATE_ROOT/slurm/state/claims" -maxdepth 1 -type f -name '*.claim' 2>/dev/null | wc -l) |
| raw_count=$(find "$HOME_RAW_VIDEO_DIR" "$SCRATCH_RAW_VIDEO_DIR" -maxdepth 1 -type f \( -name '*.mp4' -o -name '*.mkv' -o -name '*.webm' -o -name '*.mov' \) 2>/dev/null | wc -l) |
| if [[ "$download_claims" -gt 0 || "$process_claims" -gt 0 || "$raw_count" -gt 0 ]]; then |
| return 0 |
| fi |
| return 1 |
| } |
|
|
| while true; do |
| eval "$(remaining_stats)" |
| echo "[drain] remaining_unuploaded=$remaining remaining_bytes=$bytes" |
| if [[ "$remaining" == "0" ]]; then |
| echo "[drain] backlog cleared" |
| break |
| fi |
|
|
| shards="$PARALLEL_SHARDS" |
| array_parallel="$ARRAY_PARALLEL" |
| extra_args=() |
| |
| |
| needed_shards=$(( remaining / TARGET_FOLDERS )) |
| if (( needed_shards < 1 )); then |
| needed_shards=1 |
| fi |
| if (( needed_shards < shards )); then |
| shards="$needed_shards" |
| fi |
| if (( shards < 1 )); then |
| shards=1 |
| fi |
|
|
| if (( remaining < TARGET_FOLDERS && bytes < TARGET_BYTES )); then |
| echo "[drain] below upload thresholds; skip upload wave" |
| break |
| fi |
|
|
| if (( shards == 1 )); then |
| array_parallel="" |
| echo "[drain] single-shard strict mode" |
| else |
| if (( array_parallel > shards )); then |
| array_parallel="$shards" |
| fi |
| echo "[drain] adaptive shard count=$shards" |
| fi |
|
|
| submit_cmd=(bash "$SUBMIT_SCRIPT" |
| --parallel-shards "$shards" |
| --partition "$PARTITION" |
| --start-stagger-min "$START_STAGGER_MIN" |
| --start-stagger-max "$START_STAGGER_MAX" |
| --target-bytes "$TARGET_BYTES" |
| --target-folders "$TARGET_FOLDERS" |
| ) |
| if [[ -n "$array_parallel" ]]; then |
| submit_cmd+=(--array-parallel "$array_parallel") |
| fi |
| if (( ${#extra_args[@]} > 0 )); then |
| submit_cmd+=("${extra_args[@]}") |
| fi |
|
|
| submit_out="$(${submit_cmd[@]})" |
| echo "$submit_out" |
| job_id="$(awk '/Submitted batch job/{print $4}' <<< "$submit_out" | tail -n1)" |
| if [[ -z "$job_id" ]]; then |
| echo "[drain] failed to parse submitted job id" >&2 |
| exit 1 |
| fi |
|
|
| while [[ -n "$(squeue -h -j "$job_id" 2>/dev/null)" ]]; do |
| sleep "$SLEEP_BETWEEN_WAVES" |
| done |
| sleep 2 |
| done |
|
|