util.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // Copyright 2015 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/etcdserver/membership"
  20. "github.com/coreos/etcd/lease"
  21. "github.com/coreos/etcd/mvcc"
  22. "github.com/coreos/etcd/mvcc/backend"
  23. "github.com/coreos/etcd/pkg/types"
  24. "github.com/coreos/etcd/raft/raftpb"
  25. "github.com/coreos/etcd/rafthttp"
  26. "github.com/coreos/etcd/snap"
  27. )
  28. // isConnectedToQuorumSince checks whether the local member is connected to the
  29. // quorum of the cluster since the given time.
  30. func isConnectedToQuorumSince(transport rafthttp.Transporter, since time.Time, self types.ID, members []*membership.Member) bool {
  31. return numConnectedSince(transport, since, self, members) >= (len(members)/2)+1
  32. }
  33. // isConnectedSince checks whether the local member is connected to the
  34. // remote member since the given time.
  35. func isConnectedSince(transport rafthttp.Transporter, since time.Time, remote types.ID) bool {
  36. t := transport.ActiveSince(remote)
  37. return !t.IsZero() && t.Before(since)
  38. }
  39. // isConnectedFullySince checks whether the local member is connected to all
  40. // members in the cluster since the given time.
  41. func isConnectedFullySince(transport rafthttp.Transporter, since time.Time, self types.ID, members []*membership.Member) bool {
  42. return numConnectedSince(transport, since, self, members) == len(members)
  43. }
  44. // numConnectedSince counts how many members are connected to the local member
  45. // since the given time.
  46. func numConnectedSince(transport rafthttp.Transporter, since time.Time, self types.ID, members []*membership.Member) int {
  47. connectedNum := 0
  48. for _, m := range members {
  49. if m.ID == self || isConnectedSince(transport, since, m.ID) {
  50. connectedNum++
  51. }
  52. }
  53. return connectedNum
  54. }
  55. // longestConnected chooses the member with longest active-since-time.
  56. // It returns false, if nothing is active.
  57. func longestConnected(tp rafthttp.Transporter, membs []types.ID) (types.ID, bool) {
  58. var longest types.ID
  59. var oldest time.Time
  60. for _, id := range membs {
  61. tm := tp.ActiveSince(id)
  62. if tm.IsZero() { // inactive
  63. continue
  64. }
  65. if oldest.IsZero() { // first longest candidate
  66. oldest = tm
  67. longest = id
  68. }
  69. if tm.Before(oldest) {
  70. oldest = tm
  71. longest = id
  72. }
  73. }
  74. if uint64(longest) == 0 {
  75. return longest, false
  76. }
  77. return longest, true
  78. }
  79. type notifier struct {
  80. c chan struct{}
  81. err error
  82. }
  83. func newNotifier() *notifier {
  84. return &notifier{
  85. c: make(chan struct{}),
  86. }
  87. }
  88. func (nc *notifier) notify(err error) {
  89. nc.err = err
  90. close(nc.c)
  91. }
  92. // checkAndRecoverDB attempts to recover db in the scenario when
  93. // etcd server crashes before updating its in-state db
  94. // and after persisting snapshot to disk from syncing with leader,
  95. // snapshot can be newer than db where
  96. // (snapshot.Metadata.Index > db.consistentIndex ).
  97. //
  98. // when that happen:
  99. // 1. find xxx.snap.db that matches snap index.
  100. // 2. rename xxx.snap.db to db.
  101. // 3. open the new db as the backend.
  102. func checkAndRecoverDB(snapshot *raftpb.Snapshot, oldbe backend.Backend, quotaBackendBytes int64, snapdir string) (be backend.Backend, err error) {
  103. var cIndex consistentIndex
  104. kv := mvcc.New(oldbe, &lease.FakeLessor{}, &cIndex)
  105. defer kv.Close()
  106. kvindex := kv.ConsistentIndex()
  107. if snapshot.Metadata.Index <= kvindex {
  108. return oldbe, nil
  109. }
  110. id := snapshot.Metadata.Index
  111. snapfn, err := snap.DBFilePathFromID(snapdir, id)
  112. if err != nil {
  113. return nil, fmt.Errorf("finding %v error: %v", snapdir+fmt.Sprintf("%016x.snap.db", id), err)
  114. }
  115. bepath := snapdir + databaseFilename
  116. if err := os.Rename(snapfn, bepath); err != nil {
  117. return nil, fmt.Errorf("rename snapshot file error: %v", err)
  118. }
  119. oldbe.Close()
  120. be = openBackend(bepath, quotaBackendBytes)
  121. return be, nil
  122. }
  123. func openBackend(bepath string, quotaBackendBytes int64) (be backend.Backend) {
  124. beOpened := make(chan struct{})
  125. go func() {
  126. be = newBackend(bepath, quotaBackendBytes)
  127. beOpened <- struct{}{}
  128. }()
  129. select {
  130. case <-beOpened:
  131. case <-time.After(time.Second):
  132. plog.Warningf("another etcd process is running with the same data dir and holding the file lock.")
  133. plog.Warningf("waiting for it to exit before starting...")
  134. <-beOpened
  135. }
  136. return be
  137. }