From d9af11e63615a185bfc105400fe4d17af4c943be Mon Sep 17 00:00:00 2001 From: beorn7 Date: Sat, 29 Feb 2020 02:39:26 +0100 Subject: [PATCH] Do not attempt isolation for appendID == 0 Signed-off-by: beorn7 --- tsdb/head.go | 7 +++++-- tsdb/head_test.go | 16 +++++++++++++++- tsdb/isolation.go | 3 ++- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/tsdb/head.go b/tsdb/head.go index 812f44d31..0fde4c845 100644 --- a/tsdb/head.go +++ b/tsdb/head.go @@ -1847,7 +1847,8 @@ func (s *memSeries) truncateChunksBefore(mint int64) (removed int) { } // append adds the sample (t, v) to the series. The caller also has to provide -// the appendID for isolation. +// the appendID for isolation. (The appendID can be zero, which results in no +// isolation for this append.) func (s *memSeries) append(t int64, v float64, appendID uint64) (success, chunkCreated bool) { // Based on Gorilla white papers this offers near-optimal compression ratio // so anything bigger that this has diminishing returns and increases @@ -1885,7 +1886,9 @@ func (s *memSeries) append(t int64, v float64, appendID uint64) (success, chunkC s.sampleBuf[2] = s.sampleBuf[3] s.sampleBuf[3] = sample{t: t, v: v} - s.txs.add(appendID) + if appendID > 0 { + s.txs.add(appendID) + } return true, chunkCreated } diff --git a/tsdb/head_test.go b/tsdb/head_test.go index 0a274ca54..967f86665 100644 --- a/tsdb/head_test.go +++ b/tsdb/head_test.go @@ -1558,7 +1558,7 @@ func TestIsolationLowWatermarkMonotonous(t *testing.T) { _, err = app1.Add(labels.FromStrings("foo", "bar"), 0, 0) testutil.Ok(t, err) testutil.Ok(t, app1.Commit()) - testutil.Equals(t, uint64(1), hb.iso.lowWatermark()) + testutil.Equals(t, uint64(1), hb.iso.lowWatermark(), "Low watermark should by 1 after 1st append.") app1 = hb.Appender() _, err = app1.Add(labels.FromStrings("foo", "bar"), 1, 1) @@ -1580,3 +1580,17 @@ func TestIsolationLowWatermarkMonotonous(t *testing.T) { is.Close() testutil.Equals(t, uint64(3), hb.iso.lowWatermark(), "After read has finished (iso state closed), low watermark should jump to three.") } + +func TestIsolationAppendIDZeroIsNoop(t *testing.T) { + h, err := NewHead(nil, nil, nil, 1000, DefaultStripeSize) + testutil.Ok(t, err) + defer h.Close() + + h.initTime(0) + + s, _ := h.getOrCreate(1, labels.FromStrings("a", "1")) + + ok, _ := s.append(0, 0, 0) + testutil.Assert(t, ok, "Series append failed.") + testutil.Equals(t, 0, s.txs.txIDCount, "Series should not have an appendID after append with appendID=0.") +} diff --git a/tsdb/isolation.go b/tsdb/isolation.go index beaecfc48..12a9bfe92 100644 --- a/tsdb/isolation.go +++ b/tsdb/isolation.go @@ -110,7 +110,8 @@ func (i *isolation) State() *isolationState { return isoState } -// newAppendID increments the transaction counter and returns a new transaction ID. +// newAppendID increments the transaction counter and returns a new transaction +// ID. The first ID returned is 1. func (i *isolation) newAppendID() uint64 { i.appendMtx.Lock() defer i.appendMtx.Unlock()