util.go 3.0 KB

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