-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathcollector.go
396 lines (367 loc) · 8.66 KB
/
collector.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
package main
import (
"net/url"
"regexp"
"strings"
"sync"
"time"
)
const formatValues = "values"
const formatTabSeparated = "tabseparated"
var regexFormat = regexp.MustCompile("(?i)format\\s\\S+(\\s+)")
var regexValues = regexp.MustCompile("(?i)\\svalues\\s")
var regexGetFormat = regexp.MustCompile("(?i)format\\s(\\S+)")
// Table - store query table info
type Table struct {
Name string
Format string
Query string
Params string
Rows []string
count int
FlushCount int
FlushInterval int
mu sync.Mutex
Sender Sender
TickerChan *chan struct{}
lastUpdate time.Time
// todo add Last Error
}
// Collector - query collector
type Collector struct {
Tables map[string]*Table
mu sync.RWMutex
Count int
FlushInterval int
CleanInterval int
RemoveQueryID bool
Sender Sender
TickerChan *chan struct{}
}
// NewTable - default table constructor
func NewTable(name string, sender Sender, count int, interval int) (t *Table) {
t = new(Table)
t.Name = name
t.Sender = sender
t.FlushCount = count
t.FlushInterval = interval
return t
}
// NewCollector - default collector constructor
func NewCollector(sender Sender, count int, interval int, cleanInterval int, removeQueryID bool) (c *Collector) {
c = new(Collector)
c.Sender = sender
c.Tables = make(map[string]*Table)
c.Count = count
c.FlushInterval = interval
c.CleanInterval = cleanInterval
c.RemoveQueryID = removeQueryID
if cleanInterval > 0 {
c.TickerChan = c.RunTimer()
}
return c
}
// Content - get text content of rowsfor query
func (t *Table) Content() string {
rowDelimiter := "\n"
if t.Format == "RowBinary" {
rowDelimiter = ""
}
return t.Query + "\n" + strings.Join(t.Rows, rowDelimiter)
}
// Flush - sends collected data in table to clickhouse
func (t *Table) Flush() {
req := ClickhouseRequest{
Params: t.Params,
Query: t.Query,
Content: t.Content(),
Count: len(t.Rows),
isInsert: true,
}
t.Sender.Send(&req)
t.Rows = make([]string, 0, t.FlushCount)
t.count = 0
}
// CheckFlush - check if flush is need and sends data to clickhouse
func (t *Table) CheckFlush() bool {
t.mu.Lock()
defer t.mu.Unlock()
if t.count > 0 {
t.Flush()
return true
}
return false
}
// Empty - Checks if table is empty
func (t *Table) Empty() bool {
return t.GetCount() == 0
}
// GetCount - Checks if table is empty
func (t *Table) GetCount() int {
t.mu.Lock()
defer t.mu.Unlock()
return t.count
}
// RunTimer - timer for periodical savings data
func (t *Table) RunTimer() *chan struct{} {
done := make(chan struct{})
go func() {
ticker := time.NewTicker(time.Millisecond * time.Duration(t.FlushInterval))
defer ticker.Stop()
for {
select {
case <-ticker.C:
t.CheckFlush()
case <-done:
return
}
}
}()
return &done
}
// Add - Adding query to table
func (t *Table) Add(text string) {
t.mu.Lock()
defer t.mu.Unlock()
t.count++
if t.Format == "TabSeparated" {
t.Rows = append(t.Rows, strings.Split(text, "\n")...)
} else {
t.Rows = append(t.Rows, text)
}
if len(t.Rows) >= t.FlushCount {
t.Flush()
}
t.lastUpdate = time.Now()
}
// CleanTable - delete table from map
func (t *Table) CleanTable() {
t.mu.Lock()
close(*t.TickerChan)
t = nil
}
// CleanTables - clean unsused tables
func (c *Collector) CleanTables() {
c.mu.Lock()
defer c.mu.Unlock()
for k, t := range c.Tables {
if t.lastUpdate.Add(time.Duration(c.CleanInterval) * time.Millisecond).Before(time.Now()) {
// table was not updated for CleanInterval - delete that table - otherwise it can cause memLeak
t.CleanTable()
defer delete(c.Tables, k)
}
}
}
// RunTimer - timer for periodical cleaning unused tables
func (c *Collector) RunTimer() *chan struct{} {
done := make(chan struct{})
go func() {
ticker := time.NewTicker(time.Duration(c.CleanInterval) * time.Millisecond)
defer ticker.Stop()
for {
select {
case <-ticker.C:
c.CleanTables()
case <-done:
return
}
}
}()
return &done
}
// Empty - check if all tables are empty
func (c *Collector) Empty() bool {
c.mu.Lock()
defer c.mu.Unlock()
for _, t := range c.Tables {
if ok := t.Empty(); !ok {
return false
}
}
return true
}
// FlushAll - flush all tables to clickhouse
func (c *Collector) FlushAll() (count int) {
c.mu.Lock()
defer c.mu.Unlock()
count = 0
for _, t := range c.Tables {
if ok := t.CheckFlush(); ok {
count++
}
}
return count
}
// WaitFlush - wait for flush all tables
func (c *Collector) WaitFlush() (err error) {
return c.Sender.WaitFlush()
}
// AddTable - adding table to collector
func (c *Collector) AddTable(name string) {
c.mu.Lock()
defer c.mu.Unlock()
c.addTable(name)
}
func (c *Collector) separateQuery(name string) (query string, params string) {
items := strings.Split(name, "&")
for _, p := range items {
if HasPrefix(p, "query=") {
query = p[6:]
} else {
params += "&" + p
}
}
if len(params) > 0 {
params = strings.TrimSpace(params[1:])
}
q, err := url.QueryUnescape(query)
if err != nil {
return "", name
}
return q, params
}
func (c *Collector) getFormat(query string) (format string) {
format = formatValues
f := regexGetFormat.FindSubmatch([]byte(query))
if len(f) > 1 {
format = strings.TrimSpace(string(f[1]))
}
return format
}
func (c *Collector) addTable(name string) *Table {
t := NewTable(name, c.Sender, c.Count, c.FlushInterval)
query, params := c.separateQuery(name)
t.Query = query
t.Params = params
t.Format = c.getFormat(query)
c.Tables[name] = t
t.TickerChan = t.RunTimer()
t.lastUpdate = time.Now()
return t
}
// Push - adding query to collector with query params (with query) and rows
func (c *Collector) Push(paramsIn string, content string) {
// as we are using all params as a table key, we have to remove query_id
// otherwise every query will be threated as unique thus it will consume more memory
params := ""
if c.RemoveQueryID {
items := strings.Split(paramsIn, "&")
for _, p := range items {
if !HasPrefix(p, "query_id=") {
params += "&" + p
}
}
if len(params) > 0 {
params = strings.TrimSpace(params[1:])
}
} else {
params = paramsIn
}
c.mu.RLock()
table, ok := c.Tables[params]
if ok {
table.Add(content)
c.mu.RUnlock()
pushCounter.Inc()
return
}
c.mu.RUnlock()
c.mu.Lock()
table, ok = c.Tables[params]
if !ok {
table = c.addTable(params)
}
table.Add(content)
c.mu.Unlock()
pushCounter.Inc()
}
// ParseQuery - parsing inbound query to unified format (params/query), content (query data)
func (c *Collector) ParseQuery(queryString string, body string) (params string, content string, insert bool) {
i := strings.Index(queryString, "query=")
if i >= 0 {
if HasPrefix(queryString[i+6:], "insert") {
insert = true
}
var q string
eoq := strings.Index(queryString[i+6:], "&")
if eoq >= 0 {
q = queryString[i+6 : eoq+i+6]
params = queryString[:i] + queryString[eoq+i+7:]
} else {
q = queryString[i+6:]
params = queryString[:i]
}
uq, err := url.QueryUnescape(q)
if body != "" {
uq += " " + body
}
if err != nil {
return queryString, body, false
}
prefix, cnt := c.Parse(uq)
if strings.HasSuffix(params, "&") || params == "" {
params += "query=" + url.QueryEscape(strings.TrimSpace(prefix))
} else {
params += "&query=" + url.QueryEscape(strings.TrimSpace(prefix))
}
content = cnt
} else {
var q string
q, content = c.Parse(body)
q = strings.TrimSpace(q)
if HasPrefix(q, "insert") {
insert = true
}
if queryString != "" {
params = queryString + "&query=" + url.QueryEscape(q)
} else {
params = "query=" + url.QueryEscape(q)
}
}
return strings.TrimSpace(params), strings.TrimSpace(content), insert
}
// Parse - parsing text for query and data
func (c *Collector) Parse(text string) (prefix string, content string) {
i := strings.Index(text, "FORMAT")
k := strings.Index(text, "VALUES")
if k == -1 {
k = strings.Index(text, "values")
}
if i >= 0 && i < k {
w := false
off := -1
for c := i + 7; c < len(text); c++ {
if !w && text[c] != ' ' && text[c] != '\n' && text[c] != ';' {
w = true
}
if w && (text[c] == ' ' || text[c] == '\n' || text[c] == ';') {
off = c + 1
break
}
}
if off >= 0 {
prefix = text[:off]
content = text[off:]
}
} else {
if k >= 0 {
prefix = strings.TrimSpace(text[:k+6])
content = strings.TrimSpace(text[k+6:])
} else {
off := regexFormat.FindStringSubmatchIndex(text)
if len(off) > 3 {
prefix = text[:off[3]]
content = text[off[3]:]
} else {
off := regexValues.FindStringSubmatchIndex(text)
if len(off) > 0 {
prefix = text[:off[1]]
content = text[off[1]:]
} else {
prefix = text
}
}
}
}
return prefix, content
}