util.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. "go.etcd.io/etcd/pkg/fileutil"
  20. "go.uber.org/zap"
  21. )
  22. var errBadWALName = errors.New("bad wal name")
  23. // Exist returns true if there are any files in a given directory.
  24. func Exist(dir string) bool {
  25. names, err := fileutil.ReadDir(dir, fileutil.WithExt(".wal"))
  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(lg *zap.Logger, 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. if lg != nil {
  40. lg.Panic("failed to parse WAL file name", zap.String("path", name), zap.Error(err))
  41. } else {
  42. plog.Panicf("parse correct name should never fail: %v", err)
  43. }
  44. }
  45. if index >= curIndex {
  46. return i, true
  47. }
  48. }
  49. return -1, false
  50. }
  51. // names should have been sorted based on sequence number.
  52. // isValidSeq checks whether seq increases continuously.
  53. func isValidSeq(lg *zap.Logger, names []string) bool {
  54. var lastSeq uint64
  55. for _, name := range names {
  56. curSeq, _, err := parseWALName(name)
  57. if err != nil {
  58. if lg != nil {
  59. lg.Panic("failed to parse WAL file name", zap.String("path", name), zap.Error(err))
  60. } else {
  61. plog.Panicf("parse correct name should never fail: %v", err)
  62. }
  63. }
  64. if lastSeq != 0 && lastSeq != curSeq-1 {
  65. return false
  66. }
  67. lastSeq = curSeq
  68. }
  69. return true
  70. }
  71. func readWALNames(lg *zap.Logger, dirpath string) ([]string, error) {
  72. names, err := fileutil.ReadDir(dirpath)
  73. if err != nil {
  74. return nil, err
  75. }
  76. wnames := checkWalNames(lg, names)
  77. if len(wnames) == 0 {
  78. return nil, ErrFileNotFound
  79. }
  80. return wnames, nil
  81. }
  82. func checkWalNames(lg *zap.Logger, names []string) []string {
  83. wnames := make([]string, 0)
  84. for _, name := range names {
  85. if _, _, err := parseWALName(name); err != nil {
  86. // don't complain about left over tmp files
  87. if !strings.HasSuffix(name, ".tmp") {
  88. if lg != nil {
  89. lg.Warn(
  90. "ignored file in WAL directory",
  91. zap.String("path", name),
  92. )
  93. } else {
  94. plog.Warningf("ignored file %v in wal", name)
  95. }
  96. }
  97. continue
  98. }
  99. wnames = append(wnames, name)
  100. }
  101. return wnames
  102. }
  103. func parseWALName(str string) (seq, index uint64, err error) {
  104. if !strings.HasSuffix(str, ".wal") {
  105. return 0, 0, errBadWALName
  106. }
  107. _, err = fmt.Sscanf(str, "%016x-%016x.wal", &seq, &index)
  108. return seq, index, err
  109. }
  110. func walName(seq, index uint64) string {
  111. return fmt.Sprintf("%016x-%016x.wal", seq, index)
  112. }