This repository has been archived by the owner on Aug 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
nozzle.go
238 lines (204 loc) · 5.8 KB
/
nozzle.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package nozzle
import (
"io/ioutil"
"log"
"runtime"
"time"
diodes "code.cloudfoundry.org/go-diodes"
"code.cloudfoundry.org/go-loggregator"
"code.cloudfoundry.org/go-loggregator/rpc/loggregator_v2"
"code.cloudfoundry.org/log-cache/internal/metrics"
"code.cloudfoundry.org/log-cache/pkg/rpc/logcache_v1"
"golang.org/x/net/context"
"google.golang.org/grpc"
)
// Nozzle reads envelopes and writes them to LogCache.
type Nozzle struct {
log *log.Logger
s StreamConnector
metrics metrics.Initializer
shardId string
selectors []string
streamBuffer *diodes.OneToOne
// LogCache
addr string
opts []grpc.DialOption
}
const (
BATCH_FLUSH_INTERVAL = 500 * time.Millisecond
BATCH_CHANNEL_SIZE = 512
)
// StreamConnector reads envelopes from the the logs provider.
type StreamConnector interface {
// Stream creates a EnvelopeStream for the given request.
Stream(ctx context.Context, req *loggregator_v2.EgressBatchRequest) loggregator.EnvelopeStream
}
// NewNozzle creates a new Nozzle.
func NewNozzle(c StreamConnector, logCacheAddr string, shardId string, opts ...NozzleOption) *Nozzle {
n := &Nozzle{
s: c,
addr: logCacheAddr,
opts: []grpc.DialOption{grpc.WithInsecure()},
log: log.New(ioutil.Discard, "", 0),
metrics: metrics.NullMetrics{},
shardId: shardId,
selectors: []string{},
}
for _, o := range opts {
o(n)
}
n.streamBuffer = diodes.NewOneToOne(100000, diodes.AlertFunc(func(missed int) {
n.log.Printf("stream buffer dropped %d points", missed)
}))
return n
}
// NozzleOption configures a Nozzle.
type NozzleOption func(*Nozzle)
// WithLogger returns a NozzleOption that configures a nozzle's logger.
// It defaults to silent logging.
func WithLogger(l *log.Logger) NozzleOption {
return func(n *Nozzle) {
n.log = l
}
}
// WithMetrics returns a NozzleOption that configures the metrics for the
// Nozzle. It will add metrics to the given map.
func WithMetrics(metrics metrics.Initializer) NozzleOption {
return func(n *Nozzle) {
n.metrics = metrics
}
}
// WithDialOpts returns a NozzleOption that configures the dial options
// for dialing the LogCache. It defaults to grpc.WithInsecure().
func WithDialOpts(opts ...grpc.DialOption) NozzleOption {
return func(n *Nozzle) {
n.opts = opts
}
}
func WithSelectors(selectors ...string) NozzleOption {
return func(n *Nozzle) {
n.selectors = selectors
}
}
// Start starts reading envelopes from the logs provider and writes them to
// LogCache. It blocks indefinitely.
func (n *Nozzle) Start() {
rx := n.s.Stream(context.Background(), n.buildBatchReq())
conn, err := grpc.Dial(n.addr, n.opts...)
if err != nil {
log.Fatalf("failed to dial %s: %s", n.addr, err)
}
client := logcache_v1.NewIngressClient(conn)
ingressInc := n.metrics.NewCounter("nozzle_ingress")
egressInc := n.metrics.NewCounter("nozzle_egress")
errInc := n.metrics.NewCounter("nozzle_err")
go n.envelopeReader(rx, ingressInc)
ch := make(chan []*loggregator_v2.Envelope, BATCH_CHANNEL_SIZE)
log.Printf("Starting %d nozzle workers...", 2*runtime.NumCPU())
for i := 0; i < 2*runtime.NumCPU(); i++ {
go n.envelopeWriter(ch, client, errInc, egressInc)
}
// The batcher will block indefinitely.
n.envelopeBatcher(ch)
}
func (n *Nozzle) envelopeBatcher(ch chan []*loggregator_v2.Envelope) {
poller := diodes.NewPoller(n.streamBuffer)
envelopes := make([]*loggregator_v2.Envelope, 0)
t := time.NewTimer(BATCH_FLUSH_INTERVAL)
for {
data, found := poller.TryNext()
if found {
envelopes = append(envelopes, (*loggregator_v2.Envelope)(data))
}
select {
case <-t.C:
if len(envelopes) > 0 {
select {
case ch <- envelopes:
envelopes = make([]*loggregator_v2.Envelope, 0)
default:
// if we can't write into the channel, it must be full, so
// we probably need to drop these envelopes on the floor
envelopes = envelopes[:0]
}
}
t.Reset(BATCH_FLUSH_INTERVAL)
default:
if len(envelopes) >= BATCH_CHANNEL_SIZE {
select {
case ch <- envelopes:
envelopes = make([]*loggregator_v2.Envelope, 0)
default:
envelopes = envelopes[:0]
}
t.Reset(BATCH_FLUSH_INTERVAL)
}
if !found {
time.Sleep(time.Millisecond)
}
}
}
}
func (n *Nozzle) envelopeWriter(ch chan []*loggregator_v2.Envelope, client logcache_v1.IngressClient, errInc, egressInc func(uint64)) {
for {
envelopes := <-ch
ctx, _ := context.WithTimeout(context.Background(), 3*time.Second)
_, err := client.Send(ctx, &logcache_v1.SendRequest{
Envelopes: &loggregator_v2.EnvelopeBatch{
Batch: envelopes,
},
})
if err != nil {
errInc(1)
continue
}
egressInc(uint64(len(envelopes)))
}
}
func (n *Nozzle) envelopeReader(rx loggregator.EnvelopeStream, ingressInc func(uint64)) {
for {
envelopeBatch := rx()
for _, envelope := range envelopeBatch {
n.streamBuffer.Set(diodes.GenericDataType(envelope))
ingressInc(1)
}
}
}
var selectorTypes = map[string]*loggregator_v2.Selector{
"log": {
Message: &loggregator_v2.Selector_Log{
Log: &loggregator_v2.LogSelector{},
},
},
"gauge": {
Message: &loggregator_v2.Selector_Gauge{
Gauge: &loggregator_v2.GaugeSelector{},
},
},
"counter": {
Message: &loggregator_v2.Selector_Counter{
Counter: &loggregator_v2.CounterSelector{},
},
},
"timer": {
Message: &loggregator_v2.Selector_Timer{
Timer: &loggregator_v2.TimerSelector{},
},
},
"event": {
Message: &loggregator_v2.Selector_Event{
Event: &loggregator_v2.EventSelector{},
},
},
}
func (n *Nozzle) buildBatchReq() *loggregator_v2.EgressBatchRequest {
var selectors []*loggregator_v2.Selector
for _, selectorType := range n.selectors {
selectors = append(selectors, selectorTypes[selectorType])
}
return &loggregator_v2.EgressBatchRequest{
ShardId: n.shardId,
UsePreferredTags: true,
Selectors: selectors,
}
}