backend.go 3.7 KB

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