state_checkpoint.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 state
  14. import (
  15. "fmt"
  16. "path"
  17. "sync"
  18. "k8s.io/klog"
  19. "k8s.io/kubernetes/pkg/kubelet/checkpointmanager"
  20. "k8s.io/kubernetes/pkg/kubelet/checkpointmanager/errors"
  21. "k8s.io/kubernetes/pkg/kubelet/cm/cpumanager/containermap"
  22. "k8s.io/kubernetes/pkg/kubelet/cm/cpuset"
  23. )
  24. var _ State = &stateCheckpoint{}
  25. type stateCheckpoint struct {
  26. mux sync.RWMutex
  27. policyName string
  28. cache State
  29. checkpointManager checkpointmanager.CheckpointManager
  30. checkpointName string
  31. initialContainers containermap.ContainerMap
  32. }
  33. // NewCheckpointState creates new State for keeping track of cpu/pod assignment with checkpoint backend
  34. func NewCheckpointState(stateDir, checkpointName, policyName string, initialContainers containermap.ContainerMap) (State, error) {
  35. checkpointManager, err := checkpointmanager.NewCheckpointManager(stateDir)
  36. if err != nil {
  37. return nil, fmt.Errorf("failed to initialize checkpoint manager: %v", err)
  38. }
  39. stateCheckpoint := &stateCheckpoint{
  40. cache: NewMemoryState(),
  41. policyName: policyName,
  42. checkpointManager: checkpointManager,
  43. checkpointName: checkpointName,
  44. initialContainers: initialContainers,
  45. }
  46. if err := stateCheckpoint.restoreState(); err != nil {
  47. //lint:ignore ST1005 user-facing error message
  48. return nil, fmt.Errorf("could not restore state from checkpoint: %v, please drain this node and delete the CPU manager checkpoint file %q before restarting Kubelet",
  49. err, path.Join(stateDir, checkpointName))
  50. }
  51. return stateCheckpoint, nil
  52. }
  53. // migrateV1CheckpointToV2Checkpoint() converts checkpoints from the v1 format to the v2 format
  54. func (sc *stateCheckpoint) migrateV1CheckpointToV2Checkpoint(src *CPUManagerCheckpointV1, dst *CPUManagerCheckpointV2) error {
  55. if src.PolicyName != "" {
  56. dst.PolicyName = src.PolicyName
  57. }
  58. if src.DefaultCPUSet != "" {
  59. dst.DefaultCPUSet = src.DefaultCPUSet
  60. }
  61. for containerID, cset := range src.Entries {
  62. podUID, containerName, err := sc.initialContainers.GetContainerRef(containerID)
  63. if err != nil {
  64. return fmt.Errorf("containerID '%v' not found in initial containers list", containerID)
  65. }
  66. if dst.Entries == nil {
  67. dst.Entries = make(map[string]map[string]string)
  68. }
  69. if _, exists := dst.Entries[podUID]; !exists {
  70. dst.Entries[podUID] = make(map[string]string)
  71. }
  72. dst.Entries[podUID][containerName] = cset
  73. }
  74. return nil
  75. }
  76. // restores state from a checkpoint and creates it if it doesn't exist
  77. func (sc *stateCheckpoint) restoreState() error {
  78. sc.mux.Lock()
  79. defer sc.mux.Unlock()
  80. var err error
  81. checkpointV1 := newCPUManagerCheckpointV1()
  82. checkpointV2 := newCPUManagerCheckpointV2()
  83. if err = sc.checkpointManager.GetCheckpoint(sc.checkpointName, checkpointV1); err != nil {
  84. checkpointV1 = &CPUManagerCheckpointV1{} // reset it back to 0
  85. if err = sc.checkpointManager.GetCheckpoint(sc.checkpointName, checkpointV2); err != nil {
  86. if err == errors.ErrCheckpointNotFound {
  87. return sc.storeState()
  88. }
  89. return err
  90. }
  91. }
  92. if err = sc.migrateV1CheckpointToV2Checkpoint(checkpointV1, checkpointV2); err != nil {
  93. return fmt.Errorf("error migrating v1 checkpoint state to v2 checkpoint state: %s", err)
  94. }
  95. if sc.policyName != checkpointV2.PolicyName {
  96. return fmt.Errorf("configured policy %q differs from state checkpoint policy %q", sc.policyName, checkpointV2.PolicyName)
  97. }
  98. var tmpDefaultCPUSet cpuset.CPUSet
  99. if tmpDefaultCPUSet, err = cpuset.Parse(checkpointV2.DefaultCPUSet); err != nil {
  100. return fmt.Errorf("could not parse default cpu set %q: %v", checkpointV2.DefaultCPUSet, err)
  101. }
  102. var tmpContainerCPUSet cpuset.CPUSet
  103. tmpAssignments := ContainerCPUAssignments{}
  104. for pod := range checkpointV2.Entries {
  105. tmpAssignments[pod] = make(map[string]cpuset.CPUSet)
  106. for container, cpuString := range checkpointV2.Entries[pod] {
  107. if tmpContainerCPUSet, err = cpuset.Parse(cpuString); err != nil {
  108. return fmt.Errorf("could not parse cpuset %q for container %q in pod %q: %v", cpuString, container, pod, err)
  109. }
  110. tmpAssignments[pod][container] = tmpContainerCPUSet
  111. }
  112. }
  113. sc.cache.SetDefaultCPUSet(tmpDefaultCPUSet)
  114. sc.cache.SetCPUAssignments(tmpAssignments)
  115. klog.V(2).Info("[cpumanager] state checkpoint: restored state from checkpoint")
  116. klog.V(2).Infof("[cpumanager] state checkpoint: defaultCPUSet: %s", tmpDefaultCPUSet.String())
  117. return nil
  118. }
  119. // saves state to a checkpoint, caller is responsible for locking
  120. func (sc *stateCheckpoint) storeState() error {
  121. checkpoint := NewCPUManagerCheckpoint()
  122. checkpoint.PolicyName = sc.policyName
  123. checkpoint.DefaultCPUSet = sc.cache.GetDefaultCPUSet().String()
  124. assignments := sc.cache.GetCPUAssignments()
  125. for pod := range assignments {
  126. checkpoint.Entries[pod] = make(map[string]string)
  127. for container, cset := range assignments[pod] {
  128. checkpoint.Entries[pod][container] = cset.String()
  129. }
  130. }
  131. err := sc.checkpointManager.CreateCheckpoint(sc.checkpointName, checkpoint)
  132. if err != nil {
  133. klog.Errorf("[cpumanager] could not save checkpoint: %v", err)
  134. return err
  135. }
  136. return nil
  137. }
  138. // GetCPUSet returns current CPU set
  139. func (sc *stateCheckpoint) GetCPUSet(podUID string, containerName string) (cpuset.CPUSet, bool) {
  140. sc.mux.RLock()
  141. defer sc.mux.RUnlock()
  142. res, ok := sc.cache.GetCPUSet(podUID, containerName)
  143. return res, ok
  144. }
  145. // GetDefaultCPUSet returns default CPU set
  146. func (sc *stateCheckpoint) GetDefaultCPUSet() cpuset.CPUSet {
  147. sc.mux.RLock()
  148. defer sc.mux.RUnlock()
  149. return sc.cache.GetDefaultCPUSet()
  150. }
  151. // GetCPUSetOrDefault returns current CPU set, or default one if it wasn't changed
  152. func (sc *stateCheckpoint) GetCPUSetOrDefault(podUID string, containerName string) cpuset.CPUSet {
  153. sc.mux.RLock()
  154. defer sc.mux.RUnlock()
  155. return sc.cache.GetCPUSetOrDefault(podUID, containerName)
  156. }
  157. // GetCPUAssignments returns current CPU to pod assignments
  158. func (sc *stateCheckpoint) GetCPUAssignments() ContainerCPUAssignments {
  159. sc.mux.RLock()
  160. defer sc.mux.RUnlock()
  161. return sc.cache.GetCPUAssignments()
  162. }
  163. // SetCPUSet sets CPU set
  164. func (sc *stateCheckpoint) SetCPUSet(podUID string, containerName string, cset cpuset.CPUSet) {
  165. sc.mux.Lock()
  166. defer sc.mux.Unlock()
  167. sc.cache.SetCPUSet(podUID, containerName, cset)
  168. err := sc.storeState()
  169. if err != nil {
  170. klog.Warningf("store state to checkpoint error: %v", err)
  171. }
  172. }
  173. // SetDefaultCPUSet sets default CPU set
  174. func (sc *stateCheckpoint) SetDefaultCPUSet(cset cpuset.CPUSet) {
  175. sc.mux.Lock()
  176. defer sc.mux.Unlock()
  177. sc.cache.SetDefaultCPUSet(cset)
  178. err := sc.storeState()
  179. if err != nil {
  180. klog.Warningf("store state to checkpoint error: %v", err)
  181. }
  182. }
  183. // SetCPUAssignments sets CPU to pod assignments
  184. func (sc *stateCheckpoint) SetCPUAssignments(a ContainerCPUAssignments) {
  185. sc.mux.Lock()
  186. defer sc.mux.Unlock()
  187. sc.cache.SetCPUAssignments(a)
  188. err := sc.storeState()
  189. if err != nil {
  190. klog.Warningf("store state to checkpoint error: %v", err)
  191. }
  192. }
  193. // Delete deletes assignment for specified pod
  194. func (sc *stateCheckpoint) Delete(podUID string, containerName string) {
  195. sc.mux.Lock()
  196. defer sc.mux.Unlock()
  197. sc.cache.Delete(podUID, containerName)
  198. err := sc.storeState()
  199. if err != nil {
  200. klog.Warningf("store state to checkpoint error: %v", err)
  201. }
  202. }
  203. // ClearState clears the state and saves it in a checkpoint
  204. func (sc *stateCheckpoint) ClearState() {
  205. sc.mux.Lock()
  206. defer sc.mux.Unlock()
  207. sc.cache.ClearState()
  208. err := sc.storeState()
  209. if err != nil {
  210. klog.Warningf("store state to checkpoint error: %v", err)
  211. }
  212. }