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 <bjboreham@gmail.com>
This commit is contained in:
Bryan Boreham 2021-07-07 14:28:20 +01:00 committed by GitHub
parent 62598878dd
commit dc8f505595
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 8 deletions

View file

@ -1232,8 +1232,7 @@ func (h *Head) Appender(_ context.Context) storage.Appender {
} }
func (h *Head) appender() *headAppender { func (h *Head) appender() *headAppender {
appendID := h.iso.newAppendID() appendID, cleanupAppendIDsBelow := h.iso.newAppendID()
cleanupAppendIDsBelow := h.iso.lowWatermark()
// Allocate the exemplars buffer only if exemplars are enabled. // Allocate the exemplars buffer only if exemplars are enabled.
var exemplarsBuf []exemplarWithSeriesRef var exemplarsBuf []exemplarWithSeriesRef

View file

@ -86,6 +86,10 @@ func newIsolation() *isolation {
func (i *isolation) lowWatermark() uint64 { func (i *isolation) lowWatermark() uint64 {
i.appendMtx.RLock() // Take appendMtx first. i.appendMtx.RLock() // Take appendMtx first.
defer i.appendMtx.RUnlock() defer i.appendMtx.RUnlock()
return i.lowWatermarkLocked()
}
func (i *isolation) lowWatermarkLocked() uint64 {
i.readMtx.RLock() i.readMtx.RLock()
defer i.readMtx.RUnlock() defer i.readMtx.RUnlock()
if i.readsOpen.prev != i.readsOpen { 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 // newAppendID increments the transaction counter and returns a new transaction
// ID. The first ID returned is 1. // 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() i.appendMtx.Lock()
defer i.appendMtx.Unlock() defer i.appendMtx.Unlock()
@ -138,7 +143,7 @@ func (i *isolation) newAppendID() uint64 {
i.appendsOpenList.prev = app i.appendsOpenList.prev = app
i.appendsOpen[app.appendID] = app i.appendsOpen[app.appendID] = app
return app.appendID return app.appendID, i.lowWatermarkLocked()
} }
func (i *isolation) lastAppendID() uint64 { func (i *isolation) lastAppendID() uint64 {

View file

@ -35,8 +35,7 @@ func BenchmarkIsolation(b *testing.B) {
<-start <-start
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
appendID := iso.newAppendID() appendID, _ := iso.newAppendID()
_ = iso.lowWatermark()
iso.closeAppend(appendID) iso.closeAppend(appendID)
} }
@ -66,8 +65,7 @@ func BenchmarkIsolationWithState(b *testing.B) {
<-start <-start
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
appendID := iso.newAppendID() appendID, _ := iso.newAppendID()
_ = iso.lowWatermark()
iso.closeAppend(appendID) iso.closeAppend(appendID)
} }