/
aggregator_transaction.go
161 lines (131 loc) · 3.2 KB
/
aggregator_transaction.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
package cat
import (
"bytes"
"fmt"
"time"
"github.com/yodaGo/gocat/message"
)
type transactionData struct {
mtype, name string
count, fail int
sum int64
durations map[int]int
}
//noinspection GoUnhandledErrorResult
func encodeTransactionData(data *transactionData) *bytes.Buffer {
buf := newBuf()
buf.WriteRune(batchFlag)
buf.WriteInt(data.count)
buf.WriteRune(batchSplit)
buf.WriteInt(data.fail)
buf.WriteRune(batchSplit)
buf.WriteUInt64(uint64(data.sum))
buf.WriteRune(batchSplit)
i := 0
for k, v := range data.durations {
if i > 0 {
buf.WriteRune('|')
}
buf.WriteInt(k)
buf.WriteRune(',')
buf.WriteInt(v)
i++
}
buf.WriteRune(batchSplit)
return &buf.Buffer
}
func (p *transactionAggregator) GetName() string {
return "TransactionAggregator"
}
type transactionAggregator struct {
scheduleMixin
ch chan *message.Transaction
dataMap map[string]*transactionData
ticker *time.Ticker
}
func (p *transactionAggregator) collectAndSend() {
dataMap := p.dataMap
p.dataMap = make(map[string]*transactionData)
p.send(dataMap)
}
func (p *transactionAggregator) send(dataMap map[string]*transactionData) {
if len(dataMap) == 0 {
return
}
t := message.NewTransaction(typeSystem, nameTransactionAggregator, aggregator.flush)
defer t.Complete()
for _, data := range dataMap {
trans := message.NewTransaction(data.mtype, data.name, nil)
trans.SetData(encodeTransactionData(data).String())
trans.Complete()
t.AddChild(trans)
}
}
func (p *transactionAggregator) getOrDefault(transaction *message.Transaction) *transactionData {
key := fmt.Sprintf("%s,%s", transaction.Type, transaction.Name)
if data, ok := p.dataMap[key]; ok {
return data
} else {
p.dataMap[key] = &transactionData{
mtype: transaction.GetType(),
name: transaction.GetName(),
count: 0,
fail: 0,
sum: 0,
durations: make(map[int]int),
}
return p.dataMap[key]
}
}
func (p *transactionAggregator) afterStart() {
p.ticker = time.NewTicker(transactionAggregatorInterval)
}
func (p *transactionAggregator) beforeStop() {
close(p.ch)
for t := range p.ch {
p.getOrDefault(t).add(t)
}
p.collectAndSend()
p.ticker.Stop()
}
func (p *transactionAggregator) process() {
select {
case sig := <-p.signals:
p.handle(sig)
case t := <-p.ch:
p.getOrDefault(t).add(t)
case <-p.ticker.C:
p.collectAndSend()
}
}
func (p *transactionAggregator) Put(t *message.Transaction) {
if !IsEnabled() {
return
}
select {
case p.ch <- t:
default:
logger.Warning("Transaction aggregator is full")
}
}
func (data *transactionData) add(transaction *message.Transaction) {
data.count++
if transaction.GetStatus() != SUCCESS {
data.fail++
}
millis := duration2Millis(transaction.GetDuration())
data.sum += millis
duration := computeDuration(int(millis))
if _, ok := data.durations[duration]; ok {
data.durations[duration]++
} else {
data.durations[duration] = 1
}
}
func newTransactionAggregator() *transactionAggregator {
return &transactionAggregator{
scheduleMixin: makeScheduleMixedIn(signalTransactionAggregatorExit),
ch: make(chan *message.Transaction, transactionAggregatorChannelCapacity),
dataMap: make(map[string]*transactionData),
}
}