Tsdb StartTime : Use a simplier way to compute StartTime

This commit is contained in:
Thibault Chataigner 2017-10-24 16:30:39 +02:00
parent 808f79f00a
commit fc4406201e
2 changed files with 4 additions and 41 deletions

View file

@ -325,7 +325,7 @@ func main() {
}
level.Info(logger).Log("msg", "TSDB started")
startTimeMargin := int64(time.Duration(cfg.tsdb.MinBlockDuration).Seconds() * 1000)
startTimeMargin := int64(2 * time.Duration(cfg.tsdb.MinBlockDuration).Seconds() * 1000)
localStorage.Set(db, startTimeMargin)
}()

View file

@ -151,51 +151,14 @@ func Open(path string, l log.Logger, r prometheus.Registerer, opts *Options) (*t
// StartTime implements the Storage interface.
func (a adapter) StartTime() (int64, error) {
startTime := int64(model.Latest)
var startTime int64
var indexr tsdb.IndexReader
if len(a.db.Blocks()) > 0 {
var err error
indexr, err = a.db.Blocks()[0].Index()
if err != nil {
return startTime, err
}
startTime = a.db.Blocks()[0].Meta().MinTime
} else {
var err error
indexr, err = a.db.Head().Index()
if err != nil {
return startTime, err
}
startTime = int64(time.Now().Unix() * 1000)
}
joblabel := "job"
tpls, err := indexr.LabelValues(joblabel)
if err != nil {
return startTime, err
}
for i := 0; i < tpls.Len(); i++ {
vals, err := tpls.At(i)
if err != nil {
continue
}
for _, v := range vals {
p, err := indexr.Postings(joblabel, v)
if err != nil {
continue
}
if p.Next() {
var lset tsdbLabels.Labels
var chks []tsdb.ChunkMeta
indexr.Series(p.At(), &lset, &chks)
if startTime > chks[0].MinTime {
startTime = chks[0].MinTime
}
}
}
}
// Add a safety margin as it may take a few minutes for everything to spin up.
return startTime + a.startTimeMargin, nil
}