mirror of
https://github.com/prometheus/prometheus
synced 2025-01-11 08:59:37 +00:00
[RFC] Add method to get reference number for TSDB Appender (#8600)
* Add method to get reference number for TSDB Appender In situations where we need to copy labels before calling Add(), GetRef() allows to check first, then call AddFast() in the case that the series is already known. Signed-off-by: Bryan Boreham <bjboreham@gmail.com> * Add explicit interface for GetRef() method Suggested in code review by @bwplotka Signed-off-by: Bryan Boreham <bjboreham@gmail.com> * Rename OptionalGetRef to GetRef Signed-off-by: Bryan Boreham <bjboreham@gmail.com> * Simplify return value of GetRef() 0 can be relied on to mean 'no reference' Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
This commit is contained in:
parent
6719071a0f
commit
d614ae9ecf
@ -180,6 +180,14 @@ type Appender interface {
|
||||
ExemplarAppender
|
||||
}
|
||||
|
||||
// GetRef is an extra interface on Appenders used by downstream projects
|
||||
// (e.g. Cortex) to avoid maintaining a parallel set of references.
|
||||
type GetRef interface {
|
||||
// Returns reference number that can be used to pass to Appender.Append().
|
||||
// 0 means the appender does not have a reference to this series.
|
||||
GetRef(lset labels.Labels) uint64
|
||||
}
|
||||
|
||||
// ExemplarAppender provides an interface for adding samples to exemplar storage, which
|
||||
// within Prometheus is in-memory only.
|
||||
type ExemplarAppender interface {
|
||||
|
@ -795,6 +795,15 @@ type dbAppender struct {
|
||||
db *DB
|
||||
}
|
||||
|
||||
var _ storage.GetRef = dbAppender{}
|
||||
|
||||
func (a dbAppender) GetRef(lset labels.Labels) uint64 {
|
||||
if g, ok := a.Appender.(storage.GetRef); ok {
|
||||
return g.GetRef(lset)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (a dbAppender) Commit() error {
|
||||
err := a.Appender.Commit()
|
||||
|
||||
|
19
tsdb/head.go
19
tsdb/head.go
@ -1107,6 +1107,15 @@ func (a *initAppender) AppendExemplar(ref uint64, l labels.Labels, e exemplar.Ex
|
||||
return a.app.AppendExemplar(ref, l, e)
|
||||
}
|
||||
|
||||
var _ storage.GetRef = &initAppender{}
|
||||
|
||||
func (a *initAppender) GetRef(lset labels.Labels) uint64 {
|
||||
if g, ok := a.app.(storage.GetRef); ok {
|
||||
return g.GetRef(lset)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (a *initAppender) Commit() error {
|
||||
if a.app == nil {
|
||||
return nil
|
||||
@ -1331,6 +1340,16 @@ func (a *headAppender) AppendExemplar(ref uint64, _ labels.Labels, e exemplar.Ex
|
||||
return s.ref, nil
|
||||
}
|
||||
|
||||
var _ storage.GetRef = &headAppender{}
|
||||
|
||||
func (a *headAppender) GetRef(lset labels.Labels) uint64 {
|
||||
s := a.head.series.getByHash(lset.Hash(), lset)
|
||||
if s == nil {
|
||||
return 0
|
||||
}
|
||||
return s.ref
|
||||
}
|
||||
|
||||
func (a *headAppender) log() error {
|
||||
if a.head.wal == nil {
|
||||
return nil
|
||||
|
Loading…
Reference in New Issue
Block a user