checkpoint_manager.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 checkpointmanager
  14. import (
  15. "fmt"
  16. "sync"
  17. "k8s.io/kubernetes/pkg/kubelet/checkpointmanager/errors"
  18. utilstore "k8s.io/kubernetes/pkg/kubelet/util/store"
  19. utilfs "k8s.io/kubernetes/pkg/util/filesystem"
  20. )
  21. // Checkpoint provides the process checkpoint data
  22. type Checkpoint interface {
  23. MarshalCheckpoint() ([]byte, error)
  24. UnmarshalCheckpoint(blob []byte) error
  25. VerifyChecksum() error
  26. }
  27. // CheckpointManager provides the interface to manage checkpoint
  28. type CheckpointManager interface {
  29. // CreateCheckpoint persists checkpoint in CheckpointStore. checkpointKey is the key for utilstore to locate checkpoint.
  30. // For file backed utilstore, checkpointKey is the file name to write the checkpoint data.
  31. CreateCheckpoint(checkpointKey string, checkpoint Checkpoint) error
  32. // GetCheckpoint retrieves checkpoint from CheckpointStore.
  33. GetCheckpoint(checkpointKey string, checkpoint Checkpoint) error
  34. // WARNING: RemoveCheckpoint will not return error if checkpoint does not exist.
  35. RemoveCheckpoint(checkpointKey string) error
  36. // ListCheckpoint returns the list of existing checkpoints.
  37. ListCheckpoints() ([]string, error)
  38. }
  39. // impl is an implementation of CheckpointManager. It persists checkpoint in CheckpointStore
  40. type impl struct {
  41. path string
  42. store utilstore.Store
  43. mutex sync.Mutex
  44. }
  45. // NewCheckpointManager returns a new instance of a checkpoint manager
  46. func NewCheckpointManager(checkpointDir string) (CheckpointManager, error) {
  47. fstore, err := utilstore.NewFileStore(checkpointDir, utilfs.DefaultFs{})
  48. if err != nil {
  49. return nil, err
  50. }
  51. return &impl{path: checkpointDir, store: fstore}, nil
  52. }
  53. // CreateCheckpoint persists checkpoint in CheckpointStore.
  54. func (manager *impl) CreateCheckpoint(checkpointKey string, checkpoint Checkpoint) error {
  55. manager.mutex.Lock()
  56. defer manager.mutex.Unlock()
  57. blob, err := checkpoint.MarshalCheckpoint()
  58. if err != nil {
  59. return err
  60. }
  61. return manager.store.Write(checkpointKey, blob)
  62. }
  63. // GetCheckpoint retrieves checkpoint from CheckpointStore.
  64. func (manager *impl) GetCheckpoint(checkpointKey string, checkpoint Checkpoint) error {
  65. manager.mutex.Lock()
  66. defer manager.mutex.Unlock()
  67. blob, err := manager.store.Read(checkpointKey)
  68. if err != nil {
  69. if err == utilstore.ErrKeyNotFound {
  70. return errors.ErrCheckpointNotFound
  71. }
  72. return err
  73. }
  74. err = checkpoint.UnmarshalCheckpoint(blob)
  75. if err == nil {
  76. err = checkpoint.VerifyChecksum()
  77. }
  78. return err
  79. }
  80. // RemoveCheckpoint will not return error if checkpoint does not exist.
  81. func (manager *impl) RemoveCheckpoint(checkpointKey string) error {
  82. manager.mutex.Lock()
  83. defer manager.mutex.Unlock()
  84. return manager.store.Delete(checkpointKey)
  85. }
  86. // ListCheckpoints returns the list of existing checkpoints.
  87. func (manager *impl) ListCheckpoints() ([]string, error) {
  88. manager.mutex.Lock()
  89. defer manager.mutex.Unlock()
  90. keys, err := manager.store.List()
  91. if err != nil {
  92. return []string{}, fmt.Errorf("failed to list checkpoint store: %v", err)
  93. }
  94. return keys, nil
  95. }