You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

155 lines
3.8 KiB

  1. // nolint: gosec
  2. package main
  3. import (
  4. "crypto/sha256"
  5. "encoding/json"
  6. "errors"
  7. "fmt"
  8. "io/ioutil"
  9. "math"
  10. "os"
  11. "path/filepath"
  12. "sync"
  13. abci "github.com/tendermint/tendermint/abci/types"
  14. )
  15. const (
  16. snapshotChunkSize = 1e6
  17. )
  18. // SnapshotStore stores state sync snapshots. Snapshots are stored simply as
  19. // JSON files, and chunks are generated on-the-fly by splitting the JSON data
  20. // into fixed-size chunks.
  21. type SnapshotStore struct {
  22. sync.RWMutex
  23. dir string
  24. metadata []abci.Snapshot
  25. }
  26. // NewSnapshotStore creates a new snapshot store.
  27. func NewSnapshotStore(dir string) (*SnapshotStore, error) {
  28. store := &SnapshotStore{dir: dir}
  29. if err := os.MkdirAll(dir, 0755); err != nil {
  30. return nil, err
  31. }
  32. if err := store.loadMetadata(); err != nil {
  33. return nil, err
  34. }
  35. return store, nil
  36. }
  37. // loadMetadata loads snapshot metadata. Does not take out locks, since it's
  38. // called internally on construction.
  39. func (s *SnapshotStore) loadMetadata() error {
  40. file := filepath.Join(s.dir, "metadata.json")
  41. metadata := []abci.Snapshot{}
  42. bz, err := ioutil.ReadFile(file)
  43. switch {
  44. case errors.Is(err, os.ErrNotExist):
  45. case err != nil:
  46. return fmt.Errorf("failed to load snapshot metadata from %q: %w", file, err)
  47. }
  48. if len(bz) != 0 {
  49. err = json.Unmarshal(bz, &metadata)
  50. if err != nil {
  51. return fmt.Errorf("invalid snapshot data in %q: %w", file, err)
  52. }
  53. }
  54. s.metadata = metadata
  55. return nil
  56. }
  57. // saveMetadata saves snapshot metadata. Does not take out locks, since it's
  58. // called internally from e.g. Create().
  59. func (s *SnapshotStore) saveMetadata() error {
  60. bz, err := json.Marshal(s.metadata)
  61. if err != nil {
  62. return err
  63. }
  64. // save the file to a new file and move it to make saving atomic.
  65. newFile := filepath.Join(s.dir, "metadata.json.new")
  66. file := filepath.Join(s.dir, "metadata.json")
  67. err = ioutil.WriteFile(newFile, bz, 0644) // nolint: gosec
  68. if err != nil {
  69. return err
  70. }
  71. return os.Rename(newFile, file)
  72. }
  73. // Create creates a snapshot of the given application state's key/value pairs.
  74. func (s *SnapshotStore) Create(state *State) (abci.Snapshot, error) {
  75. s.Lock()
  76. defer s.Unlock()
  77. bz, err := state.Export()
  78. if err != nil {
  79. return abci.Snapshot{}, err
  80. }
  81. hash := sha256.Sum256(bz)
  82. snapshot := abci.Snapshot{
  83. Height: state.Height,
  84. Format: 1,
  85. Hash: hash[:],
  86. Chunks: byteChunks(bz),
  87. }
  88. err = ioutil.WriteFile(filepath.Join(s.dir, fmt.Sprintf("%v.json", state.Height)), bz, 0644)
  89. if err != nil {
  90. return abci.Snapshot{}, err
  91. }
  92. s.metadata = append(s.metadata, snapshot)
  93. err = s.saveMetadata()
  94. if err != nil {
  95. return abci.Snapshot{}, err
  96. }
  97. return snapshot, nil
  98. }
  99. // List lists available snapshots.
  100. func (s *SnapshotStore) List() ([]*abci.Snapshot, error) {
  101. s.RLock()
  102. defer s.RUnlock()
  103. snapshots := []*abci.Snapshot{}
  104. for _, snapshot := range s.metadata {
  105. s := snapshot // copy to avoid pointer to range variable
  106. snapshots = append(snapshots, &s)
  107. }
  108. return snapshots, nil
  109. }
  110. // LoadChunk loads a snapshot chunk.
  111. func (s *SnapshotStore) LoadChunk(height uint64, format uint32, chunk uint32) ([]byte, error) {
  112. s.RLock()
  113. defer s.RUnlock()
  114. for _, snapshot := range s.metadata {
  115. if snapshot.Height == height && snapshot.Format == format {
  116. bz, err := ioutil.ReadFile(filepath.Join(s.dir, fmt.Sprintf("%v.json", height)))
  117. if err != nil {
  118. return nil, err
  119. }
  120. return byteChunk(bz, chunk), nil
  121. }
  122. }
  123. return nil, nil
  124. }
  125. // byteChunk returns the chunk at a given index from the full byte slice.
  126. func byteChunk(bz []byte, index uint32) []byte {
  127. start := int(index * snapshotChunkSize)
  128. end := int((index + 1) * snapshotChunkSize)
  129. switch {
  130. case start >= len(bz):
  131. return nil
  132. case end >= len(bz):
  133. return bz[start:]
  134. default:
  135. return bz[start:end]
  136. }
  137. }
  138. // byteChunks calculates the number of chunks in the byte slice.
  139. func byteChunks(bz []byte) uint32 {
  140. return uint32(math.Ceil(float64(len(bz)) / snapshotChunkSize))
  141. }