Skip to content

Commit 0d2ffe0

Browse files
authored
[Feature] Pre OOM Abort (#1197)
1 parent 5d1cc4f commit 0d2ffe0

File tree

5 files changed

+115
-15
lines changed

5 files changed

+115
-15
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
- (Bugfix) Do not block reconciliation in case of Resource failure
3232
- (Improvement) Multi-arch support for ID member
3333
- (Feature) Allow to change Pod Network and PID settings
34+
- (Feature) Pre OOM Abort function
3435

3536
## [1.2.20](https://github.com/arangodb/kube-arangodb/tree/1.2.20) (2022-10-25)
3637
- (Feature) Add action progress

cmd/cmd.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ var (
9797
Run: executeMain,
9898
}
9999

100+
memoryLimit struct {
101+
hardLimit uint64
102+
}
103+
100104
logLevels []string
101105
serverOptions struct {
102106
host string
@@ -209,6 +213,7 @@ func init() {
209213
f.IntVar(&operatorKubernetesOptions.burst, "kubernetes.burst", kclient.DefaultBurst, "Burst for the k8s API")
210214
f.BoolVar(&crdOptions.install, "crd.install", true, "Install missing CRD if access is possible")
211215
f.IntVar(&operatorBackup.concurrentUploads, "backup-concurrent-uploads", globals.DefaultBackupConcurrentUploads, "Number of concurrent uploads per deployment")
216+
f.Uint64Var(&memoryLimit.hardLimit, "memory-limit", 0, "Define memory limit for hard shutdown and the dump of goroutines. Used for testing")
212217
if err := features.Init(&cmdMain); err != nil {
213218
panic(err.Error())
214219
}
@@ -236,6 +241,8 @@ func executeMain(cmd *cobra.Command, args []string) {
236241
name := os.Getenv(constants.EnvOperatorPodName)
237242
ip := os.Getenv(constants.EnvOperatorPodIP)
238243

244+
go monitorMemoryLimit()
245+
239246
deploymentApi.DefaultImage = operatorOptions.arangoImage
240247

241248
globals.GetGlobalTimeouts().Kubernetes().Set(operatorTimeouts.k8s)

cmd/memory.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//
2+
// DISCLAIMER
3+
//
4+
// Copyright 2016-2022 ArangoDB GmbH, Cologne, Germany
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
// Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
//
20+
21+
package cmd
22+
23+
import (
24+
"runtime"
25+
"syscall"
26+
"time"
27+
28+
"github.com/arangodb/kube-arangodb/pkg/util/shutdown"
29+
)
30+
31+
func monitorMemoryLimit() {
32+
if memoryLimit.hardLimit == 0 {
33+
return
34+
}
35+
36+
var m runtime.MemStats
37+
38+
t := time.NewTicker(time.Millisecond)
39+
defer t.Stop()
40+
41+
for {
42+
select {
43+
case <-t.C:
44+
runtime.ReadMemStats(&m)
45+
46+
if m.Sys > 1024*1024*memoryLimit.hardLimit {
47+
if err := syscall.Kill(syscall.Getpid(), syscall.SIGABRT); err != nil {
48+
panic(err)
49+
}
50+
}
51+
case <-shutdown.Channel():
52+
return
53+
}
54+
}
55+
}

pkg/util/shutdown/shutdown.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//
2+
// DISCLAIMER
3+
//
4+
// Copyright 2016-2022 ArangoDB GmbH, Cologne, Germany
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
// Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
//
20+
21+
package shutdown
22+
23+
import (
24+
"os"
25+
"os/signal"
26+
"syscall"
27+
)
28+
29+
func init() {
30+
shutdown = make(chan struct{})
31+
32+
sigChannel := make(chan os.Signal, 2)
33+
34+
signal.Notify(sigChannel, os.Interrupt, syscall.SIGTERM)
35+
36+
go func() {
37+
defer close(shutdown)
38+
<-sigChannel
39+
}()
40+
}
41+
42+
var shutdown chan struct{}
43+
44+
func Channel() <-chan struct{} {
45+
return shutdown
46+
}

pkg/util/signal.go

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,30 +22,21 @@ package util
2222

2323
import (
2424
"context"
25-
"os"
26-
"os/signal"
27-
"syscall"
25+
26+
"github.com/arangodb/kube-arangodb/pkg/util/shutdown"
2827
)
2928

3029
// CreateSignalContext creates and returns the context which is closed when one of the provided signal occurs.
31-
// If the provided list of signals is empty, then SIGINT and SIGTERM is used by default.
32-
func CreateSignalContext(ctx context.Context, signals ...os.Signal) context.Context {
30+
// SIGINT and SIGTERM is used by default.
31+
func CreateSignalContext(ctx context.Context) context.Context {
3332
if ctx == nil {
3433
ctx = context.Background()
3534
}
3635
ctxSignal, cancelSignal := context.WithCancel(ctx)
37-
sigChannel := make(chan os.Signal, 2)
38-
39-
if len(signals) > 0 {
40-
signal.Notify(sigChannel, signals...)
41-
} else {
42-
signal.Notify(sigChannel, os.Interrupt, syscall.SIGTERM)
43-
}
4436

4537
go func() {
46-
// Wait until signal occurs.
47-
<-sigChannel
48-
close(sigChannel)
38+
// Wait until shutdown starts
39+
<-shutdown.Channel()
4940
// Close the context which is used by the caller.
5041
cancelSignal()
5142
}()

0 commit comments

Comments
 (0)