2013-02-22 12:07:35 -08: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 03:17:31 -08:00
|
|
|
package retrieval
|
|
|
|
|
|
|
|
import (
|
2013-01-27 09:49:45 -08:00
|
|
|
"github.com/prometheus/prometheus/retrieval/format"
|
2013-01-04 05:41:47 -08:00
|
|
|
"log"
|
2013-02-22 12:07:35 -08:00
|
|
|
"sort"
|
2013-01-04 03:17:31 -08:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2013-01-28 07:36:28 -08:00
|
|
|
const (
|
|
|
|
intervalKey = "interval"
|
|
|
|
)
|
|
|
|
|
2013-01-04 05:41:47 -08:00
|
|
|
type TargetPool struct {
|
2013-02-22 12:07:35 -08:00
|
|
|
done chan bool
|
|
|
|
manager TargetManager
|
|
|
|
targets []Target
|
|
|
|
addTargetQueue chan Target
|
|
|
|
replaceTargetsQueue chan []Target
|
2013-01-04 05:41:47 -08:00
|
|
|
}
|
|
|
|
|
2013-01-15 08:06:17 -08:00
|
|
|
func NewTargetPool(m TargetManager) (p *TargetPool) {
|
|
|
|
return &TargetPool{
|
2013-02-22 12:07:35 -08:00
|
|
|
manager: m,
|
|
|
|
addTargetQueue: make(chan Target),
|
|
|
|
replaceTargetsQueue: make(chan []Target),
|
2013-01-15 08:06:17 -08:00
|
|
|
}
|
2013-01-04 05:41:47 -08:00
|
|
|
}
|
2013-01-04 03:17:31 -08:00
|
|
|
|
|
|
|
func (p TargetPool) Len() int {
|
2013-01-04 05:41:47 -08:00
|
|
|
return len(p.targets)
|
2013-01-04 03:17:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p TargetPool) Less(i, j int) bool {
|
2013-01-12 12:22:59 -08:00
|
|
|
return p.targets[i].scheduledFor().Before(p.targets[j].scheduledFor())
|
2013-01-04 03:17:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p TargetPool) Swap(i, j int) {
|
2013-01-04 05:41:47 -08:00
|
|
|
p.targets[i], p.targets[j] = p.targets[j], p.targets[i]
|
|
|
|
}
|
|
|
|
|
2013-01-22 09:37:01 -08:00
|
|
|
func (p *TargetPool) Run(results chan format.Result, interval time.Duration) {
|
2013-01-04 05:41:47 -08:00
|
|
|
ticker := time.Tick(interval)
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker:
|
2013-01-28 07:36:28 -08:00
|
|
|
p.runIteration(results, interval)
|
2013-02-22 12:07:35 -08:00
|
|
|
case newTarget := <-p.addTargetQueue:
|
|
|
|
p.addTarget(newTarget)
|
|
|
|
case newTargets := <-p.replaceTargetsQueue:
|
|
|
|
p.replaceTargets(newTargets)
|
2013-01-04 05:41:47 -08:00
|
|
|
case <-p.done:
|
|
|
|
log.Printf("TargetPool exiting...")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p TargetPool) Stop() {
|
|
|
|
p.done <- true
|
|
|
|
}
|
|
|
|
|
2013-02-22 12:07:35 -08:00
|
|
|
func (p *TargetPool) AddTarget(target Target) {
|
|
|
|
p.addTargetQueue <- target
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *TargetPool) addTarget(target Target) {
|
|
|
|
p.targets = append(p.targets, target)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *TargetPool) ReplaceTargets(newTargets []Target) {
|
|
|
|
p.replaceTargetsQueue <- newTargets
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *TargetPool) replaceTargets(newTargets []Target) {
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p.targets = newTargets
|
|
|
|
}
|
|
|
|
|
2013-01-22 09:37:01 -08:00
|
|
|
func (p *TargetPool) runSingle(earliest time.Time, results chan format.Result, t Target) {
|
2013-01-04 05:41:47 -08:00
|
|
|
p.manager.acquire()
|
|
|
|
defer p.manager.release()
|
|
|
|
|
2013-01-12 12:22:59 -08:00
|
|
|
t.Scrape(earliest, results)
|
2013-01-04 05:41:47 -08:00
|
|
|
}
|
|
|
|
|
2013-01-28 07:36:28 -08:00
|
|
|
func (p *TargetPool) runIteration(results chan format.Result, interval time.Duration) {
|
|
|
|
begin := time.Now()
|
|
|
|
|
|
|
|
targetCount := p.Len()
|
|
|
|
finished := make(chan bool, targetCount)
|
|
|
|
|
2013-02-22 12:07:35 -08:00
|
|
|
// Sort p.targets by next scheduling time so we can process the earliest
|
|
|
|
// targets first.
|
|
|
|
sort.Sort(p)
|
2013-01-04 05:41:47 -08:00
|
|
|
|
2013-02-22 12:07:35 -08:00
|
|
|
for _, target := range p.targets {
|
2013-01-04 05:41:47 -08:00
|
|
|
now := time.Now()
|
|
|
|
|
2013-01-12 12:22:59 -08:00
|
|
|
if target.scheduledFor().After(now) {
|
2013-02-21 10:48:29 -08:00
|
|
|
// None of the remaining targets are ready to be scheduled. Signal that
|
|
|
|
// we're done processing them in this scrape iteration.
|
2013-02-22 12:07:35 -08:00
|
|
|
finished <- true
|
|
|
|
continue
|
2013-01-04 05:41:47 -08:00
|
|
|
}
|
|
|
|
|
2013-02-22 12:07:35 -08:00
|
|
|
go func(t Target) {
|
|
|
|
p.runSingle(now, results, t)
|
2013-01-28 07:36:28 -08:00
|
|
|
finished <- true
|
2013-02-22 12:07:35 -08:00
|
|
|
}(target)
|
2013-01-04 05:41:47 -08:00
|
|
|
}
|
2013-01-28 07:36:28 -08:00
|
|
|
|
2013-02-22 12:07:35 -08:00
|
|
|
for i := 0; i < targetCount; {
|
|
|
|
select {
|
|
|
|
case <-finished:
|
|
|
|
i++
|
|
|
|
case newTarget := <-p.addTargetQueue:
|
|
|
|
p.addTarget(newTarget)
|
|
|
|
case newTargets := <-p.replaceTargetsQueue:
|
|
|
|
p.replaceTargets(newTargets)
|
|
|
|
}
|
2013-01-28 07:36:28 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
close(finished)
|
|
|
|
|
2013-02-21 10:48:29 -08:00
|
|
|
duration := float64(time.Since(begin) / time.Millisecond)
|
2013-01-28 07:36:28 -08:00
|
|
|
retrievalDurations.Add(map[string]string{intervalKey: interval.String()}, duration)
|
2013-01-04 03:17:31 -08:00
|
|
|
}
|
2013-02-22 12:07:35 -08:00
|
|
|
|
|
|
|
// XXX: Not really thread-safe. Only used in /status page for now.
|
|
|
|
func (p *TargetPool) Targets() []Target {
|
|
|
|
return p.targets
|
|
|
|
}
|