diff --git a/tsdb/head_append.go b/tsdb/head_append.go index 75f75dd7b..10c7d0795 100644 --- a/tsdb/head_append.go +++ b/tsdb/head_append.go @@ -85,6 +85,7 @@ func (a *initAppender) GetRef(lset labels.Labels) (uint64, labels.Labels) { func (a *initAppender) Commit() error { if a.app == nil { + a.head.metrics.activeAppenders.Dec() return nil } return a.app.Commit() @@ -92,6 +93,7 @@ func (a *initAppender) Commit() error { func (a *initAppender) Rollback() error { if a.app == nil { + a.head.metrics.activeAppenders.Dec() return nil } return a.app.Rollback() diff --git a/tsdb/head_test.go b/tsdb/head_test.go index b3b113185..b57a076bc 100644 --- a/tsdb/head_test.go +++ b/tsdb/head_test.go @@ -402,6 +402,39 @@ func TestHead_WALMultiRef(t *testing.T) { }}, series) } +func TestHead_ActiveAppenders(t *testing.T) { + head, _ := newTestHead(t, 1000, 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. + app = head.Appender(context.Background()) + _, err = app.Append(0, labels.FromStrings("foo", "bar"), 100, 1) + require.NoError(t, err) + require.NoError(t, app.Commit()) + require.Equal(t, 0.0, prom_testutil.ToFloat64(head.metrics.activeAppenders)) +} + func TestHead_UnknownWALRecord(t *testing.T) { head, w := newTestHead(t, 1000, false) w.Log([]byte{255, 42})