backend.go 2.8 KB

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