util.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright 2015 The etcd Authors
  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. "errors"
  17. "fmt"
  18. "strings"
  19. "github.com/coreos/etcd/pkg/fileutil"
  20. )
  21. var (
  22. badWalName = errors.New("bad wal name")
  23. )
  24. func Exist(dirpath string) bool {
  25. names, err := fileutil.ReadDir(dirpath)
  26. if err != nil {
  27. return false
  28. }
  29. return len(names) != 0
  30. }
  31. // searchIndex returns the last array index of names whose raft index section is
  32. // equal to or smaller than the given index.
  33. // The given names MUST be sorted.
  34. func searchIndex(names []string, index uint64) (int, bool) {
  35. for i := len(names) - 1; i >= 0; i-- {
  36. name := names[i]
  37. _, curIndex, err := parseWalName(name)
  38. if err != nil {
  39. plog.Panicf("parse correct name should never fail: %v", err)
  40. }
  41. if index >= curIndex {
  42. return i, true
  43. }
  44. }
  45. return -1, false
  46. }
  47. // names should have been sorted based on sequence number.
  48. // isValidSeq checks whether seq increases continuously.
  49. func isValidSeq(names []string) bool {
  50. var lastSeq uint64
  51. for _, name := range names {
  52. curSeq, _, err := parseWalName(name)
  53. if err != nil {
  54. plog.Panicf("parse correct name should never fail: %v", err)
  55. }
  56. if lastSeq != 0 && lastSeq != curSeq-1 {
  57. return false
  58. }
  59. lastSeq = curSeq
  60. }
  61. return true
  62. }
  63. func readWalNames(dirpath string) ([]string, error) {
  64. names, err := fileutil.ReadDir(dirpath)
  65. if err != nil {
  66. return nil, err
  67. }
  68. wnames := checkWalNames(names)
  69. if len(wnames) == 0 {
  70. return nil, ErrFileNotFound
  71. }
  72. return wnames, nil
  73. }
  74. func checkWalNames(names []string) []string {
  75. wnames := make([]string, 0)
  76. for _, name := range names {
  77. if _, _, err := parseWalName(name); err != nil {
  78. // don't complain about left over tmp files
  79. if !strings.HasSuffix(name, ".tmp") {
  80. plog.Warningf("ignored file %v in wal", name)
  81. }
  82. continue
  83. }
  84. wnames = append(wnames, name)
  85. }
  86. return wnames
  87. }
  88. func parseWalName(str string) (seq, index uint64, err error) {
  89. if !strings.HasSuffix(str, ".wal") {
  90. return 0, 0, badWalName
  91. }
  92. _, err = fmt.Sscanf(str, "%016x-%016x.wal", &seq, &index)
  93. return seq, index, err
  94. }
  95. func walName(seq, index uint64) string {
  96. return fmt.Sprintf("%016x-%016x.wal", seq, index)
  97. }