Skip to content

Commit d6827fb

Browse files
committed
Address PR remarks
Signed-off-by: Amory Hoste <[email protected]>
1 parent c78f1cb commit d6827fb

File tree

5 files changed

+76
-49
lines changed

5 files changed

+76
-49
lines changed

cri/firecracker/coordinator.go

Lines changed: 52 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,13 @@ func withoutOrchestrator() coordinatorOption {
6262
}
6363
}
6464

65-
func newFirecrackerCoordinator(orch *ctriface.Orchestrator, snapsCapacityMiB int64, isSparseSnaps bool, isFullLocal bool, opts ...coordinatorOption) *coordinator {
65+
func newFirecrackerCoordinator(
66+
orch *ctriface.Orchestrator,
67+
snapsCapacityMiB int64,
68+
isSparseSnaps bool,
69+
isFullLocal bool,
70+
opts ...coordinatorOption) *coordinator {
71+
6672
c := &coordinator{
6773
activeInstances: make(map[string]*FuncInstance),
6874
orch: orch,
@@ -114,7 +120,7 @@ func (c *coordinator) startVM(ctx context.Context, image string, revision string
114120
func (c *coordinator) stopVM(ctx context.Context, containerID string) error {
115121
c.Lock()
116122

117-
fi, present := c.activeInstances[containerID]
123+
funcInst, present := c.activeInstances[containerID]
118124
if present {
119125
delete(c.activeInstances, containerID)
120126
}
@@ -127,28 +133,28 @@ func (c *coordinator) stopVM(ctx context.Context, containerID string) error {
127133
}
128134

129135
if c.orch == nil || ! c.orch.GetSnapshotsEnabled() {
130-
return c.orchStopVM(ctx, fi)
136+
return c.orchStopVM(ctx, funcInst)
131137
}
132138

133-
id := fi.vmID
139+
id := funcInst.vmID
134140
if c.isFullLocal {
135-
id = fi.revisionId
141+
id = funcInst.revisionId
136142
}
137143

138-
if fi.snapBooted {
144+
if funcInst.snapBooted {
139145
defer c.snapshotManager.ReleaseSnapshot(id)
140146
} else {
141147
// Create snapshot
142-
err := c.orchCreateSnapshot(ctx, fi)
148+
err := c.orchCreateSnapshot(ctx, funcInst)
143149
if err != nil {
144150
log.Printf("Err creating snapshot %s\n", err)
145151
}
146152
}
147153

148154
if c.isFullLocal {
149-
return c.orchStopVM(ctx, fi)
155+
return c.orchStopVM(ctx, funcInst)
150156
} else {
151-
return c.orchOffloadVM(ctx, fi)
157+
return c.orchOffloadVM(ctx, funcInst)
152158
}
153159
}
154160

@@ -161,18 +167,18 @@ func (c *coordinator) isActive(containerID string) bool {
161167
return ok
162168
}
163169

164-
func (c *coordinator) insertActive(containerID string, fi *FuncInstance) error {
170+
func (c *coordinator) insertActive(containerID string, funcInst *FuncInstance) error {
165171
c.Lock()
166172
defer c.Unlock()
167173

168-
logger := log.WithFields(log.Fields{"containerID": containerID, "vmID": fi.vmID})
174+
logger := log.WithFields(log.Fields{"containerID": containerID, "vmID": funcInst.vmID})
169175

170176
if fi, present := c.activeInstances[containerID]; present {
171177
logger.Errorf("entry for container already exists with vmID %s" + fi.vmID)
172178
return errors.New("entry for container already exists")
173179
}
174180

175-
c.activeInstances[containerID] = fi
181+
c.activeInstances[containerID] = funcInst
176182
return nil
177183
}
178184

@@ -206,12 +212,18 @@ func (c *coordinator) orchStartVM(ctx context.Context, image, revision string, m
206212

207213
coldStartTimeMs := metrics.ToMs(time.Since(tStartCold))
208214

209-
fi := NewFuncInstance(vmID, image, revision, resp, false, memSizeMib, vCPUCount, coldStartTimeMs)
215+
funcInst := NewFuncInstance(vmID, image, revision, resp, false, memSizeMib, vCPUCount, coldStartTimeMs)
210216
logger.Debug("successfully created fresh instance")
211-
return fi, err
217+
return funcInst, err
212218
}
213219

214-
func (c *coordinator) orchStartVMSnapshot(ctx context.Context, snap *snapshotting.Snapshot, memSizeMib, vCPUCount uint32, vmID string) (*FuncInstance, error) {
220+
func (c *coordinator) orchStartVMSnapshot(
221+
ctx context.Context,
222+
snap *snapshotting.Snapshot,
223+
memSizeMib,
224+
vCPUCount uint32,
225+
vmID string) (*FuncInstance, error) {
226+
215227
tStartCold := time.Now()
216228
logger := log.WithFields(
217229
log.Fields{
@@ -242,29 +254,35 @@ func (c *coordinator) orchStartVMSnapshot(ctx context.Context, snap *snapshottin
242254
}
243255

244256
coldStartTimeMs := metrics.ToMs(time.Since(tStartCold))
245-
fi := NewFuncInstance(vmID, snap.GetImage(), snap.GetId(), resp, true, memSizeMib, vCPUCount, coldStartTimeMs)
257+
funcInst := NewFuncInstance(vmID, snap.GetImage(), snap.GetId(), resp, true, memSizeMib, vCPUCount, coldStartTimeMs)
246258
logger.Debug("successfully loaded instance from snapshot")
247259

248-
return fi, err
260+
return funcInst, err
249261
}
250262

251-
func (c *coordinator) orchCreateSnapshot(ctx context.Context, fi *FuncInstance) error {
263+
func (c *coordinator) orchCreateSnapshot(ctx context.Context, funcInst *FuncInstance) error {
252264
logger := log.WithFields(
253265
log.Fields{
254-
"vmID": fi.vmID,
255-
"image": fi.image,
266+
"vmID": funcInst.vmID,
267+
"image": funcInst.image,
256268
},
257269
)
258270

259-
id := fi.vmID
271+
id := funcInst.vmID
260272
if c.isFullLocal {
261-
id = fi.revisionId
273+
id = funcInst.revisionId
262274
}
263275

264-
removeContainerSnaps, snap, err := c.snapshotManager.InitSnapshot(id, fi.image, fi.coldStartTimeMs, fi.memSizeMib, fi.vCPUCount, c.isSparseSnaps)
276+
removeContainerSnaps, snap, err := c.snapshotManager.InitSnapshot(
277+
id,
278+
funcInst.image,
279+
funcInst.coldStartTimeMs,
280+
funcInst.memSizeMib,
281+
funcInst.vCPUCount,
282+
c.isSparseSnaps)
265283

266284
if err != nil {
267-
fi.logger.Warn(fmt.Sprint(err))
285+
funcInst.logger.Warn(fmt.Sprint(err))
268286
return nil
269287
}
270288

@@ -281,46 +299,46 @@ func (c *coordinator) orchCreateSnapshot(ctx context.Context, fi *FuncInstance)
281299

282300
logger.Debug("creating instance snapshot before stopping")
283301

284-
err = c.orch.PauseVM(ctxTimeout, fi.vmID)
302+
err = c.orch.PauseVM(ctxTimeout, funcInst.vmID)
285303
if err != nil {
286304
logger.WithError(err).Error("failed to pause VM")
287305
return nil
288306
}
289307

290-
err = c.orch.CreateSnapshot(ctxTimeout, fi.vmID, snap, c.isFullLocal)
308+
err = c.orch.CreateSnapshot(ctxTimeout, funcInst.vmID, snap, c.isFullLocal)
291309
if err != nil {
292-
fi.logger.WithError(err).Error("failed to create snapshot")
310+
funcInst.logger.WithError(err).Error("failed to create snapshot")
293311
return nil
294312
}
295313

296314
if err := c.snapshotManager.CommitSnapshot(id); err != nil {
297-
fi.logger.WithError(err).Error("failed to commit snapshot")
315+
funcInst.logger.WithError(err).Error("failed to commit snapshot")
298316
return err
299317
}
300318

301319
return nil
302320
}
303321

304-
func (c *coordinator) orchOffloadVM(ctx context.Context, fi *FuncInstance) error {
322+
func (c *coordinator) orchOffloadVM(ctx context.Context, funcInst *FuncInstance) error {
305323
if c.withoutOrchestrator {
306324
return nil
307325
}
308326

309-
if err := c.orch.OffloadVM(ctx, fi.vmID, c.isFullLocal); err != nil {
310-
fi.logger.WithError(err).Error("failed to offload VM")
327+
if err := c.orch.OffloadVM(ctx, funcInst.vmID, c.isFullLocal); err != nil {
328+
funcInst.logger.WithError(err).Error("failed to offload VM")
311329
return err
312330
}
313331

314332
return nil
315333
}
316334

317-
func (c *coordinator) orchStopVM(ctx context.Context, fi *FuncInstance) error {
335+
func (c *coordinator) orchStopVM(ctx context.Context, funcInst *FuncInstance) error {
318336
if c.withoutOrchestrator {
319337
return nil
320338
}
321339

322-
if err := c.orch.StopSingleVM(ctx, fi.vmID, c.isFullLocal); err != nil {
323-
fi.logger.WithError(err).Error("failed to stop VM for instance")
340+
if err := c.orch.StopSingleVM(ctx, funcInst.vmID, c.isFullLocal); err != nil {
341+
funcInst.logger.WithError(err).Error("failed to stop VM for instance")
324342
return err
325343
}
326344

cri/firecracker/service.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,16 @@ import (
3535
)
3636

3737
const (
38-
userContainerName = "user-container"
39-
queueProxyName = "queue-proxy"
40-
revisionEnv = "K_REVISION"
41-
guestIPEnv = "GUEST_ADDR"
42-
guestPortEnv = "GUEST_PORT"
43-
guestImageEnv = "GUEST_IMAGE"
44-
guestMemorySizeMibEnv = "MEM_SIZE_MB"
45-
guestvCPUCountEnv = "VCPU_COUNT"
38+
userContainerName = "user-container"
39+
queueProxyName = "queue-proxy"
40+
revisionEnv = "K_REVISION"
41+
guestIPEnv = "GUEST_ADDR"
42+
guestPortEnv = "GUEST_PORT"
43+
guestImageEnv = "GUEST_IMAGE"
44+
guestMemorySizeMibEnv = "MEM_SIZE_MB"
45+
guestvCPUCountEnv = "VCPU_COUNT"
46+
defaultMemSize uint32 = 256
47+
defaultvCPUCount uint32 = 1
4648
)
4749

4850
type FirecrackerService struct {
@@ -69,6 +71,7 @@ func NewFirecrackerService(orch *ctriface.Orchestrator, snapsCapacityMiB int64,
6971
return nil, err
7072
}
7173
fs.stockRuntimeClient = stockRuntimeClient
74+
7275
fs.coordinator = newFirecrackerCoordinator(orch, snapsCapacityMiB, isSparseSnaps, isFullLocal)
7376
fs.vmConfigs = make(map[string]*VMConfig)
7477
return fs, nil
@@ -260,7 +263,7 @@ func getMemorySize(config *criapi.ContainerConfig) (uint32, error) {
260263
}
261264
}
262265

263-
return uint32(256), nil
266+
return defaultMemSize, nil
264267
}
265268

266269
func getvCPUCount(config *criapi.ContainerConfig) (uint32, error) {
@@ -276,5 +279,5 @@ func getvCPUCount(config *criapi.ContainerConfig) (uint32, error) {
276279
}
277280
}
278281

279-
return uint32(1), nil
282+
return defaultvCPUCount, nil
280283
}

ctriface/iface.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,12 @@ func (o *Orchestrator) CreateSnapshot(ctx context.Context, vmID string, snap *sn
485485
}
486486

487487
// LoadSnapshot Loads a snapshot of a VM
488-
func (o *Orchestrator) LoadSnapshot(ctx context.Context, vmID string, snap *snapshotting.Snapshot, isFullLocal bool) (_ *StartVMResponse, _ *metrics.Metric, retErr error) {
488+
func (o *Orchestrator) LoadSnapshot(
489+
ctx context.Context,
490+
vmID string,
491+
snap *snapshotting.Snapshot,
492+
isFullLocal bool) (_ *StartVMResponse, _ *metrics.Metric, retErr error) {
493+
489494
var (
490495
loadSnapshotMetric *metrics.Metric = metrics.NewMetric()
491496
tStart time.Time

snapshotting/deduplicated/manager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func (mgr *ImprovedSnapshotManager) AcquireSnapshot(revision string) (*snapshott
7979

8080
// Snapshot registered in manager but creation not finished yet
8181
if ! snapStat.usable { // Could also wait until snapshot usable (trade-off)
82-
return nil, errors.New(fmt.Sprintf("Snapshot is not yet usable"))
82+
return nil, errors.New("Snapshot is not yet usable")
8383
}
8484

8585
if snapStat.numUsing == 0 {

vhive.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ var (
5656
funcPool *FuncPool
5757

5858
isSaveMemory *bool
59-
snapsCapacityMiB *int64
59+
snapsStorageSize *int64
6060
isSparseSnaps *bool
6161
isFullLocal *bool
6262
isSnapshotsEnabled *bool
@@ -89,7 +89,7 @@ func main() {
8989
isSnapshotsEnabled = flag.Bool("snapshots", false, "Use VM snapshots when adding function instances")
9090
isSparseSnaps = flag.Bool("sparsesnaps", false, "Makes memory files sparse after storing to reduce disk utilization")
9191
isFullLocal = flag.Bool("fulllocal", false, "Use improved full local snapshotting")
92-
snapsCapacityMiB = flag.Int64("snapcapacity", 102400, "Capacity set aside for storing snapshots (Mib)")
92+
snapsStorageSize = flag.Int64("snapcapacity", 100, "Total storage reserved for storing snapshots (GiB)")
9393
isUPFEnabled = flag.Bool("upf", false, "Enable user-level page faults guest memory management")
9494
isLazyMode = flag.Bool("lazy", false, "Enable lazy serving mode when UPFs are enabled")
9595

@@ -193,7 +193,8 @@ func setupFirecrackerCRI() {
193193

194194
s := grpc.NewServer()
195195

196-
fcService, err := fccri.NewFirecrackerService(orch, *snapsCapacityMiB, *isSparseSnaps, *isFullLocal)
196+
snapsCapacityMiB := *snapsStorageSize * 1024
197+
fcService, err := fccri.NewFirecrackerService(orch, snapsCapacityMiB, *isSparseSnaps, *isFullLocal)
197198
if err != nil {
198199
log.Fatalf("failed to create firecracker service %v", err)
199200
}

0 commit comments

Comments
 (0)