|
| 1 | +package tsdb |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/pkg/errors" |
| 8 | + "go.uber.org/atomic" |
| 9 | + "golang.org/x/sync/semaphore" |
| 10 | + |
| 11 | + "github.com/prometheus/prometheus/model/labels" |
| 12 | + "github.com/prometheus/prometheus/storage" |
| 13 | + "github.com/prometheus/prometheus/tsdb/chunkenc" |
| 14 | + "github.com/prometheus/prometheus/tsdb/chunks" |
| 15 | +) |
| 16 | + |
| 17 | +// asyncBlockWriter runs a background goroutine that writes series and chunks to the block asynchronously. |
| 18 | +type asyncBlockWriter struct { |
| 19 | + chunkPool chunkenc.Pool // Where to return chunks after writing. |
| 20 | + |
| 21 | + chunkw ChunkWriter |
| 22 | + indexw IndexWriter |
| 23 | + |
| 24 | + closeSemaphore *semaphore.Weighted |
| 25 | + |
| 26 | + seriesChan chan seriesToWrite |
| 27 | + finishedCh chan asyncBlockWriterResult |
| 28 | + |
| 29 | + closed bool |
| 30 | + result asyncBlockWriterResult |
| 31 | +} |
| 32 | + |
| 33 | +type asyncBlockWriterResult struct { |
| 34 | + stats BlockStats |
| 35 | + err error |
| 36 | +} |
| 37 | + |
| 38 | +type seriesToWrite struct { |
| 39 | + lbls labels.Labels |
| 40 | + chks []chunks.Meta |
| 41 | +} |
| 42 | + |
| 43 | +func newAsyncBlockWriter(chunkPool chunkenc.Pool, chunkw ChunkWriter, indexw IndexWriter, closeSema *semaphore.Weighted) *asyncBlockWriter { |
| 44 | + bw := &asyncBlockWriter{ |
| 45 | + chunkPool: chunkPool, |
| 46 | + chunkw: chunkw, |
| 47 | + indexw: indexw, |
| 48 | + seriesChan: make(chan seriesToWrite, 64), |
| 49 | + finishedCh: make(chan asyncBlockWriterResult, 1), |
| 50 | + closeSemaphore: closeSema, |
| 51 | + } |
| 52 | + |
| 53 | + go bw.loop() |
| 54 | + return bw |
| 55 | +} |
| 56 | + |
| 57 | +// loop doing the writes. Return value is only used by defer statement, and is sent to the channel, |
| 58 | +// before closing it. |
| 59 | +func (bw *asyncBlockWriter) loop() (res asyncBlockWriterResult) { |
| 60 | + defer func() { |
| 61 | + bw.finishedCh <- res |
| 62 | + close(bw.finishedCh) |
| 63 | + }() |
| 64 | + |
| 65 | + stats := BlockStats{} |
| 66 | + ref := storage.SeriesRef(0) |
| 67 | + for sw := range bw.seriesChan { |
| 68 | + if err := bw.chunkw.WriteChunks(sw.chks...); err != nil { |
| 69 | + return asyncBlockWriterResult{err: errors.Wrap(err, "write chunks")} |
| 70 | + } |
| 71 | + if err := bw.indexw.AddSeries(ref, sw.lbls, sw.chks...); err != nil { |
| 72 | + return asyncBlockWriterResult{err: errors.Wrap(err, "add series")} |
| 73 | + } |
| 74 | + |
| 75 | + stats.NumChunks += uint64(len(sw.chks)) |
| 76 | + stats.NumSeries++ |
| 77 | + for _, chk := range sw.chks { |
| 78 | + stats.NumSamples += uint64(chk.Chunk.NumSamples()) |
| 79 | + } |
| 80 | + |
| 81 | + for _, chk := range sw.chks { |
| 82 | + if err := bw.chunkPool.Put(chk.Chunk); err != nil { |
| 83 | + return asyncBlockWriterResult{err: errors.Wrap(err, "put chunk")} |
| 84 | + } |
| 85 | + } |
| 86 | + ref++ |
| 87 | + } |
| 88 | + |
| 89 | + err := bw.closeSemaphore.Acquire(context.Background(), 1) |
| 90 | + if err != nil { |
| 91 | + return asyncBlockWriterResult{err: errors.Wrap(err, "failed to acquire semaphore before closing writers")} |
| 92 | + } |
| 93 | + defer bw.closeSemaphore.Release(1) |
| 94 | + |
| 95 | + // If everything went fine with writing so far, close writers. |
| 96 | + if err := bw.chunkw.Close(); err != nil { |
| 97 | + return asyncBlockWriterResult{err: errors.Wrap(err, "closing chunk writer")} |
| 98 | + } |
| 99 | + if err := bw.indexw.Close(); err != nil { |
| 100 | + return asyncBlockWriterResult{err: errors.Wrap(err, "closing index writer")} |
| 101 | + } |
| 102 | + |
| 103 | + return asyncBlockWriterResult{stats: stats} |
| 104 | +} |
| 105 | + |
| 106 | +func (bw *asyncBlockWriter) addSeries(lbls labels.Labels, chks []chunks.Meta) error { |
| 107 | + select { |
| 108 | + case bw.seriesChan <- seriesToWrite{lbls: lbls, chks: chks}: |
| 109 | + return nil |
| 110 | + case result, ok := <-bw.finishedCh: |
| 111 | + if ok { |
| 112 | + bw.result = result |
| 113 | + } |
| 114 | + return fmt.Errorf("asyncBlockWriter doesn't run anymore") |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +func (bw *asyncBlockWriter) closeAsync() { |
| 119 | + if !bw.closed { |
| 120 | + bw.closed = true |
| 121 | + |
| 122 | + close(bw.seriesChan) |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +func (bw *asyncBlockWriter) waitFinished() (BlockStats, error) { |
| 127 | + // Wait for flusher to finish. |
| 128 | + result, ok := <-bw.finishedCh |
| 129 | + if ok { |
| 130 | + bw.result = result |
| 131 | + } |
| 132 | + |
| 133 | + return bw.result.stats, bw.result.err |
| 134 | +} |
| 135 | + |
| 136 | +type preventDoubleCloseIndexWriter struct { |
| 137 | + IndexWriter |
| 138 | + closed atomic.Bool |
| 139 | +} |
| 140 | + |
| 141 | +func newPreventDoubleCloseIndexWriter(iw IndexWriter) *preventDoubleCloseIndexWriter { |
| 142 | + return &preventDoubleCloseIndexWriter{IndexWriter: iw} |
| 143 | +} |
| 144 | + |
| 145 | +func (p *preventDoubleCloseIndexWriter) Close() error { |
| 146 | + if p.closed.CAS(false, true) { |
| 147 | + return p.IndexWriter.Close() |
| 148 | + } |
| 149 | + return nil |
| 150 | +} |
| 151 | + |
| 152 | +type preventDoubleCloseChunkWriter struct { |
| 153 | + ChunkWriter |
| 154 | + closed atomic.Bool |
| 155 | +} |
| 156 | + |
| 157 | +func newPreventDoubleCloseChunkWriter(cw ChunkWriter) *preventDoubleCloseChunkWriter { |
| 158 | + return &preventDoubleCloseChunkWriter{ChunkWriter: cw} |
| 159 | +} |
| 160 | + |
| 161 | +func (p *preventDoubleCloseChunkWriter) Close() error { |
| 162 | + if p.closed.CAS(false, true) { |
| 163 | + return p.ChunkWriter.Close() |
| 164 | + } |
| 165 | + return nil |
| 166 | +} |
0 commit comments