From ced6c527bac72efe7537b846a6cec9c3bca8f6f4 Mon Sep 17 00:00:00 2001 From: msukkari Date: Fri, 21 Feb 2025 17:13:35 -0800 Subject: [PATCH] add cpu split logic and only wait for postgres if we're going to connec to it --- Dockerfile | 5 ++++- entrypoint.sh | 51 ++++++++++++++++++++++++++++++++++++------------ supervisord.conf | 5 ++--- 3 files changed, 45 insertions(+), 16 deletions(-) diff --git a/Dockerfile b/Dockerfile index ff5eff9d..707bd57a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -87,6 +87,9 @@ ENV DATABASE_URL="postgresql://postgres@localhost:5432/sourcebot" ENV REDIS_URL="redis://localhost:6379" ENV SRC_TENANT_ENFORCEMENT_MODE=strict +# @nocheckin: we likely want to have a better solution for the self host case +ENV BACKEND_CORES_FACTOR="0.5" + ARG SOURCEBOT_VERSION=unknown ENV SOURCEBOT_VERSION=$SOURCEBOT_VERSION RUN echo "Sourcebot Version: $SOURCEBOT_VERSION" @@ -112,7 +115,7 @@ ENV POSTHOG_PAPIK=$POSTHOG_PAPIK ENV STRIPE_PUBLISHABLE_KEY="" # Configure dependencies -RUN apk add --no-cache git ca-certificates bind-tools tini jansson wget supervisor uuidgen curl perl jq redis postgresql postgresql-contrib openssl +RUN apk add --no-cache git ca-certificates bind-tools tini jansson wget supervisor uuidgen curl perl jq redis postgresql postgresql-contrib openssl util-linux # Configure zoekt COPY vendor/zoekt/install-ctags-alpine.sh . diff --git a/entrypoint.sh b/entrypoint.sh index aa01ffb1..c281843a 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -181,25 +181,52 @@ echo "{\"version\": \"$SOURCEBOT_VERSION\", \"install_id\": \"$SOURCEBOT_INSTALL # Start the database and wait for it to be ready before starting any other service -su postgres -c "postgres -D $DB_DATA_DIR" & -until pg_isready -h localhost -p 5432 -U postgres; do - echo -e "\e[34m[Info] Waiting for the database to be ready...\e[0m" - sleep 1 -done +if [ "$DATABASE_URL" = "postgresql://postgres@localhost:5432/sourcebot" ]; then + su postgres -c "postgres -D $DB_DATA_DIR" & + until pg_isready -h localhost -p 5432 -U postgres; do + echo -e "\e[34m[Info] Waiting for the database to be ready...\e[0m" + sleep 1 + done -# Check if the database already exists, and create it if it dne -EXISTING_DB=$(psql -U postgres -tAc "SELECT 1 FROM pg_database WHERE datname = '$DB_NAME'") + # Check if the database already exists, and create it if it dne + EXISTING_DB=$(psql -U postgres -tAc "SELECT 1 FROM pg_database WHERE datname = '$DB_NAME'") -if [ "$EXISTING_DB" = "1" ]; then - echo "Database '$DB_NAME' already exists; skipping creation." -else - echo "Creating database '$DB_NAME'..." - psql -U postgres -c "CREATE DATABASE \"$DB_NAME\"" + if [ "$EXISTING_DB" = "1" ]; then + echo "Database '$DB_NAME' already exists; skipping creation." + else + echo "Creating database '$DB_NAME'..." + psql -U postgres -c "CREATE DATABASE \"$DB_NAME\"" + fi fi # Run a Database migration echo -e "\e[34m[Info] Running database migration...\e[0m" yarn workspace @sourcebot/db prisma:migrate:prod +# Get total CPU cores +TOTAL_CORES=$(nproc) +echo -e "\e[34m[Info] Total CPU cores available: $TOTAL_CORES\e[0m" + +# Default to 50/50 split if BACKEND_CORES_FACTOR not set +BACKEND_CORES_FACTOR=${BACKEND_CORES_FACTOR:-0.5} + +# Calculate number of cores for backend (rounded down) +BACKEND_CORES=$(printf "%.0f" $(echo "$TOTAL_CORES * $BACKEND_CORES_FACTOR" | bc)) + +# Ensure at least 1 core for each +if [ $BACKEND_CORES -lt 1 ]; then + BACKEND_CORES=1 +fi +if [ $BACKEND_CORES -ge $TOTAL_CORES ]; then + BACKEND_CORES=$(($TOTAL_CORES - 1)) +fi + +# Generate CPU lists +export BACKEND_CPU_LIST=$(seq -s, 0 $(($BACKEND_CORES - 1))) +export FRONTEND_CPU_LIST=$(seq -s, $BACKEND_CORES $(($TOTAL_CORES - 1))) + +echo -e "\e[34m[Info] Backend will use CPUs: $BACKEND_CPU_LIST\e[0m" +echo -e "\e[34m[Info] Frontend will use CPUs: $FRONTEND_CPU_LIST\e[0m" + # Run supervisord exec supervisord -c /etc/supervisor/conf.d/supervisord.conf \ No newline at end of file diff --git a/supervisord.conf b/supervisord.conf index ea746564..b2dcc53a 100644 --- a/supervisord.conf +++ b/supervisord.conf @@ -4,7 +4,7 @@ logfile=/dev/null logfile_maxbytes=0 [program:zoekt] -command=./prefix-output.sh zoekt-webserver -index %(ENV_DATA_CACHE_DIR)s/index -rpc +command=taskset -c %(ENV_BACKEND_CPU_LIST)s ./prefix-output.sh zoekt-webserver -index %(ENV_DATA_CACHE_DIR)s/index -rpc autostart=true autorestart=true startretries=3 @@ -13,7 +13,7 @@ stdout_logfile_maxbytes=0 redirect_stderr=true [program:web] -command=./prefix-output.sh node packages/web/server.js +command=taskset -c %(ENV_FRONTEND_CPU_LIST)s ./prefix-output.sh node packages/web/server.js autostart=true autorestart=true startretries=3 @@ -21,7 +21,6 @@ stdout_logfile=/dev/fd/1 stdout_logfile_maxbytes=0 redirect_stderr=true - [program:backend] command=./prefix-output.sh node packages/backend/dist/index.js --cacheDir %(ENV_DATA_CACHE_DIR)s autostart=true