forked from kubernetes-retired/heapster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager.go
144 lines (129 loc) · 3.92 KB
/
manager.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright 2015 Google Inc. All Rights Reserved.
//
// 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 sinks
import (
"sync"
"time"
"github.com/golang/glog"
"github.com/prometheus/client_golang/prometheus"
"k8s.io/heapster/events/core"
)
const (
DefaultSinkExportEventsTimeout = 20 * time.Second
DefaultSinkStopTimeout = 60 * time.Second
)
var (
// Time spent exporting events to sink in microseconds.
exporterDuration = prometheus.NewSummaryVec(
prometheus.SummaryOpts{
Namespace: "eventer",
Subsystem: "exporter",
Name: "duration_microseconds",
Help: "Time spent exporting events to sink in microseconds.",
},
[]string{"exporter"},
)
)
func init() {
prometheus.MustRegister(exporterDuration)
}
type sinkHolder struct {
sink core.EventSink
eventBatchChannel chan *core.EventBatch
stopChannel chan bool
}
// Sink Manager - a special sink that distributes data to other sinks. It pushes data
// only to these sinks that completed their previous exports. Data that could not be
// pushed in the defined time is dropped and not retried.
type sinkManager struct {
sinkHolders []sinkHolder
exportEventsTimeout time.Duration
// Should be larger than exportEventsTimeout, although it is not a hard requirement.
stopTimeout time.Duration
}
func NewEventSinkManager(sinks []core.EventSink, exportEventsTimeout, stopTimeout time.Duration) (core.EventSink, error) {
sinkHolders := []sinkHolder{}
for _, sink := range sinks {
sh := sinkHolder{
sink: sink,
eventBatchChannel: make(chan *core.EventBatch),
stopChannel: make(chan bool),
}
sinkHolders = append(sinkHolders, sh)
go func(sh sinkHolder) {
for {
select {
case data := <-sh.eventBatchChannel:
export(sh.sink, data)
case isStop := <-sh.stopChannel:
glog.V(2).Infof("Stop received: %s", sh.sink.Name())
if isStop {
sh.sink.Stop()
return
}
}
}
}(sh)
}
return &sinkManager{
sinkHolders: sinkHolders,
exportEventsTimeout: exportEventsTimeout,
stopTimeout: stopTimeout,
}, nil
}
// Guarantees that the export will complete in exportEventsTimeout.
func (this *sinkManager) ExportEvents(data *core.EventBatch) {
var wg sync.WaitGroup
for _, sh := range this.sinkHolders {
wg.Add(1)
go func(sh sinkHolder, wg *sync.WaitGroup) {
defer wg.Done()
glog.V(2).Infof("Pushing events to: %s", sh.sink.Name())
select {
case sh.eventBatchChannel <- data:
glog.V(2).Infof("Data events completed: %s", sh.sink.Name())
// everything ok
case <-time.After(this.exportEventsTimeout):
glog.Warningf("Failed to events data to sink: %s", sh.sink.Name())
}
}(sh, &wg)
}
// Wait for all pushes to complete or timeout.
wg.Wait()
}
func (this *sinkManager) Name() string {
return "Manager"
}
func (this *sinkManager) Stop() {
for _, sh := range this.sinkHolders {
glog.V(2).Infof("Running stop for: %s", sh.sink.Name())
go func(sh sinkHolder) {
select {
case sh.stopChannel <- true:
// everything ok
glog.V(2).Infof("Stop sent to sink: %s", sh.sink.Name())
case <-time.After(this.stopTimeout):
glog.Warningf("Failed to stop sink: %s", sh.sink.Name())
}
return
}(sh)
}
}
func export(s core.EventSink, data *core.EventBatch) {
startTime := time.Now()
defer exporterDuration.
WithLabelValues(s.Name()).
Observe(float64(time.Since(startTime)) / float64(time.Microsecond))
s.ExportEvents(data)
}