db.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright 2015 The etcd Authors
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package snap
  15. import (
  16. "errors"
  17. "fmt"
  18. "io"
  19. "io/ioutil"
  20. "os"
  21. "path/filepath"
  22. "time"
  23. "go.etcd.io/etcd/pkg/fileutil"
  24. humanize "github.com/dustin/go-humanize"
  25. "go.uber.org/zap"
  26. )
  27. var ErrNoDBSnapshot = errors.New("snap: snapshot file doesn't exist")
  28. // SaveDBFrom saves snapshot of the database from the given reader. It
  29. // guarantees the save operation is atomic.
  30. func (s *Snapshotter) SaveDBFrom(r io.Reader, id uint64) (int64, error) {
  31. start := time.Now()
  32. f, err := ioutil.TempFile(s.dir, "tmp")
  33. if err != nil {
  34. return 0, err
  35. }
  36. var n int64
  37. n, err = io.Copy(f, r)
  38. if err == nil {
  39. fsyncStart := time.Now()
  40. err = fileutil.Fsync(f)
  41. snapDBFsyncSec.Observe(time.Since(fsyncStart).Seconds())
  42. }
  43. f.Close()
  44. if err != nil {
  45. os.Remove(f.Name())
  46. return n, err
  47. }
  48. fn := s.dbFilePath(id)
  49. if fileutil.Exist(fn) {
  50. os.Remove(f.Name())
  51. return n, nil
  52. }
  53. err = os.Rename(f.Name(), fn)
  54. if err != nil {
  55. os.Remove(f.Name())
  56. return n, err
  57. }
  58. if s.lg != nil {
  59. s.lg.Info(
  60. "saved database snapshot to disk",
  61. zap.String("path", fn),
  62. zap.Int64("bytes", n),
  63. zap.String("size", humanize.Bytes(uint64(n))),
  64. )
  65. } else {
  66. plog.Infof("saved database snapshot to disk [total bytes: %d]", n)
  67. }
  68. snapDBSaveSec.Observe(time.Since(start).Seconds())
  69. return n, nil
  70. }
  71. // DBFilePath returns the file path for the snapshot of the database with
  72. // given id. If the snapshot does not exist, it returns error.
  73. func (s *Snapshotter) DBFilePath(id uint64) (string, error) {
  74. if _, err := fileutil.ReadDir(s.dir); err != nil {
  75. return "", err
  76. }
  77. fn := s.dbFilePath(id)
  78. if fileutil.Exist(fn) {
  79. return fn, nil
  80. }
  81. if s.lg != nil {
  82. s.lg.Warn(
  83. "failed to find [SNAPSHOT-INDEX].snap.db",
  84. zap.Uint64("snapshot-index", id),
  85. zap.String("snapshot-file-path", fn),
  86. zap.Error(ErrNoDBSnapshot),
  87. )
  88. }
  89. return "", ErrNoDBSnapshot
  90. }
  91. func (s *Snapshotter) dbFilePath(id uint64) string {
  92. return filepath.Join(s.dir, fmt.Sprintf("%016x.snap.db", id))
  93. }