-
Notifications
You must be signed in to change notification settings - Fork 120
Persist sandbox information locally in orchestrator #376
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
Closed
tychoish
wants to merge
16
commits into
e2b-dev:main
from
tychoish:track-all-the-data-currently-cached-by-the-api-server-about-e2b-1394
Closed
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
6d3fa6a
feat: persist sandboxes locally in orchestrator
tychoish f554858
Merge remote-tracking branch 'origin/main' into track-all-the-data-cu…
tychoish 07426af
chore: switch to sqlc
tychoish ac9cedc
feat: track state machine for of orchestrator status
tychoish d17e5d4
fix: remove unneeded imports
tychoish bf4e94d
Merge remote-tracking branch 'origin/main' into track-all-the-data-cu…
tychoish d42f094
Merge remote-tracking branch 'origin/main' into track-all-the-data-cu…
tychoish f554c70
fix compiles and add sandbox configs to orchestrator database
tychoish 0aade1e
code review feedback
tychoish 0e1ea88
Merge remote-tracking branch 'origin/main' into track-all-the-data-cu…
tychoish a7088de
clean up checks
tychoish 49759a4
Update packages/orchestrator/Makefile
tychoish 2bde94c
Merge remote-tracking branch 'origin/main' into track-all-the-data-cu…
tychoish 608adc6
feat: process should initialize the database schema
tychoish 3727a6c
orchestrator status report and test
tychoish 05d6f45
Merge remote-tracking branch 'origin/main' into track-all-the-data-cu…
tychoish 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
package db | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"errors" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/e2b-dev/infra/packages/orchestrator/internal/pkg/database" | ||
) | ||
|
||
type DB struct { | ||
client *sql.DB | ||
ops *database.Queries | ||
} | ||
|
||
func New(ctx context.Context, client *sql.DB, schema string) (*DB, error) { | ||
db := &DB{client: client, ops: database.New(client)} | ||
|
||
if _, err := db.client.ExecContext(ctx, schema); err != nil { | ||
return nil, fmt.Errorf("problem initializing the database: %w", err) | ||
} | ||
|
||
if err := db.ops.SetOrchestratorStatusRunning(ctx); err != nil { | ||
return nil, err | ||
} | ||
|
||
return db, nil | ||
} | ||
|
||
func (db *DB) Close(ctx context.Context) error { | ||
return errors.Join(db.ops.SetOrchestratorStatusTerminated(ctx), db.client.Close()) | ||
} | ||
|
||
func (db *DB) Status(ctx context.Context) (*database.OrchestratorStatusRow, error) { | ||
report, err := db.ops.OrchestratorStatus(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
out := report | ||
|
||
return &out, nil | ||
} | ||
|
||
func (db *DB) CreateSandbox(ctx context.Context, params database.CreateSandboxParams) error { | ||
return db.WithTx(ctx, func(ctx context.Context, op *database.Queries) error { | ||
if _, err := op.IncGlobalVersion(ctx); err != nil { | ||
return err | ||
} | ||
|
||
if err := op.CreateSandbox(ctx, params); err != nil { | ||
return err | ||
} | ||
return nil | ||
}) | ||
} | ||
|
||
func (db *DB) UpdateSandboxDeadline(ctx context.Context, id string, deadline time.Time) error { | ||
return db.WithTx(ctx, func(ctx context.Context, op *database.Queries) error { | ||
if _, err := op.IncGlobalVersion(ctx); err != nil { | ||
return err | ||
} | ||
|
||
if err := op.UpdateSandboxDeadline(ctx, database.UpdateSandboxDeadlineParams{ID: id, Deadline: deadline}); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
}) | ||
} | ||
|
||
func (db *DB) SetSandboxTerminated(ctx context.Context, id string, duration time.Duration) error { | ||
return db.WithTx(ctx, func(ctx context.Context, op *database.Queries) error { | ||
if _, err := op.IncGlobalVersion(ctx); err != nil { | ||
return err | ||
} | ||
|
||
if err := op.ShutdownSandbox(ctx, database.ShutdownSandboxParams{ | ||
ID: id, | ||
DurationMs: duration.Milliseconds(), | ||
Status: database.SandboxStatusTerminated, | ||
}); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
}) | ||
} | ||
|
||
func (db *DB) SetSandboxPaused(ctx context.Context, id string, duration time.Duration) error { | ||
return db.WithTx(ctx, func(ctx context.Context, op *database.Queries) error { | ||
if _, err := op.IncGlobalVersion(ctx); err != nil { | ||
return err | ||
} | ||
|
||
if err := op.ShutdownSandbox(ctx, database.ShutdownSandboxParams{ | ||
ID: id, | ||
DurationMs: duration.Milliseconds(), | ||
Status: database.SandboxStatusPaused, | ||
}); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
}) | ||
} | ||
|
||
func (db *DB) WithTx(ctx context.Context, op func(context.Context, *database.Queries) error) (err error) { | ||
tx, err := db.client.BeginTx(ctx, nil) | ||
if err != nil { | ||
return err | ||
} | ||
defer func() { | ||
if err != nil { | ||
err = errors.Join(err, tx.Rollback()) | ||
} | ||
}() | ||
defer func() { | ||
if p := recover(); p != nil { | ||
tx.Rollback() | ||
panic(p) | ||
} | ||
}() | ||
defer func() { | ||
if err == nil { | ||
err = tx.Commit() | ||
} | ||
}() | ||
|
||
return op(ctx, db.ops.WithTx(tx)) | ||
} |
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,76 @@ | ||
-- name: GlobalVersion :one | ||
SELECT version FROM status WHERE id = 1; | ||
|
||
-- name: SetOrchestratorStatusRunning :exec | ||
UPDATE status | ||
SET | ||
version = version + 1, | ||
updated_at = current_timestamp, | ||
status = 'running' | ||
WHERE | ||
id = 1 AND status = 'initializing'; | ||
|
||
-- name: SetOrchestratorStatusTerminated :exec | ||
UPDATE status | ||
SET | ||
version = version + 1, | ||
updated_at = current_timestamp, | ||
status = 'terminated' | ||
WHERE | ||
id = 1 AND status != 'terminated'; | ||
|
||
-- name: IncGlobalVersion :one | ||
UPDATE status | ||
SET | ||
version = version + 1, | ||
updated_at = current_timestamp | ||
WHERE | ||
id = 1 AND status != 'terminated' | ||
RETURNING version; | ||
|
||
-- name: CreateSandbox :exec | ||
INSERT INTO sandboxes(id, status, started_at, deadline, config, global_version) | ||
VALUES ( | ||
sqlc.arg('id'), | ||
sqlc.arg('status'), | ||
sqlc.arg('started_at'), | ||
sqlc.arg('deadline'), | ||
sqlc.arg('config'), | ||
(SELECT version FROM status WHERE status.id = 1) | ||
); | ||
|
||
-- name: ShutdownSandbox :exec | ||
UPDATE sandboxes | ||
SET | ||
version = version + 1, | ||
global_version = (SELECT version FROM status WHERE status.id = 1), | ||
updated_at = current_timestamp, | ||
duration_ms = sqlc.arg('duration_ms'), | ||
status = sqlc.arg('status') | ||
WHERE | ||
sandboxes.id = sqlc.arg('id'); | ||
|
||
-- name: UpdateSandboxDeadline :exec | ||
UPDATE sandboxes | ||
SET | ||
version = version + 1, | ||
global_version = (SELECT version FROM status WHERE status.id = 1), | ||
udpated_at = current_timestamp, | ||
deadline = sqlc.arg('deadline'), | ||
status = 'running' | ||
WHERE | ||
sandboxes.id = sqlc.arg('id'); | ||
|
||
-- name: OrchestratorStatus :one | ||
SELECT | ||
status.version AS global_version, | ||
(SELECT count(*) FROM sandboxes) AS num_sandboxes, | ||
(SELECT count(*) FROM sandboxes WHERE status = 'pending') AS pending_sandboxes, | ||
(SELECT count(*) FROM sandboxes WHERE status = 'terminated') AS terminated_sandboxes, | ||
(SELECT count(*) FROM sandboxes WHERE status = 'running') AS running_sandboxes, | ||
(SELECT min(started_at) FROM sandboxes WHERE status = 'running') AS earliest_running_sandbox_started_at, | ||
(SELECT max(updated_at) FROM sandboxes WHERE status = 'running') AS most_recent_running_sandbox_updated_at, | ||
status.updated_at, | ||
status.status | ||
FROM | ||
status; |
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,28 @@ | ||
PRAGMA integrity_check; | ||
PRAGMA foreign_keys = ON; | ||
|
||
CREATE TABLE sandboxes ( | ||
id TEXT PRIMARY KEY NOT NULL, | ||
started_at TIMESTAMP NOT NULL DEFAULT current_timestamp, | ||
updated_at TIMESTAMP NOT NULL DEFAULT current_timestamp, | ||
deadline TIMESTAMP NOT NULL, | ||
status TEXT CHECK( status IN ('pending', 'running', 'paused', 'terminated')) | ||
NOT NULL DEFAULT 'pending', | ||
duration_ms INTEGER CHECK( duration_ms >= 0 ) | ||
NOT NULL DEFAULT 0, | ||
version INTEGER CHECK( version >= 0 ) | ||
NOT NULL DEFAULT 0, | ||
global_version INTEGER CHECK( global_version >= 0 ) | ||
NOT NULL, | ||
config BLOB | ||
); | ||
|
||
CREATE TABLE status ( | ||
id INTEGER PRIMARY KEY NOT NULL, | ||
version INTEGER NOT NULL DEFAULT 0, | ||
updated_at TIMESTAMP NOT NULL DEFAULT current_timestamp, | ||
status TEXT CHECK( status IN ('initializing', 'running', 'draining', 'quarantined ', 'terminated')) | ||
NOT NULL DEFAULT 'initializing' | ||
); | ||
|
||
INSERT INTO status(id) VALUES(1); |
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,14 @@ | ||
package database | ||
|
||
const ( | ||
SandboxStatusPending = "pending" | ||
SandboxStatusRunning = "running" | ||
SandboxStatusPaused = "paused" | ||
SandboxStatusTerminated = "terminated" | ||
|
||
OrchestratorStatusInitializing = "initializing" | ||
OrchestratorStatusRunning = "running" | ||
OrchestratorStatusDraining = "draining" | ||
OrchestratorStatusTerminated = "terminated" | ||
OrchestratorStatusQuarantined = "quarantined" | ||
) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.