Repository navigation
feat(cli): container cpu and memory limits for local stack services #7604
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jgoux
wants to merge
2
commits into
develop
Choose a base branch
from
juliengoux/cli-2979-managed-local-stack-cpu-and-memory
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+751
−28
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,6 +35,7 @@ import { resolveAuthConfig } from "./stack-auth-config.ts"; | |
| import { resolveSmtpEnabled } from "./smtp-enabled.ts"; | ||
| import { parseDuration } from "./duration.ts"; | ||
| import { parseFileSizeLimit } from "./storage-bucket-config.ts"; | ||
| import { ramInBytes } from "./size-units.ts"; | ||
| import { stackArtifactSelection } from "./stack-artifact-selection.ts"; | ||
| import { | ||
| type LockRequest, | ||
|
|
@@ -644,11 +645,26 @@ export const loadStackConfig = Effect.fn("StackConfig.load")( | |
| Effect.provideService(Path.Path, path), | ||
| Effect.mapError(toStackConfigError), | ||
| ); | ||
| // The memory string is schema-validated, so `ramInBytes` cannot throw here. | ||
| const withContainerLimits = (creation: ServiceCreationType): ServiceCreationType => { | ||
| const container = validatedConfig.local?.services?.[creation.service]?.container; | ||
| return container === undefined | ||
| ? creation | ||
| : { | ||
| ...creation, | ||
| container: { | ||
| ...(container.memory === undefined | ||
| ? {} | ||
| : { memoryBytes: ramInBytes(container.memory) }), | ||
| ...(container.cpus === undefined ? {} : { cpus: container.cpus }), | ||
| }, | ||
| }; | ||
|
Comment on lines
+649
to
+661
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: an empty could it just return |
||
| }; | ||
| const createCreations = ( | ||
| stackId: string, | ||
| ): Effect.Effect<ReadonlyArray<ServiceCreationType>, StackConfigError> => | ||
| Effect.map(emailTemplates, (templates) => { | ||
| return [ | ||
| const creations: ReadonlyArray<ServiceCreationType> = [ | ||
| { | ||
| service: "database", | ||
| pin: selection.database, | ||
|
|
@@ -841,6 +857,7 @@ export const loadStackConfig = Effect.fn("StackConfig.load")( | |
| ] | ||
| : []), | ||
| ]; | ||
| return creations.map(withContainerLimits); | ||
| }); | ||
| return { | ||
| selection, | ||
|
|
||
232 changes: 232 additions & 0 deletions
232
apps/cli/src/commands/experimental/stack/start/start.container-limits.integration.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,232 @@ | ||
| import { BunCrypto, BunServices } from "@effect/platform-bun"; | ||
| import { describe, expect, it } from "@effect/vitest"; | ||
| import { Effect, FileSystem, Layer, Option, Path } from "effect"; | ||
| import { FetchHttpClient } from "effect/http"; | ||
| import { ChildProcessSpawner } from "effect/process/ChildProcessSpawner"; | ||
| import { | ||
| mockCommandSettings, | ||
| mockTelemetryStateTracked, | ||
| } from "../../../../../tests/helpers/command-mocks.ts"; | ||
| import { cliConfigValuesTestLayer } from "../../../../../tests/helpers/config-values-layer.ts"; | ||
| import { mockOutput, mockTty } from "../../../../../tests/helpers/mocks.ts"; | ||
| import { destroyTestStacks } from "../../../../../tests/helpers/stack-cleanup.ts"; | ||
| import { CommandPlatformApiFactory } from "../../../../auth/command-platform-api-factory.service.ts"; | ||
| import { containerCliOutput } from "../../../../command-internal/container-cli.ts"; | ||
| import { dbConnectionLayer } from "../../../../command-internal/db-connection.sql-pg.layer.ts"; | ||
| import { DebugFlag, ExperimentalFlag, YesFlag } from "../../../../command-internal/global-flags.ts"; | ||
| import { stackCatalogSetupLayer } from "../../../../command-internal/stack-catalog-setup.ts"; | ||
| import { | ||
| StackApi, | ||
| stackApiLayer, | ||
| stackTargetResolverLayer, | ||
| } from "../../../../command-internal/stack-target.ts"; | ||
| import { CliArgs } from "../../../../shared/cli/cli-args.service.ts"; | ||
| import type { Output } from "../../../../shared/output/output.service.ts"; | ||
| import { runtimeInfoLayer } from "../../../../shared/runtime/runtime-info.layer.ts"; | ||
| import { stdinLayer } from "../../../../shared/runtime/stdin.layer.ts"; | ||
| import { stackStatus } from "../status/status.handler.ts"; | ||
| import { stackStart } from "./start.handler.ts"; | ||
|
|
||
| const excluded = [ | ||
| "rest", | ||
| "realtime", | ||
| "storage", | ||
| "functions", | ||
| "studio", | ||
| "mail", | ||
| "analytics", | ||
| "pooler", | ||
| ]; | ||
| const flags = { | ||
| exclude: excluded, | ||
| stack: Option.none<string>(), | ||
| stackId: Option.none<string>(), | ||
| runtime: "docker" as const, | ||
| preparation: "on-demand" as const, | ||
| frozen: false, | ||
| eager: true, | ||
| }; | ||
|
|
||
| const config = (database: string, auth: string) => ` | ||
| project_id = "stack-start-container-limits" | ||
|
|
||
| [api] | ||
| enabled = true | ||
|
|
||
| [auth] | ||
| enabled = true | ||
|
|
||
| [realtime] | ||
| enabled = false | ||
|
|
||
| [storage] | ||
| enabled = false | ||
|
|
||
| [edge_runtime] | ||
| enabled = false | ||
|
|
||
| [studio] | ||
| enabled = false | ||
|
|
||
| [analytics] | ||
| enabled = false | ||
|
|
||
| [db.pooler] | ||
| enabled = false | ||
|
|
||
| [local_smtp] | ||
| enabled = false | ||
|
|
||
| [local.services.database.container] | ||
| ${database} | ||
|
|
||
| [local.services.auth.container] | ||
| ${auth} | ||
| `; | ||
|
|
||
| const liveStackApi = stackApiLayer.pipe(Layer.provide(BunServices.layer)); | ||
|
|
||
| const makeLayer = (root: string, output: Layer.Layer<Output> = mockOutput().layer) => { | ||
| const settings = mockCommandSettings({ workdir: root, supabaseHome: root }); | ||
| const resolver = stackTargetResolverLayer.pipe( | ||
| Layer.provide(Layer.mergeAll(BunServices.layer, settings, liveStackApi)), | ||
| ); | ||
| const tty = mockTty({ stdinIsTty: false, stdoutIsTty: false }); | ||
| return Layer.mergeAll( | ||
| cliConfigValuesTestLayer, | ||
| FetchHttpClient.layer, | ||
| runtimeInfoLayer, | ||
| settings, | ||
| liveStackApi, | ||
| resolver, | ||
| output, | ||
| mockTelemetryStateTracked().layer, | ||
| stackCatalogSetupLayer, | ||
| dbConnectionLayer, | ||
| Layer.succeed(ExperimentalFlag, false), | ||
| Layer.succeed(DebugFlag, false), | ||
| Layer.succeed(YesFlag, false), | ||
| Layer.succeed(CliArgs, { args: ["stack", "start"] }), | ||
| Layer.succeed(CommandPlatformApiFactory, { make: Effect.die("unused") }), | ||
| stdinLayer.pipe(Layer.provide(tty)), | ||
| tty, | ||
| ).pipe(Layer.provideMerge(BunServices.layer)); | ||
| }; | ||
|
|
||
| const mib = 1024 * 1024; | ||
|
|
||
| /** Reads the limits Docker recorded for one service container of the given stack. */ | ||
| const serviceLimits = Effect.fn("StartContainerLimitsTest.serviceLimits")(function* ( | ||
| stackId: string, | ||
| service: string, | ||
| ) { | ||
| const spawner = yield* ChildProcessSpawner; | ||
| const listed = yield* containerCliOutput(spawner, [ | ||
| "container", | ||
| "ls", | ||
| "--all", | ||
| "--quiet", | ||
| "--filter", | ||
| `label=com.supabase.stack=${stackId}`, | ||
| "--filter", | ||
| `label=com.supabase.service=${service}`, | ||
| ]); | ||
| const ids = listed.stdout.split("\n").filter((line) => line !== ""); | ||
| expect(ids).toHaveLength(1); | ||
| const inspected = yield* containerCliOutput(spawner, [ | ||
| "container", | ||
| "inspect", | ||
| "--format", | ||
| "{{.HostConfig.Memory}} {{.HostConfig.MemorySwap}} {{.HostConfig.NanoCpus}}", | ||
| ...ids, | ||
| ]); | ||
| const [memory, memorySwap, nanoCpus] = inspected.stdout.trim().split(" ").map(Number); | ||
| return { memory, memorySwap, nanoCpus }; | ||
| }); | ||
|
|
||
| const limits = (memoryMiB: number, cpus: number) => ({ | ||
| memory: memoryMiB * mib, | ||
| memorySwap: memoryMiB * mib, | ||
| nanoCpus: cpus * 1_000_000_000, | ||
| }); | ||
|
|
||
| describe("experimental stack start container limits", () => { | ||
| it.live( | ||
| "applies configured limits to each service, keeps them across restarts, and reports edits as drift", | ||
| () => | ||
| Effect.gen(function* () { | ||
| const fs = yield* FileSystem.FileSystem; | ||
| const path = yield* Path.Path; | ||
| const root = yield* fs.makeTempDirectoryScoped({ prefix: "stack-start-limits-" }); | ||
| const configPath = path.join(root, "supabase", "config.toml"); | ||
| yield* fs.makeDirectory(path.join(root, "supabase"), { recursive: true }); | ||
| yield* fs.writeFileString( | ||
| configPath, | ||
| config('memory = "768MiB"\ncpus = 1.5', 'memory = "256MiB"\ncpus = 0.5'), | ||
| ); | ||
| const stateRoot = path.join(root, "stacks"); | ||
| const cacheRoot = path.join(root, "cache"); | ||
| yield* Effect.ensuring( | ||
| Effect.gen(function* () { | ||
| const api = yield* StackApi; | ||
| // Each CLI invocation loads config.toml in a fresh process, so each command gets fresh layers. | ||
| const start = Effect.suspend(() => stackStart(flags)).pipe( | ||
| Effect.scoped, | ||
| Effect.provide(makeLayer(root)), | ||
| ); | ||
| const drift = Effect.gen(function* () { | ||
| const out = mockOutput({ format: "json" }); | ||
| yield* stackStatus({ | ||
| stack: Option.none(), | ||
| stackId: Option.none(), | ||
| env: false, | ||
| overrideName: [], | ||
| }).pipe(Effect.scoped, Effect.provide(makeLayer(root, out.layer))); | ||
| return out.messages.find((message) => message.type === "success")?.data; | ||
| }); | ||
| const stop = (id: string) => | ||
| api.open({ id, stateRoot, cacheRoot }).pipe( | ||
| Effect.flatMap((stack) => stack.stop), | ||
| Effect.scoped, | ||
| ); | ||
|
|
||
| const stackId = yield* start; | ||
| expect(yield* serviceLimits(stackId, "database")).toEqual(limits(768, 1.5)); | ||
| expect(yield* serviceLimits(stackId, "auth")).toEqual(limits(256, 0.5)); | ||
|
|
||
| yield* stop(stackId); | ||
| expect(yield* start).toBe(stackId); | ||
| expect(yield* serviceLimits(stackId, "database")).toEqual(limits(768, 1.5)); | ||
| expect(yield* serviceLimits(stackId, "auth")).toEqual(limits(256, 0.5)); | ||
|
|
||
| yield* fs.writeFileString( | ||
| configPath, | ||
| config('memory = "1GiB"\ncpus = 2', 'memory = "512MiB"\ncpus = 1'), | ||
| ); | ||
| expect(yield* drift).toMatchObject({ | ||
| config_drift: { | ||
| status: "changed", | ||
| paths: expect.arrayContaining([ | ||
| "services.database.container.memoryBytes", | ||
| "services.database.container.cpus", | ||
| "services.auth.container.memoryBytes", | ||
| "services.auth.container.cpus", | ||
| ]), | ||
| }, | ||
| }); | ||
|
|
||
| yield* stop(stackId); | ||
| expect(yield* start).toBe(stackId); | ||
| expect(yield* serviceLimits(stackId, "database")).toEqual(limits(1024, 2)); | ||
| expect(yield* serviceLimits(stackId, "auth")).toEqual(limits(512, 1)); | ||
| expect(yield* drift).toMatchObject({ config_drift: { status: "unchanged" } }); | ||
| }), | ||
| Effect.gen(function* () { | ||
| const api = yield* StackApi; | ||
| yield* destroyTestStacks(api, stateRoot, cacheRoot); | ||
| }), | ||
| ).pipe(Effect.provide(makeLayer(root))); | ||
| }).pipe(Effect.provide([BunServices.layer, BunCrypto.layer])), | ||
| { timeout: 300_000 }, | ||
| ); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe for later: this hint only shows in legacy
functions serve, which ignores the table, and a stack service over its limit just exits with 137 and no hintwhat if legacy kept its old hint and the
memoryone went on that stack error?