@@ -22,11 +22,66 @@ import (
2222 "sync"
2323)
2424
25+ type DataStore struct {
26+ mu sync.RWMutex
27+ data map [string ]Indicator // Key: name, Value: Indicator
28+
29+ // Keep track of the min/max values for each metric, 0-index is min and 1-index is max.
30+ // They will be used in score plugins.
31+ RunningQueueSize [2 ]float64
32+ WaitingQueueSize [2 ]float64
33+ KVCacheUsage [2 ]float64
34+ }
35+
36+ func (d * DataStore ) Get (ctx context.Context , name string ) (Indicator , error ) {
37+ d .mu .RLock ()
38+ defer d .mu .RUnlock ()
39+
40+ metrics , exists := d .data [name ]
41+ if ! exists {
42+ return Indicator {}, fmt .Errorf ("metrics for datastore %s not found" , name )
43+ }
44+ return metrics , nil
45+ }
46+
47+ // TODO: we should not iterate all the data which may lead to performance issue.
48+ func (d * DataStore ) FilterIterate (ctx context.Context , fn func (context.Context , Indicator ) bool ) (names []string ) {
49+ d .mu .RLock ()
50+ defer d .mu .RUnlock ()
51+
52+ for name , indicator := range d .data {
53+ if fn (ctx , indicator ) {
54+ names = append (names , name )
55+ }
56+ }
57+ return
58+
59+ }
60+
61+ // TODO: return multi candidates to avoid hotspot with multi instances.
62+ func (d * DataStore ) ScoreIterate (ctx context.Context , fn func (context.Context , Indicator ) float32 ) string {
63+ d .mu .RLock ()
64+ defer d .mu .RUnlock ()
65+
66+ var highestScore float32
67+ var candidate string
68+
69+ for name , indicator := range d .data {
70+ score := fn (ctx , indicator )
71+ // Iterate the d.data is already in random order, so we can just pick the first one with the highest score.
72+ if score > highestScore {
73+ highestScore = score
74+ candidate = name
75+ }
76+ }
77+ return candidate
78+ }
79+
2580var _ Store = & MemoryStore {}
2681
2782type MemoryStore struct {
2883 mu sync.RWMutex
29- data map [string ]* DataStore // Key: modelName, Value: *podWrapperStore
84+ data map [string ]* DataStore // Key: modelName
3085}
3186
3287func NewMemoryStore () * MemoryStore {
0 commit comments