Skip to content

Commit 1169792

Browse files
committed
fix: update metaPods cache to use as the key
Signed-off-by: googs1025 <[email protected]>
1 parent e51899c commit 1169792

File tree

20 files changed

+195
-141
lines changed

20 files changed

+195
-141
lines changed

pkg/cache/cache_api.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,11 @@ type PodCache interface {
3535
// GetPod retrieves a Pod object by name
3636
// Parameters:
3737
// podName: Name of the pod
38+
// podNamespace: Namespace of the pod
3839
// Returns:
3940
// *v1.Pod: Found pod object
4041
// error: Error information if operation fails
41-
GetPod(podName string) (*v1.Pod, error)
42+
GetPod(podName, podNamespace string) (*v1.Pod, error)
4243

4344
// ListPodsByModel gets pods associated with a model
4445
// Parameters:
@@ -66,33 +67,36 @@ type ModelCache interface {
6667
// ListModelsByPod gets models associated with a pod
6768
// Parameters:
6869
// podName: Name of the pod
70+
// podNamespace: Namespace of the pod
6971
// Returns:
7072
// map[string]struct{}: Set of model names
7173
// error: Error information if operation fails
72-
ListModelsByPod(podName string) ([]string, error)
74+
ListModelsByPod(podName, podNamespace string) ([]string, error)
7375
}
7476

7577
// MetricCache defines operations for metric data caching
7678
type MetricCache interface {
7779
// GetMetricValueByPod gets metric value for a pod
7880
// Parameters:
7981
// podName: Name of the pod
82+
// podNamespace: Namespace of the pod
8083
// metricName: Name of the metric
8184
// Returns:
8285
// metrics.MetricValue: Retrieved metric value
8386
// error: Error information if operation fails
84-
GetMetricValueByPod(podName, metricName string) (metrics.MetricValue, error)
87+
GetMetricValueByPod(podName, podNamespace, metricName string) (metrics.MetricValue, error)
8588

8689
// GetMetricValueByPodModel gets metric value for pod-model pair
8790
// Parameters:
8891
// ctx: Routing context
8992
// podName: Name of the pod
93+
// podNamespace: Namespace of the pod
9094
// modelName: Name of the model
9195
// metricName: Name of the metric
9296
// Returns:
9397
// metrics.MetricValue: Retrieved metric value
9498
// error: Error information if operation fails
95-
GetMetricValueByPodModel(podName, modelName string, metricName string) (metrics.MetricValue, error)
99+
GetMetricValueByPodModel(podName, podNamespace, modelName string, metricName string) (metrics.MetricValue, error)
96100

97101
// AddSubscriber adds a metric subscriber
98102
// Parameters:

pkg/cache/cache_impl.go

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,16 @@ import (
2929
// GetPod retrieves a Pod object by name from the cache
3030
// Parameters:
3131
//
32-
// podName: Name of the pod to retrieve
32+
// podName: Name of the pod to retrieve
33+
// podNamespace: Namespace of the pod to retrieve
3334
//
3435
// Returns:
3536
//
3637
// *v1.Pod: The found Pod object
3738
// error: Error if pod doesn't exist
38-
func (c *Store) GetPod(podName string) (*v1.Pod, error) {
39-
metaPod, ok := c.metaPods.Load(podName)
39+
func (c *Store) GetPod(podName, podNamespace string) (*v1.Pod, error) {
40+
key := fmt.Sprintf("%s/%s", podNamespace, podName)
41+
metaPod, ok := c.metaPods.Load(key)
4042
if !ok {
4143
return nil, fmt.Errorf("pod does not exist in the cache: %s", podName)
4244
}
@@ -102,13 +104,15 @@ func (c *Store) HasModel(modelName string) bool {
102104
// Parameters:
103105
//
104106
// podName: Name of the Pod to query
107+
// podNamespace: Namespace of the Pod to query
105108
//
106109
// Returns:
107110
//
108111
// []string: Slice of model names
109112
// error: Error if Pod doesn't exist
110-
func (c *Store) ListModelsByPod(podName string) ([]string, error) {
111-
metaPod, ok := c.metaPods.Load(podName)
113+
func (c *Store) ListModelsByPod(podName, podNamespace string) ([]string, error) {
114+
key := fmt.Sprintf("%s/%s", podNamespace, podName)
115+
metaPod, ok := c.metaPods.Load(key)
112116
if !ok {
113117
return nil, fmt.Errorf("pod does not exist in the cache: %s", podName)
114118
}
@@ -119,15 +123,17 @@ func (c *Store) ListModelsByPod(podName string) ([]string, error) {
119123
// GetMetricValueByPod retrieves metric value for a Pod
120124
// Parameters:
121125
//
122-
// podName: Name of the Pod
123-
// metricName: Name of the metric
126+
// podName: Name of the Pod
127+
// podNamespace: Namespace of the Pod
128+
// metricName: Name of the metric
124129
//
125130
// Returns:
126131
//
127132
// metrics.MetricValue: The metric value
128133
// error: Error if Pod or metric doesn't exist
129-
func (c *Store) GetMetricValueByPod(podName, metricName string) (metrics.MetricValue, error) {
130-
metaPod, ok := c.metaPods.Load(podName)
134+
func (c *Store) GetMetricValueByPod(podName, podNamespace, metricName string) (metrics.MetricValue, error) {
135+
key := fmt.Sprintf("%s/%s", podNamespace, podName)
136+
metaPod, ok := c.metaPods.Load(key)
131137
if !ok {
132138
return nil, fmt.Errorf("pod does not exist in the cache: %s", podName)
133139
}
@@ -138,16 +144,18 @@ func (c *Store) GetMetricValueByPod(podName, metricName string) (metrics.MetricV
138144
// GetMetricValueByPodModel retrieves metric value for Pod-Model combination
139145
// Parameters:
140146
//
141-
// podName: Name of the Pod
142-
// modelName: Name of the model
143-
// metricName: Name of the metric
147+
// podName: Name of the Pod
148+
// podNamespace: Namespace of the Pod
149+
// modelName: Name of the model
150+
// metricName: Name of the metric
144151
//
145152
// Returns:
146153
//
147154
// metrics.MetricValue: The metric value
148155
// error: Error if Pod, model or metric doesn't exist
149-
func (c *Store) GetMetricValueByPodModel(podName, modelName string, metricName string) (metrics.MetricValue, error) {
150-
metaPod, ok := c.metaPods.Load(podName)
156+
func (c *Store) GetMetricValueByPodModel(podName, podNamespace, modelName string, metricName string) (metrics.MetricValue, error) {
157+
key := fmt.Sprintf("%s/%s", podNamespace, podName)
158+
metaPod, ok := c.metaPods.Load(key)
151159
if !ok {
152160
return nil, fmt.Errorf("pod does not exist in the cache: %s", podName)
153161
}

pkg/cache/cache_init.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package cache
1818

1919
import (
2020
"errors"
21+
"strings"
2122
"sync"
2223
"sync/atomic"
2324
"time"
@@ -51,7 +52,7 @@ type Store struct {
5152
numRequestsTraces int32 // Request trace counter
5253

5354
// Pod related storage
54-
metaPods utils.SyncMap[string, *Pod] // pod_name -> *Pod
55+
metaPods utils.SyncMap[string, *Pod] // pod_namespace/pod_name -> *Pod
5556

5657
// Mapping relationships
5758
metaModels utils.SyncMap[string, *Model] // model_name -> *Model
@@ -103,7 +104,14 @@ func NewTestCacheWithPods(pods []*v1.Pod, model string) *Store {
103104

104105
func NewTestCacheWithPodsMetrics(pods []*v1.Pod, model string, podMetrics map[string]map[string]metrics.MetricValue) *Store {
105106
c := NewTestCacheWithPods(pods, model)
106-
c.metaPods.Range(func(podName string, metaPod *Pod) bool {
107+
c.metaPods.Range(func(key string, metaPod *Pod) bool {
108+
// Split the key into namespace and name
109+
parts := strings.Split(key, "/")
110+
if len(parts) != 2 {
111+
// Invalid key format, skip this entry
112+
return true
113+
}
114+
_, podName := parts[0], parts[1]
107115
if podmetrics, ok := podMetrics[podName]; ok {
108116
for metricName, metric := range podmetrics {
109117
if err := c.updatePodRecord(metaPod, model, metricName, metrics.PodMetricScope, metric); err != nil {

pkg/cache/cache_log.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,14 @@ func (c *Store) debugInfo() {
2828
return
2929
}
3030

31-
c.metaPods.Range(func(podName string, pod *Pod) bool {
31+
c.metaPods.Range(func(key string, pod *Pod) bool {
32+
// Split the key into namespace and name
33+
parts := strings.Split(key, "/")
34+
if len(parts) != 2 {
35+
// Invalid key format, skip this entry
36+
return true
37+
}
38+
_, podName := parts[0], parts[1]
3239
klog.V(4).Infof("pod: %s, podIP: %v, models: %s", podName, pod.Status.PodIP, strings.Join(pod.Models.Array(), " "))
3340
pod.Metrics.Range(func(metricName string, metricVal metrics.MetricValue) bool {
3441
klog.V(5).Infof("%v_%v_%v", podName, metricName, metricVal)

pkg/cache/cache_metrics.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package cache
1818
import (
1919
"context"
2020
"fmt"
21+
"strings"
2122
"time"
2223

2324
prometheusv1 "github.com/prometheus/client_golang/api/prometheus/v1"
@@ -111,7 +112,14 @@ func (c *Store) getPodModelMetricName(modelName string, metricName string) strin
111112
}
112113

113114
func (c *Store) updatePodMetrics() {
114-
c.metaPods.Range(func(podName string, metaPod *Pod) bool {
115+
c.metaPods.Range(func(key string, metaPod *Pod) bool {
116+
// Split the key into namespace and name
117+
parts := strings.Split(key, "/")
118+
if len(parts) != 2 {
119+
// Invalid key format, skip this entry
120+
return true
121+
}
122+
_, _ = parts[0], parts[1]
115123
if !utils.FilterReadyPod(metaPod.Pod) {
116124
// Skip unready pod
117125
return true

0 commit comments

Comments
 (0)