| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 1 | // Copyright 2021 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 verify |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | |
| 20 | "go.uber.org/zap" |
| 21 | |
| 22 | "go.etcd.io/etcd/client/pkg/v3/fileutil" |
| 23 | "go.etcd.io/etcd/client/pkg/v3/verify" |
| 24 | "go.etcd.io/etcd/server/v3/storage/backend" |
| 25 | "go.etcd.io/etcd/server/v3/storage/datadir" |
| 26 | "go.etcd.io/etcd/server/v3/storage/schema" |
| 27 | wal2 "go.etcd.io/etcd/server/v3/storage/wal" |
| 28 | "go.etcd.io/etcd/server/v3/storage/wal/walpb" |
| 29 | "go.etcd.io/raft/v3/raftpb" |
| 30 | ) |
| 31 | |
| 32 | const envVerifyValueStorageWAL verify.VerificationType = "storage_wal" |
| 33 | |
| 34 | type Config struct { |
| 35 | // DataDir is a root directory where the data being verified are stored. |
| 36 | DataDir string |
| 37 | |
| 38 | // ExactIndex requires consistent_index in backend exactly match the last committed WAL entry. |
| 39 | // Usually backend's consistent_index needs to be <= WAL.commit, but for backups the match |
| 40 | // is expected to be exact. |
| 41 | ExactIndex bool |
| 42 | |
| 43 | Logger *zap.Logger |
| 44 | } |
| 45 | |
| 46 | // Verify performs consistency checks of given etcd data-directory. |
| 47 | // The errors are reported as the returned error, but for some situations |
| 48 | // the function can also panic. |
| 49 | // The function is expected to work on not-in-use data model, i.e. |
| 50 | // no file-locks should be taken. Verify does not modified the data. |
| 51 | func Verify(cfg Config) (retErr error) { |
| 52 | lg := cfg.Logger |
| 53 | if lg == nil { |
| 54 | lg = zap.NewNop() |
| 55 | } |
| 56 | |
| 57 | if !fileutil.Exist(datadir.ToBackendFileName(cfg.DataDir)) { |
| 58 | lg.Info("verification skipped due to non exist db file") |
| 59 | return nil |
| 60 | } |
| 61 | |
| 62 | lg.Info("verification of persisted state", zap.String("data-dir", cfg.DataDir)) |
| 63 | defer func() { |
| 64 | if retErr != nil { |
| 65 | lg.Error("verification of persisted state failed", |
| 66 | zap.String("data-dir", cfg.DataDir), |
| 67 | zap.Error(retErr)) |
| 68 | } else if r := recover(); r != nil { |
| 69 | lg.Error("verification of persisted state failed", |
| 70 | zap.String("data-dir", cfg.DataDir)) |
| 71 | panic(r) |
| 72 | } else { |
| 73 | lg.Info("verification of persisted state successful", zap.String("data-dir", cfg.DataDir)) |
| 74 | } |
| 75 | }() |
| 76 | |
| 77 | be := backend.NewDefaultBackend(lg, datadir.ToBackendFileName(cfg.DataDir)) |
| 78 | defer be.Close() |
| 79 | |
| 80 | snapshot, hardstate, err := validateWAL(cfg) |
| 81 | if err != nil { |
| 82 | return err |
| 83 | } |
| 84 | |
| 85 | // TODO: Perform validation of consistency of membership between |
| 86 | // backend/members & WAL confstate (and maybe storev2 if still exists). |
| 87 | |
| 88 | return validateConsistentIndex(cfg, hardstate, snapshot, be) |
| 89 | } |
| 90 | |
| 91 | // VerifyIfEnabled performs verification according to ETCD_VERIFY env settings. |
| 92 | // See Verify for more information. |
| 93 | func VerifyIfEnabled(cfg Config) error { |
| 94 | if verify.IsVerificationEnabled(envVerifyValueStorageWAL) { |
| 95 | return Verify(cfg) |
| 96 | } |
| 97 | return nil |
| 98 | } |
| 99 | |
| 100 | // MustVerifyIfEnabled performs verification according to ETCD_VERIFY env settings |
| 101 | // and exits in case of found problems. |
| 102 | // See Verify for more information. |
| 103 | func MustVerifyIfEnabled(cfg Config) { |
| 104 | if err := VerifyIfEnabled(cfg); err != nil { |
| 105 | cfg.Logger.Fatal("Verification failed", |
| 106 | zap.String("data-dir", cfg.DataDir), |
| 107 | zap.Error(err)) |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | func validateConsistentIndex(cfg Config, hardstate *raftpb.HardState, snapshot *walpb.Snapshot, be backend.Backend) error { |
| 112 | index, term := schema.ReadConsistentIndex(be.ReadTx()) |
| 113 | if cfg.ExactIndex && index != hardstate.Commit { |
| 114 | return fmt.Errorf("backend.ConsistentIndex (%v) expected == WAL.HardState.commit (%v)", index, hardstate.Commit) |
| 115 | } |
| 116 | if cfg.ExactIndex && term != hardstate.Term { |
| 117 | return fmt.Errorf("backend.Term (%v) expected == WAL.HardState.term, (%v)", term, hardstate.Term) |
| 118 | } |
| 119 | if index > hardstate.Commit { |
| 120 | return fmt.Errorf("backend.ConsistentIndex (%v) must be <= WAL.HardState.commit (%v)", index, hardstate.Commit) |
| 121 | } |
| 122 | if term > hardstate.Term { |
| 123 | return fmt.Errorf("backend.Term (%v) must be <= WAL.HardState.term, (%v)", term, hardstate.Term) |
| 124 | } |
| 125 | |
| 126 | if index < snapshot.Index { |
| 127 | return fmt.Errorf("backend.ConsistentIndex (%v) must be >= last snapshot index (%v)", index, snapshot.Index) |
| 128 | } |
| 129 | |
| 130 | cfg.Logger.Info("verification: consistentIndex OK", zap.Uint64("backend-consistent-index", index), zap.Uint64("hardstate-commit", hardstate.Commit)) |
| 131 | return nil |
| 132 | } |
| 133 | |
| 134 | func validateWAL(cfg Config) (*walpb.Snapshot, *raftpb.HardState, error) { |
| 135 | walDir := datadir.ToWALDir(cfg.DataDir) |
| 136 | |
| 137 | walSnaps, err := wal2.ValidSnapshotEntries(cfg.Logger, walDir) |
| 138 | if err != nil { |
| 139 | return nil, nil, err |
| 140 | } |
| 141 | |
| 142 | snapshot := walSnaps[len(walSnaps)-1] |
| 143 | hardstate, err := wal2.Verify(cfg.Logger, walDir, snapshot) |
| 144 | if err != nil { |
| 145 | return nil, nil, err |
| 146 | } |
| 147 | return &snapshot, hardstate, nil |
| 148 | } |