|
| 1 | +// Copyright 2026 NVIDIA CORPORATION |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package framework |
| 5 | + |
| 6 | +import ( |
| 7 | + "runtime" |
| 8 | + "strconv" |
| 9 | + "testing" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | + "go.uber.org/mock/gomock" |
| 14 | + |
| 15 | + schedulingv1alpha2 "github.com/kai-scheduler/KAI-scheduler/pkg/apis/scheduling/v1alpha2" |
| 16 | + "github.com/kai-scheduler/KAI-scheduler/pkg/scheduler/api" |
| 17 | + "github.com/kai-scheduler/KAI-scheduler/pkg/scheduler/api/bindrequest_info" |
| 18 | + "github.com/kai-scheduler/KAI-scheduler/pkg/scheduler/api/common_info" |
| 19 | + scheduler_cache "github.com/kai-scheduler/KAI-scheduler/pkg/scheduler/cache" |
| 20 | + "github.com/kai-scheduler/KAI-scheduler/pkg/scheduler/conf" |
| 21 | +) |
| 22 | + |
| 23 | +const benchmarkBindRequestCount = 5000 |
| 24 | + |
| 25 | +func TestSessionClearDropsRetainedReferences(t *testing.T) { |
| 26 | + ssn := &Session{ |
| 27 | + ClusterInfo: &api.ClusterInfo{ |
| 28 | + BindRequests: bindrequest_info.BindRequestMap{ |
| 29 | + bindrequest_info.NewKey("namespace", "pod"): &bindrequest_info.BindRequestInfo{}, |
| 30 | + }, |
| 31 | + BindRequestsForDeletedNodes: []*bindrequest_info.BindRequestInfo{{}}, |
| 32 | + }, |
| 33 | + Config: &conf.SchedulerConfiguration{}, |
| 34 | + plugins: map[string]Plugin{"plugin": nil}, |
| 35 | + eventHandlers: []*EventHandler{{}}, |
| 36 | + TaskOrderFns: []common_info.CompareFn{nil}, |
| 37 | + PrePredicateFns: []api.PrePredicateFn{nil}, |
| 38 | + BindRequestMutateFns: []api.BindRequestMutateFn{nil}, |
| 39 | + } |
| 40 | + ssn.k8sResourceStateCache.Store("resource", "state") |
| 41 | + |
| 42 | + ssn.clear() |
| 43 | + |
| 44 | + assert.Nil(t, ssn.ClusterInfo) |
| 45 | + assert.Nil(t, ssn.Config) |
| 46 | + assert.Nil(t, ssn.plugins) |
| 47 | + assert.Nil(t, ssn.eventHandlers) |
| 48 | + assert.Nil(t, ssn.TaskOrderFns) |
| 49 | + assert.Nil(t, ssn.PrePredicateFns) |
| 50 | + assert.Nil(t, ssn.BindRequestMutateFns) |
| 51 | + _, found := ssn.k8sResourceStateCache.Load("resource") |
| 52 | + assert.False(t, found) |
| 53 | +} |
| 54 | + |
| 55 | +func TestCloseSessionReleasesSnapshotReferencesWhileSessionIsLive(t *testing.T) { |
| 56 | + finalized := make(chan struct{}) |
| 57 | + cacheMock := scheduler_cache.NewMockCache(gomock.NewController(t)) |
| 58 | + cacheMock.EXPECT().WaitForWorkers(gomock.Any()).Times(1) |
| 59 | + ssn := newSessionWithFinalizedBindRequest(t, cacheMock, finalized) |
| 60 | + |
| 61 | + closeSession(ssn) |
| 62 | + |
| 63 | + requireFinalized(t, finalized, ssn) |
| 64 | +} |
| 65 | + |
| 66 | +func BenchmarkOpenCloseSessionWithLargeSnapshot(b *testing.B) { |
| 67 | + cacheMock := scheduler_cache.NewMockCache(gomock.NewController(b)) |
| 68 | + cacheMock.EXPECT().Snapshot().AnyTimes().DoAndReturn(func() (*api.ClusterInfo, error) { |
| 69 | + return newClusterInfoWithBindRequests(benchmarkBindRequestCount), nil |
| 70 | + }) |
| 71 | + cacheMock.EXPECT().WaitForWorkers(gomock.Any()).AnyTimes() |
| 72 | + |
| 73 | + runtime.GC() |
| 74 | + before := heapAlloc() |
| 75 | + |
| 76 | + b.ReportAllocs() |
| 77 | + b.ResetTimer() |
| 78 | + for i := 0; i < b.N; i++ { |
| 79 | + ssn, err := openSession(cacheMock, "benchmark", conf.SchedulerParams{}, nil) |
| 80 | + if err != nil { |
| 81 | + b.Fatal(err) |
| 82 | + } |
| 83 | + closeSession(ssn) |
| 84 | + } |
| 85 | + b.StopTimer() |
| 86 | + |
| 87 | + runtime.GC() |
| 88 | + after := heapAlloc() |
| 89 | + retained := int64(after) - int64(before) |
| 90 | + if retained < 0 { |
| 91 | + retained = 0 |
| 92 | + } |
| 93 | + b.ReportMetric(benchmarkBindRequestCount, "bind_requests/op") |
| 94 | + b.ReportMetric(float64(retained)/float64(b.N), "retained_after_1_gc_B/op") |
| 95 | +} |
| 96 | + |
| 97 | +func newSessionWithFinalizedBindRequest( |
| 98 | + t *testing.T, cache scheduler_cache.Cache, finalized chan<- struct{}, |
| 99 | +) *Session { |
| 100 | + t.Helper() |
| 101 | + |
| 102 | + bindRequest := &schedulingv1alpha2.BindRequest{ |
| 103 | + Spec: schedulingv1alpha2.BindRequestSpec{ |
| 104 | + PodName: "pod", |
| 105 | + }, |
| 106 | + } |
| 107 | + runtime.SetFinalizer(bindRequest, func(*schedulingv1alpha2.BindRequest) { |
| 108 | + close(finalized) |
| 109 | + }) |
| 110 | + |
| 111 | + ssn := &Session{ |
| 112 | + Cache: cache, |
| 113 | + ClusterInfo: &api.ClusterInfo{ |
| 114 | + BindRequests: bindrequest_info.BindRequestMap{ |
| 115 | + bindrequest_info.NewKey("namespace", "pod"): bindrequest_info.NewBindRequestInfo(bindRequest), |
| 116 | + }, |
| 117 | + BindRequestsForDeletedNodes: []*bindrequest_info.BindRequestInfo{ |
| 118 | + bindrequest_info.NewBindRequestInfo(bindRequest), |
| 119 | + }, |
| 120 | + }, |
| 121 | + } |
| 122 | + if err := ssn.InitNodeScoringPool(); err != nil { |
| 123 | + t.Fatalf("failed to initialize node scoring pool: %v", err) |
| 124 | + } |
| 125 | + return ssn |
| 126 | +} |
| 127 | + |
| 128 | +func requireFinalized(t *testing.T, finalized <-chan struct{}, keepAlive *Session) { |
| 129 | + t.Helper() |
| 130 | + |
| 131 | + deadline := time.NewTimer(3 * time.Second) |
| 132 | + defer deadline.Stop() |
| 133 | + |
| 134 | + for { |
| 135 | + runtime.GC() |
| 136 | + select { |
| 137 | + case <-finalized: |
| 138 | + runtime.KeepAlive(keepAlive) |
| 139 | + return |
| 140 | + case <-deadline.C: |
| 141 | + runtime.KeepAlive(keepAlive) |
| 142 | + t.Fatal("snapshot bind request was still reachable after closeSession") |
| 143 | + default: |
| 144 | + time.Sleep(10 * time.Millisecond) |
| 145 | + } |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +func heapAlloc() uint64 { |
| 150 | + var stats runtime.MemStats |
| 151 | + runtime.ReadMemStats(&stats) |
| 152 | + return stats.HeapAlloc |
| 153 | +} |
| 154 | + |
| 155 | +func newClusterInfoWithBindRequests(count int) *api.ClusterInfo { |
| 156 | + clusterInfo := &api.ClusterInfo{ |
| 157 | + BindRequests: make(bindrequest_info.BindRequestMap, count), |
| 158 | + } |
| 159 | + for i := 0; i < count; i++ { |
| 160 | + podName := "pod-" + strconv.Itoa(i) |
| 161 | + bindRequest := &schedulingv1alpha2.BindRequest{ |
| 162 | + Spec: schedulingv1alpha2.BindRequestSpec{ |
| 163 | + PodName: podName, |
| 164 | + }, |
| 165 | + } |
| 166 | + clusterInfo.BindRequests[bindrequest_info.NewKey("namespace", podName)] = |
| 167 | + bindrequest_info.NewBindRequestInfo(bindRequest) |
| 168 | + } |
| 169 | + return clusterInfo |
| 170 | +} |
0 commit comments