Bladeren bron

build: compose config for a prod-faithful devstack with RAM/CPU as dials

Runs the published debs at versions you name against a copy of a real data
directory, with mem_limit and cpuset as the two things you turn, so would
fszontagh 3 weken geleden
bovenliggende
commit
05955dc6bc

+ 27 - 0
packaging/devstack/.env.example

@@ -0,0 +1,27 @@
+# Copy to .env and fill in. Nothing in here is committed: .env is gitignored,
+# because DEVSTACK_DATA_DIR points at a copy of real tenant data.
+
+# --- which artefacts to test -------------------------------------------------
+# Versions exactly as the .deb filenames spell them. Read them off the box you
+# are modelling with:
+#   dpkg-query -W -f='${Package} ${Version}\n' smartbotic-database smartbotic-vectorapi
+DB_VERSION=2.21.2-1
+DB_CLIENT_VERSION=2.20.0-1
+VAPI_VERSION=0.18.0-1
+# Directory holding the .deb files named by the versions above.
+DEB_DIR=../../../smartbotics-deb-repo/dist
+
+# --- the box being modelled --------------------------------------------------
+DEVSTACK_CPUS=2
+DEVSTACK_MEM=4g
+# Equal to DEVSTACK_MEM means no swap. Set higher to model a swapfile, e.g.
+# DEVSTACK_MEMSWAP=8g alongside DEVSTACK_MEM=4g gives 4 GB of swap.
+DEVSTACK_MEMSWAP=4g
+
+# --- the data and config to run against --------------------------------------
+# A COPY of a data directory. Never point this at a live one.
+DEVSTACK_DATA_DIR=/srv/devstack/data
+DEVSTACK_DB_CONFIG=/srv/devstack/etc/config.json
+DEVSTACK_DB_CONFD=/srv/devstack/etc/conf.d
+
+DEVSTACK_PORT=8099

+ 2 - 0
packaging/devstack/.gitignore

@@ -0,0 +1,2 @@
+debs/
+.env

+ 25 - 3
packaging/devstack/Dockerfile

@@ -2,16 +2,38 @@
 # .debs. Native builds are impossible on the Void dev host (protobuf 35 removed
 # a header the generated protos need), so the Debian 13 image is the only way to
 # run the real stack locally. The web UI is served by Vite in dev, not from here.
+#
+# The versions are ARGs and are ASSERTED rather than assumed: fetch-debs.sh
+# stages exactly the ones named, and the RUN below fails the build if any is
+# missing. Installing whatever happens to be in debs/ is how a "prod-faithful"
+# image quietly stops being faithful.
 FROM debian:13
+
+ARG DB_VERSION
+ARG DB_CLIENT_VERSION
+ARG VAPI_VERSION
+
 # adduser is needed by both postinst scripts; systemctl is not present in a
 # container, so a no-op shim lets the packages configure cleanly. The services
-# are started by the entrypoint instead.
-RUN apt-get update && apt-get install -y --no-install-recommends ca-certificates curl adduser \
+# are started by the entrypoint instead. libidn2-0 is a real NEEDED library of
+# the runner's vendored curl; jq and procps are for measuring from inside.
+RUN apt-get update && apt-get install -y --no-install-recommends \
+      ca-certificates curl adduser libidn2-0 jq procps \
     && rm -rf /var/lib/apt/lists/* \
     && printf '#!/bin/sh\nexit 0\n' > /usr/bin/systemctl && chmod +x /usr/bin/systemctl
+
 COPY debs/ /tmp/debs/
-RUN apt-get update && apt-get install -y --no-install-recommends /tmp/debs/*.deb \
+RUN set -e; \
+    if [ -n "$DB_VERSION" ]; then \
+      for f in smartbotic-database_${DB_VERSION}_amd64.deb \
+               libsmartbotic-db-client_${DB_CLIENT_VERSION}_amd64.deb \
+               smartbotic-vectorapi_${VAPI_VERSION}_amd64.deb; do \
+        [ -f "/tmp/debs/$f" ] || { echo "expected deb missing from build context: $f" >&2; exit 1; }; \
+      done; \
+    fi; \
+    apt-get update && apt-get install -y --no-install-recommends /tmp/debs/*.deb \
     && rm -rf /var/lib/apt/lists/* /tmp/debs
+
 COPY entrypoint.sh /usr/local/bin/entrypoint.sh
 RUN chmod +x /usr/local/bin/entrypoint.sh
 EXPOSE 8080

+ 63 - 0
packaging/devstack/README.md

@@ -25,3 +25,66 @@ cd ../../webui && npm run dev      # http://localhost:3000
 `docker logs svapi-dev` shows both services. The container has no systemd, so the
 entrypoint starts the database, waits for :9004, then execs the API; `systemctl`
 is a no-op shim purely so the packages' postinst scripts configure cleanly.
+
+## Compose: a prod-faithful stack with the box size as a dial
+
+`docker-compose.yml` runs the same image with **the RAM and CPU count as the two
+things you turn**, so "would more memory fix this" becomes a measurement instead
+of an argument. Everything machine-specific lives in `.env` (gitignored, because
+`DEVSTACK_DATA_DIR` points at a copy of real tenant data) - the committed files
+contain no paths, hostnames or versions of their own.
+
+```bash
+cp .env.example .env && $EDITOR .env   # versions, limits, data dir
+./fetch-debs.sh                        # stage exactly those deb versions
+docker compose build && docker compose up -d
+curl -fsS localhost:"${DEVSTACK_PORT:-8099}"/healthz
+```
+
+**Modelling a specific box takes BOTH cpu settings, and this is the part that is
+easy to get wrong.** `cpus` is a CPU-*time* quota: it bounds throughput
+correctly, but the container still *sees* every host core. Measured here:
+`cpus: 2` on a 32-core host still reported `nproc=32`, and cpp-httplib sizes its
+request pool from `hardware_concurrency`, so vectorapi would have run **31
+request threads where a real 2-core box runs 8**. `cpuset` fixes the count as
+well as the quota. Verify both from inside rather than trusting the file:
+
+```bash
+docker compose exec devstack sh -c 'nproc; cat /sys/fs/cgroup/cpu.max /sys/fs/cgroup/memory.max'
+```
+
+**Swap is not controlled by this file.** `memswap_limit` equal to `mem_limit`
+asks for none, and `docker inspect --format '{{.HostConfig.MemorySwap}}'`
+reported `-1` (unlimited) anyway. What actually guaranteed no swapping was the
+*host* having no swap device. Read `swapon --show` on the host before quoting
+any memory figure from this container, because a result measured with swap
+available is a result about swap.
+
+### Pointing it at a copy of a real data directory
+
+`DEVSTACK_DATA_DIR` must be a **copy**, never a live directory. Copy the whole
+data directory: a snapshot plus the WAL is the server's own recovery *input*,
+not a copy of the dataset, so `projects/` (the authoritative LMDB store) has to
+come too. Superseded snapshots can be left behind - the server loads only the
+newest - which is usually most of the bytes.
+
+```bash
+# streamed, so nothing is staged on the source box's disk
+ssh HOST 'sudo tar -cf - -C /var/lib/smartbotic-database \
+    storage.key encryption-at-rest.v1 files projects history wal \
+    snapshots/<newest>.dat' | tar -xf - -C "$DEVSTACK_DATA_DIR"
+```
+
+Also bind-mount that box's own `config.json` and `conf.d/`
+(`DEVSTACK_DB_CONFIG`, `DEVSTACK_DB_CONFD`), or the stack under test runs on
+the package's default memory budget rather than the deployment's.
+
+**Verify the copy loaded before believing any timing from it.** The boot line
+`Recovery complete (...): N docs in M collections` must match the source box's
+own document count - and for a vector collection, run one search and check the
+ranking, because a document count cannot see a vector stored in the wrong field
+or lost in transit.
+
+The data directory is encrypted at rest, so `storage.key` travels with it. That
+makes the copy as sensitive as the original: keep it mode 700 and delete it when
+the measurement is done.

+ 61 - 0
packaging/devstack/docker-compose.yml

@@ -0,0 +1,61 @@
+# A prod-faithful stack in one container, with the box's RAM and CPU as the two
+# things you can turn. It exists to answer "would more RAM or more CPU fix this"
+# by measurement rather than by argument, so the packages are the PUBLISHED debs
+# at versions you name - never a local build - and the data directory is a copy
+# of a real one, bind-mounted from outside.
+#
+# Nothing here is specific to a machine: every path, version and limit comes
+# from the environment (see .env.example). Copy that to .env, fill it in, then:
+#
+#   docker compose -f packaging/devstack/docker-compose.yml build
+#   docker compose -f packaging/devstack/docker-compose.yml up
+#
+# The limits are enforced by the kernel, not requested: `docker stats` and
+# /sys/fs/cgroup inside the container both report them, which is what makes a
+# measurement taken here a statement about a box of that size.
+services:
+  devstack:
+    build:
+      context: .
+      dockerfile: Dockerfile
+      args:
+        # Which debs the image installs. Point DEB_DIR at a directory holding
+        # them and name the versions; the build copies exactly these, so the
+        # artefact under test is the artefact prod runs.
+        DB_VERSION: ${DB_VERSION:?set DB_VERSION, e.g. 2.21.2-1}
+        DB_CLIENT_VERSION: ${DB_CLIENT_VERSION:?set DB_CLIENT_VERSION, e.g. 2.20.0-1}
+        VAPI_VERSION: ${VAPI_VERSION:?set VAPI_VERSION, e.g. 0.18.0-1}
+    image: svapi-devstack:${VAPI_VERSION}-db${DB_VERSION}
+    # The whole point of the file, and it takes BOTH settings to model a small
+    # box. `cpus` is a CPU-time quota: it bounds throughput correctly, but the
+    # container still SEES every host core, so `nproc` reported 32 on a
+    # 2-core quota - and cpp-httplib sizes its request pool from
+    # hardware_concurrency, so vectorapi would run 31 threads where a real
+    # 2-core box runs 8. Measured, not assumed. `cpuset` pins the container to
+    # named cores so the COUNT is right as well as the quota, which is what
+    # makes a concurrency measurement here mean anything.
+    cpus: ${DEVSTACK_CPUS:-2}
+    cpuset: ${DEVSTACK_CPUSET:-0-1}
+    mem_limit: ${DEVSTACK_MEM:-4g}
+    # Equal to mem_limit asks for NO swap, because a figure measured with swap
+    # available is a figure about swap. Verify it rather than trusting it:
+    # `docker inspect --format {{.HostConfig.MemorySwap}}` reported -1
+    # (unlimited) here even with this set, and what actually guaranteed no
+    # swapping was the HOST having no swap device - check `swapon --show`
+    # before reading any memory result off this container.
+    memswap_limit: ${DEVSTACK_MEMSWAP:-${DEVSTACK_MEM:-4g}}
+    volumes:
+      # A COPY of a data directory, never a live one. Read-write, because the
+      # server replays its WAL and writes snapshots.
+      - ${DEVSTACK_DATA_DIR:?set DEVSTACK_DATA_DIR to a copy of a data directory}:/var/lib/smartbotic-database
+      # The database's own config, so the stack under test has the same memory
+      # budget and eviction thresholds as the deployment being modelled.
+      - ${DEVSTACK_DB_CONFIG:?set DEVSTACK_DB_CONFIG}:/etc/smartbotic-database/config.json:ro
+      - ${DEVSTACK_DB_CONFD:?set DEVSTACK_DB_CONFD}:/etc/smartbotic-database/conf.d:ro
+    ports:
+      - "${DEVSTACK_PORT:-8099}:8080"
+    environment:
+      # Only consulted on a FRESH store. With a copied data directory the keys
+      # already exist, so this seeds nothing and the real keys keep working.
+      SMARTBOTIC_VECTORAPI_KEY: ${SMARTBOTIC_VECTORAPI_KEY:-}
+    stop_grace_period: 120s

+ 15 - 5
packaging/devstack/entrypoint.sh

@@ -1,13 +1,23 @@
 #!/bin/bash
-# No systemd in the container, so both services run directly. The DB must be
-# accepting connections before vectorapi starts, or vectorapi exits.
+# No systemd in the container, so the services run directly. The DB must be
+# accepting connections before vectorapi starts, or vectorapi exits - so the
+# port is waited on rather than slept past. A copied data directory can take
+# minutes to recover, which is why the wait is generous and reports how long it
+# took: that number is the cold-boot cost and is worth reading.
 set -e
 mkdir -p /var/lib/smartbotic-database
 chown -R smartbotic-db:smartbotic-db /var/lib/smartbotic-database 2>/dev/null || true
-su -s /bin/bash smartbotic-db -c '/usr/bin/smartbotic-database --config /etc/smartbotic-database/config.json' &
-for i in $(seq 1 60); do
+su -s /bin/bash smartbotic-db -c '/usr/bin/smartbotic-database --config /etc/smartbotic-database/config.json' 2>&1 | sed 's/^/[db] /' &
+for i in $(seq 1 600); do
     (exec 3<>/dev/tcp/127.0.0.1/9004) 2>/dev/null && break
     sleep 1
 done
+echo "[entrypoint] database accepting connections after ${i}s"
 sed -i 's/"bind_address": *"[^"]*"/"bind_address": "0.0.0.0"/' /etc/smartbotic-vectorapi/config.json || true
-exec /usr/bin/smartbotic-vectorapi --config /etc/smartbotic-vectorapi/config.json
+/usr/bin/smartbotic-vectorapi --config /etc/smartbotic-vectorapi/config.json 2>&1 | sed 's/^/[api] /' &
+for i in $(seq 1 120); do
+    (exec 3<>/dev/tcp/127.0.0.1/8080) 2>/dev/null && break
+    sleep 1
+done
+echo "[entrypoint] READY: api up after ${i}s"
+wait -n

+ 41 - 0
packaging/devstack/fetch-debs.sh

@@ -0,0 +1,41 @@
+#!/usr/bin/env bash
+# Stage exactly the .deb versions named in .env into ./debs, so the image build
+# is reproducible from a version number rather than from whatever was left in
+# the directory last time. Run before `docker compose build`.
+#
+#   packaging/devstack/fetch-debs.sh
+#
+# Reads DEB_DIR, DB_VERSION, DB_CLIENT_VERSION and VAPI_VERSION from .env (or
+# the environment). Every name is version-matched and then the staged file's own
+# Version field is read back - a filename glob on the package name is not a
+# match, and staging the wrong version is a mistake this repo has made before.
+set -euo pipefail
+HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
+[[ -f "$HERE/.env" ]] && set -a && . "$HERE/.env" && set +a
+: "${DEB_DIR:?set DEB_DIR in .env}"
+: "${DB_VERSION:?set DB_VERSION in .env}"
+: "${DB_CLIENT_VERSION:?set DB_CLIENT_VERSION in .env}"
+: "${VAPI_VERSION:?set VAPI_VERSION in .env}"
+cd "$HERE"
+DEB_DIR="$(cd "$DEB_DIR" && pwd)"
+
+rm -rf debs && mkdir -p debs
+# name:version pairs. The webui is _all, the rest _amd64; the CLI rides the
+# client's version because it links the same shared library.
+stage() {
+    local pkg="$1" ver="$2" arch="$3" src="$DEB_DIR/${1}_${2}_${3}.deb"
+    [[ -f "$src" ]] || { echo "not found: $src" >&2; return 1; }
+    cp "$src" debs/
+    # Read the version back OUT of the file rather than trusting the name.
+    local got; got="$(dpkg-deb -f "debs/${pkg}_${ver}_${arch}.deb" Version)"
+    [[ "$got" == "$ver" ]] || { echo "$pkg: file says $got, expected $ver" >&2; return 1; }
+    printf '  %-32s %s\n' "$pkg" "$got"
+}
+echo "staging from $DEB_DIR"
+stage smartbotic-database            "$DB_VERSION"        amd64
+stage libsmartbotic-db-client        "$DB_CLIENT_VERSION" amd64
+stage smartbotic-db-cli              "$DB_CLIENT_VERSION" amd64
+stage smartbotic-vectorapi           "$VAPI_VERSION"      amd64
+stage smartbotic-vectorapi-runner    "$VAPI_VERSION"      amd64
+stage smartbotic-vectorapi-webui     "$VAPI_VERSION"      all
+echo "staged $(ls debs | wc -l) debs into $HERE/debs"