Repository: prometheus/prometheus
File: tsdb/head_test.go (Go)

Code before:
			last:      deletedKeepUntil,
			expected:  false,
		},
	}

	for _, tc := range cases {
		t.Run(tc.name, func(t *testing.T) {
			h, _ := newTestHead(t, 1000, wlog.CompressionNone, false)
			t.Cleanup(func() {
				require.NoError(t, h.Close())
			})

			if tc.prepare != nil {
				tc.prepare(t, h)
			}

			kept := h.keepSeriesInWALCheckpoint(tc.seriesRef, tc.last)
			require.Equal(t, tc.expected, kept)
		})
	}
}

func TestHead_WALMultiRef_Checkpoint(t *testing.T) {
	head, w := newTestHead(t, 10000, wlog.CompressionNone, false)

	require.NoError(t, head.Init(0))

	// Write a sample.
	app := head.Appender(context.Background())
	ref1, err := app.Append(0, labels.FromStrings("foo", "bar"), 100, 1)
	require.NoError(t, err)
	require.NoError(t, app.Commit())

	// Truncate head to remove first series entirely.
	require.NoError(t, head.Truncate(500))

	// Write another sample -- will create a new series ID.
	app = head.Appender(context.Background())
	ref2, err := app.Append(0, labels.FromStrings("foo", "bar"), 600, 2)
	require.NoError(t, err)
	require.NoError(t, app.Commit())

	require.NotEqual(t, ref1, ref2, "refs should be different")

	// Start a new WAL segment so we can checkpoint the series entry separately from future samples.
	seg, err := w.NextSegment()
	require.NoError(t, err)
	require.Equal(t, 2, seg)

	// Write another sample -- will be written using the new series ID.
	app = head.Appender(context.Background())

Review comment (refactor):
For reviewers: I kept this test to show that the issue in https://github.com/prometheus/prometheus/issues/16058 is resolved. However, this test is probably overkill, and it's enough to test that duplicate series are correctly marked as deleted during WAL replay, which I do in `TestHead_ReadWAL` above. I can leave this here or delete it before merging.

Code after:
				h.setWALExpiry(chunks.HeadSeriesRef(existingRef), deletedKeepUntil)
			},
			seriesRef: chunks.HeadSeriesRef(existingRef),
			last:      deletedKeepUntil,
			expected:  false,
		},
	}

	for _, tc := range cases {
		t.Run(tc.name, func(t *testing.T) {
			h, _ := newTestHead(t, 1000, wlog.CompressionNone, false)
			t.Cleanup(func() {
				require.NoError(t, h.Close())
			})

			if tc.prepare != nil {
				tc.prepare(t, h)
			}

			kept := h.keepSeriesInWALCheckpoint(tc.seriesRef, tc.last)
			require.Equal(t, tc.expected, kept)
		})
	}
}

func TestHead_ActiveAppenders(t *testing.T) {
	head, _ := newTestHead(t, 1000, wlog.CompressionNone, false)
	defer head.Close()

	require.NoError(t, head.Init(0))

	// First rollback with no samples.
	app := head.Appender(context.Background())
	require.Equal(t, 1.0, prom_testutil.ToFloat64(head.metrics.activeAppenders))
	require.NoError(t, app.Rollback())
	require.Equal(t, 0.0, prom_testutil.ToFloat64(head.metrics.activeAppenders))

	// Then commit with no samples.
	app = head.Appender(context.Background())
	require.NoError(t, app.Commit())
	require.Equal(t, 0.0, prom_testutil.ToFloat64(head.metrics.activeAppenders))

	// Now rollback with one sample.
	app = head.Appender(context.Background())
	_, err := app.Append(0, labels.FromStrings("foo", "bar"), 100, 1)
	require.NoError(t, err)
	require.Equal(t, 1.0, prom_testutil.ToFloat64(head.metrics.activeAppenders))
	require.NoError(t, app.Rollback())
	require.Equal(t, 0.0, prom_testutil.ToFloat64(head.metrics.activeAppenders))

	// Now commit with one sample.