Repository navigation
Expand file tree
/
Copy pathrecycle.go
More file actions
348 lines (318 loc) · 10.3 KB
/
Copy pathrecycle.go
File metadata and controls
348 lines (318 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package main
import (
"os"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/crossplane/crossplane-runtime/v2/pkg/logging"
)
// The KCL native runtime accumulates off-heap memory across reconciles (it
// recompiles the whole KCL module on every call and the native service handle
// is a long-lived process-global singleton that is never reset). Left alone,
// function-kcl pods climb to their memory limit and get OOMKilled (exit 137),
// which violently drops in-flight reconciles -> "connection reset by peer" /
// DeadlineExceeded on heavy composites.
//
// The recycler turns those hard OOMKills into clean, scheduled restarts. A
// watchdog samples the process RSS (which includes the native off-heap
// allocations) plus optional reconcile-count / lifetime limits. When a limit is
// crossed it drains in-flight RunFunction calls and exits 0 so Kubernetes
// restarts the pod *between* reconciles instead of *during* one. With multiple
// replicas this is a rolling, self-healing recycle and removes the need for a
// manual rollout restart.
//
// This is a stability backstop; the render cache (see rendercache.go) avoids the
// recompile — and its leak increment — for byte-identical reconciles. The two
// are complementary: the backstop bounds any residual growth regardless of the
// leak's exact internals.
// recycleConfig is read from the environment so it can be tuned per deployment
// via container env vars, without a rebuild.
type recycleConfig struct {
// maxRSSBytes recycles the process once its resident set size reaches this
// many bytes. 0 disables the RSS trigger.
maxRSSBytes uint64
// maxRSSRatio is used to derive maxRSSBytes from the detected cgroup memory
// limit when maxRSSBytes is not set explicitly.
maxRSSRatio float64
// maxReconciles recycles after this many RunFunction calls. 0 disables.
maxReconciles uint64
// maxLifetime recycles after the process has been up this long. 0 disables.
maxLifetime time.Duration
// checkInterval is how often the watchdog samples the triggers.
checkInterval time.Duration
// drainTimeout bounds how long we wait for in-flight calls to finish before
// exiting anyway.
drainTimeout time.Duration
}
const (
envMaxRSSBytes = "FUNCTION_KCL_MAX_RSS_BYTES"
envMaxRSSRatio = "FUNCTION_KCL_MAX_RSS_RATIO"
envMaxReconciles = "FUNCTION_KCL_MAX_RECONCILES"
envMaxLifetime = "FUNCTION_KCL_MAX_LIFETIME"
envCheckInterval = "FUNCTION_KCL_RECYCLE_CHECK_INTERVAL"
envDrainTimeout = "FUNCTION_KCL_RECYCLE_DRAIN_TIMEOUT"
defaultMaxRSSRatio = 0.85
defaultCheckInterval = 30 * time.Second
defaultDrainTimeout = 15 * time.Second
)
// recycleConfigFromEnv builds the config from environment variables, applying
// defaults. If no explicit RSS limit is set, it derives one from the cgroup
// memory limit so the in-cluster default "just works" (recycle at 85% of the
// pod's memory limit, before the kubelet OOMKills at 100%).
func recycleConfigFromEnv() recycleConfig {
cfg := recycleConfig{
maxRSSRatio: envFloat(envMaxRSSRatio, defaultMaxRSSRatio),
maxRSSBytes: envBytes(envMaxRSSBytes, 0),
maxReconciles: envUint(envMaxReconciles, 0),
maxLifetime: envDuration(envMaxLifetime, 0),
checkInterval: envDuration(envCheckInterval, defaultCheckInterval),
drainTimeout: envDuration(envDrainTimeout, defaultDrainTimeout),
}
if cfg.maxRSSBytes == 0 && cfg.maxRSSRatio > 0 {
if limit, ok := cgroupMemoryLimit(); ok {
cfg.maxRSSBytes = uint64(float64(limit) * cfg.maxRSSRatio)
}
}
return cfg
}
// enabled reports whether any recycle trigger is active. When false the
// watchdog never fires, so behaviour is identical to upstream (useful for local
// runs and tests with no cgroup limit).
func (c recycleConfig) enabled() bool {
return c.maxRSSBytes > 0 || c.maxReconciles > 0 || c.maxLifetime > 0
}
// recycler tracks in-flight RunFunction calls and drives the watchdog. The zero
// value and a nil *recycler are both safe no-ops, so existing tests that build a
// Function without one are unaffected.
type recycler struct {
log logging.Logger
cfg recycleConfig
start time.Time
// gate is held for reading for the duration of each RunFunction call.
// Draining acquires it for writing, which blocks until all in-flight calls
// release it and prevents new ones from starting.
gate sync.RWMutex
inflight atomic.Int64
count atomic.Uint64
draining atomic.Bool
// Injectable for tests.
readRSS func() (uint64, error)
exit func(int)
now func() time.Time
}
func newRecycler(log logging.Logger, cfg recycleConfig) *recycler {
return &recycler{
log: log,
cfg: cfg,
start: time.Now(),
readRSS: processRSS,
exit: os.Exit,
now: time.Now,
}
}
// begin marks the start of a RunFunction call. It returns false if the process
// is draining, in which case the caller must reject the request so Crossplane
// retries it (against another replica or after the restart). A nil recycler
// always admits the call.
func (r *recycler) begin() bool {
if r == nil {
return true
}
if r.draining.Load() {
return false
}
r.gate.RLock()
r.inflight.Add(1)
r.count.Add(1)
return true
}
// end marks the completion of a RunFunction call.
func (r *recycler) end() {
if r == nil {
return
}
r.inflight.Add(-1)
r.gate.RUnlock()
}
// start launches the watchdog goroutine if any trigger is enabled.
func (r *recycler) run() {
if r == nil || !r.cfg.enabled() {
return
}
r.log.Info("memory recycler enabled",
"maxRSSBytes", r.cfg.maxRSSBytes,
"maxReconciles", r.cfg.maxReconciles,
"maxLifetime", r.cfg.maxLifetime.String(),
"checkInterval", r.cfg.checkInterval.String())
go r.watch()
}
func (r *recycler) watch() {
ticker := time.NewTicker(r.cfg.checkInterval)
defer ticker.Stop()
for range ticker.C {
if reason := r.shouldRecycle(); reason != "" {
r.recycle(reason)
return
}
}
}
// shouldRecycle returns a non-empty human-readable reason if any trigger has
// fired, else "".
func (r *recycler) shouldRecycle() string {
if r.cfg.maxReconciles > 0 && r.count.Load() >= r.cfg.maxReconciles {
return "reconcile count " + strconv.FormatUint(r.count.Load(), 10) +
" >= " + strconv.FormatUint(r.cfg.maxReconciles, 10)
}
if r.cfg.maxLifetime > 0 && r.now().Sub(r.start) >= r.cfg.maxLifetime {
return "lifetime " + r.now().Sub(r.start).Round(time.Second).String() +
" >= " + r.cfg.maxLifetime.String()
}
if r.cfg.maxRSSBytes > 0 {
rss, err := r.readRSS()
if err != nil {
r.log.Debug("cannot read process RSS", "error", err)
return ""
}
if rss >= r.cfg.maxRSSBytes {
return "RSS " + strconv.FormatUint(rss, 10) +
" >= " + strconv.FormatUint(r.cfg.maxRSSBytes, 10) + " bytes"
}
}
return ""
}
// recycle drains in-flight calls (bounded by drainTimeout) and exits cleanly so
// the orchestrator restarts the pod.
func (r *recycler) recycle(reason string) {
if !r.draining.CompareAndSwap(false, true) {
return
}
r.log.Info("recycling function-kcl to release native memory",
"reason", reason, "inflight", r.inflight.Load())
drained := make(chan struct{})
go func() {
r.gate.Lock() // waits for all in-flight RunFunction calls to return
close(drained)
}()
select {
case <-drained:
r.log.Info("drained in-flight reconciles, exiting for clean restart")
case <-time.After(r.cfg.drainTimeout):
r.log.Info("drain timed out, forcing restart",
"inflight", r.inflight.Load(), "timeout", r.cfg.drainTimeout.String())
}
r.exit(0)
}
// processRSS returns the resident set size of the current process in bytes by
// reading /proc/self/statm. RSS includes the KCL native (off-heap) allocations,
// which is exactly what we need to bound.
func processRSS() (uint64, error) {
data, err := os.ReadFile("/proc/self/statm")
if err != nil {
return 0, err
}
fields := strings.Fields(string(data))
if len(fields) < 2 {
return 0, errInvalidStatm
}
residentPages, err := strconv.ParseUint(fields[1], 10, 64)
if err != nil {
return 0, err
}
return residentPages * uint64(os.Getpagesize()), nil
}
// cgroupMemoryLimit returns the container memory limit in bytes, trying cgroup
// v2 then v1. ok is false if no finite limit is set.
func cgroupMemoryLimit() (uint64, bool) {
// cgroup v2
if v, ok := readCgroupLimit("/sys/fs/cgroup/memory.max"); ok {
return v, true
}
// cgroup v1
if v, ok := readCgroupLimit("/sys/fs/cgroup/memory/memory.limit_in_bytes"); ok {
return v, true
}
return 0, false
}
func readCgroupLimit(path string) (uint64, bool) {
data, err := os.ReadFile(path)
if err != nil {
return 0, false
}
s := strings.TrimSpace(string(data))
if s == "" || s == "max" {
return 0, false
}
v, err := strconv.ParseUint(s, 10, 64)
if err != nil {
return 0, false
}
// cgroup v1 reports a sentinel "unlimited" value close to max uint64; treat
// anything implausibly large (>= 1 PiB) as no limit.
if v == 0 || v >= 1<<50 {
return 0, false
}
return v, true
}
var errInvalidStatm = &recycleError{"unexpected /proc/self/statm format"}
type recycleError struct{ msg string }
func (e *recycleError) Error() string { return e.msg }
// envUint, envFloat, envBytes and envDuration parse optional environment
// variables, falling back to def on absence or parse error.
func envUint(key string, def uint64) uint64 {
v, ok := os.LookupEnv(key)
if !ok {
return def
}
n, err := strconv.ParseUint(strings.TrimSpace(v), 10, 64)
if err != nil {
return def
}
return n
}
func envFloat(key string, def float64) float64 {
v, ok := os.LookupEnv(key)
if !ok {
return def
}
f, err := strconv.ParseFloat(strings.TrimSpace(v), 64)
if err != nil {
return def
}
return f
}
func envDuration(key string, def time.Duration) time.Duration {
v, ok := os.LookupEnv(key)
if !ok {
return def
}
d, err := time.ParseDuration(strings.TrimSpace(v))
if err != nil {
return def
}
return d
}
// envBytes parses a byte size that may carry a binary unit suffix (Ki, Mi, Gi)
// or be a plain integer count of bytes.
func envBytes(key string, def uint64) uint64 {
v, ok := os.LookupEnv(key)
if !ok {
return def
}
s := strings.TrimSpace(v)
mult := uint64(1)
switch {
case strings.HasSuffix(s, "Gi"):
mult, s = 1<<30, strings.TrimSuffix(s, "Gi")
case strings.HasSuffix(s, "Mi"):
mult, s = 1<<20, strings.TrimSuffix(s, "Mi")
case strings.HasSuffix(s, "Ki"):
mult, s = 1<<10, strings.TrimSuffix(s, "Ki")
}
n, err := strconv.ParseUint(strings.TrimSpace(s), 10, 64)
if err != nil {
return def
}
return n * mult
}