blob: e2aa44848408770a61a10df350c5d0f69a93730f [file] [log] [blame]
Matthias Andreas Benkard832a54e2019-01-29 09:27:38 +01001/*
2Copyright 2016 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package cache
18
19import (
20 "fmt"
21 "os"
22 "reflect"
23 "strconv"
24 "sync"
25 "time"
26
27 "github.com/golang/glog"
28
29 "k8s.io/apimachinery/pkg/runtime"
30 "k8s.io/apimachinery/pkg/util/diff"
31)
32
33var mutationDetectionEnabled = false
34
35func init() {
36 mutationDetectionEnabled, _ = strconv.ParseBool(os.Getenv("KUBE_CACHE_MUTATION_DETECTOR"))
37}
38
39type CacheMutationDetector interface {
40 AddObject(obj interface{})
41 Run(stopCh <-chan struct{})
42}
43
44func NewCacheMutationDetector(name string) CacheMutationDetector {
45 if !mutationDetectionEnabled {
46 return dummyMutationDetector{}
47 }
48 glog.Warningln("Mutation detector is enabled, this will result in memory leakage.")
49 return &defaultCacheMutationDetector{name: name, period: 1 * time.Second}
50}
51
52type dummyMutationDetector struct{}
53
54func (dummyMutationDetector) Run(stopCh <-chan struct{}) {
55}
56func (dummyMutationDetector) AddObject(obj interface{}) {
57}
58
59// defaultCacheMutationDetector gives a way to detect if a cached object has been mutated
60// It has a list of cached objects and their copies. I haven't thought of a way
61// to see WHO is mutating it, just that it's getting mutated.
62type defaultCacheMutationDetector struct {
63 name string
64 period time.Duration
65
66 lock sync.Mutex
67 cachedObjs []cacheObj
68
69 // failureFunc is injectable for unit testing. If you don't have it, the process will panic.
70 // This panic is intentional, since turning on this detection indicates you want a strong
71 // failure signal. This failure is effectively a p0 bug and you can't trust process results
72 // after a mutation anyway.
73 failureFunc func(message string)
74}
75
76// cacheObj holds the actual object and a copy
77type cacheObj struct {
78 cached interface{}
79 copied interface{}
80}
81
82func (d *defaultCacheMutationDetector) Run(stopCh <-chan struct{}) {
83 // we DON'T want protection from panics. If we're running this code, we want to die
84 for {
85 d.CompareObjects()
86
87 select {
88 case <-stopCh:
89 return
90 case <-time.After(d.period):
91 }
92 }
93}
94
95// AddObject makes a deep copy of the object for later comparison. It only works on runtime.Object
96// but that covers the vast majority of our cached objects
97func (d *defaultCacheMutationDetector) AddObject(obj interface{}) {
98 if _, ok := obj.(DeletedFinalStateUnknown); ok {
99 return
100 }
101 if obj, ok := obj.(runtime.Object); ok {
102 copiedObj := obj.DeepCopyObject()
103
104 d.lock.Lock()
105 defer d.lock.Unlock()
106 d.cachedObjs = append(d.cachedObjs, cacheObj{cached: obj, copied: copiedObj})
107 }
108}
109
110func (d *defaultCacheMutationDetector) CompareObjects() {
111 d.lock.Lock()
112 defer d.lock.Unlock()
113
114 altered := false
115 for i, obj := range d.cachedObjs {
116 if !reflect.DeepEqual(obj.cached, obj.copied) {
117 fmt.Printf("CACHE %s[%d] ALTERED!\n%v\n", d.name, i, diff.ObjectDiff(obj.cached, obj.copied))
118 altered = true
119 }
120 }
121
122 if altered {
123 msg := fmt.Sprintf("cache %s modified", d.name)
124 if d.failureFunc != nil {
125 d.failureFunc(msg)
126 return
127 }
128 panic(msg)
129 }
130}