From 143aa049da2de8ae5d034d8afaaf73cfee94655a Mon Sep 17 00:00:00 2001 From: Pierangelo Di Pilato Date: Fri, 25 Oct 2024 16:35:05 +0200 Subject: [PATCH] Log reserved Signed-off-by: Pierangelo Di Pilato --- pkg/scheduler/state/state.go | 4 +- pkg/scheduler/statefulset/scheduler.go | 11 ++- pkg/scheduler/statefulset/scheduler_test.go | 74 ++++++++++++++++++++- 3 files changed, 83 insertions(+), 6 deletions(-) diff --git a/pkg/scheduler/state/state.go b/pkg/scheduler/state/state.go index 5fd41d6d8f9..9d4503b9158 100644 --- a/pkg/scheduler/state/state.go +++ b/pkg/scheduler/state/state.go @@ -303,14 +303,14 @@ func (s *State) MarshalJSON() ([]byte, error) { Capacity: s.Capacity, Replicas: s.Replicas, StatefulSetName: s.StatefulSetName, - PodSpread: toJSONable(s.PodSpread), + PodSpread: ToJSONable(s.PodSpread), Pending: toJSONablePending(s.Pending), } return json.Marshal(sj) } -func toJSONable(ps map[types.NamespacedName]map[string]int32) map[string]map[string]int32 { +func ToJSONable(ps map[types.NamespacedName]map[string]int32) map[string]map[string]int32 { r := make(map[string]map[string]int32, len(ps)) for k, v := range ps { r[k.String()] = v diff --git a/pkg/scheduler/statefulset/scheduler.go b/pkg/scheduler/statefulset/scheduler.go index 2a6f93ee36a..64c2dd06dca 100644 --- a/pkg/scheduler/statefulset/scheduler.go +++ b/pkg/scheduler/statefulset/scheduler.go @@ -215,8 +215,6 @@ func (s *StatefulSetScheduler) scheduleVPod(ctx context.Context, vpod scheduler. return nil, err } - logger.Debugw("scheduling", zap.Any("state", state)) - existingPlacements := vpod.GetPlacements() reservedByPodName := make(map[string]int32, 2) @@ -231,6 +229,13 @@ func (s *StatefulSetScheduler) scheduleVPod(ctx context.Context, vpod scheduler. } } + logger.Debugw("scheduling", + zap.Any("state", state), + zap.Any("reservedByPodName", reservedByPodName), + zap.Any("reserved", st.ToJSONable(s.reserved)), + zap.Any("vpod", vpod), + ) + // Remove unschedulable or adjust overcommitted pods from placements var placements []duckv1alpha1.Placement if len(existingPlacements) > 0 { @@ -505,4 +510,4 @@ func upsertPlacements(placements []duckv1alpha1.Placement, placement duckv1alpha placements = append(placements, placement) } return placements -} +} \ No newline at end of file diff --git a/pkg/scheduler/statefulset/scheduler_test.go b/pkg/scheduler/statefulset/scheduler_test.go index 35245480cde..a30a659fe9d 100644 --- a/pkg/scheduler/statefulset/scheduler_test.go +++ b/pkg/scheduler/statefulset/scheduler_test.go @@ -55,6 +55,7 @@ func TestStatefulsetScheduler(t *testing.T) { pending map[types.NamespacedName]int32 initialReserved map[types.NamespacedName]map[string]int32 expectedReserved map[types.NamespacedName]map[string]int32 + capacity int32 }{ { name: "no replicas, no vreplicas", @@ -460,6 +461,72 @@ func TestStatefulsetScheduler(t *testing.T) { }, }, }, + { + name: "issue", + vreplicas: 32, + replicas: int32(2), + placements: []duckv1alpha1.Placement{ + {PodName: "statefulset-name-0", VReplicas: 1}, + }, + expected: []duckv1alpha1.Placement{ + {PodName: "statefulset-name-0", VReplicas: 13}, + {PodName: "statefulset-name-1", VReplicas: 19}, + }, + initialReserved: map[types.NamespacedName]map[string]int32{ + types.NamespacedName{Namespace: vpodNamespace + "-a", Name: vpodName}: { + "statefulset-name-0": 1, + }, + types.NamespacedName{Namespace: vpodNamespace + "-b", Name: vpodName}: { + "statefulset-name-0": 1, + }, + types.NamespacedName{Namespace: vpodNamespace + "-c", Name: vpodName}: { + "statefulset-name-0": 1, + }, + types.NamespacedName{Namespace: vpodNamespace + "-d", Name: vpodName}: { + "statefulset-name-0": 1, + }, + types.NamespacedName{Namespace: vpodNamespace + "-e", Name: vpodName}: { + "statefulset-name-0": 1, + }, + types.NamespacedName{Namespace: vpodNamespace + "-f", Name: vpodName}: { + "statefulset-name-0": 1, + }, + types.NamespacedName{Namespace: vpodNamespace + "-g", Name: vpodName}: { + "statefulset-name-0": 1, + }, + types.NamespacedName{Namespace: vpodNamespace, Name: vpodName}: { + "statefulset-name-0": 1, + }, + }, + expectedReserved: map[types.NamespacedName]map[string]int32{ + types.NamespacedName{Namespace: vpodNamespace + "-a", Name: vpodName}: { + "statefulset-name-0": 1, + }, + types.NamespacedName{Namespace: vpodNamespace + "-b", Name: vpodName}: { + "statefulset-name-0": 1, + }, + types.NamespacedName{Namespace: vpodNamespace + "-c", Name: vpodName}: { + "statefulset-name-0": 1, + }, + types.NamespacedName{Namespace: vpodNamespace + "-d", Name: vpodName}: { + "statefulset-name-0": 1, + }, + types.NamespacedName{Namespace: vpodNamespace + "-e", Name: vpodName}: { + "statefulset-name-0": 1, + }, + types.NamespacedName{Namespace: vpodNamespace + "-f", Name: vpodName}: { + "statefulset-name-0": 1, + }, + types.NamespacedName{Namespace: vpodNamespace + "-g", Name: vpodName}: { + "statefulset-name-0": 1, + }, + types.NamespacedName{Namespace: vpodNamespace, Name: vpodName}: { + "statefulset-name-0": 13, + "statefulset-name-1": 19, + }, + }, + capacity: 20, + }, } for _, tc := range testCases { @@ -480,13 +547,18 @@ func TestStatefulsetScheduler(t *testing.T) { podlist = append(podlist, pod) } + capacity := int32(0) + if tc.capacity > 0 { + capacity = tc.capacity + } + _, err := kubeclient.Get(ctx).AppsV1().StatefulSets(testNs).Create(ctx, tscheduler.MakeStatefulset(testNs, sfsName, tc.replicas), metav1.CreateOptions{}) if err != nil { t.Fatal("unexpected error", err) } lsp := listers.NewListers(podlist) scaleCache := scheduler.NewScaleCache(ctx, testNs, kubeclient.Get(ctx).AppsV1().StatefulSets(testNs), scheduler.ScaleCacheConfig{RefreshPeriod: time.Minute * 5}) - sa := state.NewStateBuilder(sfsName, vpodClient.List, 10, lsp.GetPodLister().Pods(testNs), scaleCache) + sa := state.NewStateBuilder(sfsName, vpodClient.List, capacity, lsp.GetPodLister().Pods(testNs), scaleCache) cfg := &Config{ StatefulSetNamespace: testNs, StatefulSetName: sfsName,