util.go 3.3 KB

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