Skip to content

Commit f8e5216

Browse files
committed
Merge branch 'main' of github.com:e2b-dev/infra into lev-block-cache-bitmap
2 parents b4b62cc + ba3d012 commit f8e5216

File tree

7 files changed

+368
-296
lines changed

7 files changed

+368
-296
lines changed

packages/api/internal/handlers/proxy_grpc.go

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,7 @@ func (s *SandboxService) ResumeSandbox(ctx context.Context, req *proxygrpc.Sandb
116116
return nil, status.Error(codes.InvalidArgument, "invalid sandbox ID")
117117
}
118118

119-
var autoResume *dbtypes.SandboxAutoResumeConfig
120-
snap, _, err := s.getAutoResumeSnapshot(ctx, sandboxID)
119+
snap, autoResume, err := s.getAutoResumeSnapshot(ctx, sandboxID)
121120
if err != nil {
122121
return nil, err
123122
}
@@ -155,15 +154,6 @@ func (s *SandboxService) ResumeSandbox(ctx context.Context, req *proxygrpc.Sandb
155154
}
156155
}
157156

158-
// Reload snapshot metadata after orchestrator checks so we do not resume from stale
159-
// pre-pause snapshot data.
160-
snap, autoResume, err = s.getAutoResumeSnapshot(ctx, sandboxID)
161-
if err != nil {
162-
return nil, err
163-
}
164-
165-
teamID = snap.Snapshot.TeamID
166-
167157
team, err := dbapi.GetTeamByID(ctx, s.api.authDB, teamID)
168158
if err != nil {
169159
return nil, status.Errorf(codes.Internal, "failed to get team: %v", err)
@@ -172,15 +162,7 @@ func (s *SandboxService) ResumeSandbox(ctx context.Context, req *proxygrpc.Sandb
172162

173163
timeout := calculateAutoResumeTimeout(autoResume, minAutoResumeTimeout, team)
174164

175-
autoPause := snap.Snapshot.AutoPause
176-
nodeID := &snap.Snapshot.OriginNodeID
177-
178-
alias := ""
179-
if len(snap.Aliases) > 0 {
180-
alias = snap.Aliases[0]
181-
}
182-
183-
var envdAccessToken *string = nil
165+
var envdAccessToken *string
184166
if snap.Snapshot.EnvSecure {
185167
accessToken, tokenErr := s.api.getEnvdAccessToken(snap.EnvBuild.EnvdVersion, sandboxID)
186168
if tokenErr != nil {
@@ -228,25 +210,13 @@ func (s *SandboxService) ResumeSandbox(ctx context.Context, req *proxygrpc.Sandb
228210
headers := http.Header{}
229211
sbx, apiErr := s.api.startSandboxInternal(
230212
ctx,
231-
snap.Snapshot.SandboxID,
213+
sandboxID,
232214
timeout,
233-
nil,
234-
snap.Snapshot.Metadata,
235-
alias,
236215
team,
237-
snap.EnvBuild,
216+
s.api.buildResumeSandboxData(sandboxID, nil),
238217
&headers,
239218
true,
240-
nodeID,
241-
snap.Snapshot.EnvID,
242-
snap.Snapshot.BaseEnvID,
243-
autoPause,
244-
autoResume,
245-
envdAccessToken,
246-
snap.Snapshot.AllowInternetAccess,
247-
network,
248219
nil, // mcp
249-
nil, // volumeMounts
250220
)
251221
if apiErr != nil {
252222
return nil, status.Error(sharedutils.GRPCCodeFromHTTPStatus(apiErr.Code), apiErr.ClientMsg)

packages/api/internal/handlers/sandbox.go

Lines changed: 17 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,9 @@ import (
1414

1515
"github.com/e2b-dev/infra/packages/api/internal/api"
1616
"github.com/e2b-dev/infra/packages/api/internal/middleware/otel/tracing"
17+
"github.com/e2b-dev/infra/packages/api/internal/orchestrator"
1718
"github.com/e2b-dev/infra/packages/api/internal/sandbox"
1819
typesteam "github.com/e2b-dev/infra/packages/auth/pkg/types"
19-
"github.com/e2b-dev/infra/packages/db/pkg/types"
20-
"github.com/e2b-dev/infra/packages/db/queries"
21-
"github.com/e2b-dev/infra/packages/shared/pkg/grpc/orchestrator"
2220
sbxlogger "github.com/e2b-dev/infra/packages/shared/pkg/logger/sandbox"
2321
"github.com/e2b-dev/infra/packages/shared/pkg/telemetry"
2422
)
@@ -29,45 +27,21 @@ func (a *APIStore) startSandbox(
2927
ctx context.Context,
3028
sandboxID string,
3129
timeout time.Duration,
32-
envVars map[string]string,
33-
metadata map[string]string,
34-
alias string,
3530
team *typesteam.Team,
36-
build queries.EnvBuild,
31+
getSandboxData orchestrator.SandboxDataFetcher,
3732
requestHeader *http.Header,
3833
isResume bool,
39-
nodeID *string,
40-
templateID string,
41-
baseTemplateID string,
42-
autoPause bool,
43-
autoResume *types.SandboxAutoResumeConfig,
44-
envdAccessToken *string,
45-
allowInternetAccess *bool,
46-
network *types.SandboxNetworkConfig,
4734
mcp api.Mcp,
48-
volumeMounts []*orchestrator.SandboxVolumeMount,
4935
) (*api.Sandbox, *api.APIError) {
5036
sbx, apiErr := a.startSandboxInternal(
5137
ctx,
5238
sandboxID,
5339
timeout,
54-
envVars,
55-
metadata,
56-
alias,
5740
team,
58-
build,
41+
getSandboxData,
5942
requestHeader,
6043
isResume,
61-
nodeID,
62-
templateID,
63-
baseTemplateID,
64-
autoPause,
65-
autoResume,
66-
envdAccessToken,
67-
allowInternetAccess,
68-
network,
6944
mcp,
70-
volumeMounts,
7145
)
7246
if apiErr != nil {
7347
return nil, apiErr
@@ -81,56 +55,27 @@ func (a *APIStore) startSandboxInternal(
8155
ctx context.Context,
8256
sandboxID string,
8357
timeout time.Duration,
84-
envVars map[string]string,
85-
metadata map[string]string,
86-
alias string,
8758
team *typesteam.Team,
88-
build queries.EnvBuild,
59+
getSandboxData orchestrator.SandboxDataFetcher,
8960
requestHeader *http.Header,
9061
isResume bool,
91-
nodeID *string,
92-
templateID string,
93-
baseTemplateID string,
94-
autoPause bool,
95-
autoResume *types.SandboxAutoResumeConfig,
96-
envdAccessToken *string,
97-
allowInternetAccess *bool,
98-
network *types.SandboxNetworkConfig,
9962
mcp api.Mcp,
100-
volumeMounts []*orchestrator.SandboxVolumeMount,
10163
) (sandbox.Sandbox, *api.APIError) {
10264
startTime := time.Now()
10365
endTime := startTime.Add(timeout)
10466

105-
autoResumePolicy := "unset"
106-
if autoResume != nil {
107-
autoResumePolicy = string(autoResume.Policy)
108-
}
109-
11067
// Unique ID for the execution (from start/resume to stop/pause)
11168
executionID := uuid.New().String()
11269
sbx, instanceErr := a.orchestrator.CreateSandbox(
11370
ctx,
11471
sandboxID,
11572
executionID,
116-
alias,
11773
team,
118-
build,
119-
metadata,
120-
envVars,
74+
getSandboxData,
12175
startTime,
12276
endTime,
12377
timeout,
12478
isResume,
125-
nodeID,
126-
templateID,
127-
baseTemplateID,
128-
autoPause,
129-
autoResume,
130-
envdAccessToken,
131-
allowInternetAccess,
132-
network,
133-
volumeMounts,
13479
)
13580
if instanceErr != nil {
13681
telemetry.ReportError(ctx, "error when creating instance", instanceErr.Err)
@@ -146,15 +91,15 @@ func (a *APIStore) startSandboxInternal(
14691
props := properties.
14792
Set("environment", sbx.TemplateID).
14893
Set("instance_id", sbx.SandboxID).
149-
Set("alias", alias).
94+
Set("alias", sbx.Alias).
15095
Set("resume", isResume).
15196
Set("build_id", sbx.BuildID).
152-
Set("envd_version", build.EnvdVersion).
97+
Set("envd_version", sbx.EnvdVersion).
15398
Set("node_id", sbx.NodeID).
15499
Set("vcpu", sbx.VCpu).
155100
Set("ram_mb", sbx.RamMB).
156101
Set("total_disk_size_mb", sbx.TotalDiskSizeMB).
157-
Set("auto_pause", autoPause)
102+
Set("auto_pause", sbx.AutoPause)
158103

159104
// Calculate the time it took for the sandbox to start from request receipt
160105
if requestStartTime, ok := tracing.GetRequestStartTime(ctx); ok {
@@ -171,7 +116,7 @@ func (a *APIStore) startSandboxInternal(
171116
telemetry.ReportEvent(ctx, "Created analytics event")
172117

173118
go func() {
174-
a.templateSpawnCounter.IncreaseTemplateSpawnCount(baseTemplateID, time.Now())
119+
a.templateSpawnCounter.IncreaseTemplateSpawnCount(sbx.BaseTemplateID, time.Now())
175120
}()
176121

177122
telemetry.SetAttributes(ctx,
@@ -184,13 +129,19 @@ func (a *APIStore) startSandboxInternal(
184129
TeamID: team.ID.String(),
185130
}
186131
sbxlogger.E(logMetadata).Info(ctx, "Sandbox created", zap.String("end_time", endTime.Format("2006-01-02 15:04:05 -07:00")))
132+
133+
autoResumePolicy := "unset"
134+
if sbx.AutoResume != nil {
135+
autoResumePolicy = string(sbx.AutoResume.Policy)
136+
}
137+
187138
sbxlogger.I(logMetadata).Info(
188139
ctx,
189140
"Sandbox created details",
190141
zap.String("end_time", endTime.Format("2006-01-02 15:04:05 -07:00")),
191142
zap.String("auto_resume_policy", autoResumePolicy),
192-
zap.Bool("auto_pause", autoPause),
193-
zap.String("parent_template_id", baseTemplateID),
143+
zap.Bool("auto_pause", sbx.AutoPause),
144+
zap.String("parent_template_id", sbx.BaseTemplateID),
194145
)
195146

196147
return sbx, nil

packages/api/internal/handlers/sandbox_connect.go

Lines changed: 3 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"github.com/e2b-dev/infra/packages/api/internal/sandbox"
1616
"github.com/e2b-dev/infra/packages/api/internal/utils"
1717
"github.com/e2b-dev/infra/packages/auth/pkg/auth"
18-
"github.com/e2b-dev/infra/packages/db/pkg/types"
1918
"github.com/e2b-dev/infra/packages/shared/pkg/ginutils"
2019
"github.com/e2b-dev/infra/packages/shared/pkg/logger"
2120
sbxlogger "github.com/e2b-dev/infra/packages/shared/pkg/logger/sandbox"
@@ -139,76 +138,21 @@ func (a *APIStore) PostSandboxesSandboxIDConnect(c *gin.Context, sandboxID api.S
139138
return
140139
}
141140

142-
autoPause := lastSnapshot.Snapshot.AutoPause
143-
snap := lastSnapshot.Snapshot
144-
build := lastSnapshot.EnvBuild
145-
146-
nodeID := &snap.OriginNodeID
147-
148-
alias := ""
149-
if len(lastSnapshot.Aliases) > 0 {
150-
alias = lastSnapshot.Aliases[0]
151-
}
152-
153141
sbxlogger.E(&sbxlogger.SandboxMetadata{
154142
SandboxID: sandboxID,
155-
TemplateID: snap.EnvID,
143+
TemplateID: lastSnapshot.Snapshot.EnvID,
156144
TeamID: teamID.String(),
157145
}).Debug(ctx, "Started resuming sandbox")
158146

159-
var envdAccessToken *string = nil
160-
if snap.EnvSecure {
161-
accessToken, tokenErr := a.getEnvdAccessToken(build.EnvdVersion, sandboxID)
162-
if tokenErr != nil {
163-
telemetry.ReportErrorByCode(ctx, tokenErr.Code, "Secure envd access token error", tokenErr.Err,
164-
telemetry.WithTemplateID(snap.EnvID),
165-
telemetry.WithBuildID(build.ID.String()),
166-
telemetry.WithSandboxID(sandboxID),
167-
telemetry.WithTeamID(teamID.String()),
168-
)
169-
a.sendAPIStoreError(c, tokenErr.Code, tokenErr.ClientMsg)
170-
171-
return
172-
}
173-
174-
envdAccessToken = &accessToken
175-
}
176-
177-
var network *types.SandboxNetworkConfig
178-
if snap.Config != nil {
179-
network = snap.Config.Network
180-
}
181-
var autoResume *types.SandboxAutoResumeConfig
182-
if snap.Config != nil {
183-
autoResume = snap.Config.AutoResume
184-
}
185-
186-
var volumes []*types.SandboxVolumeMountConfig
187-
if snap.Config != nil {
188-
volumes = snap.Config.VolumeMounts
189-
}
190-
191147
sbx, createErr := a.startSandbox(
192148
ctx,
193-
snap.SandboxID,
149+
sandboxID,
194150
timeout,
195-
nil,
196-
snap.Metadata,
197-
alias,
198151
teamInfo,
199-
build,
152+
a.buildResumeSandboxData(sandboxID, nil),
200153
&c.Request.Header,
201154
true,
202-
nodeID,
203-
snap.EnvID,
204-
snap.BaseEnvID,
205-
autoPause,
206-
autoResume,
207-
envdAccessToken,
208-
snap.AllowInternetAccess,
209-
network,
210155
nil, // mcp
211-
convertDatabaseMountsToOrchestratorMounts(volumes), // volumes
212156
)
213157
if createErr != nil {
214158
a.sendAPIStoreError(c, createErr.Code, createErr.ClientMsg)

packages/api/internal/handlers/sandbox_create.go

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/e2b-dev/infra/packages/api/internal/api"
2323
templatecache "github.com/e2b-dev/infra/packages/api/internal/cache/templates"
2424
"github.com/e2b-dev/infra/packages/api/internal/middleware/otel/metrics"
25+
apiorch "github.com/e2b-dev/infra/packages/api/internal/orchestrator"
2526
"github.com/e2b-dev/infra/packages/api/internal/sandbox"
2627
"github.com/e2b-dev/infra/packages/auth/pkg/auth"
2728
sqlcdb "github.com/e2b-dev/infra/packages/db/client"
@@ -219,27 +220,34 @@ func (a *APIStore) PostSandboxes(c *gin.Context) {
219220
return
220221
}
221222

223+
getSandboxData := func(_ context.Context) (apiorch.SandboxMetadata, *api.APIError) {
224+
// The data can't be influenced by action on the same sandbox as other operations,
225+
// so it's safe to reuse the data
226+
return apiorch.SandboxMetadata{
227+
Metadata: metadata,
228+
EnvVars: envVars,
229+
Build: *build,
230+
AllowInternetAccess: allowInternetAccess,
231+
Network: network,
232+
Alias: alias,
233+
TemplateID: env.TemplateID,
234+
BaseTemplateID: env.TemplateID,
235+
AutoPause: autoPause,
236+
AutoResume: autoResume,
237+
VolumeMounts: sbxVolumeMounts,
238+
EnvdAccessToken: envdAccessToken,
239+
}, nil
240+
}
241+
222242
sbx, createErr := a.startSandbox(
223243
ctx,
224244
sandboxID,
225245
timeout,
226-
envVars,
227-
metadata,
228-
alias,
229246
teamInfo,
230-
*build,
247+
getSandboxData,
231248
&c.Request.Header,
232249
false,
233-
nil,
234-
env.TemplateID,
235-
env.TemplateID,
236-
autoPause,
237-
autoResume,
238-
envdAccessToken,
239-
allowInternetAccess,
240-
network,
241250
mcp,
242-
sbxVolumeMounts,
243251
)
244252
if createErr != nil {
245253
a.sendAPIStoreError(c, createErr.Code, createErr.ClientMsg)

0 commit comments

Comments
 (0)