-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathdrain.go
More file actions
493 lines (452 loc) · 13.8 KB
/
drain.go
File metadata and controls
493 lines (452 loc) · 13.8 KB
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
package loggingdrain
import (
"encoding/json"
"fmt"
"strings"
"sync"
lru "github.com/hashicorp/golang-lru/v2"
)
type SearchStrategy int
const (
SEARCH_STRATEGY_NEVER SearchStrategy = iota
SEARCH_STRATEGY_FALLBACK
SEARCH_STRATEGY_ALWAYS
)
type ClusterUpdateType int
const (
CLUSTER_UPDATE_TYPE_NONE ClusterUpdateType = iota
CLUSTER_UPDATE_TYPE_NEW_CLUSTER
CLUSTER_UPDATE_TYPE_UPDATE_CLUSTER
)
const (
default_max_depth = 4
default_sim = 0.4
default_max_children = 100
default_max_clusters = 1000
default_wildcard_str = "[*]"
)
type drain struct {
maxDepth int
sim float32
maxChildren int
maxClusters int
mu sync.Mutex
idToCluster *lru.Cache[int64, *LogCluster]
clusterCounter int64
rootNode *treeNode
}
type drainMarshalStruct struct {
MaxDepth int
Sim float32
MaxChildren int
MaxClusters int
ClusterCounter int64
Clusters []*LogCluster
RootNode *treeNode
}
func (drain *drain) MarshalJSON() ([]byte, error) {
clusters := []*LogCluster{}
clusters = append(clusters, drain.idToCluster.Values()...)
marshalStruct := drainMarshalStruct{
MaxDepth: drain.maxDepth,
Sim: drain.sim,
MaxChildren: drain.maxChildren,
MaxClusters: drain.maxClusters,
Clusters: clusters,
RootNode: drain.rootNode,
ClusterCounter: drain.clusterCounter,
}
return json.Marshal(&marshalStruct)
}
func (drain *drain) UnmarshalJSON(data []byte) error {
var marshalStruct drainMarshalStruct
err := json.Unmarshal(data, &marshalStruct)
if err != nil {
return err
}
l, _ := lru.New[int64, *LogCluster](marshalStruct.MaxClusters)
for _, cluster := range marshalStruct.Clusters {
l.Add(cluster.id, cluster)
}
drain.clusterCounter = marshalStruct.ClusterCounter
drain.idToCluster = l
drain.maxChildren = marshalStruct.MaxChildren
drain.maxClusters = marshalStruct.MaxClusters
drain.maxDepth = marshalStruct.MaxDepth
drain.mu = sync.Mutex{}
drain.rootNode = marshalStruct.RootNode
drain.sim = marshalStruct.Sim
return nil
}
func (drain *drain) status() string {
countStr := fmt.Sprintf("cluster count %v", drain.idToCluster.Len())
clustersStr := []string{}
for _, clusterKey := range drain.idToCluster.Keys() {
cluster, _ := drain.idToCluster.Get(clusterKey)
clustersStr = append(clustersStr, fmt.Sprintf("%s\n", cluster.getTemplate()))
}
status := fmt.Sprintf("%s\n%s", countStr, strings.Join(clustersStr, "\n"))
return status
}
func (drain *drain) addLogMessage(message string) (*LogCluster, ClusterUpdateType) {
tokens := getStringTokens(message)
cluster := drain.treeSearch(drain.rootNode, tokens, drain.sim, false)
if cluster == nil {
drain.clusterCounter += 1
id := drain.clusterCounter
cluster = newLogCluster(id, tokens)
drain.idToCluster.Add(id, cluster)
drain.addSeqToPrefixTree(drain.rootNode, cluster)
return cluster, CLUSTER_UPDATE_TYPE_NEW_CLUSTER
}
updatedTemplate, err := drain.updateTemplate(tokens, cluster.logTemplateTokens)
if err != nil {
return cluster, CLUSTER_UPDATE_TYPE_NONE
}
if !updatedTemplate {
return cluster, CLUSTER_UPDATE_TYPE_NONE
}
drain.idToCluster.Get(cluster.id)
return cluster, CLUSTER_UPDATE_TYPE_UPDATE_CLUSTER
}
// match log message against an already existing cluster.
// Match shall be perfect (sim_th=1.0).
// New cluster will not be created as a result of this call, nor any cluster modifications.
//
// :param content: log message to match
// :param full_search_strategy: when to perform full cluster search.
//
// (1) "never" is the fastest, will always perform a tree search [O(log(n)] but might produce
// false negatives (wrong mismatches) on some edge cases;
// (2) "fallback" will perform a linear search [O(n)] among all clusters with the same token count, but only in
// case tree search found no match.
// It should not have false negatives, however tree-search may find a non-optimal match with
// more wildcard parameters than necessary;
// (3) "always" is the slowest. It will select the best match among all known clusters, by always evaluating
// all clusters with the same token count, and selecting the cluster with perfect all token match and least
// count of wildcard matches.
//
// :return: Matched cluster or None if no match found.
func (drain *drain) match(content string, strategy SearchStrategy) *LogCluster {
tokens := getStringTokens(content)
requireSim := float32(1)
fullMatch := func() *LogCluster {
clusters := drain.getClustersForSeqLen(len(tokens))
cluster := drain.fastMatch(clusters, tokens, requireSim, true)
return cluster
}
switch strategy {
case SEARCH_STRATEGY_ALWAYS:
return fullMatch()
case SEARCH_STRATEGY_FALLBACK:
matchCluster := drain.treeSearch(drain.rootNode, tokens, requireSim, true)
if matchCluster != nil {
return matchCluster
}
return fullMatch()
case SEARCH_STRATEGY_NEVER:
matchCluster := drain.treeSearch(drain.rootNode, tokens, requireSim, true)
if matchCluster != nil {
return matchCluster
}
return nil
default:
matchCluster := drain.treeSearch(drain.rootNode, tokens, requireSim, true)
if matchCluster != nil {
return matchCluster
}
return nil
}
}
func (drain *drain) getMaxNodeDepth() int {
return drain.maxDepth - 2
}
func (drain *drain) GetTotalClusterSize() int {
return drain.idToCluster.Len()
}
func (drain *drain) treeSearch(
rootNode *treeNode,
tokens []string,
requireSim float32,
includeParams bool,
) *LogCluster {
tokenCount := len(tokens)
lengthNode, ok := rootNode.lengthNodeChildren[tokenCount]
if !ok {
return nil
}
if tokenCount == 0 {
if len(lengthNode.clusters) == 0 {
return nil
}
return lengthNode.clusters[0]
}
var currentNode = lengthNode
currentDepth := 1
for _, token := range tokens {
if currentDepth >= drain.getMaxNodeDepth() {
break
}
if currentDepth == tokenCount {
break
}
subNode, ok := currentNode.tokenNodeChildren[token]
if !ok {
wildcardNode := currentNode.tokenNodeChildren[default_wildcard_str]
currentNode = wildcardNode
} else {
currentNode = subNode
}
// no wildcard node
if currentNode == nil {
return nil
}
currentDepth += 1
}
return drain.fastMatch(currentNode.clusters, tokens, requireSim, includeParams)
}
func (drain *drain) updateTemplate(seq1, template []string) (bool, error) {
updated := false
if len(seq1) != len(template) {
return updated, errInternalRaw(
fmt.Sprintf("seq1 length %v not equals to template length %v", len(seq1), len(template)))
}
for i := range template {
if seq1[i] != template[i] && template[i] != default_wildcard_str {
updated = true
template[i] = default_wildcard_str
}
}
return updated, nil
}
// addSeqToPrefixTree add the logCluster into tree
//
// TODO(qujiabao): refactor the code with a smooth logic
//
// step1: add lengthNode
// step2: for loop each token until maxNodeDepth or latest token
// step3: the children of current node contains the token
// yes -> change the current node and continue loop
// no -> the token has number
// yes -> wildcard node in the children of current node
// yes -> action1: change the current node to wildcard node and continue loop
// no -> action2: create the wildcard
// action1: change the current node to wildcard node and continue loop
// no -> wildcard node in the children of current node
// yes -> the children count of current node is less than maxChildren
// yes -> action3: create a new node with token
// action4: change the current node and continue loop
// no -> action1: change the current node to wildcard node and continue loop
// no -> the children count of current node is less than maxChildren
// less than -> action3: create a new node with token,
// action4: change the current node and continue loop
// equals -> action2: create the wildcard
// action1: change the current node to wildcard node and continue loop
// greater than -> action1: change the current node to wildcard node and continue loop
func (drain *drain) addSeqToPrefixTree(rootNode *treeNode, cluster *LogCluster) {
tokenCount := len(cluster.logTemplateTokens)
lengthNode, ok := rootNode.lengthNodeChildren[tokenCount]
if !ok {
lengthNode = newLengthTreeNode(tokenCount)
rootNode.lengthNodeChildren[tokenCount] = lengthNode
}
currentNode := lengthNode
currentDepth := 1
if tokenCount == 0 {
currentNode.clusters = []*LogCluster{cluster}
}
for _, token := range cluster.logTemplateTokens {
if currentDepth >= drain.getMaxNodeDepth() || currentDepth >= tokenCount {
newClusters := []*LogCluster{}
for _, c := range currentNode.clusters {
if drain.idToCluster.Contains(c.id) {
newClusters = append(newClusters, c)
}
}
newClusters = append(newClusters, cluster)
currentNode.clusters = newClusters
break
}
node, containsInChildren := currentNode.tokenNodeChildren[token]
if containsInChildren {
currentNode = node
} else {
wildcardNode, hasWildcardNode := currentNode.tokenNodeChildren[default_wildcard_str]
if stringHasNumber(token) {
if hasWildcardNode {
currentNode = wildcardNode
} else {
newNode := newTokenTreeNode()
currentNode.tokenNodeChildren[default_wildcard_str] = newNode
currentNode = newNode
}
} else {
if hasWildcardNode {
if len(currentNode.tokenNodeChildren) < drain.maxChildren {
newNode := newTokenTreeNode()
currentNode.tokenNodeChildren[token] = newNode
currentNode = newNode
} else {
currentNode = currentNode.tokenNodeChildren[default_wildcard_str]
}
} else {
if len(currentNode.tokenNodeChildren)+1 < default_max_children {
newNode := newTokenTreeNode()
currentNode.tokenNodeChildren[token] = newNode
currentNode = newNode
} else if len(currentNode.tokenNodeChildren)+1 == default_max_children {
newNode := newTokenTreeNode()
currentNode.tokenNodeChildren[default_wildcard_str] = newNode
currentNode = newNode
} else {
currentNode = currentNode.tokenNodeChildren[default_wildcard_str]
}
}
}
}
currentDepth += 1
}
}
// fastMatch find the best match for a log message (represented as tokens) versus a list of clusters
// :param clusters: List of clusters to match against
// :param tokens: the log message, separated to tokens.
// :return: Best match cluster
func (drain *drain) fastMatch(clusters []*LogCluster, tokens []string, requireSim float32, includeParams bool) *LogCluster {
maxSim := float32(-1)
maxParamCount := int64(-1)
var maxMatchCluster *LogCluster
for _, cluster := range clusters {
if !drain.idToCluster.Contains(cluster.id) {
continue
}
sim, paramCount, err := drain.getSeqDistance(cluster.logTemplateTokens, tokens, includeParams)
if err != nil {
continue
}
if sim > maxSim || (sim == maxSim && paramCount > maxParamCount) {
maxSim = sim
maxParamCount = paramCount
maxMatchCluster = cluster
}
}
if maxSim >= requireSim {
return maxMatchCluster
}
return nil
}
func (drain *drain) getClustersForSeqLen(length int) []*LogCluster {
stack := newTreeNodes()
lengthNode, ok := drain.rootNode.lengthNodeChildren[length]
if !ok {
return []*LogCluster{}
}
stack = stack.push(lengthNode)
clusters := []*LogCluster{}
for {
if len(stack) == 0 {
break
}
var currNode *treeNode
stack, currNode = stack.pop()
if len(currNode.clusters) > 0 {
clusters = append(clusters, currNode.clusters...)
}
for _, child := range currNode.tokenNodeChildren {
stack = stack.push(child)
}
}
return clusters
}
func (drain *drain) getSeqDistance(seq1, seq2 []string, includeParams bool) (float32, int64, error) {
if len(seq1) != len(seq2) {
return 0, 0, errInternalRaw(
fmt.Sprintf("seq1 length %v not equals to seq2 length %v", len(seq1), len(seq2)))
}
if len(seq1) == 0 {
return 1, 0, nil
}
var simTokens int64
var paramCount int64
for i, token1 := range seq1 {
token2 := seq2[i]
if token1 == default_wildcard_str {
paramCount += 1
continue
}
if token1 == token2 {
simTokens += 1
}
}
if includeParams {
simTokens += paramCount
}
retVal := float32(simTokens) / float32(len(seq1))
return retVal, paramCount, nil
}
func newDrain(options ...drainOption) *drain {
conf := newDrainConfig(options)
return newDrainWithConfig(conf)
}
func newDrainWithConfig(conf drainConfig) *drain {
maxCluster := default_max_clusters
if conf.MaxCluster > 0 {
maxCluster = conf.MaxCluster
}
l, _ := lru.New[int64, *LogCluster](maxCluster)
return &drain{
maxDepth: conf.Depth,
sim: conf.Similarity,
maxChildren: conf.MaxChildren,
maxClusters: conf.MaxCluster,
mu: sync.Mutex{},
idToCluster: l,
clusterCounter: 0,
rootNode: newRootTreeNode(),
}
}
func withDepth(depth int) drainOption {
return drainOptionFunc(func(conf drainConfig) drainConfig {
conf.Depth = depth
return conf
})
}
func withSim(sim float32) drainOption {
return drainOptionFunc(func(conf drainConfig) drainConfig {
conf.Similarity = sim
return conf
})
}
func withMaxChildren(maxChildren int) drainOption {
return drainOptionFunc(func(conf drainConfig) drainConfig {
conf.MaxChildren = maxChildren
return conf
})
}
func withMaxClusters(maxCluster int) drainOption {
return drainOptionFunc(func(conf drainConfig) drainConfig {
conf.MaxCluster = maxCluster
return conf
})
}
type drainOption interface {
apply(drainConfig) drainConfig
}
// apply returns a config with option(s) applied.
func (o drainOptionFunc) apply(conf drainConfig) drainConfig {
return o(conf)
}
// drainOptionFunc applies a set of options to a config.
type drainOptionFunc func(drainConfig) drainConfig
// newDrainConfig returns a config configured with options.
func newDrainConfig(options []drainOption) drainConfig {
conf := drainConfig{
Depth: default_max_depth,
Similarity: default_sim,
MaxChildren: default_max_children,
MaxCluster: default_max_clusters,
}
for _, o := range options {
conf = o.apply(conf)
}
return conf
}