2013-02-22 20:07:35 +00:00
|
|
|
// Copyright 2013 Prometheus Team
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2013-01-04 11:17:31 +00:00
|
|
|
package retrieval
|
|
|
|
|
|
|
|
import (
|
2013-06-05 12:44:20 +00:00
|
|
|
"sync"
|
2013-01-04 11:17:31 +00:00
|
|
|
"time"
|
2013-06-05 12:44:20 +00:00
|
|
|
|
2013-08-12 15:18:02 +00:00
|
|
|
"github.com/golang/glog"
|
2013-06-25 12:02:27 +00:00
|
|
|
"github.com/prometheus/client_golang/extraction"
|
2014-06-18 17:43:15 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2013-01-04 11:17:31 +00:00
|
|
|
)
|
|
|
|
|
2013-01-28 15:36:28 +00:00
|
|
|
const (
|
2013-06-05 12:44:20 +00:00
|
|
|
targetAddQueueSize = 100
|
2013-06-05 14:29:05 +00:00
|
|
|
targetReplaceQueueSize = 1
|
2014-06-18 17:43:15 +00:00
|
|
|
|
|
|
|
intervalKey = "interval"
|
2013-01-28 15:36:28 +00:00
|
|
|
)
|
|
|
|
|
2014-06-18 17:43:15 +00:00
|
|
|
var (
|
|
|
|
retrievalDurations = prometheus.NewSummaryVec(
|
|
|
|
prometheus.SummaryOpts{
|
|
|
|
Name: "prometheus_targetpool_duration_ms",
|
|
|
|
Help: "The durations for each TargetPool to retrieve state from all included entities.",
|
|
|
|
Objectives: []float64{0.01, 0.05, 0.5, 0.90, 0.99},
|
|
|
|
},
|
|
|
|
[]string{intervalKey},
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
prometheus.MustRegister(retrievalDurations)
|
|
|
|
}
|
|
|
|
|
2013-01-04 13:41:47 +00:00
|
|
|
type TargetPool struct {
|
2013-06-05 12:44:20 +00:00
|
|
|
sync.RWMutex
|
|
|
|
|
2013-02-22 20:07:35 +00:00
|
|
|
done chan bool
|
|
|
|
manager TargetManager
|
2013-06-05 12:44:20 +00:00
|
|
|
targets targets
|
2013-02-22 20:07:35 +00:00
|
|
|
addTargetQueue chan Target
|
2013-06-05 12:44:20 +00:00
|
|
|
replaceTargetsQueue chan targets
|
2013-06-11 20:59:27 +00:00
|
|
|
|
|
|
|
targetProvider TargetProvider
|
2013-01-04 13:41:47 +00:00
|
|
|
}
|
|
|
|
|
2013-06-11 20:59:27 +00:00
|
|
|
func NewTargetPool(m TargetManager, p TargetProvider) *TargetPool {
|
2013-01-15 16:06:17 +00:00
|
|
|
return &TargetPool{
|
2013-02-22 20:07:35 +00:00
|
|
|
manager: m,
|
2013-06-05 12:44:20 +00:00
|
|
|
addTargetQueue: make(chan Target, targetAddQueueSize),
|
|
|
|
replaceTargetsQueue: make(chan targets, targetReplaceQueueSize),
|
2013-06-11 20:59:27 +00:00
|
|
|
targetProvider: p,
|
2013-12-11 14:30:27 +00:00
|
|
|
done: make(chan bool),
|
2013-01-15 16:06:17 +00:00
|
|
|
}
|
2013-01-04 13:41:47 +00:00
|
|
|
}
|
2013-01-04 11:17:31 +00:00
|
|
|
|
2013-08-13 14:14:18 +00:00
|
|
|
func (p *TargetPool) Run(ingester extraction.Ingester, interval time.Duration) {
|
2013-06-05 12:44:20 +00:00
|
|
|
ticker := time.NewTicker(interval)
|
|
|
|
defer ticker.Stop()
|
2013-01-04 13:41:47 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
2013-06-05 12:44:20 +00:00
|
|
|
case <-ticker.C:
|
2013-08-13 14:14:18 +00:00
|
|
|
p.runIteration(ingester, interval)
|
2013-02-22 20:07:35 +00:00
|
|
|
case newTarget := <-p.addTargetQueue:
|
|
|
|
p.addTarget(newTarget)
|
|
|
|
case newTargets := <-p.replaceTargetsQueue:
|
|
|
|
p.replaceTargets(newTargets)
|
2013-01-04 13:41:47 +00:00
|
|
|
case <-p.done:
|
2013-08-12 15:18:02 +00:00
|
|
|
glog.Info("TargetPool exiting...")
|
2013-06-05 12:44:20 +00:00
|
|
|
return
|
2013-01-04 13:41:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p TargetPool) Stop() {
|
|
|
|
p.done <- true
|
|
|
|
}
|
|
|
|
|
2013-02-22 20:07:35 +00:00
|
|
|
func (p *TargetPool) AddTarget(target Target) {
|
|
|
|
p.addTargetQueue <- target
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *TargetPool) addTarget(target Target) {
|
2013-06-05 12:44:20 +00:00
|
|
|
p.Lock()
|
|
|
|
defer p.Unlock()
|
|
|
|
|
2013-02-22 20:07:35 +00:00
|
|
|
p.targets = append(p.targets, target)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *TargetPool) ReplaceTargets(newTargets []Target) {
|
2013-06-05 14:29:05 +00:00
|
|
|
p.Lock()
|
|
|
|
defer p.Unlock()
|
|
|
|
|
|
|
|
// If there is anything remaining in the queue for effectuation, clear it out,
|
|
|
|
// because the last mutation should win.
|
|
|
|
select {
|
|
|
|
case <-p.replaceTargetsQueue:
|
|
|
|
default:
|
|
|
|
p.replaceTargetsQueue <- newTargets
|
|
|
|
}
|
2013-02-22 20:07:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *TargetPool) replaceTargets(newTargets []Target) {
|
2013-06-05 12:44:20 +00:00
|
|
|
p.Lock()
|
|
|
|
defer p.Unlock()
|
|
|
|
|
2013-02-22 20:07:35 +00:00
|
|
|
// Replace old target list by new one, but reuse those targets from the old
|
|
|
|
// list of targets which are also in the new list (to preserve scheduling and
|
|
|
|
// health state).
|
|
|
|
for j, newTarget := range newTargets {
|
|
|
|
for _, oldTarget := range p.targets {
|
|
|
|
if oldTarget.Address() == newTarget.Address() {
|
|
|
|
oldTarget.Merge(newTargets[j])
|
|
|
|
newTargets[j] = oldTarget
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-06-05 12:44:20 +00:00
|
|
|
|
2013-02-22 20:07:35 +00:00
|
|
|
p.targets = newTargets
|
|
|
|
}
|
|
|
|
|
2014-07-29 16:28:48 +00:00
|
|
|
func (p *TargetPool) runSingle(ingester extraction.Ingester, t Target) {
|
2013-01-04 13:41:47 +00:00
|
|
|
p.manager.acquire()
|
|
|
|
defer p.manager.release()
|
|
|
|
|
2014-07-29 16:28:48 +00:00
|
|
|
t.Scrape(ingester)
|
2013-01-04 13:41:47 +00:00
|
|
|
}
|
|
|
|
|
2013-08-13 14:14:18 +00:00
|
|
|
func (p *TargetPool) runIteration(ingester extraction.Ingester, interval time.Duration) {
|
2013-06-11 20:59:27 +00:00
|
|
|
if p.targetProvider != nil {
|
|
|
|
targets, err := p.targetProvider.Targets()
|
|
|
|
if err != nil {
|
2013-08-12 16:22:48 +00:00
|
|
|
glog.Warningf("Error looking up targets, keeping old list: %s", err)
|
2013-07-12 20:38:02 +00:00
|
|
|
} else {
|
|
|
|
p.ReplaceTargets(targets)
|
2013-06-11 20:59:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-05 12:44:20 +00:00
|
|
|
p.RLock()
|
|
|
|
defer p.RUnlock()
|
2013-01-28 15:36:28 +00:00
|
|
|
|
2013-06-05 12:44:20 +00:00
|
|
|
begin := time.Now()
|
|
|
|
wait := sync.WaitGroup{}
|
2013-01-28 15:36:28 +00:00
|
|
|
|
2013-02-22 20:07:35 +00:00
|
|
|
for _, target := range p.targets {
|
2013-06-05 12:44:20 +00:00
|
|
|
wait.Add(1)
|
|
|
|
|
2013-02-22 20:07:35 +00:00
|
|
|
go func(t Target) {
|
2014-07-29 16:28:48 +00:00
|
|
|
p.runSingle(ingester, t)
|
2013-06-05 12:44:20 +00:00
|
|
|
wait.Done()
|
2013-02-22 20:07:35 +00:00
|
|
|
}(target)
|
2013-01-04 13:41:47 +00:00
|
|
|
}
|
2013-01-28 15:36:28 +00:00
|
|
|
|
2013-06-05 12:44:20 +00:00
|
|
|
wait.Wait()
|
2013-01-28 15:36:28 +00:00
|
|
|
|
2013-02-21 18:48:29 +00:00
|
|
|
duration := float64(time.Since(begin) / time.Millisecond)
|
2014-06-18 17:43:15 +00:00
|
|
|
retrievalDurations.WithLabelValues(interval.String()).Observe(duration)
|
2013-01-04 11:17:31 +00:00
|
|
|
}
|
2013-02-22 20:07:35 +00:00
|
|
|
|
|
|
|
func (p *TargetPool) Targets() []Target {
|
2013-06-05 12:44:20 +00:00
|
|
|
p.RLock()
|
|
|
|
defer p.RUnlock()
|
|
|
|
|
|
|
|
targets := make([]Target, len(p.targets))
|
|
|
|
copy(targets, p.targets)
|
|
|
|
|
|
|
|
return targets
|
2013-02-22 20:07:35 +00:00
|
|
|
}
|