Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
79d30b1a
Commit
79d30b1a
authored
Apr 25, 2018
by
Jonathan Basseri
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Hide EquivalenceCache mutex from users.
Since the equiv. cache lock no longer needs to be held across multiple method calls, move the locking inside and don't expose it to users.
parent
b8518422
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
26 additions
and
26 deletions
+26
-26
equivalence_cache.go
pkg/scheduler/core/equivalence_cache.go
+12
-21
equivalence_cache_test.go
pkg/scheduler/core/equivalence_cache_test.go
+14
-5
No files found.
pkg/scheduler/core/equivalence_cache.go
View file @
79d30b1a
...
@@ -38,7 +38,7 @@ const maxCacheEntries = 100
...
@@ -38,7 +38,7 @@ const maxCacheEntries = 100
// 1. a map of AlgorithmCache with node name as key
// 1. a map of AlgorithmCache with node name as key
// 2. function to get equivalence pod
// 2. function to get equivalence pod
type
EquivalenceCache
struct
{
type
EquivalenceCache
struct
{
sync
.
RW
Mutex
mu
sync
.
Mutex
algorithmCache
map
[
string
]
AlgorithmCache
algorithmCache
map
[
string
]
AlgorithmCache
}
}
...
@@ -84,9 +84,9 @@ func (ec *EquivalenceCache) RunPredicate(
...
@@ -84,9 +84,9 @@ func (ec *EquivalenceCache) RunPredicate(
equivClassInfo
*
equivalenceClassInfo
,
equivClassInfo
*
equivalenceClassInfo
,
cache
schedulercache
.
Cache
,
cache
schedulercache
.
Cache
,
)
(
bool
,
[]
algorithm
.
PredicateFailureReason
,
error
)
{
)
(
bool
,
[]
algorithm
.
PredicateFailureReason
,
error
)
{
ec
.
Lock
()
ec
.
mu
.
Lock
()
defer
ec
.
Unlock
()
defer
ec
.
mu
.
Unlock
()
fit
,
reasons
,
invalid
:=
ec
.
lookupResult
(
pod
.
GetName
(),
nodeInfo
.
Node
()
.
GetName
(),
predicateKey
,
equivClassInfo
.
hash
,
false
)
fit
,
reasons
,
invalid
:=
ec
.
lookupResult
(
pod
.
GetName
(),
nodeInfo
.
Node
()
.
GetName
(),
predicateKey
,
equivClassInfo
.
hash
)
if
!
invalid
{
if
!
invalid
{
return
fit
,
reasons
,
nil
return
fit
,
reasons
,
nil
}
}
...
@@ -96,7 +96,7 @@ func (ec *EquivalenceCache) RunPredicate(
...
@@ -96,7 +96,7 @@ func (ec *EquivalenceCache) RunPredicate(
}
}
// Skip update if NodeInfo is stale.
// Skip update if NodeInfo is stale.
if
cache
!=
nil
&&
cache
.
IsUpToDate
(
nodeInfo
)
{
if
cache
!=
nil
&&
cache
.
IsUpToDate
(
nodeInfo
)
{
ec
.
updateResult
(
pod
.
GetName
(),
nodeInfo
.
Node
()
.
GetName
(),
predicateKey
,
fit
,
reasons
,
equivClassInfo
.
hash
,
false
)
ec
.
updateResult
(
pod
.
GetName
(),
nodeInfo
.
Node
()
.
GetName
(),
predicateKey
,
fit
,
reasons
,
equivClassInfo
.
hash
)
}
}
return
fit
,
reasons
,
nil
return
fit
,
reasons
,
nil
}
}
...
@@ -107,12 +107,7 @@ func (ec *EquivalenceCache) updateResult(
...
@@ -107,12 +107,7 @@ func (ec *EquivalenceCache) updateResult(
fit
bool
,
fit
bool
,
reasons
[]
algorithm
.
PredicateFailureReason
,
reasons
[]
algorithm
.
PredicateFailureReason
,
equivalenceHash
uint64
,
equivalenceHash
uint64
,
needLock
bool
,
)
{
)
{
if
needLock
{
ec
.
Lock
()
defer
ec
.
Unlock
()
}
if
_
,
exist
:=
ec
.
algorithmCache
[
nodeName
];
!
exist
{
if
_
,
exist
:=
ec
.
algorithmCache
[
nodeName
];
!
exist
{
ec
.
algorithmCache
[
nodeName
]
=
newAlgorithmCache
()
ec
.
algorithmCache
[
nodeName
]
=
newAlgorithmCache
()
}
}
...
@@ -140,12 +135,8 @@ func (ec *EquivalenceCache) updateResult(
...
@@ -140,12 +135,8 @@ func (ec *EquivalenceCache) updateResult(
// 3. if cache item is not found
// 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
,
needLock
bool
,
equivalenceHash
uint64
,
)
(
bool
,
[]
algorithm
.
PredicateFailureReason
,
bool
)
{
)
(
bool
,
[]
algorithm
.
PredicateFailureReason
,
bool
)
{
if
needLock
{
ec
.
RLock
()
defer
ec
.
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
algorithmCache
,
exist
:=
ec
.
algorithmCache
[
nodeName
];
exist
{
if
algorithmCache
,
exist
:=
ec
.
algorithmCache
[
nodeName
];
exist
{
...
@@ -170,8 +161,8 @@ func (ec *EquivalenceCache) InvalidateCachedPredicateItem(nodeName string, predi
...
@@ -170,8 +161,8 @@ func (ec *EquivalenceCache) InvalidateCachedPredicateItem(nodeName string, predi
if
len
(
predicateKeys
)
==
0
{
if
len
(
predicateKeys
)
==
0
{
return
return
}
}
ec
.
Lock
()
ec
.
mu
.
Lock
()
defer
ec
.
Unlock
()
defer
ec
.
mu
.
Unlock
()
if
algorithmCache
,
exist
:=
ec
.
algorithmCache
[
nodeName
];
exist
{
if
algorithmCache
,
exist
:=
ec
.
algorithmCache
[
nodeName
];
exist
{
for
predicateKey
:=
range
predicateKeys
{
for
predicateKey
:=
range
predicateKeys
{
algorithmCache
.
predicatesCache
.
Remove
(
predicateKey
)
algorithmCache
.
predicatesCache
.
Remove
(
predicateKey
)
...
@@ -185,8 +176,8 @@ func (ec *EquivalenceCache) InvalidateCachedPredicateItemOfAllNodes(predicateKey
...
@@ -185,8 +176,8 @@ func (ec *EquivalenceCache) InvalidateCachedPredicateItemOfAllNodes(predicateKey
if
len
(
predicateKeys
)
==
0
{
if
len
(
predicateKeys
)
==
0
{
return
return
}
}
ec
.
Lock
()
ec
.
mu
.
Lock
()
defer
ec
.
Unlock
()
defer
ec
.
mu
.
Unlock
()
// algorithmCache uses nodeName as key, so we just iterate it and invalid given predicates
// algorithmCache uses nodeName as key, so we just iterate it and invalid given predicates
for
_
,
algorithmCache
:=
range
ec
.
algorithmCache
{
for
_
,
algorithmCache
:=
range
ec
.
algorithmCache
{
for
predicateKey
:=
range
predicateKeys
{
for
predicateKey
:=
range
predicateKeys
{
...
@@ -199,8 +190,8 @@ func (ec *EquivalenceCache) InvalidateCachedPredicateItemOfAllNodes(predicateKey
...
@@ -199,8 +190,8 @@ func (ec *EquivalenceCache) InvalidateCachedPredicateItemOfAllNodes(predicateKey
// InvalidateAllCachedPredicateItemOfNode marks all cached items on given node as invalid
// InvalidateAllCachedPredicateItemOfNode marks all cached items on given node as invalid
func
(
ec
*
EquivalenceCache
)
InvalidateAllCachedPredicateItemOfNode
(
nodeName
string
)
{
func
(
ec
*
EquivalenceCache
)
InvalidateAllCachedPredicateItemOfNode
(
nodeName
string
)
{
ec
.
Lock
()
ec
.
mu
.
Lock
()
defer
ec
.
Unlock
()
defer
ec
.
mu
.
Unlock
()
delete
(
ec
.
algorithmCache
,
nodeName
)
delete
(
ec
.
algorithmCache
,
nodeName
)
glog
.
V
(
5
)
.
Infof
(
"Done invalidating all cached predicates on node: %s"
,
nodeName
)
glog
.
V
(
5
)
.
Infof
(
"Done invalidating all cached predicates on node: %s"
,
nodeName
)
}
}
...
...
pkg/scheduler/core/equivalence_cache_test.go
View file @
79d30b1a
...
@@ -253,7 +253,9 @@ func TestRunPredicate(t *testing.T) {
...
@@ -253,7 +253,9 @@ func TestRunPredicate(t *testing.T) {
ecache
:=
NewEquivalenceCache
()
ecache
:=
NewEquivalenceCache
()
equivClass
:=
ecache
.
getEquivalenceClassInfo
(
pod
)
equivClass
:=
ecache
.
getEquivalenceClassInfo
(
pod
)
if
test
.
expectCacheHit
{
if
test
.
expectCacheHit
{
ecache
.
mu
.
Lock
()
ecache
.
updateResult
(
pod
.
Name
,
node
.
Node
()
.
Name
,
"testPredicate"
,
test
.
expectFit
,
test
.
expectedReasons
,
equivClass
.
hash
)
ecache
.
updateResult
(
pod
.
Name
,
node
.
Node
()
.
Name
,
"testPredicate"
,
test
.
expectFit
,
test
.
expectedReasons
,
equivClass
.
hash
)
ecache
.
mu
.
Unlock
()
}
}
fit
,
reasons
,
err
:=
ecache
.
RunPredicate
(
test
.
pred
.
predicate
,
"testPredicate"
,
pod
,
meta
,
node
,
equivClass
,
test
.
cache
)
fit
,
reasons
,
err
:=
ecache
.
RunPredicate
(
test
.
pred
.
predicate
,
"testPredicate"
,
pod
,
meta
,
node
,
equivClass
,
test
.
cache
)
...
@@ -287,7 +289,9 @@ func TestRunPredicate(t *testing.T) {
...
@@ -287,7 +289,9 @@ 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"
)
}
}
ecache
.
mu
.
Lock
()
_
,
_
,
invalid
:=
ecache
.
lookupResult
(
pod
.
Name
,
node
.
Node
()
.
Name
,
"testPredicate"
,
equivClass
.
hash
)
_
,
_
,
invalid
:=
ecache
.
lookupResult
(
pod
.
Name
,
node
.
Node
()
.
Name
,
"testPredicate"
,
equivClass
.
hash
)
ecache
.
mu
.
Unlock
()
if
invalid
&&
test
.
expectCacheWrite
{
if
invalid
&&
test
.
expectCacheWrite
{
t
.
Errorf
(
"Cache write should happen"
)
t
.
Errorf
(
"Cache write should happen"
)
}
}
...
@@ -350,6 +354,7 @@ func TestUpdateResult(t *testing.T) {
...
@@ -350,6 +354,7 @@ func TestUpdateResult(t *testing.T) {
test
.
equivalenceHash
:
predicateItem
,
test
.
equivalenceHash
:
predicateItem
,
})
})
}
}
ecache
.
mu
.
Lock
()
ecache
.
updateResult
(
ecache
.
updateResult
(
test
.
pod
,
test
.
pod
,
test
.
nodeName
,
test
.
nodeName
,
...
@@ -357,8 +362,8 @@ func TestUpdateResult(t *testing.T) {
...
@@ -357,8 +362,8 @@ func TestUpdateResult(t *testing.T) {
test
.
fit
,
test
.
fit
,
test
.
reasons
,
test
.
reasons
,
test
.
equivalenceHash
,
test
.
equivalenceHash
,
true
,
)
)
ecache
.
mu
.
Unlock
()
value
,
ok
:=
ecache
.
algorithmCache
[
test
.
nodeName
]
.
predicatesCache
.
Get
(
test
.
predicateKey
)
value
,
ok
:=
ecache
.
algorithmCache
[
test
.
nodeName
]
.
predicatesCache
.
Get
(
test
.
predicateKey
)
if
!
ok
{
if
!
ok
{
...
@@ -460,6 +465,7 @@ func TestLookupResult(t *testing.T) {
...
@@ -460,6 +465,7 @@ func TestLookupResult(t *testing.T) {
for
_
,
test
:=
range
tests
{
for
_
,
test
:=
range
tests
{
ecache
:=
NewEquivalenceCache
()
ecache
:=
NewEquivalenceCache
()
// set cached item to equivalence cache
// set cached item to equivalence cache
ecache
.
mu
.
Lock
()
ecache
.
updateResult
(
ecache
.
updateResult
(
test
.
podName
,
test
.
podName
,
test
.
nodeName
,
test
.
nodeName
,
...
@@ -467,8 +473,8 @@ func TestLookupResult(t *testing.T) {
...
@@ -467,8 +473,8 @@ func TestLookupResult(t *testing.T) {
test
.
cachedItem
.
fit
,
test
.
cachedItem
.
fit
,
test
.
cachedItem
.
reasons
,
test
.
cachedItem
.
reasons
,
test
.
equivalenceHashForUpdatePredicate
,
test
.
equivalenceHashForUpdatePredicate
,
true
,
)
)
ecache
.
mu
.
Unlock
()
// 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
.
expectedInvalidPredicateKey
{
predicateKeys
:=
sets
.
NewString
()
predicateKeys
:=
sets
.
NewString
()
...
@@ -476,12 +482,13 @@ func TestLookupResult(t *testing.T) {
...
@@ -476,12 +482,13 @@ func TestLookupResult(t *testing.T) {
ecache
.
InvalidateCachedPredicateItem
(
test
.
nodeName
,
predicateKeys
)
ecache
.
InvalidateCachedPredicateItem
(
test
.
nodeName
,
predicateKeys
)
}
}
// calculate predicate with equivalence cache
// calculate predicate with equivalence cache
ecache
.
mu
.
Lock
()
fit
,
reasons
,
invalid
:=
ecache
.
lookupResult
(
test
.
podName
,
fit
,
reasons
,
invalid
:=
ecache
.
lookupResult
(
test
.
podName
,
test
.
nodeName
,
test
.
nodeName
,
test
.
predicateKey
,
test
.
predicateKey
,
test
.
equivalenceHashForCalPredicate
,
test
.
equivalenceHashForCalPredicate
,
true
,
)
)
ecache
.
mu
.
Unlock
()
// returned invalid should match expectedInvalidPredicateKey or expectedInvalidEquivalenceHash
// returned invalid should match expectedInvalidPredicateKey or expectedInvalidEquivalenceHash
if
test
.
equivalenceHashForUpdatePredicate
!=
test
.
equivalenceHashForCalPredicate
{
if
test
.
equivalenceHashForUpdatePredicate
!=
test
.
equivalenceHashForCalPredicate
{
if
invalid
!=
test
.
expectedInvalidEquivalenceHash
{
if
invalid
!=
test
.
expectedInvalidEquivalenceHash
{
...
@@ -668,6 +675,7 @@ func TestInvalidateCachedPredicateItemOfAllNodes(t *testing.T) {
...
@@ -668,6 +675,7 @@ func TestInvalidateCachedPredicateItemOfAllNodes(t *testing.T) {
for
_
,
test
:=
range
tests
{
for
_
,
test
:=
range
tests
{
// set cached item to equivalence cache
// set cached item to equivalence cache
ecache
.
mu
.
Lock
()
ecache
.
updateResult
(
ecache
.
updateResult
(
test
.
podName
,
test
.
podName
,
test
.
nodeName
,
test
.
nodeName
,
...
@@ -675,8 +683,8 @@ func TestInvalidateCachedPredicateItemOfAllNodes(t *testing.T) {
...
@@ -675,8 +683,8 @@ func TestInvalidateCachedPredicateItemOfAllNodes(t *testing.T) {
test
.
cachedItem
.
fit
,
test
.
cachedItem
.
fit
,
test
.
cachedItem
.
reasons
,
test
.
cachedItem
.
reasons
,
test
.
equivalenceHashForUpdatePredicate
,
test
.
equivalenceHashForUpdatePredicate
,
true
,
)
)
ecache
.
mu
.
Unlock
()
}
}
// invalidate cached predicate for all nodes
// invalidate cached predicate for all nodes
...
@@ -735,6 +743,7 @@ func TestInvalidateAllCachedPredicateItemOfNode(t *testing.T) {
...
@@ -735,6 +743,7 @@ func TestInvalidateAllCachedPredicateItemOfNode(t *testing.T) {
for
_
,
test
:=
range
tests
{
for
_
,
test
:=
range
tests
{
// set cached item to equivalence cache
// set cached item to equivalence cache
ecache
.
mu
.
Lock
()
ecache
.
updateResult
(
ecache
.
updateResult
(
test
.
podName
,
test
.
podName
,
test
.
nodeName
,
test
.
nodeName
,
...
@@ -742,8 +751,8 @@ func TestInvalidateAllCachedPredicateItemOfNode(t *testing.T) {
...
@@ -742,8 +751,8 @@ func TestInvalidateAllCachedPredicateItemOfNode(t *testing.T) {
test
.
cachedItem
.
fit
,
test
.
cachedItem
.
fit
,
test
.
cachedItem
.
reasons
,
test
.
cachedItem
.
reasons
,
test
.
equivalenceHashForUpdatePredicate
,
test
.
equivalenceHashForUpdatePredicate
,
true
,
)
)
ecache
.
mu
.
Unlock
()
}
}
for
_
,
test
:=
range
tests
{
for
_
,
test
:=
range
tests
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment