Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions internal/k8s/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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{}
Expand Down
63 changes: 52 additions & 11 deletions internal/k8s/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Expand All @@ -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)
}
}

Expand Down Expand Up @@ -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"},
Expand All @@ -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"])
}
Expand Down
56 changes: 30 additions & 26 deletions internal/k8s/vta_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}

Expand Down
8 changes: 6 additions & 2 deletions internal/k8s/vta_resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
Expand Down
97 changes: 0 additions & 97 deletions scripts/backfill-vta-malloc-arena.sh

This file was deleted.