Skip to content

Commit f0a81e6

Browse files
committed
Add 2s jitter to termination grace period
The additional 2s should prevent an issue where the grace period is exactly the same as the timeout, and kills the Pod before the remaining requests have completed. Signed-off-by: Alex Ellis (OpenFaaS Ltd) <[email protected]>
1 parent db3c254 commit f0a81e6

File tree

4 files changed

+23
-16
lines changed

4 files changed

+23
-16
lines changed

pkg/controller/deployment.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ func newDeployment(
5757
}
5858
}
5959

60-
terminationGracePeriod := time.Second * 30
60+
// add 2s jitter to avoid a race condition between write_timeout and grace period
61+
jitter := time.Second * 2
62+
terminationGracePeriod := time.Second*30 + jitter
6163

6264
if function.Spec.Environment != nil {
6365
e := *function.Spec.Environment
@@ -67,9 +69,11 @@ func newDeployment(
6769
glog.Warningf("Function %s failed to parse write_timeout: %s",
6870
function.Spec.Name, err.Error())
6971
}
70-
terminationGracePeriod = period
72+
73+
terminationGracePeriod = period + jitter
7174
}
7275
}
76+
7377
terminationGracePeriodSeconds := int64(terminationGracePeriod.Seconds())
7478

7579
allowPrivilegeEscalation := false

pkg/controller/deployment_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,18 @@ import (
1313
func Test_GracePeriodFromWriteTimeout(t *testing.T) {
1414

1515
scenarios := []struct {
16-
name string
17-
seconds int64
18-
envs map[string]string
16+
name string
17+
wantSeconds int64
18+
envs map[string]string
1919
}{
20-
{"grace period is the default", 30, map[string]string{}},
21-
{"grace period is set from write_timeout", 60, map[string]string{"write_timeout": "60s"}},
20+
{"grace period is the default", 32, map[string]string{}},
21+
{"grace period is set from write_timeout", 62, map[string]string{"write_timeout": "60s"}},
2222
}
2323

2424
for _, s := range scenarios {
2525
t.Run(s.name, func(t *testing.T) {
2626

27-
want := int64(s.seconds)
27+
want := int64(s.wantSeconds)
2828
function := &faasv1.Function{
2929
ObjectMeta: metav1.ObjectMeta{
3030
Name: "alpine",

pkg/handlers/deploy.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,9 @@ func makeDeploymentSpec(request types.FunctionDeployment, existingSecrets map[st
174174
return nil, err
175175
}
176176

177-
terminationGracePeriod := time.Second * 30
177+
// add 2s jitter to avoid a race condition between write_timeout and grace period
178+
jitter := time.Second * 2
179+
terminationGracePeriod := time.Second*30 + jitter
178180

179181
if request.EnvVars != nil {
180182
if v, ok := request.EnvVars["write_timeout"]; ok && len(v) > 0 {
@@ -183,7 +185,8 @@ func makeDeploymentSpec(request types.FunctionDeployment, existingSecrets map[st
183185
glog.Warningf("Function %s failed to parse write_timeout: %s",
184186
request.Service, err.Error())
185187
}
186-
terminationGracePeriod = period
188+
189+
terminationGracePeriod = period + jitter
187190
}
188191
}
189192

pkg/handlers/deploy_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ import (
1616
func Test_GracePeriodFromWriteTimeout(t *testing.T) {
1717

1818
scenarios := []struct {
19-
name string
20-
seconds int64
21-
envs map[string]string
19+
name string
20+
wantSeconds int64
21+
envs map[string]string
2222
}{
23-
{"grace period is the default", 30, map[string]string{}},
24-
{"grace period is set from write_timeout", 60, map[string]string{"write_timeout": "60s"}},
23+
{"grace period is the default", 32, map[string]string{}},
24+
{"grace period is set from write_timeout", 62, map[string]string{"write_timeout": "60s"}},
2525
}
2626

2727
for _, s := range scenarios {
@@ -38,7 +38,7 @@ func Test_GracePeriodFromWriteTimeout(t *testing.T) {
3838
if err != nil {
3939
t.Errorf("unexpected makeDeploymentSpec error: %s", err.Error())
4040
}
41-
want := s.seconds
41+
want := s.wantSeconds
4242
got := deployment.Spec.Template.Spec.TerminationGracePeriodSeconds
4343

4444
if got == nil {

0 commit comments

Comments
 (0)