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