mutation_detector.go 3.4 KB

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