From dc8f50559534a0820823c27ded103f3cee4b2af4 Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Wed, 7 Jul 2021 14:28:20 +0100 Subject: [PATCH] tsdb: coalesce lock/unlock operations for append (#9061) Fetch the low watermark value under the same lock as we need for the appender, rather than releasing then re-aquiring a lock on the same Mutex. Signed-off-by: Bryan Boreham --- tsdb/head.go | 3 +-- tsdb/isolation.go | 9 +++++++-- tsdb/isolation_test.go | 6 ++---- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/tsdb/head.go b/tsdb/head.go index 1fce0368fc..02ae2a140d 100644 --- a/tsdb/head.go +++ b/tsdb/head.go @@ -1232,8 +1232,7 @@ func (h *Head) Appender(_ context.Context) storage.Appender { } func (h *Head) appender() *headAppender { - appendID := h.iso.newAppendID() - cleanupAppendIDsBelow := h.iso.lowWatermark() + appendID, cleanupAppendIDsBelow := h.iso.newAppendID() // Allocate the exemplars buffer only if exemplars are enabled. var exemplarsBuf []exemplarWithSeriesRef diff --git a/tsdb/isolation.go b/tsdb/isolation.go index cee6899dd7..51302b23d4 100644 --- a/tsdb/isolation.go +++ b/tsdb/isolation.go @@ -86,6 +86,10 @@ func newIsolation() *isolation { func (i *isolation) lowWatermark() uint64 { i.appendMtx.RLock() // Take appendMtx first. defer i.appendMtx.RUnlock() + return i.lowWatermarkLocked() +} + +func (i *isolation) lowWatermarkLocked() uint64 { i.readMtx.RLock() defer i.readMtx.RUnlock() if i.readsOpen.prev != i.readsOpen { @@ -122,7 +126,8 @@ func (i *isolation) State() *isolationState { // newAppendID increments the transaction counter and returns a new transaction // ID. The first ID returned is 1. -func (i *isolation) newAppendID() uint64 { +// Also returns the low watermark, to keep lock/unlock operations down +func (i *isolation) newAppendID() (uint64, uint64) { i.appendMtx.Lock() defer i.appendMtx.Unlock() @@ -138,7 +143,7 @@ func (i *isolation) newAppendID() uint64 { i.appendsOpenList.prev = app i.appendsOpen[app.appendID] = app - return app.appendID + return app.appendID, i.lowWatermarkLocked() } func (i *isolation) lastAppendID() uint64 { diff --git a/tsdb/isolation_test.go b/tsdb/isolation_test.go index 3a90547b20..79ae21435a 100644 --- a/tsdb/isolation_test.go +++ b/tsdb/isolation_test.go @@ -35,8 +35,7 @@ func BenchmarkIsolation(b *testing.B) { <-start for i := 0; i < b.N; i++ { - appendID := iso.newAppendID() - _ = iso.lowWatermark() + appendID, _ := iso.newAppendID() iso.closeAppend(appendID) } @@ -66,8 +65,7 @@ func BenchmarkIsolationWithState(b *testing.B) { <-start for i := 0; i < b.N; i++ { - appendID := iso.newAppendID() - _ = iso.lowWatermark() + appendID, _ := iso.newAppendID() iso.closeAppend(appendID) }