mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-09-14 21:53:52 +00:00
Change the return of EquivalenceClass.lookupResult.
This makes the lookup behave like a normal map lookup, so it is easier for readers to follow the logic. It also inverts the "invalid" bool to an "ok" bool because `!invalid` is a double negative.
This commit is contained in:
@@ -85,9 +85,9 @@ func (ec *EquivalenceCache) RunPredicate(
|
|||||||
return false, []algorithm.PredicateFailureReason{}, fmt.Errorf("nodeInfo is nil or node is invalid")
|
return false, []algorithm.PredicateFailureReason{}, fmt.Errorf("nodeInfo is nil or node is invalid")
|
||||||
}
|
}
|
||||||
|
|
||||||
fit, reasons, invalid := ec.lookupResult(pod.GetName(), nodeInfo.Node().GetName(), predicateKey, equivClassInfo.hash)
|
result, ok := ec.lookupResult(pod.GetName(), nodeInfo.Node().GetName(), predicateKey, equivClassInfo.hash)
|
||||||
if !invalid {
|
if ok {
|
||||||
return fit, reasons, nil
|
return result.Fit, result.FailReasons, nil
|
||||||
}
|
}
|
||||||
fit, reasons, err := pred(pod, meta, nodeInfo)
|
fit, reasons, err := pred(pod, meta, nodeInfo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -139,26 +139,18 @@ func (ec *EquivalenceCache) updateResult(
|
|||||||
glog.V(5).Infof("Updated cached predicate: %v for pod: %v on node: %s, with item %v", predicateKey, podName, nodeName, predicateItem)
|
glog.V(5).Infof("Updated cached predicate: %v for pod: %v on node: %s, with item %v", predicateKey, podName, nodeName, predicateItem)
|
||||||
}
|
}
|
||||||
|
|
||||||
// lookupResult returns cached predicate results:
|
// lookupResult returns cached predicate results and a bool saying whether a
|
||||||
// 1. if pod fit
|
// cache entry was found.
|
||||||
// 2. reasons if pod did not fit
|
|
||||||
// 3. if cache item is not found
|
|
||||||
func (ec *EquivalenceCache) lookupResult(
|
func (ec *EquivalenceCache) lookupResult(
|
||||||
podName, nodeName, predicateKey string,
|
podName, nodeName, predicateKey string,
|
||||||
equivalenceHash uint64,
|
equivalenceHash uint64,
|
||||||
) (bool, []algorithm.PredicateFailureReason, bool) {
|
) (value predicateResult, ok bool) {
|
||||||
ec.mu.RLock()
|
ec.mu.RLock()
|
||||||
defer ec.mu.RUnlock()
|
defer ec.mu.RUnlock()
|
||||||
glog.V(5).Infof("Begin to calculate predicate: %v for pod: %s on node: %s based on equivalence cache",
|
glog.V(5).Infof("Begin to calculate predicate: %v for pod: %s on node: %s based on equivalence cache",
|
||||||
predicateKey, podName, nodeName)
|
predicateKey, podName, nodeName)
|
||||||
if result, exist := ec.cache[nodeName][predicateKey][equivalenceHash]; exist {
|
value, ok = ec.cache[nodeName][predicateKey][equivalenceHash]
|
||||||
if result.Fit {
|
return value, ok
|
||||||
return true, []algorithm.PredicateFailureReason{}, false
|
|
||||||
}
|
|
||||||
return false, result.FailReasons, false
|
|
||||||
}
|
|
||||||
// is invalid
|
|
||||||
return false, []algorithm.PredicateFailureReason{}, true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// InvalidateCachedPredicateItem marks all items of given predicateKeys, of all pods, on the given node as invalid
|
// InvalidateCachedPredicateItem marks all items of given predicateKeys, of all pods, on the given node as invalid
|
||||||
|
@@ -287,14 +287,14 @@ func TestRunPredicate(t *testing.T) {
|
|||||||
if !test.expectCacheHit && test.pred.callCount == 0 {
|
if !test.expectCacheHit && test.pred.callCount == 0 {
|
||||||
t.Errorf("Predicate should be called")
|
t.Errorf("Predicate should be called")
|
||||||
}
|
}
|
||||||
_, _, invalid := ecache.lookupResult(pod.Name, node.Node().Name, "testPredicate", equivClass.hash)
|
_, ok := ecache.lookupResult(pod.Name, node.Node().Name, "testPredicate", equivClass.hash)
|
||||||
if invalid && test.expectCacheWrite {
|
if !ok && test.expectCacheWrite {
|
||||||
t.Errorf("Cache write should happen")
|
t.Errorf("Cache write should happen")
|
||||||
}
|
}
|
||||||
if !test.expectCacheHit && test.expectCacheWrite && invalid {
|
if !test.expectCacheHit && test.expectCacheWrite && !ok {
|
||||||
t.Errorf("Cache write should happen")
|
t.Errorf("Cache write should happen")
|
||||||
}
|
}
|
||||||
if !test.expectCacheHit && !test.expectCacheWrite && !invalid {
|
if !test.expectCacheHit && !test.expectCacheWrite && ok {
|
||||||
t.Errorf("Cache write should not happen")
|
t.Errorf("Cache write should not happen")
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -396,8 +396,8 @@ func TestLookupResult(t *testing.T) {
|
|||||||
equivalenceHashForUpdatePredicate uint64
|
equivalenceHashForUpdatePredicate uint64
|
||||||
equivalenceHashForCalPredicate uint64
|
equivalenceHashForCalPredicate uint64
|
||||||
cachedItem predicateItemType
|
cachedItem predicateItemType
|
||||||
expectedInvalidPredicateKey bool
|
expectedPredicateKeyMiss bool
|
||||||
expectedInvalidEquivalenceHash bool
|
expectedEquivalenceHashMiss bool
|
||||||
expectedPredicateItem predicateItemType
|
expectedPredicateItem predicateItemType
|
||||||
cache schedulercache.Cache
|
cache schedulercache.Cache
|
||||||
}{
|
}{
|
||||||
@@ -412,7 +412,7 @@ func TestLookupResult(t *testing.T) {
|
|||||||
fit: false,
|
fit: false,
|
||||||
reasons: []algorithm.PredicateFailureReason{predicates.ErrPodNotFitsHostPorts},
|
reasons: []algorithm.PredicateFailureReason{predicates.ErrPodNotFitsHostPorts},
|
||||||
},
|
},
|
||||||
expectedInvalidPredicateKey: true,
|
expectedPredicateKeyMiss: true,
|
||||||
expectedPredicateItem: predicateItemType{
|
expectedPredicateItem: predicateItemType{
|
||||||
fit: false,
|
fit: false,
|
||||||
reasons: []algorithm.PredicateFailureReason{},
|
reasons: []algorithm.PredicateFailureReason{},
|
||||||
@@ -429,7 +429,7 @@ func TestLookupResult(t *testing.T) {
|
|||||||
cachedItem: predicateItemType{
|
cachedItem: predicateItemType{
|
||||||
fit: true,
|
fit: true,
|
||||||
},
|
},
|
||||||
expectedInvalidPredicateKey: false,
|
expectedPredicateKeyMiss: false,
|
||||||
expectedPredicateItem: predicateItemType{
|
expectedPredicateItem: predicateItemType{
|
||||||
fit: true,
|
fit: true,
|
||||||
reasons: []algorithm.PredicateFailureReason{},
|
reasons: []algorithm.PredicateFailureReason{},
|
||||||
@@ -447,7 +447,7 @@ func TestLookupResult(t *testing.T) {
|
|||||||
fit: false,
|
fit: false,
|
||||||
reasons: []algorithm.PredicateFailureReason{predicates.ErrPodNotFitsHostPorts},
|
reasons: []algorithm.PredicateFailureReason{predicates.ErrPodNotFitsHostPorts},
|
||||||
},
|
},
|
||||||
expectedInvalidPredicateKey: false,
|
expectedPredicateKeyMiss: false,
|
||||||
expectedPredicateItem: predicateItemType{
|
expectedPredicateItem: predicateItemType{
|
||||||
fit: false,
|
fit: false,
|
||||||
reasons: []algorithm.PredicateFailureReason{predicates.ErrPodNotFitsHostPorts},
|
reasons: []algorithm.PredicateFailureReason{predicates.ErrPodNotFitsHostPorts},
|
||||||
@@ -465,8 +465,8 @@ func TestLookupResult(t *testing.T) {
|
|||||||
fit: false,
|
fit: false,
|
||||||
reasons: []algorithm.PredicateFailureReason{predicates.ErrPodNotFitsHostPorts},
|
reasons: []algorithm.PredicateFailureReason{predicates.ErrPodNotFitsHostPorts},
|
||||||
},
|
},
|
||||||
expectedInvalidPredicateKey: false,
|
expectedPredicateKeyMiss: false,
|
||||||
expectedInvalidEquivalenceHash: true,
|
expectedEquivalenceHashMiss: true,
|
||||||
expectedPredicateItem: predicateItemType{
|
expectedPredicateItem: predicateItemType{
|
||||||
fit: false,
|
fit: false,
|
||||||
reasons: []algorithm.PredicateFailureReason{},
|
reasons: []algorithm.PredicateFailureReason{},
|
||||||
@@ -490,27 +490,32 @@ func TestLookupResult(t *testing.T) {
|
|||||||
node,
|
node,
|
||||||
)
|
)
|
||||||
// if we want to do invalid, invalid the cached item
|
// if we want to do invalid, invalid the cached item
|
||||||
if test.expectedInvalidPredicateKey {
|
if test.expectedPredicateKeyMiss {
|
||||||
predicateKeys := sets.NewString()
|
predicateKeys := sets.NewString()
|
||||||
predicateKeys.Insert(test.predicateKey)
|
predicateKeys.Insert(test.predicateKey)
|
||||||
ecache.InvalidateCachedPredicateItem(test.nodeName, predicateKeys)
|
ecache.InvalidateCachedPredicateItem(test.nodeName, predicateKeys)
|
||||||
}
|
}
|
||||||
// calculate predicate with equivalence cache
|
// calculate predicate with equivalence cache
|
||||||
fit, reasons, invalid := ecache.lookupResult(test.podName,
|
result, ok := ecache.lookupResult(test.podName,
|
||||||
test.nodeName,
|
test.nodeName,
|
||||||
test.predicateKey,
|
test.predicateKey,
|
||||||
test.equivalenceHashForCalPredicate,
|
test.equivalenceHashForCalPredicate,
|
||||||
)
|
)
|
||||||
// returned invalid should match expectedInvalidPredicateKey or expectedInvalidEquivalenceHash
|
fit, reasons := result.Fit, result.FailReasons
|
||||||
|
// returned invalid should match expectedPredicateKeyMiss or expectedEquivalenceHashMiss
|
||||||
if test.equivalenceHashForUpdatePredicate != test.equivalenceHashForCalPredicate {
|
if test.equivalenceHashForUpdatePredicate != test.equivalenceHashForCalPredicate {
|
||||||
if invalid != test.expectedInvalidEquivalenceHash {
|
if ok && test.expectedEquivalenceHashMiss {
|
||||||
t.Errorf("Failed: %s, expected invalid: %v, but got: %v",
|
t.Errorf("Failed: %s, expected (equivalence hash) cache miss", test.name)
|
||||||
test.name, test.expectedInvalidEquivalenceHash, invalid)
|
}
|
||||||
|
if !ok && !test.expectedEquivalenceHashMiss {
|
||||||
|
t.Errorf("Failed: %s, expected (equivalence hash) cache hit", test.name)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if invalid != test.expectedInvalidPredicateKey {
|
if ok && test.expectedPredicateKeyMiss {
|
||||||
t.Errorf("Failed: %s, expected invalid: %v, but got: %v",
|
t.Errorf("Failed: %s, expected (predicate key) cache miss", test.name)
|
||||||
test.name, test.expectedInvalidPredicateKey, invalid)
|
}
|
||||||
|
if !ok && !test.expectedPredicateKeyMiss {
|
||||||
|
t.Errorf("Failed: %s, expected (predicate key) cache hit", test.name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// returned predicate result should match expected predicate item
|
// returned predicate result should match expected predicate item
|
||||||
|
Reference in New Issue
Block a user