util.go 3.1 KB

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