Matthias Andreas Benkard | 832a54e | 2019-01-29 09:27:38 +0100 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2016 The Kubernetes Authors. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package cache |
| 18 | |
| 19 | import ( |
| 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 | |
| 33 | var mutationDetectionEnabled = false |
| 34 | |
| 35 | func init() { |
| 36 | mutationDetectionEnabled, _ = strconv.ParseBool(os.Getenv("KUBE_CACHE_MUTATION_DETECTOR")) |
| 37 | } |
| 38 | |
| 39 | type CacheMutationDetector interface { |
| 40 | AddObject(obj interface{}) |
| 41 | Run(stopCh <-chan struct{}) |
| 42 | } |
| 43 | |
| 44 | func 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 | |
| 52 | type dummyMutationDetector struct{} |
| 53 | |
| 54 | func (dummyMutationDetector) Run(stopCh <-chan struct{}) { |
| 55 | } |
| 56 | func (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. |
| 62 | type 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 |
| 77 | type cacheObj struct { |
| 78 | cached interface{} |
| 79 | copied interface{} |
| 80 | } |
| 81 | |
| 82 | func (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 |
| 97 | func (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 | |
| 110 | func (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 | } |