|
| 1 | +// Copyright 2025 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 etcdutl |
| 16 | + |
| 17 | +import ( |
| 18 | + "testing" |
| 19 | + |
| 20 | + "github.com/stretchr/testify/require" |
| 21 | + "go.uber.org/zap" |
| 22 | + |
| 23 | + "go.etcd.io/etcd/api/v3/etcdserverpb" |
| 24 | + "go.etcd.io/etcd/client/pkg/v3/fileutil" |
| 25 | + "go.etcd.io/etcd/pkg/v3/pbutil" |
| 26 | + "go.etcd.io/etcd/server/v3/etcdserver/api/snap" |
| 27 | + "go.etcd.io/etcd/server/v3/storage/datadir" |
| 28 | + "go.etcd.io/etcd/server/v3/storage/wal" |
| 29 | + "go.etcd.io/etcd/server/v3/storage/wal/walpb" |
| 30 | + "go.etcd.io/raft/v3/raftpb" |
| 31 | +) |
| 32 | + |
| 33 | +func TestGetLatestWalSnap(t *testing.T) { |
| 34 | + testCases := []struct { |
| 35 | + name string |
| 36 | + walSnaps []walpb.Snapshot |
| 37 | + snapshots []raftpb.Snapshot |
| 38 | + expectedLatestWALSnap walpb.Snapshot |
| 39 | + }{ |
| 40 | + { |
| 41 | + name: "wal snapshot records match the snapshot files", |
| 42 | + walSnaps: []walpb.Snapshot{ |
| 43 | + {Index: 10, Term: 2}, |
| 44 | + {Index: 20, Term: 3}, |
| 45 | + {Index: 30, Term: 5}, |
| 46 | + }, |
| 47 | + snapshots: []raftpb.Snapshot{ |
| 48 | + {Metadata: raftpb.SnapshotMetadata{Index: 10, Term: 2}}, |
| 49 | + {Metadata: raftpb.SnapshotMetadata{Index: 20, Term: 3}}, |
| 50 | + {Metadata: raftpb.SnapshotMetadata{Index: 30, Term: 5}}, |
| 51 | + }, |
| 52 | + expectedLatestWALSnap: walpb.Snapshot{Index: 30, Term: 5}, |
| 53 | + }, |
| 54 | + { |
| 55 | + name: "there are orphan snapshot files", |
| 56 | + walSnaps: []walpb.Snapshot{ |
| 57 | + {Index: 10, Term: 2}, |
| 58 | + {Index: 20, Term: 3}, |
| 59 | + {Index: 35, Term: 5}, |
| 60 | + }, |
| 61 | + snapshots: []raftpb.Snapshot{ |
| 62 | + {Metadata: raftpb.SnapshotMetadata{Index: 10, Term: 2}}, |
| 63 | + {Metadata: raftpb.SnapshotMetadata{Index: 20, Term: 3}}, |
| 64 | + {Metadata: raftpb.SnapshotMetadata{Index: 35, Term: 5}}, |
| 65 | + {Metadata: raftpb.SnapshotMetadata{Index: 40, Term: 6}}, |
| 66 | + {Metadata: raftpb.SnapshotMetadata{Index: 50, Term: 7}}, |
| 67 | + }, |
| 68 | + expectedLatestWALSnap: walpb.Snapshot{Index: 35, Term: 5}, |
| 69 | + }, |
| 70 | + { |
| 71 | + name: "there are orphan snapshot records in wal file", |
| 72 | + walSnaps: []walpb.Snapshot{ |
| 73 | + {Index: 10, Term: 2}, |
| 74 | + {Index: 20, Term: 3}, |
| 75 | + {Index: 30, Term: 4}, |
| 76 | + {Index: 45, Term: 5}, |
| 77 | + {Index: 55, Term: 6}, |
| 78 | + }, |
| 79 | + snapshots: []raftpb.Snapshot{ |
| 80 | + {Metadata: raftpb.SnapshotMetadata{Index: 10, Term: 2}}, |
| 81 | + {Metadata: raftpb.SnapshotMetadata{Index: 20, Term: 3}}, |
| 82 | + {Metadata: raftpb.SnapshotMetadata{Index: 30, Term: 4}}, |
| 83 | + }, |
| 84 | + expectedLatestWALSnap: walpb.Snapshot{Index: 30, Term: 4}, |
| 85 | + }, |
| 86 | + { |
| 87 | + name: "wal snapshot records do not match the snapshot files at all", |
| 88 | + walSnaps: []walpb.Snapshot{ |
| 89 | + {Index: 10, Term: 2}, |
| 90 | + {Index: 20, Term: 3}, |
| 91 | + {Index: 30, Term: 4}, |
| 92 | + }, |
| 93 | + snapshots: []raftpb.Snapshot{ |
| 94 | + {Metadata: raftpb.SnapshotMetadata{Index: 40, Term: 5}}, |
| 95 | + {Metadata: raftpb.SnapshotMetadata{Index: 50, Term: 6}}, |
| 96 | + }, |
| 97 | + expectedLatestWALSnap: walpb.Snapshot{}, |
| 98 | + }, |
| 99 | + } |
| 100 | + |
| 101 | + for _, tc := range testCases { |
| 102 | + t.Run(tc.name, func(t *testing.T) { |
| 103 | + dataDir := t.TempDir() |
| 104 | + lg := zap.NewNop() |
| 105 | + |
| 106 | + require.NoError(t, fileutil.TouchDirAll(lg, datadir.ToMemberDir(dataDir))) |
| 107 | + require.NoError(t, fileutil.TouchDirAll(lg, datadir.ToWALDir(dataDir))) |
| 108 | + require.NoError(t, fileutil.TouchDirAll(lg, datadir.ToSnapDir(dataDir))) |
| 109 | + |
| 110 | + // populate wal file |
| 111 | + w, err := wal.Create(lg, datadir.ToWALDir(dataDir), pbutil.MustMarshal( |
| 112 | + &etcdserverpb.Metadata{ |
| 113 | + NodeID: 1, |
| 114 | + ClusterID: 2, |
| 115 | + }, |
| 116 | + )) |
| 117 | + require.NoError(t, err) |
| 118 | + |
| 119 | + for _, walSnap := range tc.walSnaps { |
| 120 | + walSnap.ConfState = &raftpb.ConfState{Voters: []uint64{1}} |
| 121 | + walErr := w.SaveSnapshot(walSnap) |
| 122 | + require.NoError(t, walErr) |
| 123 | + walErr = w.Save(raftpb.HardState{Term: walSnap.Term, Commit: walSnap.Index, Vote: 1}, nil) |
| 124 | + require.NoError(t, walErr) |
| 125 | + } |
| 126 | + err = w.Close() |
| 127 | + require.NoError(t, err) |
| 128 | + |
| 129 | + // generate snapshot files |
| 130 | + ss := snap.New(lg, datadir.ToSnapDir(dataDir)) |
| 131 | + for _, snap := range tc.snapshots { |
| 132 | + snap.Metadata.ConfState = raftpb.ConfState{Voters: []uint64{1}} |
| 133 | + snapErr := ss.SaveSnap(snap) |
| 134 | + require.NoError(t, snapErr) |
| 135 | + } |
| 136 | + |
| 137 | + walSnap, err := getLatestWALSnap(lg, dataDir) |
| 138 | + require.NoError(t, err) |
| 139 | + |
| 140 | + require.Equal(t, tc.expectedLatestWALSnap, walSnap) |
| 141 | + }) |
| 142 | + } |
| 143 | +} |
0 commit comments