tsdb: create isolation transaction slice on demand

When Prometheus restarts it creates every series read in from the WAL,
but many of those series will be finished, and never receive any more
samples. By defering allocation of the txRing slice to when it is first
needed, we save 32 bytes per stale series.

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
This commit is contained in:
Bryan Boreham 2023-10-21 13:45:47 +00:00
parent 6fe8217ce4
commit 90e98e0235
2 changed files with 9 additions and 2 deletions

View file

@ -2011,7 +2011,7 @@ func newMemSeries(lset labels.Labels, id chunks.HeadSeriesRef, isolationDisabled
nextAt: math.MinInt64,
}
if !isolationDisabled {
s.txs = newTxRing(4)
s.txs = newTxRing(0)
}
return s
}

View file

@ -253,7 +253,11 @@ func newTxRing(capacity int) *txRing {
func (txr *txRing) add(appendID uint64) {
if int(txr.txIDCount) == len(txr.txIDs) {
// Ring buffer is full, expand by doubling.
newRing := make([]uint64, txr.txIDCount*2)
newLen := txr.txIDCount * 2
if newLen == 0 {
newLen = 4
}
newRing := make([]uint64, newLen)
idx := copy(newRing, txr.txIDs[txr.txIDFirst:])
copy(newRing[idx:], txr.txIDs[:txr.txIDFirst])
txr.txIDs = newRing
@ -265,6 +269,9 @@ func (txr *txRing) add(appendID uint64) {
}
func (txr *txRing) cleanupAppendIDsBelow(bound uint64) {
if len(txr.txIDs) == 0 {
return
}
pos := int(txr.txIDFirst)
for txr.txIDCount > 0 {