backend.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright 2017 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 etcdserver
  15. import (
  16. "fmt"
  17. "os"
  18. "time"
  19. "go.etcd.io/etcd/etcdserver/api/snap"
  20. "go.etcd.io/etcd/lease"
  21. "go.etcd.io/etcd/mvcc"
  22. "go.etcd.io/etcd/mvcc/backend"
  23. "go.etcd.io/etcd/raft/raftpb"
  24. "go.uber.org/zap"
  25. )
  26. func newBackend(cfg ServerConfig) backend.Backend {
  27. bcfg := backend.DefaultBackendConfig()
  28. bcfg.Path = cfg.backendPath()
  29. if cfg.BackendBatchLimit != 0 {
  30. bcfg.BatchLimit = cfg.BackendBatchLimit
  31. if cfg.Logger != nil {
  32. cfg.Logger.Info("setting backend batch limit", zap.Int("batch limit", cfg.BackendBatchLimit))
  33. }
  34. }
  35. if cfg.BackendBatchInterval != 0 {
  36. bcfg.BatchInterval = cfg.BackendBatchInterval
  37. if cfg.Logger != nil {
  38. cfg.Logger.Info("setting backend batch interval", zap.Duration("batch interval", cfg.BackendBatchInterval))
  39. }
  40. }
  41. bcfg.BackendFreelistType = cfg.BackendFreelistType
  42. bcfg.Logger = cfg.Logger
  43. if cfg.QuotaBackendBytes > 0 && cfg.QuotaBackendBytes != DefaultQuotaBytes {
  44. // permit 10% excess over quota for disarm
  45. bcfg.MmapSize = uint64(cfg.QuotaBackendBytes + cfg.QuotaBackendBytes/10)
  46. }
  47. return backend.New(bcfg)
  48. }
  49. // openSnapshotBackend renames a snapshot db to the current etcd db and opens it.
  50. func openSnapshotBackend(cfg ServerConfig, ss *snap.Snapshotter, snapshot raftpb.Snapshot) (backend.Backend, error) {
  51. snapPath, err := ss.DBFilePath(snapshot.Metadata.Index)
  52. if err != nil {
  53. return nil, fmt.Errorf("failed to find database snapshot file (%v)", err)
  54. }
  55. if err := os.Rename(snapPath, cfg.backendPath()); err != nil {
  56. return nil, fmt.Errorf("failed to rename database snapshot file (%v)", err)
  57. }
  58. return openBackend(cfg), nil
  59. }
  60. // openBackend returns a backend using the current etcd db.
  61. func openBackend(cfg ServerConfig) backend.Backend {
  62. fn := cfg.backendPath()
  63. now, beOpened := time.Now(), make(chan backend.Backend)
  64. go func() {
  65. beOpened <- newBackend(cfg)
  66. }()
  67. select {
  68. case be := <-beOpened:
  69. if cfg.Logger != nil {
  70. cfg.Logger.Info("opened backend db", zap.String("path", fn), zap.Duration("took", time.Since(now)))
  71. }
  72. return be
  73. case <-time.After(10 * time.Second):
  74. if cfg.Logger != nil {
  75. cfg.Logger.Info(
  76. "db file is flocked by another process, or taking too long",
  77. zap.String("path", fn),
  78. zap.Duration("took", time.Since(now)),
  79. )
  80. } else {
  81. plog.Warningf("another etcd process is using %q and holds the file lock, or loading backend file is taking >10 seconds", fn)
  82. plog.Warningf("waiting for it to exit before starting...")
  83. }
  84. }
  85. return <-beOpened
  86. }
  87. // recoverBackendSnapshot recovers the DB from a snapshot in case etcd crashes
  88. // before updating the backend db after persisting raft snapshot to disk,
  89. // violating the invariant snapshot.Metadata.Index < db.consistentIndex. In this
  90. // case, replace the db with the snapshot db sent by the leader.
  91. func recoverSnapshotBackend(cfg ServerConfig, oldbe backend.Backend, snapshot raftpb.Snapshot) (backend.Backend, error) {
  92. var cIndex consistentIndex
  93. kv := mvcc.New(cfg.Logger, oldbe, &lease.FakeLessor{}, &cIndex, mvcc.StoreConfig{CompactionBatchLimit: cfg.CompactionBatchLimit})
  94. defer kv.Close()
  95. if snapshot.Metadata.Index <= kv.ConsistentIndex() {
  96. return oldbe, nil
  97. }
  98. oldbe.Close()
  99. return openSnapshotBackend(cfg, snap.New(cfg.Logger, cfg.SnapDir()), snapshot)
  100. }