forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 1
/
trans.go
210 lines (171 loc) · 3.93 KB
/
trans.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
package cassandra
import (
"time"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/packetbeat/procs"
"github.com/elastic/beats/packetbeat/protos/applayer"
)
type transactions struct {
config *transactionConfig
requests messageList
responses messageList
onTransaction transactionHandler
}
type transactionConfig struct {
transactionTimeout time.Duration
}
type transactionHandler func(requ, resp *message) error
// List of messages available for correlation
type messageList struct {
head, tail *message
}
func (trans *transactions) init(c *transactionConfig, cb transactionHandler) {
trans.config = c
trans.onTransaction = cb
}
func (trans *transactions) onMessage(
tuple *common.IPPortTuple,
dir uint8,
msg *message,
) error {
var err error
msg.Tuple = *tuple
msg.Transport = applayer.TransportTCP
msg.CmdlineTuple = procs.ProcWatcher.FindProcessesTuple(&msg.Tuple)
if msg.IsRequest {
if isDebug {
debugf("Received request with tuple: %s", tuple)
}
err = trans.onRequest(tuple, dir, msg)
} else {
if isDebug {
debugf("Received response with tuple: %s", tuple)
}
err = trans.onResponse(tuple, dir, msg)
}
return err
}
// onRequest handles request messages, merging with incomplete requests
// and adding non-merged requests into the correlation list.
func (trans *transactions) onRequest(
tuple *common.IPPortTuple,
dir uint8,
msg *message,
) error {
prev := trans.requests.last()
merged, err := trans.tryMergeRequests(prev, msg)
if err != nil {
return err
}
if merged {
msg = prev
} else {
trans.requests.append(msg)
}
if !msg.isComplete {
return nil
}
return trans.correlate()
}
// onRequest handles response messages, merging with incomplete requests
// and adding non-merged responses into the correlation list.
func (trans *transactions) onResponse(
tuple *common.IPPortTuple,
dir uint8,
msg *message,
) error {
prev := trans.responses.last()
merged, err := trans.tryMergeResponses(prev, msg)
if err != nil {
return err
}
if merged {
msg = prev
} else {
trans.responses.append(msg)
}
if !msg.isComplete {
return nil
}
return trans.correlate()
}
func (trans *transactions) tryMergeRequests(
prev, msg *message,
) (merged bool, err error) {
msg.isComplete = true
return false, nil
}
func (trans *transactions) tryMergeResponses(prev, msg *message) (merged bool, err error) {
msg.isComplete = true
return false, nil
}
func (trans *transactions) correlate() error {
requests := &trans.requests
responses := &trans.responses
// drop responses with missing requests
if requests.empty() {
for !responses.empty() {
//if the response is EVENT, which pushed from server, we can accept that
resp := responses.first()
if !resp.isComplete {
break
}
if resp.header["op"] == "EVENT" {
if isDebug {
logp.Debug("cassandra", "server pushed message,%v", resp.header)
}
responses.pop()
if err := trans.onTransaction(nil, resp); err != nil {
return err
}
return nil
}
logp.Warn("Response from unknown transaction. Ignoring. %v", resp.header)
responses.pop()
}
return nil
}
// merge requests with responses into transactions
for !responses.empty() && !requests.empty() {
resp := responses.first()
if !resp.isComplete {
break
}
requ := requests.pop()
responses.pop()
if err := trans.onTransaction(requ, resp); err != nil {
return err
}
}
return nil
}
func (ml *messageList) append(msg *message) {
if ml.tail == nil {
ml.head = msg
} else {
ml.tail.next = msg
}
msg.next = nil
ml.tail = msg
}
func (ml *messageList) empty() bool {
return ml.head == nil
}
func (ml *messageList) pop() *message {
if ml.head == nil {
return nil
}
msg := ml.head
ml.head = ml.head.next
if ml.head == nil {
ml.tail = nil
}
return msg
}
func (ml *messageList) first() *message {
return ml.head
}
func (ml *messageList) last() *message {
return ml.tail
}