util.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. Copyright 2014 CoreOS, Inc.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package wal
  14. import (
  15. "fmt"
  16. "log"
  17. "os"
  18. "path"
  19. "github.com/coreos/etcd/pkg/types"
  20. )
  21. // WalVersion is an enum for versions of etcd logs.
  22. type WalVersion string
  23. const (
  24. WALUnknown WalVersion = "Unknown WAL"
  25. WALNotExist WalVersion = "No WAL"
  26. WALv0_4 WalVersion = "0.4.x"
  27. WALv0_5 WalVersion = "0.5.x"
  28. )
  29. func DetectVersion(dirpath string) WalVersion {
  30. names, err := readDir(dirpath)
  31. if err != nil || len(names) == 0 {
  32. return WALNotExist
  33. }
  34. nameSet := types.NewUnsafeSet(names...)
  35. if nameSet.ContainsAll([]string{"snap", "wal"}) {
  36. // .../wal cannot be empty to exist.
  37. if Exist(path.Join(dirpath, "wal")) {
  38. return WALv0_5
  39. }
  40. return WALNotExist
  41. }
  42. if nameSet.ContainsAll([]string{"snapshot", "conf", "log"}) {
  43. return WALv0_4
  44. }
  45. return WALUnknown
  46. }
  47. func Exist(dirpath string) bool {
  48. names, err := readDir(dirpath)
  49. if err != nil {
  50. return false
  51. }
  52. return len(names) != 0
  53. }
  54. // searchIndex returns the last array index of names whose raft index section is
  55. // equal to or smaller than the given index.
  56. // The given names MUST be sorted.
  57. func searchIndex(names []string, index uint64) (int, bool) {
  58. for i := len(names) - 1; i >= 0; i-- {
  59. name := names[i]
  60. _, curIndex, err := parseWalName(name)
  61. if err != nil {
  62. log.Panicf("parse correct name should never fail: %v", err)
  63. }
  64. if index >= curIndex {
  65. return i, true
  66. }
  67. }
  68. return -1, false
  69. }
  70. // names should have been sorted based on sequence number.
  71. // isValidSeq checks whether seq increases continuously.
  72. func isValidSeq(names []string) bool {
  73. var lastSeq uint64
  74. for _, name := range names {
  75. curSeq, _, err := parseWalName(name)
  76. if err != nil {
  77. log.Panicf("parse correct name should never fail: %v", err)
  78. }
  79. if lastSeq != 0 && lastSeq != curSeq-1 {
  80. return false
  81. }
  82. lastSeq = curSeq
  83. }
  84. return true
  85. }
  86. // readDir returns the filenames in wal directory.
  87. func readDir(dirpath string) ([]string, error) {
  88. dir, err := os.Open(dirpath)
  89. if err != nil {
  90. return nil, err
  91. }
  92. defer dir.Close()
  93. names, err := dir.Readdirnames(-1)
  94. if err != nil {
  95. return nil, err
  96. }
  97. return names, nil
  98. }
  99. func checkWalNames(names []string) []string {
  100. wnames := make([]string, 0)
  101. for _, name := range names {
  102. if _, _, err := parseWalName(name); err != nil {
  103. log.Printf("wal: parse %s error: %v", name, err)
  104. continue
  105. }
  106. wnames = append(wnames, name)
  107. }
  108. return wnames
  109. }
  110. func parseWalName(str string) (seq, index uint64, err error) {
  111. var num int
  112. num, err = fmt.Sscanf(str, "%016x-%016x.wal", &seq, &index)
  113. if num != 2 && err == nil {
  114. err = fmt.Errorf("bad wal name: %s", str)
  115. }
  116. return
  117. }
  118. func walName(seq, index uint64) string {
  119. return fmt.Sprintf("%016x-%016x.wal", seq, index)
  120. }
  121. func max(a, b int64) int64 {
  122. if a > b {
  123. return a
  124. }
  125. return b
  126. }