Skip to content

fix(lambda): fix race condition in lambda server spin up #8013

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 3, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions dgraph/cmd/alpha/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"path/filepath"
"strconv"
"strings"
"sync"
"sync/atomic"
"syscall"
"time"
Expand Down Expand Up @@ -487,11 +488,13 @@ func setupLambdaServer(closer *z.Closer) {
}

type lambda struct {
sync.Mutex
cmd *exec.Cmd
active bool
lastActive int64
health string
port int

health string
port int
}

lambdas := make([]*lambda, 0, num)
Expand Down Expand Up @@ -519,9 +522,11 @@ func setupLambdaServer(closer *z.Closer) {
cmd.Env = append(cmd.Env, fmt.Sprintf("DGRAPH_URL="+dgraphUrl))
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
lambdas[i].Lock()
lambdas[i].cmd = cmd
lambdas[i].lastActive = time.Now().UnixNano()
lambdas[i].active = true
lambdas[i].Unlock()
glog.Infof("Running node command: %+v\n", cmd)
if err := cmd.Run(); err != nil {
glog.Errorf("Lambda server at port: %d stopped with error: %v",
Expand All @@ -544,23 +549,32 @@ func setupLambdaServer(closer *z.Closer) {
case <-closer.HasBeenClosed():
return
case <-ticker.C:
timestamp := time.Now().UnixNano()
for _, l := range lambdas {
healthCheck := func(l *lambda) {
l.Lock()
defer l.Unlock()

if !l.active {
continue
return
}

timestamp := time.Now().UnixNano()
resp, err := client.Get(l.health)
if err != nil || resp.StatusCode != 200 {
if time.Duration(timestamp-l.lastActive) > x.Config.Lambda.RestartAfter {
glog.Warningf("Lambda Server at port: %d not responding."+
" Killed it with err: %v", l.port, l.cmd.Process.Kill())
l.active = false
}
continue
return
}

resp.Body.Close()
l.lastActive = timestamp
}

for _, l := range lambdas {
healthCheck(l)
}
}
}
}()
Expand Down