node_tree.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. Copyright 2018 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package cache
  14. import (
  15. "fmt"
  16. "k8s.io/api/core/v1"
  17. "k8s.io/klog"
  18. utilnode "k8s.io/kubernetes/pkg/util/node"
  19. )
  20. // nodeTree is a tree-like data structure that holds node names in each zone. Zone names are
  21. // keys to "NodeTree.tree" and values of "NodeTree.tree" are arrays of node names.
  22. // NodeTree is NOT thread-safe, any concurrent updates/reads from it must be synchronized by the caller.
  23. // It is used only by schedulerCache, and should stay as such.
  24. type nodeTree struct {
  25. tree map[string]*nodeArray // a map from zone (region-zone) to an array of nodes in the zone.
  26. zones []string // a list of all the zones in the tree (keys)
  27. zoneIndex int
  28. numNodes int
  29. }
  30. // nodeArray is a struct that has nodes that are in a zone.
  31. // We use a slice (as opposed to a set/map) to store the nodes because iterating over the nodes is
  32. // a lot more frequent than searching them by name.
  33. type nodeArray struct {
  34. nodes []string
  35. lastIndex int
  36. }
  37. func (na *nodeArray) next() (nodeName string, exhausted bool) {
  38. if len(na.nodes) == 0 {
  39. klog.Error("The nodeArray is empty. It should have been deleted from NodeTree.")
  40. return "", false
  41. }
  42. if na.lastIndex >= len(na.nodes) {
  43. return "", true
  44. }
  45. nodeName = na.nodes[na.lastIndex]
  46. na.lastIndex++
  47. return nodeName, false
  48. }
  49. // newNodeTree creates a NodeTree from nodes.
  50. func newNodeTree(nodes []*v1.Node) *nodeTree {
  51. nt := &nodeTree{
  52. tree: make(map[string]*nodeArray),
  53. }
  54. for _, n := range nodes {
  55. nt.addNode(n)
  56. }
  57. return nt
  58. }
  59. // addNode adds a node and its corresponding zone to the tree. If the zone already exists, the node
  60. // is added to the array of nodes in that zone.
  61. func (nt *nodeTree) addNode(n *v1.Node) {
  62. zone := utilnode.GetZoneKey(n)
  63. if na, ok := nt.tree[zone]; ok {
  64. for _, nodeName := range na.nodes {
  65. if nodeName == n.Name {
  66. klog.Warningf("node %q already exist in the NodeTree", n.Name)
  67. return
  68. }
  69. }
  70. na.nodes = append(na.nodes, n.Name)
  71. } else {
  72. nt.zones = append(nt.zones, zone)
  73. nt.tree[zone] = &nodeArray{nodes: []string{n.Name}, lastIndex: 0}
  74. }
  75. klog.V(2).Infof("Added node %q in group %q to NodeTree", n.Name, zone)
  76. nt.numNodes++
  77. }
  78. // removeNode removes a node from the NodeTree.
  79. func (nt *nodeTree) removeNode(n *v1.Node) error {
  80. zone := utilnode.GetZoneKey(n)
  81. if na, ok := nt.tree[zone]; ok {
  82. for i, nodeName := range na.nodes {
  83. if nodeName == n.Name {
  84. na.nodes = append(na.nodes[:i], na.nodes[i+1:]...)
  85. if len(na.nodes) == 0 {
  86. nt.removeZone(zone)
  87. }
  88. klog.V(2).Infof("Removed node %q in group %q from NodeTree", n.Name, zone)
  89. nt.numNodes--
  90. return nil
  91. }
  92. }
  93. }
  94. klog.Errorf("Node %q in group %q was not found", n.Name, zone)
  95. return fmt.Errorf("node %q in group %q was not found", n.Name, zone)
  96. }
  97. // removeZone removes a zone from tree.
  98. // This function must be called while writer locks are hold.
  99. func (nt *nodeTree) removeZone(zone string) {
  100. delete(nt.tree, zone)
  101. for i, z := range nt.zones {
  102. if z == zone {
  103. nt.zones = append(nt.zones[:i], nt.zones[i+1:]...)
  104. return
  105. }
  106. }
  107. }
  108. // updateNode updates a node in the NodeTree.
  109. func (nt *nodeTree) updateNode(old, new *v1.Node) {
  110. var oldZone string
  111. if old != nil {
  112. oldZone = utilnode.GetZoneKey(old)
  113. }
  114. newZone := utilnode.GetZoneKey(new)
  115. // If the zone ID of the node has not changed, we don't need to do anything. Name of the node
  116. // cannot be changed in an update.
  117. if oldZone == newZone {
  118. return
  119. }
  120. nt.removeNode(old) // No error checking. We ignore whether the old node exists or not.
  121. nt.addNode(new)
  122. }
  123. func (nt *nodeTree) resetExhausted() {
  124. for _, na := range nt.tree {
  125. na.lastIndex = 0
  126. }
  127. nt.zoneIndex = 0
  128. }
  129. // next returns the name of the next node. NodeTree iterates over zones and in each zone iterates
  130. // over nodes in a round robin fashion.
  131. func (nt *nodeTree) next() string {
  132. if len(nt.zones) == 0 {
  133. return ""
  134. }
  135. numExhaustedZones := 0
  136. for {
  137. if nt.zoneIndex >= len(nt.zones) {
  138. nt.zoneIndex = 0
  139. }
  140. zone := nt.zones[nt.zoneIndex]
  141. nt.zoneIndex++
  142. // We do not check the exhausted zones before calling next() on the zone. This ensures
  143. // that if more nodes are added to a zone after it is exhausted, we iterate over the new nodes.
  144. nodeName, exhausted := nt.tree[zone].next()
  145. if exhausted {
  146. numExhaustedZones++
  147. if numExhaustedZones >= len(nt.zones) { // all zones are exhausted. we should reset.
  148. nt.resetExhausted()
  149. }
  150. } else {
  151. return nodeName
  152. }
  153. }
  154. }