util.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Copyright 2015 CoreOS, Inc.
  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 wal
  15. import (
  16. "fmt"
  17. "log"
  18. "os"
  19. "path"
  20. "github.com/coreos/etcd/pkg/fileutil"
  21. "github.com/coreos/etcd/pkg/types"
  22. )
  23. // WalVersion is an enum for versions of etcd logs.
  24. type WalVersion string
  25. const (
  26. WALUnknown WalVersion = "Unknown WAL"
  27. WALNotExist WalVersion = "No WAL"
  28. WALv0_4 WalVersion = "0.4.x"
  29. WALv2_0 WalVersion = "2.0.0"
  30. WALv2_0Proxy WalVersion = "2.0 proxy"
  31. WALv2_0_1 WalVersion = "2.0.1"
  32. )
  33. func DetectVersion(dirpath string) (WalVersion, error) {
  34. names, err := fileutil.ReadDir(dirpath)
  35. if err != nil {
  36. if os.IsNotExist(err) {
  37. err = nil
  38. }
  39. // Error reading the directory
  40. return WALNotExist, err
  41. }
  42. if len(names) == 0 {
  43. // Empty WAL directory
  44. return WALNotExist, nil
  45. }
  46. nameSet := types.NewUnsafeSet(names...)
  47. if nameSet.Contains("member") {
  48. ver, err := DetectVersion(path.Join(dirpath, "member"))
  49. if ver == WALv2_0 {
  50. return WALv2_0_1, nil
  51. } else if ver == WALv0_4 {
  52. // How in the blazes did it get there?
  53. return WALUnknown, nil
  54. }
  55. return ver, err
  56. }
  57. if nameSet.ContainsAll([]string{"snap", "wal"}) {
  58. // .../wal cannot be empty to exist.
  59. if Exist(path.Join(dirpath, "wal")) {
  60. return WALv2_0, nil
  61. }
  62. }
  63. if nameSet.ContainsAll([]string{"proxy"}) {
  64. return WALv2_0Proxy, nil
  65. }
  66. if nameSet.ContainsAll([]string{"snapshot", "conf", "log"}) {
  67. return WALv0_4, nil
  68. }
  69. if nameSet.ContainsAll([]string{"standby_info"}) {
  70. return WALv0_4, nil
  71. }
  72. return WALUnknown, nil
  73. }
  74. func Exist(dirpath string) bool {
  75. names, err := fileutil.ReadDir(dirpath)
  76. if err != nil {
  77. return false
  78. }
  79. return len(names) != 0
  80. }
  81. // searchIndex returns the last array index of names whose raft index section is
  82. // equal to or smaller than the given index.
  83. // The given names MUST be sorted.
  84. func searchIndex(names []string, index uint64) (int, bool) {
  85. for i := len(names) - 1; i >= 0; i-- {
  86. name := names[i]
  87. _, curIndex, err := parseWalName(name)
  88. if err != nil {
  89. log.Panicf("parse correct name should never fail: %v", err)
  90. }
  91. if index >= curIndex {
  92. return i, true
  93. }
  94. }
  95. return -1, false
  96. }
  97. // names should have been sorted based on sequence number.
  98. // isValidSeq checks whether seq increases continuously.
  99. func isValidSeq(names []string) bool {
  100. var lastSeq uint64
  101. for _, name := range names {
  102. curSeq, _, err := parseWalName(name)
  103. if err != nil {
  104. log.Panicf("parse correct name should never fail: %v", err)
  105. }
  106. if lastSeq != 0 && lastSeq != curSeq-1 {
  107. return false
  108. }
  109. lastSeq = curSeq
  110. }
  111. return true
  112. }
  113. func checkWalNames(names []string) []string {
  114. wnames := make([]string, 0)
  115. for _, name := range names {
  116. if _, _, err := parseWalName(name); err != nil {
  117. log.Printf("wal: parse %s error: %v", name, err)
  118. continue
  119. }
  120. wnames = append(wnames, name)
  121. }
  122. return wnames
  123. }
  124. func parseWalName(str string) (seq, index uint64, err error) {
  125. _, err = fmt.Sscanf(str, "%016x-%016x.wal", &seq, &index)
  126. return
  127. }
  128. func walName(seq, index uint64) string {
  129. return fmt.Sprintf("%016x-%016x.wal", seq, index)
  130. }