|
|
|
@ -4786,3 +4786,58 @@ func TestGaugeFloatHistogramWALAndChunkHeader(t *testing.T) { |
|
|
|
|
|
|
|
|
|
checkHeaders() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestSnapshotAheadOfWALError(t *testing.T) { |
|
|
|
|
head, _ := newTestHead(t, 120*4, false, false) |
|
|
|
|
head.opts.EnableMemorySnapshotOnShutdown = true |
|
|
|
|
// Add a sample to fill WAL.
|
|
|
|
|
app := head.Appender(context.Background()) |
|
|
|
|
_, err := app.Append(0, labels.FromStrings("foo", "bar"), 10, 10) |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
require.NoError(t, app.Commit()) |
|
|
|
|
|
|
|
|
|
// Increment snapshot index to create sufficiently large difference.
|
|
|
|
|
for i := 0; i < 2; i++ { |
|
|
|
|
_, err = head.wal.NextSegment() |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
} |
|
|
|
|
require.NoError(t, head.Close()) // This will create a snapshot.
|
|
|
|
|
|
|
|
|
|
_, idx, _, err := LastChunkSnapshot(head.opts.ChunkDirRoot) |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
require.Equal(t, 2, idx) |
|
|
|
|
|
|
|
|
|
// Restart the WAL while keeping the old snapshot. The new head is created manually in this case in order
|
|
|
|
|
// to keep using the same snapshot directory instead of a random one.
|
|
|
|
|
require.NoError(t, os.RemoveAll(head.wal.Dir())) |
|
|
|
|
head.opts.EnableMemorySnapshotOnShutdown = false |
|
|
|
|
w, _ := wlog.NewSize(nil, nil, head.wal.Dir(), 32768, false) |
|
|
|
|
head, err = NewHead(nil, nil, w, nil, head.opts, nil) |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
// Add a sample to fill WAL.
|
|
|
|
|
app = head.Appender(context.Background()) |
|
|
|
|
_, err = app.Append(0, labels.FromStrings("foo", "bar"), 10, 10) |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
require.NoError(t, app.Commit()) |
|
|
|
|
lastSegment, _, _ := w.LastSegmentAndOffset() |
|
|
|
|
require.Equal(t, 0, lastSegment) |
|
|
|
|
require.NoError(t, head.Close()) |
|
|
|
|
|
|
|
|
|
// New WAL is saved, but old snapshot still exists.
|
|
|
|
|
_, idx, _, err = LastChunkSnapshot(head.opts.ChunkDirRoot) |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
require.Equal(t, 2, idx) |
|
|
|
|
|
|
|
|
|
// Create new Head which should detect the incorrect index and delete the snapshot.
|
|
|
|
|
head.opts.EnableMemorySnapshotOnShutdown = true |
|
|
|
|
w, _ = wlog.NewSize(nil, nil, head.wal.Dir(), 32768, false) |
|
|
|
|
head, err = NewHead(nil, nil, w, nil, head.opts, nil) |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
require.NoError(t, head.Init(math.MinInt64)) |
|
|
|
|
|
|
|
|
|
// Verify that snapshot directory does not exist anymore.
|
|
|
|
|
_, _, _, err = LastChunkSnapshot(head.opts.ChunkDirRoot) |
|
|
|
|
require.Equal(t, record.ErrNotFound, err) |
|
|
|
|
|
|
|
|
|
require.NoError(t, head.Close()) |
|
|
|
|
} |
|
|
|
|