diff --git a/pkg/metrics/wal/wal.go b/pkg/metrics/wal/wal.go index e1176afdeccb..13c0b441d437 100644 --- a/pkg/metrics/wal/wal.go +++ b/pkg/metrics/wal/wal.go @@ -699,6 +699,11 @@ func (a *appender) Append(ref storage.SeriesRef, l labels.Labels, t int64, v flo series.Lock() defer series.Unlock() + if t < series.lastTs { + a.w.metrics.totalOutOfOrderSamples.Inc() + return 0, storage.ErrOutOfOrderSample + } + // NOTE(rfratto): always modify pendingSamples and sampleSeries together. a.pendingSamples = append(a.pendingSamples, record.RefSample{ Ref: series.ref, @@ -819,6 +824,11 @@ func (a *appender) AppendHistogram(ref storage.SeriesRef, l labels.Labels, t int series.Lock() defer series.Unlock() + if t < series.lastTs { + a.w.metrics.totalOutOfOrderSamples.Inc() + return 0, storage.ErrOutOfOrderSample + } + switch { case h != nil: // NOTE(rfratto): always modify pendingHistograms and histogramSeries diff --git a/pkg/metrics/wal/wal_test.go b/pkg/metrics/wal/wal_test.go index 9aaf52d639d1..c99bad9bf579 100644 --- a/pkg/metrics/wal/wal_test.go +++ b/pkg/metrics/wal/wal_test.go @@ -434,35 +434,6 @@ func TestGlobalReferenceID_Normal(t *testing.T) { require.True(t, ref3 == 2) } -func TestDBAllowOOOSamples(t *testing.T) { - walDir := t.TempDir() - - s, err := NewStorage(log.NewNopLogger(), nil, walDir) - require.NoError(t, err) - defer func() { - require.NoError(t, s.Close()) - }() - - app := s.Appender(context.Background()) - - // Write some samples - payload := buildSeries([]string{"foo", "bar", "baz"}) - for _, metric := range payload { - metric.Write(t, app) - } - - require.NoError(t, app.Commit()) - for _, metric := range payload { - for _, sa := range metric.samples { - // We want to set the timestamp to before. This should no longer trigger an out of order. - sa.ts = sa.ts - 10_000 - } - } - for _, metric := range payload { - metric.Write(t, app) - } -} - func BenchmarkAppendExemplar(b *testing.B) { walDir := b.TempDir()