util.go 3.0 KB

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