#!/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 - </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=() # Keep each normal shard at or above TARGET_FOLDERS when possible. # For example, 311 folders should use 7 shards, not 8, so each shard still has >=40 folders. 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