-
Notifications
You must be signed in to change notification settings - Fork 2
/
outputTgt.go
251 lines (202 loc) · 4.72 KB
/
outputTgt.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
239
240
241
242
243
244
245
246
247
248
249
250
251
package main
import (
"context"
fmt "fmt"
"net"
"net/url"
"sync"
"sync/atomic"
rpb "github.com/blind-oracle/riemann-relay/riemannpb"
fh "github.com/valyala/fasthttp"
)
type target struct {
id int
host string
typ outputType
conns []*tConn
connsAliveMap map[int]*tConn
connsAlive []*tConn
connsAliveCnt int
connsCnt int
connNext int
connMtx sync.Mutex
o *output
stats struct {
buffered uint64
dropped uint64
}
wg sync.WaitGroup
sync.RWMutex
*logger
}
func newOutputTgt(h string, cf *outputCfg, o *output) (*target, error) {
t := &target{
host: h,
typ: o.typ,
o: o,
connsCnt: cf.Connections,
connsAliveMap: map[int]*tConn{},
logger: &logger{fmt.Sprintf("%s: %s", cf.Name, h)},
}
for i := 0; i < cf.Connections; i++ {
c := &tConn{
t: t,
host: h,
id: i,
reconnectInterval: cf.ReconnectInterval.Duration,
timeoutConnect: cf.TimeoutConnect.Duration,
timeoutWrite: cf.TimeoutWrite.Duration,
chanIn: make(chan *rpb.Event, cf.BufferSize/cf.Connections),
logger: &logger{fmt.Sprintf("%s: %s[%d]", cf.Name, h, i)},
}
t.conns = append(t.conns, c)
c.batch.buf = make([]*rpb.Event, cf.BatchSize)
c.batch.size = cf.BatchSize
c.batch.timeout = cf.BatchTimeout.Duration
c.ctx, c.ctxCancel = context.WithCancel(context.Background())
switch o.typ {
case outputTypeCarbon, outputTypeRiemann:
if _, err := net.ResolveTCPAddr("tcp", h); err != nil {
return nil, fmt.Errorf("Bad TCP address '%s': %s", h, err)
}
}
switch o.typ {
case outputTypeCarbon:
c.writeBatch = c.writeBatchCarbon
case outputTypeRiemann:
c.timeoutRead = cf.TimeoutRead.Duration
c.writeBatch = c.writeBatchRiemann
case outputTypeClickhouse, outputTypeFlatbuf:
c.alive = true
c.httpCli = &fh.Client{
WriteTimeout: c.timeoutWrite,
}
u, err := url.Parse(h)
if err != nil {
return nil, fmt.Errorf("Unable to parse URL '%s': %s", h, err)
}
if o.typ == outputTypeClickhouse {
c.writeBatch = c.writeBatchClickhouse
if cf.CHTable == "" {
return nil, fmt.Errorf("You need to specify 'ch_table'")
}
q := u.Query()
q.Set("query", fmt.Sprintf("INSERT INTO %s FORMAT RowBinary", cf.CHTable))
u.RawQuery = q.Encode()
} else {
c.writeBatch = c.writeBatchFlatbuf
}
c.url = u.String()
}
switch o.typ {
case outputTypeRiemann, outputTypeCarbon:
c.wg.Add(1)
go c.run(o.typ)
}
c.wg.Add(2)
go c.dispatch()
go c.periodicFlush()
}
return t, nil
}
func (t *target) close() {
t.Warnf("Closing connections")
for _, c := range t.conns {
c.close()
}
t.Warnf("All connections closed")
return
}
func (t *target) setConnAlive(id int, s bool) {
t.Lock()
if s {
t.connsAliveMap[id] = t.conns[id]
} else {
delete(t.connsAliveMap, id)
}
t.connsAliveCnt = len(t.connsAliveMap)
t.connsAlive = make([]*tConn, t.connsAliveCnt)
i := 0
for _, v := range t.connsAliveMap {
t.connsAlive[i] = v
i++
}
if t.connsAliveCnt == 0 {
t.o.setTgtAlive(t.id, false)
} else {
t.o.setTgtAlive(t.id, true)
}
t.Unlock()
}
func (t *target) push(e *rpb.Event) bool {
t.connMtx.Lock()
next := t.connNext
if t.connNext++; t.connNext >= t.connsCnt {
t.connNext = 0
}
t.connMtx.Unlock()
t.RLock()
left := t.connsAliveCnt
for left > 0 {
if next >= t.connsAliveCnt {
next = 0
}
if t.connsAlive[next].bufferEvent(e) {
atomic.AddUint64(&t.stats.buffered, 1)
promTgtBuffered.WithLabelValues(t.o.name, t.host).Add(1)
t.RUnlock()
return true
}
next++
left--
}
t.RUnlock()
atomic.AddUint64(&t.stats.dropped, 1)
promTgtDroppedBufferFull.WithLabelValues(t.o.name, t.host).Add(1)
return false
}
func (t *target) getStats() (s []string) {
var (
cfT, sentT, ffT uint64
bufT, szT int
rows []string
)
t.RLock()
for _, c := range t.conns {
cf := atomic.LoadUint64(&c.stats.connFailed)
cfT += cf
ff := atomic.LoadUint64(&c.stats.flushFailed)
ffT += ff
sent := atomic.LoadUint64(&c.stats.sent)
sentT += sent
buf, sz := len(c.chanIn), cap(c.chanIn)
bufT += buf
szT += sz
_, alive := t.connsAliveMap[c.id]
r := fmt.Sprintf(" %d: buffered %d sent %d dropped %d connFailed %d flushFailed %d bufferFill %.3f (alive: %t)",
c.id,
atomic.LoadUint64(&c.stats.buffered),
sent,
atomic.LoadUint64(&c.stats.dropped),
cf,
ff,
float64(buf)/float64(sz),
alive,
)
rows = append(rows, r)
}
t.RUnlock()
r := fmt.Sprintf(" buffered %d sent %d dropped %d connFailed %d flushFailed %d bufferFill %.3f (alive: %t)",
atomic.LoadUint64(&t.stats.buffered),
sentT,
atomic.LoadUint64(&t.stats.dropped),
cfT,
ffT,
float64(bufT)/float64(szT),
t.connsAliveCnt > 0,
)
return append(
[]string{r},
rows...,
)
}