util.go 2.8 KB

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