2015-06-09 11:25:30 +00:00
|
|
|
// Copyright 2015 The Prometheus Authors
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
package discovery
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2015-08-26 14:53:37 +00:00
|
|
|
"strconv"
|
2015-06-09 11:25:30 +00:00
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2015-10-03 08:21:43 +00:00
|
|
|
"github.com/prometheus/common/log"
|
2015-08-22 07:42:45 +00:00
|
|
|
"github.com/prometheus/common/model"
|
2015-06-09 11:25:30 +00:00
|
|
|
"github.com/samuel/go-zookeeper/zk"
|
|
|
|
|
|
|
|
"github.com/prometheus/prometheus/config"
|
2015-07-18 21:23:58 +00:00
|
|
|
"github.com/prometheus/prometheus/util/strutil"
|
2015-06-09 11:25:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
serversetNodePrefix = "member_"
|
|
|
|
|
2015-08-20 15:18:46 +00:00
|
|
|
serversetLabelPrefix = model.MetaLabelPrefix + "serverset_"
|
2015-06-09 11:25:30 +00:00
|
|
|
serversetStatusLabel = serversetLabelPrefix + "status"
|
|
|
|
serversetPathLabel = serversetLabelPrefix + "path"
|
|
|
|
serversetEndpointLabelPrefix = serversetLabelPrefix + "endpoint"
|
2015-08-26 14:53:37 +00:00
|
|
|
serversetShardLabel = serversetLabelPrefix + "shard"
|
2015-06-09 11:25:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type serversetMember struct {
|
|
|
|
ServiceEndpoint serversetEndpoint
|
|
|
|
AdditionalEndpoints map[string]serversetEndpoint
|
|
|
|
Status string `json:"status"`
|
2015-08-26 14:53:37 +00:00
|
|
|
Shard int `json:"shard"`
|
2015-06-09 11:25:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type serversetEndpoint struct {
|
|
|
|
Host string
|
|
|
|
Port int
|
|
|
|
}
|
|
|
|
|
2015-08-24 13:07:27 +00:00
|
|
|
type zookeeperLogger struct {
|
2015-06-09 11:25:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Implements zk.Logger
|
2015-08-24 13:07:27 +00:00
|
|
|
func (zl zookeeperLogger) Printf(s string, i ...interface{}) {
|
2015-06-09 11:25:30 +00:00
|
|
|
log.Infof(s, i...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServersetDiscovery retrieves target information from a Serverset server
|
|
|
|
// and updates them via watches.
|
|
|
|
type ServersetDiscovery struct {
|
2015-10-06 13:47:34 +00:00
|
|
|
conf *config.ServersetSDConfig
|
|
|
|
conn *zk.Conn
|
|
|
|
mu sync.RWMutex
|
|
|
|
sources map[string]*config.TargetGroup
|
2015-10-08 16:06:58 +00:00
|
|
|
sdUpdates *chan<- config.TargetGroup
|
2015-10-06 13:47:34 +00:00
|
|
|
updates chan zookeeperTreeCacheEvent
|
|
|
|
treeCaches []*zookeeperTreeCache
|
2015-06-09 11:25:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewServersetDiscovery returns a new ServersetDiscovery for the given config.
|
|
|
|
func NewServersetDiscovery(conf *config.ServersetSDConfig) *ServersetDiscovery {
|
|
|
|
conn, _, err := zk.Connect(conf.Servers, time.Duration(conf.Timeout))
|
2015-08-24 13:07:27 +00:00
|
|
|
conn.SetLogger(zookeeperLogger{})
|
2015-06-09 11:25:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
updates := make(chan zookeeperTreeCacheEvent)
|
|
|
|
sd := &ServersetDiscovery{
|
|
|
|
conf: conf,
|
|
|
|
conn: conn,
|
|
|
|
updates: updates,
|
|
|
|
sources: map[string]*config.TargetGroup{},
|
|
|
|
}
|
|
|
|
go sd.processUpdates()
|
2015-10-06 13:47:34 +00:00
|
|
|
for _, path := range conf.Paths {
|
|
|
|
sd.treeCaches = append(sd.treeCaches, newZookeeperTreeCache(conn, path, updates))
|
|
|
|
}
|
2015-06-09 11:25:30 +00:00
|
|
|
return sd
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sources implements the TargetProvider interface.
|
|
|
|
func (sd *ServersetDiscovery) Sources() []string {
|
|
|
|
sd.mu.RLock()
|
|
|
|
defer sd.mu.RUnlock()
|
|
|
|
srcs := []string{}
|
|
|
|
for t := range sd.sources {
|
|
|
|
srcs = append(srcs, t)
|
|
|
|
}
|
|
|
|
return srcs
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sd *ServersetDiscovery) processUpdates() {
|
|
|
|
defer sd.conn.Close()
|
|
|
|
for event := range sd.updates {
|
|
|
|
tg := &config.TargetGroup{
|
2015-08-07 11:18:19 +00:00
|
|
|
Source: event.Path,
|
2015-06-09 11:25:30 +00:00
|
|
|
}
|
|
|
|
sd.mu.Lock()
|
|
|
|
if event.Data != nil {
|
|
|
|
labelSet, err := parseServersetMember(*event.Data, event.Path)
|
|
|
|
if err == nil {
|
2015-08-20 15:18:46 +00:00
|
|
|
tg.Targets = []model.LabelSet{*labelSet}
|
2015-06-09 11:25:30 +00:00
|
|
|
sd.sources[event.Path] = tg
|
|
|
|
} else {
|
|
|
|
delete(sd.sources, event.Path)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
delete(sd.sources, event.Path)
|
|
|
|
}
|
|
|
|
sd.mu.Unlock()
|
|
|
|
if sd.sdUpdates != nil {
|
2015-10-08 16:06:58 +00:00
|
|
|
*sd.sdUpdates <- *tg
|
2015-06-09 11:25:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if sd.sdUpdates != nil {
|
|
|
|
close(*sd.sdUpdates)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run implements the TargetProvider interface.
|
2015-10-08 16:06:58 +00:00
|
|
|
func (sd *ServersetDiscovery) Run(ch chan<- config.TargetGroup, done <-chan struct{}) {
|
2015-06-09 11:25:30 +00:00
|
|
|
// Send on everything we have seen so far.
|
|
|
|
sd.mu.Lock()
|
|
|
|
for _, targetGroup := range sd.sources {
|
2015-10-08 16:06:58 +00:00
|
|
|
ch <- *targetGroup
|
2015-06-09 11:25:30 +00:00
|
|
|
}
|
|
|
|
// Tell processUpdates to send future updates.
|
|
|
|
sd.sdUpdates = &ch
|
|
|
|
sd.mu.Unlock()
|
|
|
|
|
2015-08-10 14:44:32 +00:00
|
|
|
<-done
|
2015-10-06 13:47:34 +00:00
|
|
|
for _, tc := range sd.treeCaches {
|
|
|
|
tc.Stop()
|
|
|
|
}
|
2015-06-09 11:25:30 +00:00
|
|
|
}
|
|
|
|
|
2015-08-20 15:18:46 +00:00
|
|
|
func parseServersetMember(data []byte, path string) (*model.LabelSet, error) {
|
2015-06-09 11:25:30 +00:00
|
|
|
member := serversetMember{}
|
|
|
|
err := json.Unmarshal(data, &member)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error unmarshaling serverset member %q: %s", path, err)
|
|
|
|
}
|
|
|
|
|
2015-08-20 15:18:46 +00:00
|
|
|
labels := model.LabelSet{}
|
|
|
|
labels[serversetPathLabel] = model.LabelValue(path)
|
|
|
|
labels[model.AddressLabel] = model.LabelValue(
|
2015-06-09 11:25:30 +00:00
|
|
|
fmt.Sprintf("%s:%d", member.ServiceEndpoint.Host, member.ServiceEndpoint.Port))
|
|
|
|
|
2015-08-20 15:18:46 +00:00
|
|
|
labels[serversetEndpointLabelPrefix+"_host"] = model.LabelValue(member.ServiceEndpoint.Host)
|
|
|
|
labels[serversetEndpointLabelPrefix+"_port"] = model.LabelValue(fmt.Sprintf("%d", member.ServiceEndpoint.Port))
|
2015-06-09 11:25:30 +00:00
|
|
|
|
|
|
|
for name, endpoint := range member.AdditionalEndpoints {
|
2015-08-20 15:18:46 +00:00
|
|
|
cleanName := model.LabelName(strutil.SanitizeLabelName(name))
|
|
|
|
labels[serversetEndpointLabelPrefix+"_host_"+cleanName] = model.LabelValue(
|
2015-06-09 11:25:30 +00:00
|
|
|
endpoint.Host)
|
2015-08-20 15:18:46 +00:00
|
|
|
labels[serversetEndpointLabelPrefix+"_port_"+cleanName] = model.LabelValue(
|
2015-06-09 11:25:30 +00:00
|
|
|
fmt.Sprintf("%d", endpoint.Port))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-08-20 15:18:46 +00:00
|
|
|
labels[serversetStatusLabel] = model.LabelValue(member.Status)
|
2015-08-26 14:53:37 +00:00
|
|
|
labels[serversetShardLabel] = model.LabelValue(strconv.Itoa(member.Shard))
|
2015-06-09 11:25:30 +00:00
|
|
|
|
|
|
|
return &labels, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type zookeeperTreeCache struct {
|
|
|
|
conn *zk.Conn
|
|
|
|
prefix string
|
|
|
|
events chan zookeeperTreeCacheEvent
|
|
|
|
zkEvents chan zk.Event
|
|
|
|
stop chan struct{}
|
|
|
|
head *zookeeperTreeCacheNode
|
|
|
|
}
|
|
|
|
|
|
|
|
type zookeeperTreeCacheEvent struct {
|
|
|
|
Path string
|
|
|
|
Data *[]byte
|
|
|
|
}
|
|
|
|
|
|
|
|
type zookeeperTreeCacheNode struct {
|
|
|
|
data *[]byte
|
|
|
|
events chan zk.Event
|
|
|
|
done chan struct{}
|
|
|
|
stopped bool
|
|
|
|
children map[string]*zookeeperTreeCacheNode
|
|
|
|
}
|
|
|
|
|
2015-08-24 13:07:27 +00:00
|
|
|
func newZookeeperTreeCache(conn *zk.Conn, path string, events chan zookeeperTreeCacheEvent) *zookeeperTreeCache {
|
2015-06-09 11:25:30 +00:00
|
|
|
tc := &zookeeperTreeCache{
|
|
|
|
conn: conn,
|
|
|
|
prefix: path,
|
|
|
|
events: events,
|
|
|
|
stop: make(chan struct{}),
|
|
|
|
}
|
|
|
|
tc.head = &zookeeperTreeCacheNode{
|
|
|
|
events: make(chan zk.Event),
|
|
|
|
children: map[string]*zookeeperTreeCacheNode{},
|
2015-07-01 12:53:39 +00:00
|
|
|
stopped: true,
|
2015-06-09 11:25:30 +00:00
|
|
|
}
|
|
|
|
err := tc.recursiveNodeUpdate(path, tc.head)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Error during initial read of Zookeeper: %s", err)
|
|
|
|
}
|
|
|
|
go tc.loop(err != nil)
|
|
|
|
return tc
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tc *zookeeperTreeCache) Stop() {
|
|
|
|
tc.stop <- struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tc *zookeeperTreeCache) loop(failureMode bool) {
|
|
|
|
retryChan := make(chan struct{})
|
|
|
|
|
|
|
|
failure := func() {
|
|
|
|
failureMode = true
|
|
|
|
time.AfterFunc(time.Second*10, func() {
|
|
|
|
retryChan <- struct{}{}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if failureMode {
|
|
|
|
failure()
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case ev := <-tc.head.events:
|
|
|
|
log.Debugf("Received Zookeeper event: %s", ev)
|
|
|
|
if failureMode {
|
|
|
|
continue
|
|
|
|
}
|
2015-11-16 06:25:54 +00:00
|
|
|
|
2015-06-09 11:25:30 +00:00
|
|
|
if ev.Type == zk.EventNotWatching {
|
|
|
|
log.Infof("Lost connection to Zookeeper.")
|
|
|
|
failure()
|
|
|
|
} else {
|
|
|
|
path := strings.TrimPrefix(ev.Path, tc.prefix)
|
|
|
|
parts := strings.Split(path, "/")
|
|
|
|
node := tc.head
|
|
|
|
for _, part := range parts[1:] {
|
|
|
|
childNode := node.children[part]
|
|
|
|
if childNode == nil {
|
|
|
|
childNode = &zookeeperTreeCacheNode{
|
|
|
|
events: tc.head.events,
|
|
|
|
children: map[string]*zookeeperTreeCacheNode{},
|
|
|
|
done: make(chan struct{}, 1),
|
|
|
|
}
|
|
|
|
node.children[part] = childNode
|
|
|
|
}
|
|
|
|
node = childNode
|
|
|
|
}
|
2015-11-16 06:25:54 +00:00
|
|
|
|
2015-06-09 11:25:30 +00:00
|
|
|
err := tc.recursiveNodeUpdate(ev.Path, node)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Error during processing of Zookeeper event: %s", err)
|
|
|
|
failure()
|
2015-07-01 12:53:39 +00:00
|
|
|
} else if tc.head.data == nil {
|
|
|
|
log.Errorf("Error during processing of Zookeeper event: path %s no longer exists", tc.prefix)
|
|
|
|
failure()
|
2015-06-09 11:25:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case <-retryChan:
|
|
|
|
log.Infof("Attempting to resync state with Zookeeper")
|
2015-11-16 06:25:54 +00:00
|
|
|
// Reset root child nodes before traversing the Zookeeper path.
|
|
|
|
tc.head.children = make(map[string]*zookeeperTreeCacheNode)
|
2015-06-09 11:25:30 +00:00
|
|
|
err := tc.recursiveNodeUpdate(tc.prefix, tc.head)
|
2015-07-01 12:53:39 +00:00
|
|
|
if err != nil {
|
2015-06-09 11:25:30 +00:00
|
|
|
log.Errorf("Error during Zookeeper resync: %s", err)
|
|
|
|
failure()
|
2015-07-01 12:53:39 +00:00
|
|
|
} else {
|
|
|
|
log.Infof("Zookeeper resync successful")
|
|
|
|
failureMode = false
|
2015-06-09 11:25:30 +00:00
|
|
|
}
|
|
|
|
case <-tc.stop:
|
|
|
|
close(tc.events)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tc *zookeeperTreeCache) recursiveNodeUpdate(path string, node *zookeeperTreeCacheNode) error {
|
|
|
|
data, _, dataWatcher, err := tc.conn.GetW(path)
|
|
|
|
if err == zk.ErrNoNode {
|
|
|
|
tc.recursiveDelete(path, node)
|
2015-07-01 12:53:39 +00:00
|
|
|
if node == tc.head {
|
|
|
|
return fmt.Errorf("path %s does not exist", path)
|
|
|
|
}
|
2015-06-09 11:25:30 +00:00
|
|
|
return nil
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if node.data == nil || !bytes.Equal(*node.data, data) {
|
|
|
|
node.data = &data
|
|
|
|
tc.events <- zookeeperTreeCacheEvent{Path: path, Data: node.data}
|
|
|
|
}
|
|
|
|
|
|
|
|
children, _, childWatcher, err := tc.conn.ChildrenW(path)
|
|
|
|
if err == zk.ErrNoNode {
|
|
|
|
tc.recursiveDelete(path, node)
|
|
|
|
return nil
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
currentChildren := map[string]struct{}{}
|
|
|
|
for _, child := range children {
|
|
|
|
currentChildren[child] = struct{}{}
|
|
|
|
childNode := node.children[child]
|
2015-11-16 06:25:54 +00:00
|
|
|
// Does not already exists or we previous had a watch that
|
|
|
|
// triggered.
|
|
|
|
if childNode == nil || childNode.stopped {
|
2015-06-09 11:25:30 +00:00
|
|
|
node.children[child] = &zookeeperTreeCacheNode{
|
|
|
|
events: node.events,
|
|
|
|
children: map[string]*zookeeperTreeCacheNode{},
|
|
|
|
done: make(chan struct{}, 1),
|
|
|
|
}
|
2015-11-16 06:25:54 +00:00
|
|
|
err = tc.recursiveNodeUpdate(path+"/"+child, node.children[child])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-06-09 11:25:30 +00:00
|
|
|
}
|
|
|
|
}
|
2015-11-16 06:25:54 +00:00
|
|
|
|
2015-06-09 11:25:30 +00:00
|
|
|
// Remove nodes that no longer exist
|
|
|
|
for name, childNode := range node.children {
|
|
|
|
if _, ok := currentChildren[name]; !ok || node.data == nil {
|
|
|
|
tc.recursiveDelete(path+"/"+name, childNode)
|
|
|
|
delete(node.children, name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
// Pass up zookeeper events, until the node is deleted.
|
|
|
|
select {
|
|
|
|
case event := <-dataWatcher:
|
|
|
|
node.events <- event
|
|
|
|
case event := <-childWatcher:
|
|
|
|
node.events <- event
|
|
|
|
case <-node.done:
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tc *zookeeperTreeCache) recursiveDelete(path string, node *zookeeperTreeCacheNode) {
|
|
|
|
if !node.stopped {
|
|
|
|
node.done <- struct{}{}
|
|
|
|
node.stopped = true
|
|
|
|
}
|
|
|
|
if node.data != nil {
|
|
|
|
tc.events <- zookeeperTreeCacheEvent{Path: path, Data: nil}
|
|
|
|
node.data = nil
|
|
|
|
}
|
|
|
|
for name, childNode := range node.children {
|
|
|
|
tc.recursiveDelete(path+"/"+name, childNode)
|
|
|
|
}
|
|
|
|
}
|