cycle_state.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. Copyright 2019 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 v1alpha1
  14. import (
  15. "errors"
  16. "sync"
  17. )
  18. const (
  19. // NotFound is the not found error message.
  20. NotFound = "not found"
  21. )
  22. // StateData is a generic type for arbitrary data stored in CycleState.
  23. type StateData interface {
  24. // Clone is an interface to make a copy of StateData. For performance reasons,
  25. // clone should make shallow copies for members (e.g., slices or maps) that are not
  26. // impacted by PreFilter's optional AddPod/RemovePod methods.
  27. Clone() StateData
  28. }
  29. // StateKey is the type of keys stored in CycleState.
  30. type StateKey string
  31. // CycleState provides a mechanism for plugins to store and retrieve arbitrary data.
  32. // StateData stored by one plugin can be read, altered, or deleted by another plugin.
  33. // CycleState does not provide any data protection, as all plugins are assumed to be
  34. // trusted.
  35. type CycleState struct {
  36. mx sync.RWMutex
  37. storage map[StateKey]StateData
  38. // if recordPluginMetrics is true, PluginExecutionDuration will be recorded for this cycle.
  39. recordPluginMetrics bool
  40. }
  41. // NewCycleState initializes a new CycleState and returns its pointer.
  42. func NewCycleState() *CycleState {
  43. return &CycleState{
  44. storage: make(map[StateKey]StateData),
  45. }
  46. }
  47. // ShouldRecordPluginMetrics returns whether PluginExecutionDuration metrics should be recorded.
  48. func (c *CycleState) ShouldRecordPluginMetrics() bool {
  49. if c == nil {
  50. return false
  51. }
  52. return c.recordPluginMetrics
  53. }
  54. // SetRecordPluginMetrics sets recordPluginMetrics to the given value.
  55. func (c *CycleState) SetRecordPluginMetrics(flag bool) {
  56. if c == nil {
  57. return
  58. }
  59. c.recordPluginMetrics = flag
  60. }
  61. // Clone creates a copy of CycleState and returns its pointer. Clone returns
  62. // nil if the context being cloned is nil.
  63. func (c *CycleState) Clone() *CycleState {
  64. if c == nil {
  65. return nil
  66. }
  67. copy := NewCycleState()
  68. for k, v := range c.storage {
  69. copy.Write(k, v.Clone())
  70. }
  71. return copy
  72. }
  73. // Read retrieves data with the given "key" from CycleState. If the key is not
  74. // present an error is returned.
  75. // This function is not thread safe. In multi-threaded code, lock should be
  76. // acquired first.
  77. func (c *CycleState) Read(key StateKey) (StateData, error) {
  78. if v, ok := c.storage[key]; ok {
  79. return v, nil
  80. }
  81. return nil, errors.New(NotFound)
  82. }
  83. // Write stores the given "val" in CycleState with the given "key".
  84. // This function is not thread safe. In multi-threaded code, lock should be
  85. // acquired first.
  86. func (c *CycleState) Write(key StateKey, val StateData) {
  87. c.storage[key] = val
  88. }
  89. // Delete deletes data with the given key from CycleState.
  90. // This function is not thread safe. In multi-threaded code, lock should be
  91. // acquired first.
  92. func (c *CycleState) Delete(key StateKey) {
  93. delete(c.storage, key)
  94. }
  95. // Lock acquires CycleState lock.
  96. func (c *CycleState) Lock() {
  97. c.mx.Lock()
  98. }
  99. // Unlock releases CycleState lock.
  100. func (c *CycleState) Unlock() {
  101. c.mx.Unlock()
  102. }
  103. // RLock acquires CycleState read lock.
  104. func (c *CycleState) RLock() {
  105. c.mx.RLock()
  106. }
  107. // RUnlock releases CycleState read lock.
  108. func (c *CycleState) RUnlock() {
  109. c.mx.RUnlock()
  110. }