Skip to content

Commit 89ab733

Browse files
Improve docs on framework.CycleState
Signed-off-by: Adrian Cole <[email protected]> Co-authored-by: Kante Yin <[email protected]>
1 parent 5c72df7 commit 89ab733

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

pkg/scheduler/framework/cycle_state.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ type StateKey string
4343
// StateData stored by one plugin can be read, altered, or deleted by another plugin.
4444
// CycleState does not provide any data protection, as all plugins are assumed to be
4545
// trusted.
46-
// Note: CycleState uses a sync.Map to back the storage. It's aimed to optimize for the "write once and read many times" scenarios.
47-
// It is the recommended pattern used in all in-tree plugins - plugin-specific state is written once in PreFilter/PreScore and afterwards read many times in Filter/Score.
46+
// Note: CycleState uses a sync.Map to back the storage, because it is thread safe. It's aimed to optimize for the "write once and read many times" scenarios.
47+
// It is the recommended pattern used in all in-tree plugins - plugin-specific state is written once in PreFilter/PreScore and afterward read many times in Filter/Score.
4848
type CycleState struct {
4949
// storage is keyed with StateKey, and valued with StateData.
5050
storage sync.Map
51-
// if recordPluginMetrics is true, PluginExecutionDuration will be recorded for this cycle.
51+
// if recordPluginMetrics is true, metrics.PluginExecutionDuration will be recorded for this cycle.
5252
recordPluginMetrics bool
5353
// SkipFilterPlugins are plugins that will be skipped in the Filter extension point.
5454
SkipFilterPlugins sets.Set[string]
@@ -61,7 +61,7 @@ func NewCycleState() *CycleState {
6161
return &CycleState{}
6262
}
6363

64-
// ShouldRecordPluginMetrics returns whether PluginExecutionDuration metrics should be recorded.
64+
// ShouldRecordPluginMetrics returns whether metrics.PluginExecutionDuration metrics should be recorded.
6565
func (c *CycleState) ShouldRecordPluginMetrics() bool {
6666
if c == nil {
6767
return false
@@ -84,10 +84,12 @@ func (c *CycleState) Clone() *CycleState {
8484
return nil
8585
}
8686
copy := NewCycleState()
87+
// Safe copy storage in case of overwriting.
8788
c.storage.Range(func(k, v interface{}) bool {
8889
copy.storage.Store(k, v.(StateData).Clone())
8990
return true
9091
})
92+
// The below are not mutated, so we don't have to safe copy.
9193
copy.recordPluginMetrics = c.recordPluginMetrics
9294
copy.SkipFilterPlugins = c.SkipFilterPlugins
9395
copy.SkipScorePlugins = c.SkipScorePlugins
@@ -96,8 +98,9 @@ func (c *CycleState) Clone() *CycleState {
9698
}
9799

98100
// Read retrieves data with the given "key" from CycleState. If the key is not
99-
// present an error is returned.
100-
// This function is thread safe by using sync.Map.
101+
// present, ErrNotFound is returned.
102+
//
103+
// See CycleState for notes on concurrency.
101104
func (c *CycleState) Read(key StateKey) (StateData, error) {
102105
if v, ok := c.storage.Load(key); ok {
103106
return v.(StateData), nil
@@ -106,13 +109,15 @@ func (c *CycleState) Read(key StateKey) (StateData, error) {
106109
}
107110

108111
// Write stores the given "val" in CycleState with the given "key".
109-
// This function is thread safe by using sync.Map.
112+
//
113+
// See CycleState for notes on concurrency.
110114
func (c *CycleState) Write(key StateKey, val StateData) {
111115
c.storage.Store(key, val)
112116
}
113117

114118
// Delete deletes data with the given key from CycleState.
115-
// This function is thread safe by using sync.Map.
119+
//
120+
// See CycleState for notes on concurrency.
116121
func (c *CycleState) Delete(key StateKey) {
117122
c.storage.Delete(key)
118123
}

0 commit comments

Comments
 (0)