-
Notifications
You must be signed in to change notification settings - Fork 275
Expand file tree
/
Copy pathpause_instance.go
More file actions
178 lines (145 loc) · 5.66 KB
/
pause_instance.go
File metadata and controls
178 lines (145 loc) · 5.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package orchestrator
import (
"context"
"errors"
"fmt"
"time"
"github.com/gogo/status"
"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgtype"
"google.golang.org/grpc/codes"
"github.com/e2b-dev/infra/packages/api/internal/orchestrator/nodemanager"
"github.com/e2b-dev/infra/packages/api/internal/sandbox"
"github.com/e2b-dev/infra/packages/db/pkg/dberrors"
"github.com/e2b-dev/infra/packages/db/pkg/types"
"github.com/e2b-dev/infra/packages/db/queries"
"github.com/e2b-dev/infra/packages/shared/pkg/grpc/orchestrator"
"github.com/e2b-dev/infra/packages/shared/pkg/id"
"github.com/e2b-dev/infra/packages/shared/pkg/telemetry"
"github.com/e2b-dev/infra/packages/shared/pkg/utils"
)
type PauseQueueExhaustedError struct{}
func (PauseQueueExhaustedError) Error() string {
return "The pause queue is exhausted"
}
// BaseTemplateDeletedError is returned when a snapshot cannot be created because
// the base template was deleted between sandbox creation and pause.
type BaseTemplateDeletedError struct {
BaseTemplateID string
}
func (e BaseTemplateDeletedError) Error() string {
return fmt.Sprintf("base template '%s' was deleted, cannot create snapshot", e.BaseTemplateID)
}
func (o *Orchestrator) pauseSandbox(ctx context.Context, node *nodemanager.Node, sbx sandbox.Sandbox) error {
ctx, span := tracer.Start(ctx, "pause-sandbox")
defer span.End()
result, err := o.throttledUpsertSnapshot(ctx, buildUpsertSnapshotParams(sbx, node))
if err != nil {
// Check if the error is an FK violation on base_env_id, which means
// the base template was deleted between sandbox creation and pause.
if dberrors.IsForeignKeyViolation(err) {
telemetry.ReportError(ctx, "base template was deleted, cannot create snapshot",
err, telemetry.WithTemplateID(sbx.BaseTemplateID))
return BaseTemplateDeletedError{BaseTemplateID: sbx.BaseTemplateID}
}
telemetry.ReportCriticalError(ctx, "error inserting snapshot for env", err)
return err
}
err = snapshotInstance(ctx, node, sbx, result.TemplateID, result.BuildID.String())
if errors.Is(err, PauseQueueExhaustedError{}) {
telemetry.ReportCriticalError(ctx, "pause queue exhausted", err)
return PauseQueueExhaustedError{}
}
if err != nil && !errors.Is(err, PauseQueueExhaustedError{}) {
telemetry.ReportCriticalError(ctx, "error pausing sandbox", err)
return fmt.Errorf("error pausing sandbox: %w", err)
}
now := time.Now()
err = o.sqlcDB.UpdateEnvBuildStatus(ctx, queries.UpdateEnvBuildStatusParams{
Status: types.BuildStatusSuccess,
FinishedAt: &now,
Reason: types.BuildReason{},
BuildID: result.BuildID,
})
if err != nil {
telemetry.ReportCriticalError(ctx, "error pausing sandbox", err)
return fmt.Errorf("error pausing sandbox: %w", err)
}
o.snapshotCache.Invalidate(context.WithoutCancel(ctx), sbx.SandboxID)
return nil
}
func snapshotInstance(ctx context.Context, node *nodemanager.Node, sbx sandbox.Sandbox, templateID, buildID string) error {
childCtx, childSpan := tracer.Start(ctx, "snapshot-instance")
defer childSpan.End()
client, childCtx := node.GetSandboxDeleteCtx(childCtx, sbx.SandboxID, sbx.ExecutionID)
_, err := client.Sandbox.Pause(
childCtx, &orchestrator.SandboxPauseRequest{
SandboxId: sbx.SandboxID,
TemplateId: templateID,
BuildId: buildID,
},
)
if err == nil {
telemetry.ReportEvent(ctx, "Paused sandbox")
return nil
}
st, ok := status.FromError(err)
if !ok {
return err
}
if st.Code() == codes.ResourceExhausted {
return PauseQueueExhaustedError{}
}
return fmt.Errorf("failed to pause sandbox '%s': %w", sbx.SandboxID, err)
}
func (o *Orchestrator) WaitForStateChange(ctx context.Context, teamID uuid.UUID, sandboxID string) error {
return o.sandboxStore.WaitForStateChange(ctx, teamID, sandboxID)
}
func buildUpsertSnapshotParams(sbx sandbox.Sandbox, node *nodemanager.Node) queries.UpsertSnapshotParams {
machineInfo := node.MachineInfo()
metadata := types.JSONBStringMap(sbx.Metadata)
if metadata == nil {
metadata = types.JSONBStringMap{}
}
return queries.UpsertSnapshotParams{
// Used if there's no snapshot for this sandbox yet
TemplateID: id.Generate(),
TeamID: sbx.TeamID,
BaseTemplateID: sbx.BaseTemplateID,
SandboxID: sbx.SandboxID,
StartedAt: pgtype.Timestamptz{Time: sbx.StartTime, Valid: true},
Vcpu: sbx.VCpu,
RamMb: sbx.RamMB,
// We don't know this information
FreeDiskSizeMb: 0,
TotalDiskSizeMb: &sbx.TotalDiskSizeMB,
Metadata: metadata,
KernelVersion: sbx.KernelVersion,
FirecrackerVersion: sbx.FirecrackerVersion,
EnvdVersion: &sbx.EnvdVersion,
Secure: sbx.EnvdAccessToken != nil,
AllowInternetAccess: sbx.AllowInternetAccess,
AutoPause: sbx.AutoPause,
Config: &types.PausedSandboxConfig{
Version: types.PausedSandboxConfigVersion,
Network: sbx.Network,
AutoResume: sbx.AutoResume,
VolumeMounts: sbx.VolumeMounts,
},
OriginNodeID: node.ID,
Status: types.BuildStatusSnapshotting,
CpuArchitecture: utils.ToPtr(machineInfo.CPUArchitecture),
CpuFamily: utils.ToPtr(machineInfo.CPUFamily),
CpuModel: utils.ToPtr(machineInfo.CPUModel),
CpuModelName: utils.ToPtr(machineInfo.CPUModelName),
CpuFlags: machineInfo.CPUFlags,
}
}
// throttledUpsertSnapshot runs UpsertSnapshot gated by the snapshot upsert semaphore.
func (o *Orchestrator) throttledUpsertSnapshot(ctx context.Context, params queries.UpsertSnapshotParams) (queries.UpsertSnapshotRow, error) {
if err := o.snapshotUpsertSem.Acquire(ctx, 1); err != nil {
return queries.UpsertSnapshotRow{}, err
}
defer o.snapshotUpsertSem.Release(1)
return o.sqlcDB.UpsertSnapshot(ctx, params)
}