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
a5045d10
Commit
a5045d10
authored
Jul 27, 2018
by
Bobby (Babak) Salamat
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add NodeTree to the scheduler cache
parent
c1896c97
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
42 additions
and
13 deletions
+42
-13
cache.go
pkg/scheduler/cache/cache.go
+9
-0
cache_test.go
pkg/scheduler/cache/cache_test.go
+12
-0
interface.go
pkg/scheduler/cache/interface.go
+3
-0
node_tree.go
pkg/scheduler/cache/node_tree.go
+14
-12
fake_cache.go
pkg/scheduler/testing/fake_cache.go
+4
-1
No files found.
pkg/scheduler/cache/cache.go
View file @
a5045d10
...
@@ -59,6 +59,7 @@ type schedulerCache struct {
...
@@ -59,6 +59,7 @@ type schedulerCache struct {
// a map from pod key to podState.
// a map from pod key to podState.
podStates
map
[
string
]
*
podState
podStates
map
[
string
]
*
podState
nodes
map
[
string
]
*
NodeInfo
nodes
map
[
string
]
*
NodeInfo
nodeTree
*
NodeTree
pdbs
map
[
string
]
*
policy
.
PodDisruptionBudget
pdbs
map
[
string
]
*
policy
.
PodDisruptionBudget
// A map from image name to its imageState.
// A map from image name to its imageState.
imageStates
map
[
string
]
*
imageState
imageStates
map
[
string
]
*
imageState
...
@@ -102,6 +103,7 @@ func newSchedulerCache(ttl, period time.Duration, stop <-chan struct{}) *schedul
...
@@ -102,6 +103,7 @@ func newSchedulerCache(ttl, period time.Duration, stop <-chan struct{}) *schedul
stop
:
stop
,
stop
:
stop
,
nodes
:
make
(
map
[
string
]
*
NodeInfo
),
nodes
:
make
(
map
[
string
]
*
NodeInfo
),
nodeTree
:
newNodeTree
(
nil
),
assumedPods
:
make
(
map
[
string
]
bool
),
assumedPods
:
make
(
map
[
string
]
bool
),
podStates
:
make
(
map
[
string
]
*
podState
),
podStates
:
make
(
map
[
string
]
*
podState
),
pdbs
:
make
(
map
[
string
]
*
policy
.
PodDisruptionBudget
),
pdbs
:
make
(
map
[
string
]
*
policy
.
PodDisruptionBudget
),
...
@@ -426,6 +428,7 @@ func (cache *schedulerCache) AddNode(node *v1.Node) error {
...
@@ -426,6 +428,7 @@ func (cache *schedulerCache) AddNode(node *v1.Node) error {
cache
.
removeNodeImageStates
(
n
.
node
)
cache
.
removeNodeImageStates
(
n
.
node
)
}
}
cache
.
nodeTree
.
AddNode
(
node
)
cache
.
addNodeImageStates
(
node
,
n
)
cache
.
addNodeImageStates
(
node
,
n
)
return
n
.
SetNode
(
node
)
return
n
.
SetNode
(
node
)
}
}
...
@@ -442,6 +445,7 @@ func (cache *schedulerCache) UpdateNode(oldNode, newNode *v1.Node) error {
...
@@ -442,6 +445,7 @@ func (cache *schedulerCache) UpdateNode(oldNode, newNode *v1.Node) error {
cache
.
removeNodeImageStates
(
n
.
node
)
cache
.
removeNodeImageStates
(
n
.
node
)
}
}
cache
.
nodeTree
.
UpdateNode
(
oldNode
,
newNode
)
cache
.
addNodeImageStates
(
newNode
,
n
)
cache
.
addNodeImageStates
(
newNode
,
n
)
return
n
.
SetNode
(
newNode
)
return
n
.
SetNode
(
newNode
)
}
}
...
@@ -462,6 +466,7 @@ func (cache *schedulerCache) RemoveNode(node *v1.Node) error {
...
@@ -462,6 +466,7 @@ func (cache *schedulerCache) RemoveNode(node *v1.Node) error {
delete
(
cache
.
nodes
,
node
.
Name
)
delete
(
cache
.
nodes
,
node
.
Name
)
}
}
cache
.
nodeTree
.
RemoveNode
(
node
)
cache
.
removeNodeImageStates
(
node
)
cache
.
removeNodeImageStates
(
node
)
return
nil
return
nil
}
}
...
@@ -598,3 +603,7 @@ func (cache *schedulerCache) expirePod(key string, ps *podState) error {
...
@@ -598,3 +603,7 @@ func (cache *schedulerCache) expirePod(key string, ps *podState) error {
delete
(
cache
.
podStates
,
key
)
delete
(
cache
.
podStates
,
key
)
return
nil
return
nil
}
}
func
(
cache
*
schedulerCache
)
NodeTree
()
*
NodeTree
{
return
cache
.
nodeTree
}
pkg/scheduler/cache/cache_test.go
View file @
a5045d10
...
@@ -1065,6 +1065,9 @@ func TestNodeOperators(t *testing.T) {
...
@@ -1065,6 +1065,9 @@ func TestNodeOperators(t *testing.T) {
if
!
found
{
if
!
found
{
t
.
Errorf
(
"Failed to find node %v in schedulercache."
,
node
.
Name
)
t
.
Errorf
(
"Failed to find node %v in schedulercache."
,
node
.
Name
)
}
}
if
cache
.
nodeTree
.
NumNodes
!=
1
||
cache
.
nodeTree
.
Next
()
!=
node
.
Name
{
t
.
Errorf
(
"cache.nodeTree is not updated correctly after adding node: %v"
,
node
.
Name
)
}
// Generations are globally unique. We check in our unit tests that they are incremented correctly.
// Generations are globally unique. We check in our unit tests that they are incremented correctly.
expected
.
generation
=
got
.
generation
expected
.
generation
=
got
.
generation
...
@@ -1100,12 +1103,21 @@ func TestNodeOperators(t *testing.T) {
...
@@ -1100,12 +1103,21 @@ func TestNodeOperators(t *testing.T) {
if
!
reflect
.
DeepEqual
(
got
,
expected
)
{
if
!
reflect
.
DeepEqual
(
got
,
expected
)
{
t
.
Errorf
(
"Failed to update node in schedulercache:
\n
got: %+v
\n
expected: %+v"
,
got
,
expected
)
t
.
Errorf
(
"Failed to update node in schedulercache:
\n
got: %+v
\n
expected: %+v"
,
got
,
expected
)
}
}
// Check nodeTree after update
if
cache
.
nodeTree
.
NumNodes
!=
1
||
cache
.
nodeTree
.
Next
()
!=
node
.
Name
{
t
.
Errorf
(
"unexpected cache.nodeTree after updating node: %v"
,
node
.
Name
)
}
// Case 4: the node can not be removed if pods is not empty.
// Case 4: the node can not be removed if pods is not empty.
cache
.
RemoveNode
(
node
)
cache
.
RemoveNode
(
node
)
if
_
,
found
:=
cache
.
nodes
[
node
.
Name
];
!
found
{
if
_
,
found
:=
cache
.
nodes
[
node
.
Name
];
!
found
{
t
.
Errorf
(
"The node %v should not be removed if pods is not empty."
,
node
.
Name
)
t
.
Errorf
(
"The node %v should not be removed if pods is not empty."
,
node
.
Name
)
}
}
// Check nodeTree after remove. The node should be removed from the nodeTree even if there are
// still pods on it.
if
cache
.
nodeTree
.
NumNodes
!=
0
||
cache
.
nodeTree
.
Next
()
!=
""
{
t
.
Errorf
(
"unexpected cache.nodeTree after removing node: %v"
,
node
.
Name
)
}
}
}
}
}
...
...
pkg/scheduler/cache/interface.go
View file @
a5045d10
...
@@ -125,6 +125,9 @@ type Cache interface {
...
@@ -125,6 +125,9 @@ type Cache interface {
// IsUpToDate returns true if the given NodeInfo matches the current data in the cache.
// IsUpToDate returns true if the given NodeInfo matches the current data in the cache.
IsUpToDate
(
n
*
NodeInfo
)
bool
IsUpToDate
(
n
*
NodeInfo
)
bool
// NodeTree returns a node tree structure
NodeTree
()
*
NodeTree
}
}
// Snapshot is a snapshot of cache state
// Snapshot is a snapshot of cache state
...
...
pkg/scheduler/cache/node_tree.go
View file @
a5045d10
...
@@ -21,8 +21,8 @@ import (
...
@@ -21,8 +21,8 @@ import (
"sync"
"sync"
"k8s.io/api/core/v1"
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/sets"
utilnode
"k8s.io/kubernetes/pkg/util/node"
utilnode
"k8s.io/kubernetes/pkg/util/node"
"k8s.io/kubernetes/staging/src/k8s.io/apimachinery/pkg/util/sets"
"github.com/golang/glog"
"github.com/golang/glog"
)
)
...
@@ -33,7 +33,7 @@ type NodeTree struct {
...
@@ -33,7 +33,7 @@ type NodeTree struct {
tree
map
[
string
]
*
nodeArray
// a map from zone (region-zone) to an array of nodes in the zone.
tree
map
[
string
]
*
nodeArray
// a map from zone (region-zone) to an array of nodes in the zone.
zones
[]
string
// a list of all the zones in the tree (keys)
zones
[]
string
// a list of all the zones in the tree (keys)
zoneIndex
int
zoneIndex
int
E
xhaustedZones
sets
.
String
// set of zones that all of their nodes are returned by next()
e
xhaustedZones
sets
.
String
// set of zones that all of their nodes are returned by next()
NumNodes
int
NumNodes
int
mu
sync
.
RWMutex
mu
sync
.
RWMutex
}
}
...
@@ -62,7 +62,7 @@ func (na *nodeArray) next() (nodeName string, exhausted bool) {
...
@@ -62,7 +62,7 @@ func (na *nodeArray) next() (nodeName string, exhausted bool) {
func
newNodeTree
(
nodes
[]
*
v1
.
Node
)
*
NodeTree
{
func
newNodeTree
(
nodes
[]
*
v1
.
Node
)
*
NodeTree
{
nt
:=
&
NodeTree
{
nt
:=
&
NodeTree
{
tree
:
make
(
map
[
string
]
*
nodeArray
),
tree
:
make
(
map
[
string
]
*
nodeArray
),
E
xhaustedZones
:
sets
.
NewString
(),
e
xhaustedZones
:
sets
.
NewString
(),
}
}
for
_
,
n
:=
range
nodes
{
for
_
,
n
:=
range
nodes
{
nt
.
AddNode
(
n
)
nt
.
AddNode
(
n
)
...
@@ -83,18 +83,20 @@ func (nt *NodeTree) addNode(n *v1.Node) {
...
@@ -83,18 +83,20 @@ func (nt *NodeTree) addNode(n *v1.Node) {
if
na
,
ok
:=
nt
.
tree
[
zone
];
ok
{
if
na
,
ok
:=
nt
.
tree
[
zone
];
ok
{
for
_
,
nodeName
:=
range
na
.
nodes
{
for
_
,
nodeName
:=
range
na
.
nodes
{
if
nodeName
==
n
.
Name
{
if
nodeName
==
n
.
Name
{
glog
.
Warningf
(
"node %v already exist in the NodeTree"
,
n
.
Name
)
return
return
}
}
}
}
na
.
nodes
=
append
(
na
.
nodes
,
n
.
Name
)
na
.
nodes
=
append
(
na
.
nodes
,
n
.
Name
)
nt
.
tree
[
zone
]
=
na
}
else
{
}
else
{
nt
.
zones
=
append
(
nt
.
zones
,
zone
)
nt
.
zones
=
append
(
nt
.
zones
,
zone
)
nt
.
tree
[
zone
]
=
&
nodeArray
{
nodes
:
[]
string
{
n
.
Name
},
lastIndex
:
0
}
nt
.
tree
[
zone
]
=
&
nodeArray
{
nodes
:
[]
string
{
n
.
Name
},
lastIndex
:
0
}
}
}
glog
.
V
(
5
)
.
Infof
(
"Added node %v in group %v to NodeTree"
,
n
.
Name
,
zone
)
nt
.
NumNodes
++
nt
.
NumNodes
++
}
}
// RemoveNode removes a node from the NodeTree.
func
(
nt
*
NodeTree
)
RemoveNode
(
n
*
v1
.
Node
)
error
{
func
(
nt
*
NodeTree
)
RemoveNode
(
n
*
v1
.
Node
)
error
{
nt
.
mu
.
Lock
()
nt
.
mu
.
Lock
()
defer
nt
.
mu
.
Unlock
()
defer
nt
.
mu
.
Unlock
()
...
@@ -106,19 +108,18 @@ func (nt *NodeTree) removeNode(n *v1.Node) error {
...
@@ -106,19 +108,18 @@ func (nt *NodeTree) removeNode(n *v1.Node) error {
if
na
,
ok
:=
nt
.
tree
[
zone
];
ok
{
if
na
,
ok
:=
nt
.
tree
[
zone
];
ok
{
for
i
,
nodeName
:=
range
na
.
nodes
{
for
i
,
nodeName
:=
range
na
.
nodes
{
if
nodeName
==
n
.
Name
{
if
nodeName
==
n
.
Name
{
// delete without preserving order
na
.
nodes
=
append
(
na
.
nodes
[
:
i
],
na
.
nodes
[
i
+
1
:
]
...
)
na
.
nodes
[
i
]
=
na
.
nodes
[
len
(
na
.
nodes
)
-
1
]
na
.
nodes
=
na
.
nodes
[
:
len
(
na
.
nodes
)
-
1
]
nt
.
tree
[
zone
]
=
na
if
len
(
na
.
nodes
)
==
0
{
if
len
(
na
.
nodes
)
==
0
{
nt
.
removeZone
(
zone
)
nt
.
removeZone
(
zone
)
}
}
glog
.
V
(
5
)
.
Infof
(
"Removed node %v in group %v from NodeTree"
,
n
.
Name
,
zone
)
nt
.
NumNodes
--
nt
.
NumNodes
--
return
nil
return
nil
}
}
}
}
}
}
return
fmt
.
Errorf
(
"node %v in zone %v was not found"
,
n
.
Name
,
zone
)
glog
.
Errorf
(
"Node %v in group %v was not found"
,
n
.
Name
,
zone
)
return
fmt
.
Errorf
(
"node %v in group %v was not found"
,
n
.
Name
,
zone
)
}
}
// removeZone removes a zone from tree.
// removeZone removes a zone from tree.
...
@@ -132,6 +133,7 @@ func (nt *NodeTree) removeZone(zone string) {
...
@@ -132,6 +133,7 @@ func (nt *NodeTree) removeZone(zone string) {
}
}
}
}
// UpdateNode updates a node in the NodeTree.
func
(
nt
*
NodeTree
)
UpdateNode
(
old
,
new
*
v1
.
Node
)
{
func
(
nt
*
NodeTree
)
UpdateNode
(
old
,
new
*
v1
.
Node
)
{
var
oldZone
string
var
oldZone
string
if
old
!=
nil
{
if
old
!=
nil
{
...
@@ -153,7 +155,7 @@ func (nt *NodeTree) resetExhausted() {
...
@@ -153,7 +155,7 @@ func (nt *NodeTree) resetExhausted() {
for
_
,
na
:=
range
nt
.
tree
{
for
_
,
na
:=
range
nt
.
tree
{
na
.
lastIndex
=
0
na
.
lastIndex
=
0
}
}
nt
.
E
xhaustedZones
=
sets
.
NewString
()
nt
.
e
xhaustedZones
=
sets
.
NewString
()
}
}
// Next returns the name of the next node. NodeTree iterates over zones and in each zone iterates
// Next returns the name of the next node. NodeTree iterates over zones and in each zone iterates
...
@@ -174,8 +176,8 @@ func (nt *NodeTree) Next() string {
...
@@ -174,8 +176,8 @@ func (nt *NodeTree) Next() string {
// that if more nodes are added to a zone after it is exhausted, we iterate over the new nodes.
// that if more nodes are added to a zone after it is exhausted, we iterate over the new nodes.
nodeName
,
exhausted
:=
nt
.
tree
[
zone
]
.
next
()
nodeName
,
exhausted
:=
nt
.
tree
[
zone
]
.
next
()
if
exhausted
{
if
exhausted
{
nt
.
E
xhaustedZones
.
Insert
(
zone
)
nt
.
e
xhaustedZones
.
Insert
(
zone
)
if
len
(
nt
.
E
xhaustedZones
)
==
len
(
nt
.
zones
)
{
// all zones are exhausted. we should reset.
if
len
(
nt
.
e
xhaustedZones
)
==
len
(
nt
.
zones
)
{
// all zones are exhausted. we should reset.
nt
.
resetExhausted
()
nt
.
resetExhausted
()
}
}
}
else
{
}
else
{
...
...
pkg/scheduler/testing/fake_cache.go
View file @
a5045d10
...
@@ -106,5 +106,8 @@ func (f *FakeCache) Snapshot() *schedulercache.Snapshot {
...
@@ -106,5 +106,8 @@ func (f *FakeCache) Snapshot() *schedulercache.Snapshot {
return
&
schedulercache
.
Snapshot
{}
return
&
schedulercache
.
Snapshot
{}
}
}
// IsUpToDate is a fake mthod for testing
// IsUpToDate is a fake m
e
thod for testing
func
(
f
*
FakeCache
)
IsUpToDate
(
*
schedulercache
.
NodeInfo
)
bool
{
return
true
}
func
(
f
*
FakeCache
)
IsUpToDate
(
*
schedulercache
.
NodeInfo
)
bool
{
return
true
}
// NodeTree is a fake method for testing.
func
(
f
*
FakeCache
)
NodeTree
()
*
schedulercache
.
NodeTree
{
return
nil
}
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