-
Notifications
You must be signed in to change notification settings - Fork 181
Update sandbox metadata #956
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
base: main
Are you sure you want to change the base?
Changes from 6 commits
195acc5
c651166
29d13bc
50a276d
eef6e6f
c96040c
120e7f8
acc1f8c
b59ec86
254bab9
a741dd1
9d463b2
747a01d
ea01389
6af9e3f
ce4d8f5
0afff6d
f8bbc7c
5b25337
7bf56be
d4a2b22
19727e8
e352556
7a12801
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Large diffs are not rendered by default.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package handlers | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
"go.uber.org/zap" | ||
|
||
"github.com/e2b-dev/infra/packages/api/internal/api" | ||
"github.com/e2b-dev/infra/packages/api/internal/utils" | ||
"github.com/e2b-dev/infra/packages/db/queries" | ||
"github.com/e2b-dev/infra/packages/db/types" | ||
"github.com/e2b-dev/infra/packages/shared/pkg/logger" | ||
"github.com/e2b-dev/infra/packages/shared/pkg/telemetry" | ||
) | ||
|
||
func (a *APIStore) PutSandboxesSandboxIDMetadata( | ||
c *gin.Context, | ||
sandboxID api.SandboxID, | ||
) { | ||
ctx := c.Request.Context() | ||
sandboxID = utils.ShortID(sandboxID) | ||
|
||
// Get team from context | ||
teamID := a.GetTeamInfo(c).Team.ID | ||
|
||
metadata, err := utils.ParseBody[api.PutSandboxesSandboxIDMetadataJSONRequestBody](ctx, c) | ||
if err != nil { | ||
a.sendAPIStoreError(c, http.StatusBadRequest, fmt.Sprintf("Error when parsing request: %s", err)) | ||
|
||
telemetry.ReportCriticalError(ctx, "error when parsing request", err) | ||
|
||
return | ||
} | ||
|
||
telemetry.SetAttributes(ctx, | ||
telemetry.WithSandboxID(sandboxID), | ||
telemetry.WithTeamID(teamID.String()), | ||
) | ||
|
||
sbx, err := a.orchestrator.GetSandbox(sandboxID) | ||
if err == nil { | ||
// Verify the sandbox belongs to the team | ||
if sbx.TeamID != teamID { | ||
telemetry.ReportCriticalError(ctx, fmt.Sprintf("sandbox '%s' doesn't belong to team '%s'", sandboxID, teamID.String()), nil) | ||
a.sendAPIStoreError(c, http.StatusUnauthorized, fmt.Sprintf("Error updating sandbox - sandbox '%s' does not belong to your team '%s'", sandboxID, teamID.String())) | ||
|
||
return | ||
} | ||
|
||
sbx.Lock() | ||
defer sbx.Unlock() | ||
jakubno marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
apiErr := a.orchestrator.UpdateSandboxMetadata(ctx, sbx, metadata) | ||
if apiErr != nil { | ||
telemetry.ReportError(ctx, "error when updating sandbox metadata", apiErr.Err) | ||
a.sendAPIStoreError(c, apiErr.Code, apiErr.ClientMsg) | ||
|
||
return | ||
} | ||
} else { | ||
// Sandbox not found in cache, might be paused | ||
// Try to update the snapshot metadata in the database | ||
zap.L().Debug("Sandbox not found in cache, checking if it's paused", | ||
logger.WithSandboxID(sandboxID), | ||
zap.String("team.id", teamID.String())) | ||
dobrac marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
updated, err := a.sqlcDB.UpdateSnapshotMetadata(ctx, queries.UpdateSnapshotMetadataParams{ | ||
SandboxID: sandboxID, | ||
TeamID: teamID, | ||
Metadata: types.JSONBStringMap(metadata), | ||
}) | ||
if err != nil { | ||
telemetry.ReportCriticalError(ctx, "error when updating paused sandbox metadata", err) | ||
a.sendAPIStoreError(c, http.StatusInternalServerError, fmt.Sprintf("Error when updating paused sandbox metadata: %s", err)) | ||
|
||
return | ||
} | ||
|
||
if len(updated) == 0 { | ||
telemetry.ReportCriticalError(ctx, "sandbox not found", nil) | ||
a.sendAPIStoreError(c, http.StatusNotFound, fmt.Sprintf("Sandbox '%s' not found", sandboxID)) | ||
|
||
return | ||
} | ||
|
||
telemetry.ReportEvent(ctx, "Updated paused sandbox metadata") | ||
} | ||
|
||
c.Status(http.StatusOK) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package orchestrator | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
|
||
"github.com/e2b-dev/infra/packages/api/internal/api" | ||
"github.com/e2b-dev/infra/packages/api/internal/cache/instance" | ||
"github.com/e2b-dev/infra/packages/api/internal/utils" | ||
"github.com/e2b-dev/infra/packages/shared/pkg/grpc/orchestrator" | ||
"github.com/e2b-dev/infra/packages/shared/pkg/telemetry" | ||
) | ||
|
||
func (o *Orchestrator) UpdateSandboxMetadata( | ||
ctx context.Context, | ||
sbx *instance.InstanceInfo, | ||
metadata map[string]string, | ||
) *api.APIError { | ||
Comment on lines
+14
to
+18
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. is there a missing lock for concurrent metadata updates? e.g. first request succeeds second on the orchestrator, but second request sets the metadata on the API -> inconsistency 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. I refactor how we lock, now if there's some handler or background process working with sandbox it should always acquire lock |
||
childCtx, childSpan := o.tracer.Start(ctx, "update-sandbox-metadata") | ||
defer childSpan.End() | ||
|
||
client, childCtx, err := o.GetClient(childCtx, sbx.ClusterID, sbx.NodeID) | ||
if err != nil { | ||
return &api.APIError{ | ||
Err: err, | ||
ClientMsg: "Failed to connect to sandbox node", | ||
Code: http.StatusInternalServerError, | ||
} | ||
} | ||
|
||
// Replace metadata completely instead of merging | ||
// Send metadata to the orchestrator first - don't update local cache until success | ||
_, err = client.Sandbox.Update( | ||
childCtx, &orchestrator.SandboxUpdateRequest{ | ||
SandboxId: sbx.SandboxID, | ||
Metadata: metadata, | ||
}, | ||
) | ||
|
||
err = utils.UnwrapGRPCError(err) | ||
if err != nil { | ||
return &api.APIError{ | ||
Err: err, | ||
ClientMsg: "Failed to update sandbox metadata", | ||
Code: http.StatusInternalServerError, | ||
} | ||
} | ||
|
||
// Only update local cache after orchestrator call succeeds | ||
sbx.Metadata = metadata | ||
dobrac marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
telemetry.ReportEvent(childCtx, "Updated running sandbox metadata") | ||
|
||
return nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
-- name: UpdateSnapshotMetadata :many | ||
UPDATE "public"."snapshots" s | ||
SET metadata = $3 | ||
FROM "public"."envs" e | ||
WHERE s.env_id = e.id | ||
AND s.sandbox_id = $1 | ||
AND e.team_id = $2 RETURNING s.id; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.