#!/bin/bash
# Start SGLang server for a specific target context length.
set -e

TARGET_LEN="${1:-}"
if [[ -z "$TARGET_LEN" ]]; then
  echo "Usage: $0 <target_input_length_in_tokens>"
  exit 1
fi

cd /data/user1/yy
mkdir -p logs

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck source=/dev/null
source "${SCRIPT_DIR}/config.env"

export PATH="${VENV_SGLANG}/bin:$PATH"
export PYTHONUNBUFFERED=1
export SGLANG_LOG_LEVEL=info
export TMPDIR=/data/user1/yy/tmp
export CUDA_VISIBLE_DEVICES="${CUDA_VISIBLE_DEVICES}"

MAX_CONTEXT_LEN=$(( TARGET_LEN + CONTEXT_PAD ))

LOG="/data/user1/yy/logs/dsv4_h200_max_context_length_sglang_${TARGET_LEN}_$(date +%Y%m%d_%H%M%S).log"
PID_FILE="/data/user1/yy/dsv4_h200_max_context_length_sglang.pid"

rm -f "$PID_FILE"

echo "=== Starting SGLang server (TP=$TP, target_input_len=$TARGET_LEN, max_context_len=$MAX_CONTEXT_LEN) ==="
echo "Model: $MODEL_PATH"
echo "Port: $SGLANG_PORT"
echo "Log: $LOG"

nohup sglang serve \
    --trust-remote-code \
    --model-path "$MODEL_PATH" \
    --tp "$TP" \
    --moe-runner-backend marlin \
    --context-length "$MAX_CONTEXT_LEN" \
    --max-running-requests 1 \
    --mem-fraction-static 0.88 \
    --host 0.0.0.0 \
    --port "$SGLANG_PORT" \
    > "$LOG" 2>&1 &

PID=$!
echo $PID > "$PID_FILE"
echo "PID: $PID"
echo "Waiting for health..."

for i in $(seq 1 240); do
    if curl --fail --silent --show-error --max-time 5 "http://127.0.0.1:${SGLANG_PORT}/health" >/dev/null 2>&1; then
        echo "SGLang server is ready at http://127.0.0.1:${SGLANG_PORT}"
        echo "Log: $LOG"
        exit 0
    fi
    if ! kill -0 $PID 2>/dev/null; then
        echo "ERROR: SGLang server exited early"
        tail -200 "$LOG"
        exit 1
    fi
    echo "Waiting... ($i/240)"
    sleep 5
done

echo "ERROR: SGLang server not healthy after 240 retries"
tail -200 "$LOG"
exit 1
