backend.go 2.7 KB

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