[ENHANCEMENT] TSDB: Save map lookup on validation

Goes faster.

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
This commit is contained in:
Bryan Boreham 2024-05-11 17:32:17 +01:00
parent 7d98487447
commit 3ee52abb53

View file

@ -214,12 +214,12 @@ func (ce *CircularExemplarStorage) ValidateExemplar(l labels.Labels, e exemplar.
// Optimize by moving the lock to be per series (& benchmark it). // Optimize by moving the lock to be per series (& benchmark it).
ce.lock.RLock() ce.lock.RLock()
defer ce.lock.RUnlock() defer ce.lock.RUnlock()
return ce.validateExemplar(seriesLabels, e, false) return ce.validateExemplar(ce.index[string(seriesLabels)], e, false)
} }
// Not thread safe. The appended parameters tells us whether this is an external validation, or internal // Not thread safe. The appended parameters tells us whether this is an external validation, or internal
// as a result of an AddExemplar call, in which case we should update any relevant metrics. // as a result of an AddExemplar call, in which case we should update any relevant metrics.
func (ce *CircularExemplarStorage) validateExemplar(key []byte, e exemplar.Exemplar, appended bool) error { func (ce *CircularExemplarStorage) validateExemplar(idx *indexEntry, e exemplar.Exemplar, appended bool) error {
if len(ce.exemplars) == 0 { if len(ce.exemplars) == 0 {
return storage.ErrExemplarsDisabled return storage.ErrExemplarsDisabled
} }
@ -239,8 +239,7 @@ func (ce *CircularExemplarStorage) validateExemplar(key []byte, e exemplar.Exemp
return err return err
} }
idx, ok := ce.index[string(key)] if idx == nil {
if !ok {
return nil return nil
} }
@ -362,7 +361,8 @@ func (ce *CircularExemplarStorage) AddExemplar(l labels.Labels, e exemplar.Exemp
ce.lock.Lock() ce.lock.Lock()
defer ce.lock.Unlock() defer ce.lock.Unlock()
err := ce.validateExemplar(seriesLabels, e, true) idx, ok := ce.index[string(seriesLabels)]
err := ce.validateExemplar(idx, e, true)
if err != nil { if err != nil {
if errors.Is(err, storage.ErrDuplicateExemplar) { if errors.Is(err, storage.ErrDuplicateExemplar) {
// Duplicate exemplar, noop. // Duplicate exemplar, noop.
@ -371,7 +371,6 @@ func (ce *CircularExemplarStorage) AddExemplar(l labels.Labels, e exemplar.Exemp
return err return err
} }
idx, ok := ce.index[string(seriesLabels)]
if !ok { if !ok {
idx = &indexEntry{oldest: ce.nextIndex, seriesLabels: l} idx = &indexEntry{oldest: ce.nextIndex, seriesLabels: l}
ce.index[string(seriesLabels)] = idx ce.index[string(seriesLabels)] = idx