mirror of
https://github.com/prometheus/prometheus.git
synced 2025-02-02 08:31:11 -08:00
* Write chunks via queue, predicting the refs Our load tests have shown that there is a latency spike in the remote write handler whenever the head chunks need to be written, because chunkDiskMapper.WriteChunk() blocks until the chunks are written to disk. This adds a queue to the chunk disk mapper which makes the WriteChunk() method non-blocking unless the queue is full. Reads can still be served from the queue. Signed-off-by: Mauro Stettler <mauro.stettler@gmail.com> * address PR feeddback Signed-off-by: Mauro Stettler <mauro.stettler@gmail.com> * initialize metrics without .Add(0) Signed-off-by: Mauro Stettler <mauro.stettler@gmail.com> * change isRunningMtx to normal lock Signed-off-by: Mauro Stettler <mauro.stettler@gmail.com> * do not re-initialize chunkrefmap Signed-off-by: Mauro Stettler <mauro.stettler@gmail.com> * update metric outside of lock scope Signed-off-by: Mauro Stettler <mauro.stettler@gmail.com> * add benchmark for adding job to chunk write queue Signed-off-by: Mauro Stettler <mauro.stettler@gmail.com> * remove unnecessary "success" var Signed-off-by: Mauro Stettler <mauro.stettler@gmail.com> * gofumpt -extra Signed-off-by: Mauro Stettler <mauro.stettler@gmail.com> * avoid WithLabelValues call in addJob Signed-off-by: Mauro Stettler <mauro.stettler@gmail.com> * format comments Signed-off-by: Mauro Stettler <mauro.stettler@gmail.com> * addressing PR feedback Signed-off-by: Mauro Stettler <mauro.stettler@gmail.com> * rename cutExpectRef to cutAndExpectRef Signed-off-by: Mauro Stettler <mauro.stettler@gmail.com> * use head.Init() instead of .initTime() Signed-off-by: Mauro Stettler <mauro.stettler@gmail.com> * address PR feedback Signed-off-by: Mauro Stettler <mauro.stettler@gmail.com> * PR feedback Co-authored-by: Ganesh Vernekar <15064823+codesome@users.noreply.github.com> Signed-off-by: Mauro Stettler <mauro.stettler@gmail.com> * update test according to PR feedback Signed-off-by: Mauro Stettler <mauro.stettler@gmail.com> * replace callbackWg -> awaitCb Signed-off-by: Mauro Stettler <mauro.stettler@gmail.com> * better test of truncation with empty files Signed-off-by: Mauro Stettler <mauro.stettler@gmail.com> * replace callbackWg -> awaitCb Signed-off-by: Mauro Stettler <mauro.stettler@gmail.com> Co-authored-by: Ganesh Vernekar <15064823+codesome@users.noreply.github.com>
166 lines
3.9 KiB
Go
166 lines
3.9 KiB
Go
// Copyright 2021 The Prometheus Authors
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package chunks
|
|
|
|
import (
|
|
"errors"
|
|
"sync"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"github.com/prometheus/prometheus/tsdb/chunkenc"
|
|
)
|
|
|
|
type chunkWriteJob struct {
|
|
cutFile bool
|
|
seriesRef HeadSeriesRef
|
|
mint int64
|
|
maxt int64
|
|
chk chunkenc.Chunk
|
|
ref ChunkDiskMapperRef
|
|
callback func(error)
|
|
}
|
|
|
|
// chunkWriteQueue is a queue for writing chunks to disk in a non-blocking fashion.
|
|
// Chunks that shall be written get added to the queue, which is consumed asynchronously.
|
|
// Adding jobs to the job is non-blocking as long as the queue isn't full.
|
|
type chunkWriteQueue struct {
|
|
jobs chan chunkWriteJob
|
|
|
|
chunkRefMapMtx sync.RWMutex
|
|
chunkRefMap map[ChunkDiskMapperRef]chunkenc.Chunk
|
|
|
|
isRunningMtx sync.Mutex // Protects the isRunning property.
|
|
isRunning bool // Used to prevent that new jobs get added to the queue when the chan is already closed.
|
|
|
|
workerWg sync.WaitGroup
|
|
|
|
writeChunk writeChunkF
|
|
|
|
// Keeping three separate counters instead of only a single CounterVec to improve the performance of the critical
|
|
// addJob() method which otherwise would need to perform a WithLabelValues call on the CounterVec.
|
|
adds prometheus.Counter
|
|
gets prometheus.Counter
|
|
completed prometheus.Counter
|
|
}
|
|
|
|
// writeChunkF is a function which writes chunks, it is dynamic to allow mocking in tests.
|
|
type writeChunkF func(HeadSeriesRef, int64, int64, chunkenc.Chunk, ChunkDiskMapperRef, bool) error
|
|
|
|
func newChunkWriteQueue(reg prometheus.Registerer, size int, writeChunk writeChunkF) *chunkWriteQueue {
|
|
counters := prometheus.NewCounterVec(
|
|
prometheus.CounterOpts{
|
|
Name: "prometheus_tsdb_chunk_write_queue_operations_total",
|
|
Help: "Number of operations on the chunk_write_queue.",
|
|
},
|
|
[]string{"operation"},
|
|
)
|
|
|
|
q := &chunkWriteQueue{
|
|
jobs: make(chan chunkWriteJob, size),
|
|
chunkRefMap: make(map[ChunkDiskMapperRef]chunkenc.Chunk, size),
|
|
writeChunk: writeChunk,
|
|
|
|
adds: counters.WithLabelValues("add"),
|
|
gets: counters.WithLabelValues("get"),
|
|
completed: counters.WithLabelValues("complete"),
|
|
}
|
|
|
|
if reg != nil {
|
|
reg.MustRegister(counters)
|
|
}
|
|
|
|
q.start()
|
|
return q
|
|
}
|
|
|
|
func (c *chunkWriteQueue) start() {
|
|
c.workerWg.Add(1)
|
|
go func() {
|
|
defer c.workerWg.Done()
|
|
|
|
for job := range c.jobs {
|
|
c.processJob(job)
|
|
}
|
|
}()
|
|
|
|
c.isRunningMtx.Lock()
|
|
c.isRunning = true
|
|
c.isRunningMtx.Unlock()
|
|
}
|
|
|
|
func (c *chunkWriteQueue) processJob(job chunkWriteJob) {
|
|
err := c.writeChunk(job.seriesRef, job.mint, job.maxt, job.chk, job.ref, job.cutFile)
|
|
if job.callback != nil {
|
|
job.callback(err)
|
|
}
|
|
|
|
c.chunkRefMapMtx.Lock()
|
|
defer c.chunkRefMapMtx.Unlock()
|
|
|
|
delete(c.chunkRefMap, job.ref)
|
|
|
|
c.completed.Inc()
|
|
}
|
|
|
|
func (c *chunkWriteQueue) addJob(job chunkWriteJob) (err error) {
|
|
defer func() {
|
|
if err == nil {
|
|
c.adds.Inc()
|
|
}
|
|
}()
|
|
|
|
c.isRunningMtx.Lock()
|
|
defer c.isRunningMtx.Unlock()
|
|
|
|
if !c.isRunning {
|
|
return errors.New("queue is not started")
|
|
}
|
|
|
|
c.chunkRefMapMtx.Lock()
|
|
c.chunkRefMap[job.ref] = job.chk
|
|
c.chunkRefMapMtx.Unlock()
|
|
|
|
c.jobs <- job
|
|
|
|
return nil
|
|
}
|
|
|
|
func (c *chunkWriteQueue) get(ref ChunkDiskMapperRef) chunkenc.Chunk {
|
|
c.chunkRefMapMtx.RLock()
|
|
defer c.chunkRefMapMtx.RUnlock()
|
|
|
|
chk, ok := c.chunkRefMap[ref]
|
|
if ok {
|
|
c.gets.Inc()
|
|
}
|
|
|
|
return chk
|
|
}
|
|
|
|
func (c *chunkWriteQueue) stop() {
|
|
c.isRunningMtx.Lock()
|
|
defer c.isRunningMtx.Unlock()
|
|
|
|
if !c.isRunning {
|
|
return
|
|
}
|
|
|
|
c.isRunning = false
|
|
|
|
close(c.jobs)
|
|
|
|
c.workerWg.Wait()
|
|
}
|