Skip to content

Commit 6bf296c

Browse files
committed
Minor refactoring
1 parent 4054cc9 commit 6bf296c

4 files changed

Lines changed: 44 additions & 49 deletions

File tree

internal/container/container.go

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/base64"
66
"encoding/json"
77
"fmt"
8+
"github.com/serverledge-faas/serverledge/internal/function"
89
"io"
910
"log"
1011
"net/http"
@@ -13,8 +14,41 @@ import (
1314
"github.com/serverledge-faas/serverledge/internal/executor"
1415
)
1516

16-
// NewContainer creates and starts a new container.
17-
func NewContainer(image, codeTar string, opts *ContainerOptions) (*Container, error) {
17+
// CreateContainer creates and starts a new container.
18+
func CreateContainer(f *function.Function, forceImagePull bool) (*Container, error) {
19+
image, err := getImageForFunction(f)
20+
if err != nil {
21+
return nil, err
22+
}
23+
if forceImagePull {
24+
err := cf.PullImage(image)
25+
if err != nil {
26+
return nil, err
27+
}
28+
}
29+
return newContainer(image, f.TarFunctionCode, &ContainerOptions{
30+
MemoryMB: f.MemoryMB,
31+
CPUQuota: f.CPUDemand,
32+
})
33+
}
34+
35+
func getImageForFunction(fun *function.Function) (string, error) {
36+
var image string
37+
if fun.Runtime == CUSTOM_RUNTIME {
38+
image = fun.CustomImage
39+
} else {
40+
runtime, ok := RuntimeToInfo[fun.Runtime]
41+
if !ok {
42+
log.Printf("Unknown runtime: %s\n", fun.Runtime)
43+
return "", fmt.Errorf("Invalid runtime: %s", fun.Runtime)
44+
}
45+
image = runtime.Image
46+
}
47+
return image, nil
48+
}
49+
50+
// CreateContainer creates and starts a new container.
51+
func newContainer(image, codeTar string, opts *ContainerOptions) (*Container, error) {
1852
contID, err := cf.Create(image, opts) // cf = container factory
1953
if err != nil {
2054
log.Printf("Failed container creation\n")

internal/container/factory.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,3 @@ type Container struct {
3535

3636
// cf is the container factory for the node
3737
var cf Factory
38-
39-
func DownloadImage(image string, forceRefresh bool) error {
40-
if forceRefresh || !cf.HasImage(image) {
41-
return cf.PullImage(image)
42-
}
43-
return nil
44-
}

internal/node/pool.go

Lines changed: 7 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package node
33
import (
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

438414
func 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

internal/scheduling/policy_default.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func (p *DefaultLocalPolicy) OnCompletion(_ *function.Function, _ *function.Exec
5454
// start, but also allows us to check for resource
5555
// availability before dequeueing
5656
go func() {
57-
newContainer, err := node.NewContainerWithAcquiredResources(req.Fun, false)
57+
newContainer, err := node.NewContainerWithAcquiredResources(req.Fun, false, false)
5858
if err != nil {
5959
dropRequest(req)
6060
} else {

0 commit comments

Comments
 (0)