@@ -263,6 +263,12 @@ var (
263
263
},
264
264
WaitingModels : map [string ]int {},
265
265
}
266
+ pod3 = & corev1.Pod {
267
+ ObjectMeta : metav1.ObjectMeta {
268
+ Name : "pod2" ,
269
+ },
270
+ }
271
+
266
272
pod1NamespacedName = types.NamespacedName {Name : pod1 .Name , Namespace : pod1 .Namespace }
267
273
pod2NamespacedName = types.NamespacedName {Name : pod2 .Name , Namespace : pod2 .Namespace }
268
274
inferencePool = & v1alpha2.InferencePool {
@@ -312,17 +318,19 @@ func TestMetrics(t *testing.T) {
312
318
},
313
319
},
314
320
storePods : []* corev1.Pod {pod1 , pod2 },
315
- want : []* backendmetrics.MetricsState {
316
- pod1Metrics ,
317
- // Failed to fetch pod2 metrics so it remains the default values.
318
- {
319
- ActiveModels : map [string ]int {},
320
- WaitingModels : map [string ]int {},
321
- WaitingQueueSize : 0 ,
322
- KVCacheUsagePercent : 0 ,
323
- MaxActiveModels : 0 ,
321
+ want : []* backendmetrics.MetricsState {pod1Metrics },
322
+ },
323
+ {
324
+ name : "Filter stale metrics" ,
325
+ pmc : & backendmetrics.FakePodMetricsClient {
326
+ Res : map [types.NamespacedName ]* backendmetrics.MetricsState {
327
+ pod1NamespacedName : pod1Metrics ,
328
+ pod2NamespacedName : pod2Metrics ,
324
329
},
325
330
},
331
+ storePods : []* corev1.Pod {pod1 , pod2 , pod3 },
332
+ want : []* backendmetrics.MetricsState {pod1Metrics , pod2Metrics }, // pod3 metrics were stale and should not be included.
333
+
326
334
},
327
335
}
328
336
@@ -336,16 +344,15 @@ func TestMetrics(t *testing.T) {
336
344
fakeClient := fake .NewClientBuilder ().
337
345
WithScheme (scheme ).
338
346
Build ()
339
- pmf := backendmetrics .NewPodMetricsFactory (test .pmc , time .Millisecond , config . DefaultMetricsStalenessThreshold )
347
+ pmf := backendmetrics .NewPodMetricsFactory (test .pmc , time .Millisecond , time . Second * 10 )
340
348
ds := NewDatastore (ctx , pmf )
341
349
_ = ds .PoolSet (ctx , fakeClient , inferencePool )
342
350
for _ , pod := range test .storePods {
343
351
ds .PodUpdateOrAddIfNotExist (pod )
344
352
}
353
+ time .Sleep (1 * time .Second ) // Give some time for the metrics to be fetched.
345
354
assert .EventuallyWithT (t , func (t * assert.CollectT ) {
346
- got := ds .PodList (func (backendmetrics.PodMetrics ) bool {
347
- return true
348
- })
355
+ got := ds .PodGetAllWithFreshMetrics ()
349
356
metrics := []* backendmetrics.MetricsState {}
350
357
for _ , one := range got {
351
358
metrics = append (metrics , one .GetMetrics ())
0 commit comments