Substitute boolean maps with String sets

This commit is contained in:
Deepak Nair
2021-02-18 14:53:00 -08:00
parent 20b03f6365
commit 49a9e28dd8
6 changed files with 36 additions and 42 deletions

View File

@@ -63,7 +63,7 @@ type schedulerCache struct {
mu sync.RWMutex
// a set of assumed pod keys.
// The key could further be used to get an entry in podStates.
assumedPods map[string]bool
assumedPods sets.String
// a map from pod key to podState.
podStates map[string]*podState
nodes map[string]*nodeInfoListItem
@@ -106,7 +106,7 @@ func newSchedulerCache(ttl, period time.Duration, stop <-chan struct{}) *schedul
nodes: make(map[string]*nodeInfoListItem),
nodeTree: newNodeTree(nil),
assumedPods: make(map[string]bool),
assumedPods: make(sets.String),
podStates: make(map[string]*podState),
imageStates: make(map[string]*imageState),
}
@@ -183,14 +183,9 @@ func (cache *schedulerCache) Dump() *Dump {
nodes[k] = v.info.Clone()
}
assumedPods := make(map[string]bool, len(cache.assumedPods))
for k, v := range cache.assumedPods {
assumedPods[k] = v
}
return &Dump{
Nodes: nodes,
AssumedPods: assumedPods,
AssumedPods: cache.assumedPods.Union(nil),
}
}
@@ -375,7 +370,7 @@ func (cache *schedulerCache) AssumePod(pod *v1.Pod) error {
pod: pod,
}
cache.podStates[key] = ps
cache.assumedPods[key] = true
cache.assumedPods.Insert(key)
return nil
}
@@ -395,7 +390,7 @@ func (cache *schedulerCache) finishBinding(pod *v1.Pod, now time.Time) error {
klog.V(5).Infof("Finished binding for pod %v. Can be expired.", key)
currState, ok := cache.podStates[key]
if ok && cache.assumedPods[key] {
if ok && cache.assumedPods.Has(key) {
dl := now.Add(cache.ttl)
currState.bindingFinished = true
currState.deadline = &dl
@@ -419,7 +414,7 @@ func (cache *schedulerCache) ForgetPod(pod *v1.Pod) error {
switch {
// Only assumed pod can be forgotten.
case ok && cache.assumedPods[key]:
case ok && cache.assumedPods.Has(key):
err := cache.removePod(pod)
if err != nil {
return err
@@ -484,7 +479,7 @@ func (cache *schedulerCache) AddPod(pod *v1.Pod) error {
currState, ok := cache.podStates[key]
switch {
case ok && cache.assumedPods[key]:
case ok && cache.assumedPods.Has(key):
if currState.pod.Spec.NodeName != pod.Spec.NodeName {
// The pod was added to a different node than it was assumed to.
klog.Warningf("Pod %v was assumed to be on %v but got added to %v", key, pod.Spec.NodeName, currState.pod.Spec.NodeName)
@@ -523,7 +518,7 @@ func (cache *schedulerCache) UpdatePod(oldPod, newPod *v1.Pod) error {
switch {
// An assumed pod won't have Update/Remove event. It needs to have Add event
// before Update event, in which case the state would change from Assumed to Added.
case ok && !cache.assumedPods[key]:
case ok && !cache.assumedPods.Has(key):
if currState.pod.Spec.NodeName != newPod.Spec.NodeName {
klog.Errorf("Pod %v updated on a different node than previously added to.", key)
klog.Fatalf("Schedulercache is corrupted and can badly affect scheduling decisions")
@@ -551,7 +546,7 @@ func (cache *schedulerCache) RemovePod(pod *v1.Pod) error {
switch {
// An assumed pod won't have Delete/Remove event. It needs to have Add event
// before Remove event, in which case the state would change from Assumed to Added.
case ok && !cache.assumedPods[key]:
case ok && !cache.assumedPods.Has(key):
if currState.pod.Spec.NodeName != pod.Spec.NodeName {
klog.Errorf("Pod %v was assumed to be on %v but got added to %v", key, pod.Spec.NodeName, currState.pod.Spec.NodeName)
klog.Fatalf("Schedulercache is corrupted and can badly affect scheduling decisions")
@@ -576,11 +571,7 @@ func (cache *schedulerCache) IsAssumedPod(pod *v1.Pod) (bool, error) {
cache.mu.RLock()
defer cache.mu.RUnlock()
b, found := cache.assumedPods[key]
if !found {
return false, nil
}
return b, nil
return cache.assumedPods.Has(key), nil
}
// GetPod might return a pod for which its node has already been deleted from

View File

@@ -17,7 +17,8 @@ limitations under the License.
package cache
import (
"k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/kubernetes/pkg/scheduler/framework"
)
@@ -114,6 +115,6 @@ type Cache interface {
// Dump is a dump of the cache state.
type Dump struct {
AssumedPods map[string]bool
AssumedPods sets.String
Nodes map[string]*framework.NodeInfo
}