mirror of https://github.com/prometheus/prometheus
commit
344a50b9d8
8
block.go
8
block.go
|
@ -60,6 +60,11 @@ type Block interface {
|
|||
type headBlock interface {
|
||||
Block
|
||||
Appendable
|
||||
|
||||
// ActiveWriters returns the number of currently active appenders.
|
||||
ActiveWriters() int
|
||||
// HighTimestamp returns the highest currently inserted timestamp.
|
||||
HighTimestamp() int64
|
||||
}
|
||||
|
||||
// Snapshottable defines an entity that can be backedup online.
|
||||
|
@ -71,9 +76,6 @@ type Snapshottable interface {
|
|||
type Appendable interface {
|
||||
// Appender returns a new Appender against an underlying store.
|
||||
Appender() Appender
|
||||
|
||||
// Busy returns whether there are any currently active appenders.
|
||||
Busy() bool
|
||||
}
|
||||
|
||||
// Queryable defines an entity which provides a Querier.
|
||||
|
|
23
db.go
23
db.go
|
@ -80,6 +80,7 @@ type Appender interface {
|
|||
// Returned reference numbers are ephemeral and may be rejected in calls
|
||||
// to AddFast() at any point. Adding the sample via Add() returns a new
|
||||
// reference number.
|
||||
// If the reference is the empty string it must not be used for caching.
|
||||
Add(l labels.Labels, t int64, v float64) (string, error)
|
||||
|
||||
// Add adds a sample pair for the referenced series. It is generally faster
|
||||
|
@ -305,6 +306,15 @@ func (db *DB) retentionCutoff() (bool, error) {
|
|||
return retentionCutoff(db.dir, mint)
|
||||
}
|
||||
|
||||
// headFullness returns up to which fraction of a blocks time range samples
|
||||
// were already inserted.
|
||||
func headFullness(h headBlock) float64 {
|
||||
m := h.Meta()
|
||||
a := float64(h.HighTimestamp() - m.MinTime)
|
||||
b := float64(m.MaxTime - m.MinTime)
|
||||
return a / b
|
||||
}
|
||||
|
||||
func (db *DB) compact() (changes bool, err error) {
|
||||
db.cmtx.Lock()
|
||||
defer db.cmtx.Unlock()
|
||||
|
@ -319,12 +329,14 @@ func (db *DB) compact() (changes bool, err error) {
|
|||
// returning the lock to not block Appenders.
|
||||
// Selected blocks are semantically ensured to not be written to afterwards
|
||||
// by appendable().
|
||||
if len(db.heads) > 2 {
|
||||
for _, h := range db.heads[:len(db.heads)-2] {
|
||||
if len(db.heads) > 1 {
|
||||
f := headFullness(db.heads[len(db.heads)-1])
|
||||
|
||||
for _, h := range db.heads[:len(db.heads)-1] {
|
||||
// Blocks that won't be appendable when instantiating a new appender
|
||||
// might still have active appenders on them.
|
||||
// Abort at the first one we encounter.
|
||||
if h.Busy() {
|
||||
if h.ActiveWriters() > 0 || f < 0.5 {
|
||||
break
|
||||
}
|
||||
singles = append(singles, h)
|
||||
|
@ -605,6 +617,9 @@ func (a *dbAppender) Add(lset labels.Labels, t int64, v float64) (string, error)
|
|||
}
|
||||
a.samples++
|
||||
|
||||
if ref == "" {
|
||||
return "", nil
|
||||
}
|
||||
return string(append(h.meta.ULID[:], ref...)), nil
|
||||
}
|
||||
|
||||
|
@ -849,6 +864,8 @@ func (db *DB) createHeadBlock(mint, maxt int64) (headBlock, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
db.logger.Log("msg", "created head block", "ulid", newHead.meta.ULID, "mint", mint, "maxt", maxt)
|
||||
|
||||
db.blocks = append(db.blocks, newHead) // TODO(fabxc): this is a race!
|
||||
db.heads = append(db.heads, newHead)
|
||||
|
||||
|
|
33
head.go
33
head.go
|
@ -57,6 +57,7 @@ type HeadBlock struct {
|
|||
wal WAL
|
||||
|
||||
activeWriters uint64
|
||||
highTimestamp int64
|
||||
closed bool
|
||||
|
||||
// descs holds all chunk descs for the head block. Each chunk implicitly
|
||||
|
@ -389,9 +390,14 @@ func (h *HeadBlock) Appender() Appender {
|
|||
return &headAppender{HeadBlock: h, samples: getHeadAppendBuffer()}
|
||||
}
|
||||
|
||||
// Busy returns true if the block has open write transactions.
|
||||
func (h *HeadBlock) Busy() bool {
|
||||
return atomic.LoadUint64(&h.activeWriters) > 0
|
||||
// ActiveWriters returns true if the block has open write transactions.
|
||||
func (h *HeadBlock) ActiveWriters() int {
|
||||
return int(atomic.LoadUint64(&h.activeWriters))
|
||||
}
|
||||
|
||||
// HighTimestamp returns the highest inserted sample timestamp.
|
||||
func (h *HeadBlock) HighTimestamp() int64 {
|
||||
return atomic.LoadInt64(&h.highTimestamp)
|
||||
}
|
||||
|
||||
var headPool = sync.Pool{}
|
||||
|
@ -416,6 +422,7 @@ type headAppender struct {
|
|||
newHashes map[uint64]uint64
|
||||
|
||||
samples []RefSample
|
||||
highTimestamp int64
|
||||
}
|
||||
|
||||
type hashedLabels struct {
|
||||
|
@ -443,7 +450,7 @@ func (a *headAppender) Add(lset labels.Labels, t int64, v float64) (string, erro
|
|||
// XXX(fabxc): there's no fast path for multiple samples for the same new series
|
||||
// in the same transaction. We always return the invalid empty ref. It's has not
|
||||
// been a relevant use case so far and is not worth the trouble.
|
||||
return nullRef, a.AddFast(string(refb), t, v)
|
||||
return "", a.AddFast(string(refb), t, v)
|
||||
}
|
||||
|
||||
// The series is completely new.
|
||||
|
@ -464,11 +471,9 @@ func (a *headAppender) Add(lset labels.Labels, t int64, v float64) (string, erro
|
|||
a.newHashes[hash] = ref
|
||||
binary.BigEndian.PutUint64(refb, ref)
|
||||
|
||||
return nullRef, a.AddFast(string(refb), t, v)
|
||||
return "", a.AddFast(string(refb), t, v)
|
||||
}
|
||||
|
||||
var nullRef = string([]byte{0, 0, 0, 0, 0, 0, 0, 0})
|
||||
|
||||
func (a *headAppender) AddFast(ref string, t int64, v float64) error {
|
||||
if len(ref) != 8 {
|
||||
return errors.Wrap(ErrNotFound, "invalid ref length")
|
||||
|
@ -513,6 +518,10 @@ func (a *headAppender) AddFast(ref string, t int64, v float64) error {
|
|||
}
|
||||
}
|
||||
|
||||
if t > a.highTimestamp {
|
||||
a.highTimestamp = t
|
||||
}
|
||||
|
||||
a.samples = append(a.samples, RefSample{
|
||||
Ref: refn,
|
||||
T: t,
|
||||
|
@ -593,6 +602,16 @@ func (a *headAppender) Commit() error {
|
|||
atomic.AddUint64(&a.meta.Stats.NumSamples, total)
|
||||
atomic.AddUint64(&a.meta.Stats.NumSeries, uint64(len(a.newSeries)))
|
||||
|
||||
for {
|
||||
ht := a.HeadBlock.HighTimestamp()
|
||||
if a.highTimestamp <= ht {
|
||||
break
|
||||
}
|
||||
if atomic.CompareAndSwapInt64(&a.HeadBlock.highTimestamp, ht, a.highTimestamp) {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
@ -38,7 +38,7 @@ type Querier interface {
|
|||
Close() error
|
||||
}
|
||||
|
||||
// Series represents a single time series.
|
||||
// Series exposes a single time series.
|
||||
type Series interface {
|
||||
// Labels returns the complete set of labels identifying the series.
|
||||
Labels() labels.Labels
|
||||
|
|
Loading…
Reference in New Issue