backend.go 3.2 KB

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