Skip to content

Commit d7da8ea

Browse files
committed
Fix: redundant CPU reservation in prewarming
1 parent 1acaca3 commit d7da8ea

2 files changed

Lines changed: 53 additions & 2 deletions

File tree

internal/cli/cli.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ var createCmd = &cobra.Command{
4646
Run: create,
4747
}
4848

49+
var prewarmCmd = &cobra.Command{
50+
Use: "prewarm",
51+
Short: "Prewarms instances for a function",
52+
Run: prewarm,
53+
}
54+
4955
var deleteCmd = &cobra.Command{
5056
Use: "delete",
5157
Short: "Deletes a function",
@@ -103,6 +109,8 @@ var verbose bool
103109
var returnOutput bool
104110
var update bool
105111
var maxConcurrency int16
112+
var prewarmCount int64
113+
var forcePull bool
106114

107115
func Init() {
108116
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose output")
@@ -131,6 +139,11 @@ func Init() {
131139
createCmd.Flags().StringSliceVarP(&inputs, "input", "i", nil, "Input parameter: <name>:<type>")
132140
createCmd.Flags().StringSliceVarP(&outputs, "output", "o", nil, "Output specification: <name>:<type>")
133141

142+
rootCmd.AddCommand(prewarmCmd)
143+
prewarmCmd.Flags().StringVarP(&funcName, "function", "f", "", "name of the function")
144+
prewarmCmd.Flags().Int64VarP(&prewarmCount, "count", "c", 1, "num of instances to launch")
145+
prewarmCmd.Flags().BoolVarP(&forcePull, "force_pull", "", false, "Force pull of container image")
146+
134147
rootCmd.AddCommand(deleteCmd)
135148
deleteCmd.Flags().StringVarP(&funcName, "function", "f", "", "name of the function")
136149

@@ -273,6 +286,36 @@ func buildSignature() (*function.Signature, error) {
273286
return sb.Build(), nil
274287
}
275288

289+
func prewarm(cmd *cobra.Command, args []string) {
290+
if funcName == "" {
291+
showHelpAndExit(cmd)
292+
}
293+
if prewarmCount < 1 {
294+
fmt.Printf("Invalid prewarm count: %d\n", prewarmCount)
295+
showHelpAndExit(cmd)
296+
}
297+
298+
request := client.PrewarmingRequest{
299+
Function: funcName,
300+
Instances: prewarmCount,
301+
ForceImagePull: forcePull,
302+
}
303+
requestBody, err := json.Marshal(request)
304+
if err != nil {
305+
showHelpAndExit(cmd)
306+
}
307+
308+
apiName := "prewarm"
309+
310+
url := fmt.Sprintf("http://%s:%d/%s", ServerConfig.Host, ServerConfig.Port, apiName)
311+
resp, err := utils.PostJson(url, requestBody)
312+
if err != nil {
313+
fmt.Printf("Prewarming request failed: %v\n", err)
314+
os.Exit(2)
315+
}
316+
utils.PrintJsonResponse(resp.Body)
317+
}
318+
276319
func create(cmd *cobra.Command, args []string) {
277320
if funcName == "" || runtime == "" {
278321
showHelpAndExit(cmd)

internal/node/pool.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,15 @@ func HandleCompletion(cont *container.Container, f *function.Function) {
189189
// in the busy pool.
190190
func NewContainer(fun *function.Function, markAsIdle bool, forceImagePull bool) (*container.Container, error) {
191191
Resources.Lock()
192-
if !acquireResources(fun.CPUDemand, fun.MemoryMB, true) {
192+
193+
var cpuDemand float64
194+
if markAsIdle {
195+
cpuDemand = 0.0
196+
} else {
197+
cpuDemand = fun.CPUDemand
198+
}
199+
200+
if !acquireResources(cpuDemand, fun.MemoryMB, true) {
193201
//log.Printf("Not enough resources for the new container.\n")
194202
Resources.Unlock()
195203
return nil, OutOfResourcesErr
@@ -205,7 +213,7 @@ func NewContainer(fun *function.Function, markAsIdle bool, forceImagePull bool)
205213
// function, assuming that the required CPU and memory resources have been
206214
// already been acquired.
207215
func NewContainerWithAcquiredResources(fun *function.Function, startAsIdle bool, forceImagePull bool) (*container.Container, error) {
208-
cont, err := container.CreateContainer(fun, false)
216+
cont, err := container.CreateContainer(fun, forceImagePull)
209217

210218
if err != nil {
211219
log.Printf("Failed container creation: %v\n", err)

0 commit comments

Comments
 (0)