@@ -3,7 +3,6 @@ package node
33import (
44 "container/list"
55 "errors"
6- "fmt"
76 "log"
87 "time"
98
@@ -151,7 +150,7 @@ func AcquireContainer(f *function.Function) (*container.Container, bool, error)
151150 if ! AcquireResources (f .CPUDemand , f .MemoryMB , true ) {
152151 return nil , false , OutOfResourcesErr
153152 }
154- c , err = NewContainerWithAcquiredResources (f , false )
153+ c , err = NewContainerWithAcquiredResources (f , false , false )
155154 return c , false , err
156155}
157156
@@ -188,7 +187,7 @@ func HandleCompletion(cont *container.Container, f *function.Function) {
188187// NewContainer creates and starts a new container for the given function.
189188// The container can be directly used to schedule a request, as it is already
190189// in the busy pool.
191- func NewContainer (fun * function.Function , markAsIdle bool ) (* container.Container , error ) {
190+ func NewContainer (fun * function.Function , markAsIdle bool , forceImagePull bool ) (* container.Container , error ) {
192191 Resources .Lock ()
193192 if ! acquireResources (fun .CPUDemand , fun .MemoryMB , true ) {
194193 //log.Printf("Not enough resources for the new container.\n")
@@ -199,37 +198,14 @@ func NewContainer(fun *function.Function, markAsIdle bool) (*container.Container
199198 //log.Printf("Acquired resources for new container. Now: %v", Resources)
200199 Resources .Unlock ()
201200
202- return NewContainerWithAcquiredResources (fun , markAsIdle )
203- }
204-
205- func getImageForFunction (fun * function.Function ) (string , error ) {
206- var image string
207- if fun .Runtime == container .CUSTOM_RUNTIME {
208- image = fun .CustomImage
209- } else {
210- runtime , ok := container .RuntimeToInfo [fun .Runtime ]
211- if ! ok {
212- log .Printf ("Unknown runtime: %s\n " , fun .Runtime )
213- return "" , fmt .Errorf ("Invalid runtime: %s" , fun .Runtime )
214- }
215- image = runtime .Image
216- }
217- return image , nil
201+ return NewContainerWithAcquiredResources (fun , markAsIdle , forceImagePull )
218202}
219203
220204// NewContainerWithAcquiredResources spawns a new container for the given
221205// function, assuming that the required CPU and memory resources have been
222206// already been acquired.
223- func NewContainerWithAcquiredResources (fun * function.Function , startAsIdle bool ) (* container.Container , error ) {
224- image , err := getImageForFunction (fun )
225- if err != nil {
226- return nil , err
227- }
228-
229- cont , err := container .NewContainer (image , fun .TarFunctionCode , & container.ContainerOptions {
230- MemoryMB : fun .MemoryMB ,
231- CPUQuota : fun .CPUDemand ,
232- })
207+ func NewContainerWithAcquiredResources (fun * function.Function , startAsIdle bool , forceImagePull bool ) (* container.Container , error ) {
208+ cont , err := container .CreateContainer (fun , false )
233209
234210 if err != nil {
235211 log .Printf ("Failed container creation: %v\n " , err )
@@ -436,23 +412,15 @@ func WarmStatus() map[string]int {
436412}
437413
438414func PrewarmInstances (f * function.Function , count int64 , forcePull bool ) (int64 , error ) {
439- image , err := getImageForFunction (f )
440- if err != nil {
441- return 0 , err
442- }
443- err = container .DownloadImage (image , forcePull )
444- if err != nil {
445- return 0 , err
446- }
447-
448415 var spawned int64 = 0
449416 for spawned < count {
450- _ , err = NewContainer (f , true )
417+ _ , err : = NewContainer (f , true , forcePull )
451418 if err != nil {
452419 log .Printf ("Prespawning failed: %v\n " , err )
453420 return spawned , err
454421 }
455422 spawned += 1
423+ forcePull = false // not needed more than once
456424 }
457425
458426 return spawned , nil
0 commit comments