Skip to content

Commit 8710414

Browse files
committed
Move DataStore to memStore
Signed-off-by: kerthcet <kerthcet@gmail.com>
1 parent ff95bd4 commit 8710414

2 files changed

Lines changed: 56 additions & 57 deletions

File tree

components/router/pkg/store/mem.go

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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+
2580
var _ Store = &MemoryStore{}
2681

2782
type MemoryStore struct {
2883
mu sync.RWMutex
29-
data map[string]*DataStore // Key: modelName, Value: *podWrapperStore
84+
data map[string]*DataStore // Key: modelName
3085
}
3186

3287
func NewMemoryStore() *MemoryStore {

components/router/pkg/store/store.go

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ package store
1818

1919
import (
2020
"context"
21-
"fmt"
22-
"sync"
2321
)
2422

2523
type Store interface {
@@ -31,57 +29,3 @@ type Store interface {
3129
// Should only used for testing.
3230
Len() int32
3331
}
34-
35-
type DataStore struct {
36-
mu sync.RWMutex
37-
data map[string]Indicator // Key: name, Value: Indicator
38-
39-
// Keep track of the min/max values for each metric, 0-index is min and 1-index is max.
40-
// They will be used in score plugins.
41-
RunningQueueSize [2]float64
42-
WaitingQueueSize [2]float64
43-
KVCacheUsage [2]float64
44-
}
45-
46-
func (d *DataStore) Get(ctx context.Context, name string) (Indicator, error) {
47-
d.mu.RLock()
48-
defer d.mu.RUnlock()
49-
50-
metrics, exists := d.data[name]
51-
if !exists {
52-
return Indicator{}, fmt.Errorf("metrics for datastore %s not found", name)
53-
}
54-
return metrics, nil
55-
}
56-
57-
// TODO: we should not iterate all the dataStore which may lead to performance issue.
58-
func (d *DataStore) FilterIterate(ctx context.Context, fn func(context.Context, Indicator) bool) (names []string) {
59-
d.mu.RLock()
60-
defer d.mu.RUnlock()
61-
62-
for name, indicator := range d.data {
63-
if fn(ctx, indicator) {
64-
names = append(names, name)
65-
}
66-
}
67-
return
68-
69-
}
70-
71-
func (d *DataStore) ScoreIterate(ctx context.Context, fn func(context.Context, Indicator) float32) string {
72-
d.mu.RLock()
73-
defer d.mu.RUnlock()
74-
75-
var highestScore float32
76-
var candidate string
77-
78-
for name, indicator := range d.data {
79-
score := fn(ctx, indicator)
80-
// Iterate the d.data is already in random order, so we can just pick the first one with the highest score.
81-
if score > highestScore {
82-
highestScore = score
83-
candidate = name
84-
}
85-
}
86-
return candidate
87-
}

0 commit comments

Comments
 (0)