Skip to content

Commit

Permalink
[release-1.8] Improve scheduler logging for state and pending vpods (#…
Browse files Browse the repository at this point in the history
…6735)

This is an automated cherry-pick of #6729

Signed-off-by: Pierangelo Di Pilato <pierdipi@redhat.com>
Co-authored-by: Pierangelo Di Pilato <pierdipi@redhat.com>
  • Loading branch information
knative-prow-robot and pierDipi authored Feb 7, 2023
1 parent 00d2ae3 commit 738a1fc
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 12 deletions.
14 changes: 7 additions & 7 deletions pkg/scheduler/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,28 +50,28 @@ const (
// Policy describes a struct of a policy resource.
type SchedulerPolicy struct {
// Holds the information to configure the fit predicate functions.
Predicates []PredicatePolicy
Predicates []PredicatePolicy `json:"predicates"`
// Holds the information to configure the priority functions.
Priorities []PriorityPolicy
Priorities []PriorityPolicy `json:"priorities"`
}

// PredicatePolicy describes a struct of a predicate policy.
type PredicatePolicy struct {
// Identifier of the predicate policy
Name string
Name string `json:"name"`
// Holds the parameters to configure the given predicate
Args interface{}
Args interface{} `json:"args"`
}

// PriorityPolicy describes a struct of a priority policy.
type PriorityPolicy struct {
// Identifier of the priority policy
Name string
Name string `json:"name"`
// The numeric multiplier for the pod scores that the priority function generates
// The weight should be a positive integer
Weight uint64
Weight uint64 `json:"weight"`
// Holds the parameters to configure the given priority function
Args interface{}
Args interface{} `json:"args"`
}

// VPodLister is the function signature for returning a list of VPods
Expand Down
60 changes: 56 additions & 4 deletions pkg/scheduler/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ package state

import (
"context"
"encoding/json"
"errors"
"fmt"
"strconv"
"time"

Expand Down Expand Up @@ -319,10 +319,13 @@ func (s *stateBuilder) State(reserved map[types.NamespacedName]map[string]int32)
}
}

s.logger.Infow("cluster state info", zap.String("NumPods", fmt.Sprint(scale.Spec.Replicas)), zap.String("NumZones", fmt.Sprint(len(zoneMap))), zap.String("NumNodes", fmt.Sprint(len(nodeToZoneMap))), zap.String("Schedulable", fmt.Sprint(schedulablePods)))
return &State{FreeCap: free, SchedulablePods: schedulablePods.List(), LastOrdinal: last, Capacity: s.capacity, Replicas: scale.Spec.Replicas, NumZones: int32(len(zoneMap)), NumNodes: int32(len(nodeToZoneMap)),
state := &State{FreeCap: free, SchedulablePods: schedulablePods.List(), LastOrdinal: last, Capacity: s.capacity, Replicas: scale.Spec.Replicas, NumZones: int32(len(zoneMap)), NumNodes: int32(len(nodeToZoneMap)),
SchedulerPolicy: s.schedulerPolicy, SchedPolicy: s.schedPolicy, DeschedPolicy: s.deschedPolicy, NodeToZoneMap: nodeToZoneMap, StatefulSetName: s.statefulSetName, PodLister: s.podLister,
PodSpread: podSpread, NodeSpread: nodeSpread, ZoneSpread: zoneSpread}, nil
PodSpread: podSpread, NodeSpread: nodeSpread, ZoneSpread: zoneSpread}

s.logger.Infow("cluster state info", zap.Any("state", state), zap.Any("reserved", toJSONable(reserved)))

return state, nil
}

func (s *stateBuilder) updateFreeCapacity(free []int32, last int32, podName string, vreplicas int32) ([]int32, int32) {
Expand Down Expand Up @@ -413,3 +416,52 @@ func contains(taints []v1.Taint, taint *v1.Taint) bool {
}
return false
}

func (s *State) MarshalJSON() ([]byte, error) {

type S struct {
FreeCap []int32 `json:"freeCap"`
SchedulablePods []int32 `json:"schedulablePods"`
LastOrdinal int32 `json:"lastOrdinal"`
Capacity int32 `json:"capacity"`
Replicas int32 `json:"replicas"`
NumZones int32 `json:"numZones"`
NumNodes int32 `json:"numNodes"`
NodeToZoneMap map[string]string `json:"nodeToZoneMap"`
StatefulSetName string `json:"statefulSetName"`
PodSpread map[string]map[string]int32 `json:"podSpread"`
NodeSpread map[string]map[string]int32 `json:"nodeSpread"`
ZoneSpread map[string]map[string]int32 `json:"zoneSpread"`
SchedulerPolicy scheduler.SchedulerPolicyType `json:"schedulerPolicy"`
SchedPolicy *scheduler.SchedulerPolicy `json:"schedPolicy"`
DeschedPolicy *scheduler.SchedulerPolicy `json:"deschedPolicy"`
}

sj := S{
FreeCap: s.FreeCap,
SchedulablePods: s.SchedulablePods,
LastOrdinal: s.LastOrdinal,
Capacity: s.Capacity,
Replicas: s.Replicas,
NumZones: s.NumZones,
NumNodes: s.NumNodes,
NodeToZoneMap: s.NodeToZoneMap,
StatefulSetName: s.StatefulSetName,
PodSpread: toJSONable(s.PodSpread),
NodeSpread: toJSONable(s.NodeSpread),
ZoneSpread: toJSONable(s.ZoneSpread),
SchedulerPolicy: s.SchedulerPolicy,
SchedPolicy: s.SchedPolicy,
DeschedPolicy: s.DeschedPolicy,
}

return json.Marshal(sj)
}

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
}
return r
}
10 changes: 9 additions & 1 deletion pkg/scheduler/statefulset/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func (s *StatefulSetScheduler) Schedule(vpod scheduler.VPod) ([]duckv1alpha1.Pla

func (s *StatefulSetScheduler) scheduleVPod(vpod scheduler.VPod) ([]duckv1alpha1.Placement, error) {
logger := s.logger.With("key", vpod.GetKey())
logger.Info("scheduling")
logger.Infow("scheduling", zap.Any("pending", toJSONable(s.pending)))

// Get the current placements state
// Quite an expensive operation but safe and simple.
Expand Down Expand Up @@ -271,6 +271,14 @@ func (s *StatefulSetScheduler) scheduleVPod(vpod scheduler.VPod) ([]duckv1alpha1
return placements, nil
}

func toJSONable(pending map[types.NamespacedName]int32) map[string]int32 {
r := make(map[string]int32, len(pending))
for k, v := range pending {
r[k.String()] = v
}
return r
}

func (s *StatefulSetScheduler) rebalanceReplicasWithPolicy(vpod scheduler.VPod, diff int32, placements []duckv1alpha1.Placement) ([]duckv1alpha1.Placement, int32) {
s.makeZeroPlacements(vpod, placements)
placements, diff = s.addReplicasWithPolicy(vpod, diff, make([]duckv1alpha1.Placement, 0)) //start fresh with a new placements list
Expand Down

0 comments on commit 738a1fc

Please sign in to comment.