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-04 05:41:47 -08:00
|
|
|
"log"
|
2013-02-22 12:07:35 -08:00
|
|
|
"sort"
|
2013-06-05 05:44:20 -07:00
|
|
|
"sync"
|
2013-01-04 03:17:31 -08:00
|
|
|
"time"
|
2013-06-05 05:44:20 -07:00
|
|
|
|
2013-06-25 05:02:27 -07:00
|
|
|
"github.com/prometheus/client_golang/extraction"
|
2013-01-04 03:17:31 -08:00
|
|
|
)
|
|
|
|
|
2013-01-28 07:36:28 -08:00
|
|
|
const (
|
|
|
|
intervalKey = "interval"
|
2013-06-05 05:44:20 -07:00
|
|
|
|
|
|
|
targetAddQueueSize = 100
|
2013-06-05 07:29:05 -07:00
|
|
|
targetReplaceQueueSize = 1
|
2013-01-28 07:36:28 -08:00
|
|
|
)
|
|
|
|
|
2013-01-04 05:41:47 -08:00
|
|
|
type TargetPool struct {
|
2013-06-05 05:44:20 -07:00
|
|
|
sync.RWMutex
|
|
|
|
|
2013-02-22 12:07:35 -08:00
|
|
|
done chan bool
|
|
|
|
manager TargetManager
|
2013-06-05 05:44:20 -07:00
|
|
|
targets targets
|
2013-02-22 12:07:35 -08:00
|
|
|
addTargetQueue chan Target
|
2013-06-05 05:44:20 -07:00
|
|
|
replaceTargetsQueue chan targets
|
2013-06-11 13:59:27 -07:00
|
|
|
|
|
|
|
targetProvider TargetProvider
|
2013-01-04 05:41:47 -08:00
|
|
|
}
|
|
|
|
|
2013-06-11 13:59:27 -07:00
|
|
|
func NewTargetPool(m TargetManager, p TargetProvider) *TargetPool {
|
2013-01-15 08:06:17 -08:00
|
|
|
return &TargetPool{
|
2013-02-22 12:07:35 -08:00
|
|
|
manager: m,
|
2013-06-05 05:44:20 -07:00
|
|
|
addTargetQueue: make(chan Target, targetAddQueueSize),
|
|
|
|
replaceTargetsQueue: make(chan targets, targetReplaceQueueSize),
|
2013-06-11 13:59:27 -07:00
|
|
|
targetProvider: p,
|
2013-01-15 08:06:17 -08:00
|
|
|
}
|
2013-01-04 05:41:47 -08:00
|
|
|
}
|
2013-01-04 03:17:31 -08:00
|
|
|
|
2013-06-25 05:02:27 -07:00
|
|
|
func (p *TargetPool) Run(results chan<- *extraction.Result, interval time.Duration) {
|
2013-06-05 05:44:20 -07:00
|
|
|
ticker := time.NewTicker(interval)
|
|
|
|
defer ticker.Stop()
|
2013-01-04 05:41:47 -08:00
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
2013-06-05 05:44:20 -07:00
|
|
|
case <-ticker.C:
|
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...")
|
2013-06-05 05:44:20 -07:00
|
|
|
return
|
2013-01-04 05:41:47 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2013-06-05 05:44:20 -07:00
|
|
|
p.Lock()
|
|
|
|
defer p.Unlock()
|
|
|
|
|
2013-02-22 12:07:35 -08:00
|
|
|
p.targets = append(p.targets, target)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *TargetPool) ReplaceTargets(newTargets []Target) {
|
2013-06-05 07:29:05 -07: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 12:07:35 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *TargetPool) replaceTargets(newTargets []Target) {
|
2013-06-05 05:44:20 -07:00
|
|
|
p.Lock()
|
|
|
|
defer p.Unlock()
|
|
|
|
|
2013-02-22 12:07:35 -08: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 05:44:20 -07:00
|
|
|
|
2013-02-22 12:07:35 -08:00
|
|
|
p.targets = newTargets
|
|
|
|
}
|
|
|
|
|
2013-06-25 05:02:27 -07:00
|
|
|
func (p *TargetPool) runSingle(earliest time.Time, results chan<- *extraction.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-06-25 05:02:27 -07:00
|
|
|
func (p *TargetPool) runIteration(results chan<- *extraction.Result, interval time.Duration) {
|
2013-06-11 13:59:27 -07:00
|
|
|
if p.targetProvider != nil {
|
|
|
|
targets, err := p.targetProvider.Targets()
|
|
|
|
if err != nil {
|
2013-07-12 13:38:02 -07:00
|
|
|
log.Printf("Error looking up targets, keeping old list: %s", err)
|
|
|
|
} else {
|
|
|
|
p.ReplaceTargets(targets)
|
2013-06-11 13:59:27 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-05 05:44:20 -07:00
|
|
|
p.RLock()
|
|
|
|
defer p.RUnlock()
|
2013-01-28 07:36:28 -08:00
|
|
|
|
2013-06-05 05:44:20 -07:00
|
|
|
begin := time.Now()
|
|
|
|
wait := sync.WaitGroup{}
|
2013-01-28 07:36:28 -08:00
|
|
|
|
2013-02-22 12:07:35 -08:00
|
|
|
// Sort p.targets by next scheduling time so we can process the earliest
|
|
|
|
// targets first.
|
2013-06-05 05:44:20 -07:00
|
|
|
sort.Sort(p.targets)
|
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-07-14 09:48:03 -07: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
|
|
|
continue
|
2013-01-04 05:41:47 -08:00
|
|
|
}
|
|
|
|
|
2013-06-05 05:44:20 -07:00
|
|
|
wait.Add(1)
|
|
|
|
|
2013-02-22 12:07:35 -08:00
|
|
|
go func(t Target) {
|
|
|
|
p.runSingle(now, results, t)
|
2013-06-05 05:44:20 -07:00
|
|
|
wait.Done()
|
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-06-05 05:44:20 -07:00
|
|
|
wait.Wait()
|
2013-01-28 07:36:28 -08:00
|
|
|
|
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
|
|
|
|
|
|
|
func (p *TargetPool) Targets() []Target {
|
2013-06-05 05:44:20 -07:00
|
|
|
p.RLock()
|
|
|
|
defer p.RUnlock()
|
|
|
|
|
|
|
|
targets := make([]Target, len(p.targets))
|
|
|
|
copy(targets, p.targets)
|
|
|
|
|
|
|
|
return targets
|
2013-02-22 12:07:35 -08:00
|
|
|
}
|