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
0377c69a
Commit
0377c69a
authored
May 09, 2018
by
Harry Zhang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use simple cache instead of LRU
Update generated bazel Use map instead
parent
ce2bb3d7
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
20 additions
and
45 deletions
+20
-45
BUILD
pkg/scheduler/core/BUILD
+1
-1
equivalence_cache.go
pkg/scheduler/core/equivalence_cache.go
+14
-38
equivalence_cache_test.go
pkg/scheduler/core/equivalence_cache_test.go
+5
-6
No files found.
pkg/scheduler/core/BUILD
View file @
0377c69a
...
@@ -54,10 +54,10 @@ go_library(
...
@@ -54,10 +54,10 @@ go_library(
"//pkg/scheduler/metrics:go_default_library",
"//pkg/scheduler/metrics:go_default_library",
"//pkg/scheduler/schedulercache:go_default_library",
"//pkg/scheduler/schedulercache:go_default_library",
"//pkg/scheduler/util:go_default_library",
"//pkg/scheduler/util:go_default_library",
"//pkg/scheduler/util/cache:go_default_library",
"//pkg/scheduler/volumebinder:go_default_library",
"//pkg/scheduler/volumebinder:go_default_library",
"//pkg/util/hash:go_default_library",
"//pkg/util/hash:go_default_library",
"//vendor/github.com/golang/glog:go_default_library",
"//vendor/github.com/golang/glog:go_default_library",
"//vendor/github.com/golang/groupcache/lru:go_default_library",
"//vendor/k8s.io/api/core/v1:go_default_library",
"//vendor/k8s.io/api/core/v1:go_default_library",
"//vendor/k8s.io/api/policy/v1beta1:go_default_library",
"//vendor/k8s.io/api/policy/v1beta1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
...
...
pkg/scheduler/core/equivalence_cache.go
View file @
0377c69a
...
@@ -28,12 +28,8 @@ import (
...
@@ -28,12 +28,8 @@ import (
hashutil
"k8s.io/kubernetes/pkg/util/hash"
hashutil
"k8s.io/kubernetes/pkg/util/hash"
"github.com/golang/glog"
"github.com/golang/glog"
"github.com/golang/groupcache/lru"
)
)
// We use predicate names as cache's key, its count is limited
const
maxCacheEntries
=
100
// EquivalenceCache holds:
// EquivalenceCache holds:
// 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
...
@@ -42,11 +38,8 @@ type EquivalenceCache struct {
...
@@ -42,11 +38,8 @@ type EquivalenceCache struct {
algorithmCache
map
[
string
]
AlgorithmCache
algorithmCache
map
[
string
]
AlgorithmCache
}
}
// The AlgorithmCache stores PredicateMap with predicate name as key
// The AlgorithmCache stores PredicateMap with predicate name as key, PredicateMap as value.
type
AlgorithmCache
struct
{
type
AlgorithmCache
map
[
string
]
PredicateMap
// Only consider predicates for now
predicatesCache
*
lru
.
Cache
}
// PredicateMap stores HostPrediacte with equivalence hash as key
// PredicateMap stores HostPrediacte with equivalence hash as key
type
PredicateMap
map
[
uint64
]
HostPredicate
type
PredicateMap
map
[
uint64
]
HostPredicate
...
@@ -57,12 +50,6 @@ type HostPredicate struct {
...
@@ -57,12 +50,6 @@ type HostPredicate struct {
FailReasons
[]
algorithm
.
PredicateFailureReason
FailReasons
[]
algorithm
.
PredicateFailureReason
}
}
func
newAlgorithmCache
()
AlgorithmCache
{
return
AlgorithmCache
{
predicatesCache
:
lru
.
New
(
maxCacheEntries
),
}
}
// NewEquivalenceCache returns EquivalenceCache to speed up predicates by caching
// NewEquivalenceCache returns EquivalenceCache to speed up predicates by caching
// result from previous scheduling.
// result from previous scheduling.
func
NewEquivalenceCache
()
*
EquivalenceCache
{
func
NewEquivalenceCache
()
*
EquivalenceCache
{
...
@@ -109,22 +96,21 @@ func (ec *EquivalenceCache) updateResult(
...
@@ -109,22 +96,21 @@ func (ec *EquivalenceCache) updateResult(
equivalenceHash
uint64
,
equivalenceHash
uint64
,
)
{
)
{
if
_
,
exist
:=
ec
.
algorithmCache
[
nodeName
];
!
exist
{
if
_
,
exist
:=
ec
.
algorithmCache
[
nodeName
];
!
exist
{
ec
.
algorithmCache
[
nodeName
]
=
newAlgorithmCache
()
ec
.
algorithmCache
[
nodeName
]
=
AlgorithmCache
{}
}
}
predicateItem
:=
HostPredicate
{
predicateItem
:=
HostPredicate
{
Fit
:
fit
,
Fit
:
fit
,
FailReasons
:
reasons
,
FailReasons
:
reasons
,
}
}
// if cached predicate map already exists, just update the predicate by key
// if cached predicate map already exists, just update the predicate by key
if
v
,
ok
:=
ec
.
algorithmCache
[
nodeName
]
.
predicatesCache
.
Get
(
predicateKey
);
ok
{
if
predicateMap
,
ok
:=
ec
.
algorithmCache
[
nodeName
][
predicateKey
];
ok
{
predicateMap
:=
v
.
(
PredicateMap
)
// maps in golang are references, no need to add them back
// maps in golang are references, no need to add them back
predicateMap
[
equivalenceHash
]
=
predicateItem
predicateMap
[
equivalenceHash
]
=
predicateItem
}
else
{
}
else
{
ec
.
algorithmCache
[
nodeName
]
.
predicatesCache
.
Add
(
predicateKey
,
ec
.
algorithmCache
[
nodeName
]
[
predicateKey
]
=
PredicateMap
{
PredicateMap
{
equivalenceHash
:
predicateItem
,
equivalenceHash
:
predicateItem
,
}
)
}
}
}
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
)
}
}
...
@@ -139,20 +125,13 @@ func (ec *EquivalenceCache) lookupResult(
...
@@ -139,20 +125,13 @@ func (ec *EquivalenceCache) lookupResult(
)
(
bool
,
[]
algorithm
.
PredicateFailureReason
,
bool
)
{
)
(
bool
,
[]
algorithm
.
PredicateFailureReason
,
bool
)
{
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
hostPredicate
,
exist
:=
ec
.
algorithmCache
[
nodeName
][
predicateKey
][
equivalenceHash
];
exist
{
if
cachePredicate
,
exist
:=
algorithmCache
.
predicatesCache
.
Get
(
predicateKey
);
exist
{
if
hostPredicate
.
Fit
{
predicateMap
:=
cachePredicate
.
(
PredicateMap
)
return
true
,
[]
algorithm
.
PredicateFailureReason
{},
false
// TODO(resouer) Is it possible a race that cache failed to update immediately?
if
hostPredicate
,
ok
:=
predicateMap
[
equivalenceHash
];
ok
{
if
hostPredicate
.
Fit
{
return
true
,
[]
algorithm
.
PredicateFailureReason
{},
false
}
return
false
,
hostPredicate
.
FailReasons
,
false
}
// is invalid
return
false
,
[]
algorithm
.
PredicateFailureReason
{},
true
}
}
return
false
,
hostPredicate
.
FailReasons
,
false
}
}
// is invalid
return
false
,
[]
algorithm
.
PredicateFailureReason
{},
true
return
false
,
[]
algorithm
.
PredicateFailureReason
{},
true
}
}
...
@@ -163,10 +142,8 @@ func (ec *EquivalenceCache) InvalidateCachedPredicateItem(nodeName string, predi
...
@@ -163,10 +142,8 @@ func (ec *EquivalenceCache) InvalidateCachedPredicateItem(nodeName string, predi
}
}
ec
.
mu
.
Lock
()
ec
.
mu
.
Lock
()
defer
ec
.
mu
.
Unlock
()
defer
ec
.
mu
.
Unlock
()
if
algorithmCache
,
exist
:=
ec
.
algorithmCache
[
nodeName
];
exist
{
for
predicateKey
:=
range
predicateKeys
{
for
predicateKey
:=
range
predicateKeys
{
delete
(
ec
.
algorithmCache
[
nodeName
],
predicateKey
)
algorithmCache
.
predicatesCache
.
Remove
(
predicateKey
)
}
}
}
glog
.
V
(
5
)
.
Infof
(
"Done invalidating cached predicates: %v on node: %s"
,
predicateKeys
,
nodeName
)
glog
.
V
(
5
)
.
Infof
(
"Done invalidating cached predicates: %v on node: %s"
,
predicateKeys
,
nodeName
)
}
}
...
@@ -181,8 +158,7 @@ func (ec *EquivalenceCache) InvalidateCachedPredicateItemOfAllNodes(predicateKey
...
@@ -181,8 +158,7 @@ func (ec *EquivalenceCache) InvalidateCachedPredicateItemOfAllNodes(predicateKey
// 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
{
// just use keys is enough
delete
(
algorithmCache
,
predicateKey
)
algorithmCache
.
predicatesCache
.
Remove
(
predicateKey
)
}
}
}
}
glog
.
V
(
5
)
.
Infof
(
"Done invalidating cached predicates: %v on all node"
,
predicateKeys
)
glog
.
V
(
5
)
.
Infof
(
"Done invalidating cached predicates: %v on all node"
,
predicateKeys
)
...
...
pkg/scheduler/core/equivalence_cache_test.go
View file @
0377c69a
...
@@ -345,14 +345,14 @@ func TestUpdateResult(t *testing.T) {
...
@@ -345,14 +345,14 @@ func TestUpdateResult(t *testing.T) {
for
_
,
test
:=
range
tests
{
for
_
,
test
:=
range
tests
{
ecache
:=
NewEquivalenceCache
()
ecache
:=
NewEquivalenceCache
()
if
test
.
expectPredicateMap
{
if
test
.
expectPredicateMap
{
ecache
.
algorithmCache
[
test
.
nodeName
]
=
newAlgorithmCache
()
ecache
.
algorithmCache
[
test
.
nodeName
]
=
AlgorithmCache
{}
predicateItem
:=
HostPredicate
{
predicateItem
:=
HostPredicate
{
Fit
:
true
,
Fit
:
true
,
}
}
ecache
.
algorithmCache
[
test
.
nodeName
]
.
predicatesCache
.
Add
(
test
.
predicateKey
,
ecache
.
algorithmCache
[
test
.
nodeName
]
[
test
.
predicateKey
]
=
PredicateMap
{
PredicateMap
{
test
.
equivalenceHash
:
predicateItem
,
test
.
equivalenceHash
:
predicateItem
,
}
)
}
}
}
ecache
.
mu
.
Lock
()
ecache
.
mu
.
Lock
()
ecache
.
updateResult
(
ecache
.
updateResult
(
...
@@ -365,12 +365,11 @@ func TestUpdateResult(t *testing.T) {
...
@@ -365,12 +365,11 @@ func TestUpdateResult(t *testing.T) {
)
)
ecache
.
mu
.
Unlock
()
ecache
.
mu
.
Unlock
()
value
,
ok
:=
ecache
.
algorithmCache
[
test
.
nodeName
]
.
predicatesCache
.
Get
(
test
.
predicateKey
)
cachedMapItem
,
ok
:=
ecache
.
algorithmCache
[
test
.
nodeName
][
test
.
predicateKey
]
if
!
ok
{
if
!
ok
{
t
.
Errorf
(
"Failed: %s, can't find expected cache item: %v"
,
t
.
Errorf
(
"Failed: %s, can't find expected cache item: %v"
,
test
.
name
,
test
.
expectCacheItem
)
test
.
name
,
test
.
expectCacheItem
)
}
else
{
}
else
{
cachedMapItem
:=
value
.
(
PredicateMap
)
if
!
reflect
.
DeepEqual
(
cachedMapItem
[
test
.
equivalenceHash
],
test
.
expectCacheItem
)
{
if
!
reflect
.
DeepEqual
(
cachedMapItem
[
test
.
equivalenceHash
],
test
.
expectCacheItem
)
{
t
.
Errorf
(
"Failed: %s, expected cached item: %v, but got: %v"
,
t
.
Errorf
(
"Failed: %s, expected cached item: %v, but got: %v"
,
test
.
name
,
test
.
expectCacheItem
,
cachedMapItem
[
test
.
equivalenceHash
])
test
.
name
,
test
.
expectCacheItem
,
cachedMapItem
[
test
.
equivalenceHash
])
...
@@ -693,7 +692,7 @@ func TestInvalidateCachedPredicateItemOfAllNodes(t *testing.T) {
...
@@ -693,7 +692,7 @@ func TestInvalidateCachedPredicateItemOfAllNodes(t *testing.T) {
// there should be no cached predicate any more
// there should be no cached predicate any more
for
_
,
test
:=
range
tests
{
for
_
,
test
:=
range
tests
{
if
algorithmCache
,
exist
:=
ecache
.
algorithmCache
[
test
.
nodeName
];
exist
{
if
algorithmCache
,
exist
:=
ecache
.
algorithmCache
[
test
.
nodeName
];
exist
{
if
_
,
exist
:=
algorithmCache
.
predicatesCache
.
Get
(
testPredicate
)
;
exist
{
if
_
,
exist
:=
algorithmCache
[
testPredicate
]
;
exist
{
t
.
Errorf
(
"Failed: cached item for predicate key: %v on node: %v should be invalidated"
,
t
.
Errorf
(
"Failed: cached item for predicate key: %v on node: %v should be invalidated"
,
testPredicate
,
test
.
nodeName
)
testPredicate
,
test
.
nodeName
)
break
break
...
...
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