diff --git a/internal/k8s/upgrade.go b/internal/k8s/upgrade.go index 1fb0a00..b2a66d7 100644 --- a/internal/k8s/upgrade.go +++ b/internal/k8s/upgrade.go @@ -37,7 +37,7 @@ func (c *Client) SetDeploymentImage(ctx context.Context, ns, name, image string) for i := range deploy.Spec.Template.Spec.Containers { deploy.Spec.Template.Spec.Containers[i].Image = image } - ensureTuningEnv(deploy) + reconcileTuningEnv(deploy) ensureMediatorVaultCA(deploy, image) _, err = c.kube.AppsV1().Deployments(ns).Update(ctx, deploy, metav1.UpdateOptions{}) return err @@ -61,7 +61,7 @@ func (c *Client) RecoverDeploymentImage(ctx context.Context, ns, name, image str for i := range deploy.Spec.Template.Spec.Containers { deploy.Spec.Template.Spec.Containers[i].Image = image } - ensureTuningEnv(deploy) + reconcileTuningEnv(deploy) ensureMediatorVaultCA(deploy, image) if deploy.Spec.Template.Annotations == nil { deploy.Spec.Template.Annotations = map[string]string{} diff --git a/internal/k8s/upgrade_test.go b/internal/k8s/upgrade_test.go index 1aedb82..0ad4b09 100644 --- a/internal/k8s/upgrade_test.go +++ b/internal/k8s/upgrade_test.go @@ -73,19 +73,20 @@ func deploymentEnv(t *testing.T, client *Client, name string) map[string]string func TestSetDeploymentImageAddsMissingVtaTuningEnv(t *testing.T) { for _, app := range []string{"vta", "fs-vta"} { t.Run(app, func(t *testing.T) { - // A Deployment created before MALLOC_ARENA_MAX joined the template, - // with one storage value an operator tuned by hand. + // A Deployment created before the journal budget joined the + // template, with one storage value an operator tuned by hand. client := &Client{kube: fake.NewSimpleClientset(tuningTestDeployment("vta-7", app, corev1.EnvVar{Name: "NO_COLOR", Value: "1"}, corev1.EnvVar{Name: "STORAGE_FJALL_BLOCK_CACHE", Value: "4194304"}, + corev1.EnvVar{Name: "STORAGE_FJALL_WRITE_BUFFER", Value: "16777216"}, ))} if err := client.SetDeploymentImage(context.Background(), "test", "vta-7", "example/vta:new"); err != nil { t.Fatalf("SetDeploymentImage() error = %v", err) } env := deploymentEnv(t, client, "vta-7") - if env["MALLOC_ARENA_MAX"] != "2" { - t.Fatalf("MALLOC_ARENA_MAX = %q, want 2", env["MALLOC_ARENA_MAX"]) + if env["STORAGE_FJALL_MAX_JOURNAL"] != "67108864" { + t.Fatalf("STORAGE_FJALL_MAX_JOURNAL = %q, want 67108864", env["STORAGE_FJALL_MAX_JOURNAL"]) } if env["STORAGE_FJALL_BLOCK_CACHE"] != "4194304" { t.Fatalf("hand-tuned STORAGE_FJALL_BLOCK_CACHE was overwritten: %q", env["STORAGE_FJALL_BLOCK_CACHE"]) @@ -108,8 +109,52 @@ func TestRecoverDeploymentImageAddsMissingVtaTuningEnv(t *testing.T) { if err := client.RecoverDeploymentImage(context.Background(), "test", "vta-7", "example/vta:new"); err != nil { t.Fatalf("RecoverDeploymentImage() error = %v", err) } - if env := deploymentEnv(t, client, "vta-7"); env["MALLOC_ARENA_MAX"] != "2" { - t.Fatalf("MALLOC_ARENA_MAX = %q, want 2", env["MALLOC_ARENA_MAX"]) + env := deploymentEnv(t, client, "vta-7") + for _, want := range VtaTuningEnv() { + if env[want.Name] != want.Value { + t.Fatalf("%s = %q, want %q", want.Name, env[want.Name], want.Value) + } + } +} + +// MALLOC_ARENA_MAX was part of the VTA, DID hosting and VTC templates while +// those images used glibc's allocator. They link jemalloc now, so an upgrade +// takes the variable off Deployments that still carry it. +func TestSetDeploymentImageRemovesRetiredTuningEnv(t *testing.T) { + for _, app := range []string{"vta", "fs-vta", "fs-dids", "fs-vtc"} { + t.Run(app, func(t *testing.T) { + env := append([]corev1.EnvVar{{Name: "NO_COLOR", Value: "1"}}, tuningEnvFor(app)...) + env = append(env, corev1.EnvVar{Name: "MALLOC_ARENA_MAX", Value: "2"}) + env[1].Value = "4194304" // hand-tuned STORAGE_FJALL_BLOCK_CACHE + client := &Client{kube: fake.NewSimpleClientset(tuningTestDeployment("fs-7", app, env...))} + + if err := client.SetDeploymentImage(context.Background(), "test", "fs-7", "example/image:new"); err != nil { + t.Fatalf("SetDeploymentImage() error = %v", err) + } + got := deploymentEnv(t, client, "fs-7") + if value, ok := got["MALLOC_ARENA_MAX"]; ok { + t.Fatalf("MALLOC_ARENA_MAX = %q, want it removed", value) + } + if got["STORAGE_FJALL_BLOCK_CACHE"] != "4194304" { + t.Fatalf("hand-tuned STORAGE_FJALL_BLOCK_CACHE was overwritten: %q", got["STORAGE_FJALL_BLOCK_CACHE"]) + } + if len(got) != len(env)-1 { + t.Fatalf("unexpected env after the upgrade: %v", got) + } + }) + } +} + +func TestRecoverDeploymentImageRemovesRetiredTuningEnv(t *testing.T) { + client := &Client{kube: fake.NewSimpleClientset(tuningTestDeployment("vta-7", "vta", + corev1.EnvVar{Name: "MALLOC_ARENA_MAX", Value: "2"}, + ))} + + if err := client.RecoverDeploymentImage(context.Background(), "test", "vta-7", "example/vta:new"); err != nil { + t.Fatalf("RecoverDeploymentImage() error = %v", err) + } + if value, ok := deploymentEnv(t, client, "vta-7")["MALLOC_ARENA_MAX"]; ok { + t.Fatalf("MALLOC_ARENA_MAX = %q, want it removed", value) } } @@ -192,8 +237,7 @@ func TestSetDeploymentImageAddsMissingDidsAndVtcTuningEnv(t *testing.T) { } for _, tc := range cases { t.Run(tc.app, func(t *testing.T) { - // Created before MALLOC_ARENA_MAX joined the template, with one - // budget tuned by hand. + // Created with only the block cache budget, tuned by hand. client := &Client{kube: fake.NewSimpleClientset(tuningTestDeployment("fs-7", tc.app, corev1.EnvVar{Name: "NO_COLOR", Value: "1"}, corev1.EnvVar{Name: "STORAGE_FJALL_BLOCK_CACHE", Value: "4194304"}, @@ -203,9 +247,6 @@ func TestSetDeploymentImageAddsMissingDidsAndVtcTuningEnv(t *testing.T) { t.Fatalf("SetDeploymentImage() error = %v", err) } env := deploymentEnv(t, client, "fs-7") - if env["MALLOC_ARENA_MAX"] != "2" { - t.Fatalf("MALLOC_ARENA_MAX = %q, want 2", env["MALLOC_ARENA_MAX"]) - } if env["STORAGE_FJALL_BLOCK_CACHE"] != "4194304" { t.Fatalf("hand-tuned STORAGE_FJALL_BLOCK_CACHE was overwritten: %q", env["STORAGE_FJALL_BLOCK_CACHE"]) } diff --git a/internal/k8s/vta_resources.go b/internal/k8s/vta_resources.go index 717c862..c39943e 100644 --- a/internal/k8s/vta_resources.go +++ b/internal/k8s/vta_resources.go @@ -63,50 +63,42 @@ func vtaServiceName(sessionID uint) string { const VtaServiceAccount = "vta" // VtaTuningEnv is shared by the VTA-only and full-stack Deployments. -// -// MALLOC_ARENA_MAX caps glibc's per-thread malloc arenas. The VTA runs ~24 -// threads, and without the cap each arena keeps the memory a burst of requests -// freed, so the pod stays near its high-water mark long after going idle. On -// staging the same light session left a VTA at 50Mi without the cap and 18Mi -// with it. glibc reads the variable once at process start. func VtaTuningEnv() []corev1.EnvVar { return []corev1.EnvVar{ {Name: "STORAGE_FJALL_BLOCK_CACHE", Value: "8388608"}, // 8 MiB {Name: "STORAGE_FJALL_WRITE_BUFFER", Value: "16777216"}, // 16 MiB {Name: "STORAGE_FJALL_MAX_JOURNAL", Value: "67108864"}, // 64 MiB - {Name: "MALLOC_ARENA_MAX", Value: "2"}, } } // DidsTuningEnv is the DID hosting daemon's tuning. The daemon applies the // Fjall budgets to each of its two stores. -// -// MALLOC_ARENA_MAX: same glibc arena retention as the VTA. The platform DID -// host on staging held 80 MB in 26 arenas after 90 hours without the cap and -// 15 MB in one arena after 86 hours with it. func DidsTuningEnv() []corev1.EnvVar { return []corev1.EnvVar{ {Name: "STORAGE_FJALL_BLOCK_CACHE", Value: "8388608"}, // 8 MiB {Name: "STORAGE_FJALL_WRITE_BUFFER", Value: "16777216"}, // 16 MiB {Name: "STORAGE_FJALL_MAX_JOURNAL", Value: "67108864"}, // 64 MiB - {Name: "MALLOC_ARENA_MAX", Value: "2"}, } } // VtcTuningEnv is the VTC's tuning. -// -// MALLOC_ARENA_MAX: the VTC runs 40–55 threads and held 60–110 MB in 22–29 -// glibc arenas on staging; with the cap that was 15 MB in one arena. The cap -// does not bound the main heap, which still grows on a busy VTC. func VtcTuningEnv() []corev1.EnvVar { return []corev1.EnvVar{ {Name: "STORAGE_FJALL_BLOCK_CACHE", Value: "16777216"}, // 16 MiB {Name: "STORAGE_FJALL_WRITE_BUFFER", Value: "33554432"}, // 32 MiB {Name: "STORAGE_FJALL_MAX_JOURNAL", Value: "134217728"}, // 128 MiB - {Name: "MALLOC_ARENA_MAX", Value: "2"}, } } +// retiredTuningEnv lists variables an earlier template set that no longer do +// anything, so the upgrade path removes them. +// +// MALLOC_ARENA_MAX capped glibc's malloc arenas. The VTA, DID hosting and VTC +// images now link jemalloc, which does not read the variable. +var retiredTuningEnv = map[string]bool{ + "MALLOC_ARENA_MAX": true, +} + // tuningEnvFor returns the tuning template for a Deployment's app label, or // nil for components whose env is not reconciled on upgrade. func tuningEnvFor(app string) []corev1.EnvVar { @@ -121,29 +113,41 @@ func tuningEnvFor(app string) []corev1.EnvVar { return nil } -// ensureTuningEnv adds any tuning variable a VTA, DID hosting or VTC -// Deployment is missing from its component's template. Deployments are only -// ever created from the template, never re-rendered, so one created before a -// variable existed would otherwise lack it forever. It only adds: a value -// already set is left alone, because operators tune individual Deployments by -// hand. Other components are not touched. Reports whether it changed anything. -func ensureTuningEnv(deployment *appsv1.Deployment) bool { +// reconcileTuningEnv brings a VTA, DID hosting or VTC Deployment's tuning +// variables in line with its component's template. Deployments are only ever +// created from the template, never re-rendered, so without this one created +// under an older template would keep that template's variables forever. +// +// It adds any template variable that is missing and removes the retired ones. +// A template variable that is already set keeps its value, because operators +// tune individual Deployments by hand. Other components are not touched. +// Reports whether it changed anything. +func reconcileTuningEnv(deployment *appsv1.Deployment) bool { want := tuningEnvFor(deployment.Labels["app"]) if len(want) == 0 || len(deployment.Spec.Template.Spec.Containers) == 0 { return false } container := &deployment.Spec.Template.Spec.Containers[0] + changed := false + kept := make([]corev1.EnvVar, 0, len(container.Env)+len(want)) present := make(map[string]bool, len(container.Env)) for _, env := range container.Env { + if retiredTuningEnv[env.Name] { + changed = true + continue + } + kept = append(kept, env) present[env.Name] = true } - changed := false for _, env := range want { if !present[env.Name] { - container.Env = append(container.Env, env) + kept = append(kept, env) changed = true } } + if changed { + container.Env = kept + } return changed } diff --git a/internal/k8s/vta_resources_test.go b/internal/k8s/vta_resources_test.go index fd4aaf1..42ba745 100644 --- a/internal/k8s/vta_resources_test.go +++ b/internal/k8s/vta_resources_test.go @@ -77,15 +77,19 @@ func TestCreateVtaDeploymentSetsTuningEnv(t *testing.T) { "STORAGE_FJALL_BLOCK_CACHE": "8388608", "STORAGE_FJALL_WRITE_BUFFER": "16777216", "STORAGE_FJALL_MAX_JOURNAL": "67108864", - "MALLOC_ARENA_MAX": "2", } for name, value := range want { if env[name] != value { t.Errorf("%s = %q, want %q", name, env[name], value) } } + for name := range retiredTuningEnv { + if _, ok := env[name]; ok { + t.Errorf("retired variable %s is set on a new Deployment", name) + } + } // A new Deployment must already be what the upgrade path converges to. - if ensureTuningEnv(deployment) { + if reconcileTuningEnv(deployment) { t.Fatal("a freshly created VTA Deployment is missing tuning env") } } diff --git a/scripts/backfill-vta-malloc-arena.sh b/scripts/backfill-vta-malloc-arena.sh deleted file mode 100755 index 48aa342..0000000 --- a/scripts/backfill-vta-malloc-arena.sh +++ /dev/null @@ -1,97 +0,0 @@ -#!/bin/bash -set -euo pipefail - -# Set MALLOC_ARENA_MAX=2 on every VTA Deployment that does not have it yet. -# -# KUBE_CONTEXT= ./scripts/backfill-vta-malloc-arena.sh # dry run — prints the plan -# KUBE_CONTEXT= ./scripts/backfill-vta-malloc-arena.sh --apply # do it -# -# KUBE_CONTEXT kubectl context to use (required — this restarts VTAs, -# so it never falls back to the current context) -# NS_PREFIX user namespace prefix (default: vtafarm-user) -# ROLLOUT_TIMEOUT per-VTA rollout wait (default: 180s) -# -# ── Why this has to exist ──────────────────────────────────────────────────── -# -# vtafarm-api only ever *creates* a VTA Deployment from its template — -# AlreadyExists is ignored. VtaTuningEnv() (internal/k8s/vta_resources.go) now -# carries MALLOC_ARENA_MAX=2, which stops glibc keeping freed memory in one -# arena per thread (a VTA sat at 50Mi after a light session without it, 18Mi -# with it). That reaches new VTAs only. Existing ones pick the variable up at -# their next image upgrade, because SetDeploymentImage adds missing tuning -# variables; this script is for getting there without waiting for a release. -# -# What it does, one VTA at a time: -# -# 1. finds Deployments labelled app=vta or app=fs-vta in the user namespaces -# whose first container has no MALLOC_ARENA_MAX, -# 2. sets it, which restarts the pod (strategy Recreate, ~30–40 s of downtime -# for that VTA), -# 3. waits for the rollout and stops at the first one that fails. -# -# A value that is already set is left alone, whatever it is. Parked pool VTAs -# (0 replicas) get the variable without a rollout wait. -# -# Run it while no upgrade batch or placement move is in progress: a placement -# rollback restores the pod template it saved moments earlier and would drop -# the variable again for that one VTA. -# -# Safe to re-run. - -if [ -z "${KUBE_CONTEXT:-}" ]; then - echo "KUBE_CONTEXT is required (for example KUBE_CONTEXT=rke2-vtafarm-staging)" >&2 - exit 2 -fi -CTX="$KUBE_CONTEXT" -NS_PREFIX="${NS_PREFIX:-vtafarm-user}" -ROLLOUT_TIMEOUT="${ROLLOUT_TIMEOUT:-180s}" -APPLY=false -[ "${1:-}" = "--apply" ] && APPLY=true - -VAR="MALLOC_ARENA_MAX" -VALUE="2" - -k() { kubectl --context "$CTX" "$@"; } - -# namespace, name, replicas — one line per VTA Deployment missing the variable. -missing() { - k get deployments --all-namespaces -l 'app in (vta, fs-vta)' \ - -o jsonpath='{range .items[*]}{.metadata.namespace}{"\t"}{.metadata.name}{"\t"}{.spec.replicas}{"\t"}{range .spec.template.spec.containers[0].env[*]}{.name}{","}{end}{"\n"}{end}' | - awk -F'\t' -v prefix="$NS_PREFIX-" -v var="$VAR" \ - 'index($1, prefix) == 1 && index("," $4, "," var ",") == 0 { print $1 "\t" $2 "\t" $3 }' | - sort -t "$(printf '\t')" -k1,1 -k2,2V -} - -TARGETS="$(missing)" -if [ -z "$TARGETS" ]; then - echo "[$CTX] every VTA Deployment already has $VAR — nothing to do" - exit 0 -fi -COUNT="$(printf '%s\n' "$TARGETS" | wc -l)" - -if ! $APPLY; then - echo "[$CTX] dry run — $COUNT VTA Deployment(s) would get $VAR=$VALUE and restart:" - printf '%s\n' "$TARGETS" | awk -F'\t' '{ printf " %s/%s (replicas %s)\n", $1, $2, $3 }' - echo "Re-run with --apply to do it." - exit 0 -fi - -echo "[$CTX] setting $VAR=$VALUE on $COUNT VTA Deployment(s), one at a time" -DONE=0 -while IFS="$(printf '\t')" read -r NS NAME REPLICAS; do - DONE=$((DONE + 1)) - echo "[$DONE/$COUNT] $NS/$NAME" - k -n "$NS" set env "deployment/$NAME" "$VAR=$VALUE" >/dev/null - if [ "${REPLICAS:-0}" = "0" ]; then - echo " parked (0 replicas) — variable set, no rollout to wait for" - continue - fi - if ! k -n "$NS" rollout status "deployment/$NAME" --timeout="$ROLLOUT_TIMEOUT" >/dev/null; then - echo " rollout of $NS/$NAME did not finish within $ROLLOUT_TIMEOUT — stopping." >&2 - echo " Inspect it with: kubectl --context $CTX -n $NS describe deployment/$NAME" >&2 - exit 1 - fi -done <<<"$TARGETS" - -LEFT="$(missing | wc -l)" -echo "[$CTX] done — $DONE updated, $LEFT VTA Deployment(s) still without $VAR"