|
| 1 | +// Unless explicitly stated otherwise all files in this repository are licensed |
| 2 | +// under the Apache License Version 2.0. |
| 3 | +// This product includes software developed at Datadog (https://www.datadoghq.com/). |
| 4 | +// Copyright 2016-present Datadog, Inc. |
| 5 | + |
| 6 | +// Package clustering provides clustering functionality for grouping similar TokenLists, |
| 7 | +// extracting patterns wildcard, and managing pattern lifecycle through eviction policies. |
| 8 | +package clustering |
| 9 | + |
| 10 | +import ( |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/DataDog/datadog-agent/pkg/logs/patterns/eviction" |
| 14 | + "github.com/DataDog/datadog-agent/pkg/logs/patterns/token" |
| 15 | + "github.com/DataDog/datadog-agent/pkg/util/log" |
| 16 | +) |
| 17 | + |
| 18 | +// EvictionPolicy is the policy for evicting patterns from the cluster manager. |
| 19 | +type EvictionPolicy int |
| 20 | + |
| 21 | +const ( |
| 22 | + // EvictionPolicyLFUDecay uses LFU with exponential age decay |
| 23 | + EvictionPolicyLFUDecay EvictionPolicy = iota |
| 24 | +) |
| 25 | + |
| 26 | +// CollectEvictables collects all patterns as evictables (implements eviction.EvictableCollection). |
| 27 | +func (cm *ClusterManager) CollectEvictables() []eviction.Evictable { |
| 28 | + cm.mu.RLock() |
| 29 | + defer cm.mu.RUnlock() |
| 30 | + |
| 31 | + evictables := make([]eviction.Evictable, 0, cm.patternCount) |
| 32 | + for _, clusters := range cm.hashBuckets { |
| 33 | + for _, cluster := range clusters { |
| 34 | + for _, pattern := range cluster.Patterns { |
| 35 | + evictables = append(evictables, pattern) |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + return evictables |
| 40 | +} |
| 41 | + |
| 42 | +// RemoveEvictable removes a specific pattern from the cluster manager (implements eviction.EvictableCollection). |
| 43 | +func (cm *ClusterManager) RemoveEvictable(item eviction.Evictable) { |
| 44 | + pattern := item.(*Pattern) |
| 45 | + cm.removePattern(pattern) |
| 46 | +} |
| 47 | + |
| 48 | +// removePattern removes a specific pattern from the cluster manager's hash buckets. |
| 49 | +func (cm *ClusterManager) removePattern(pattern *Pattern) { |
| 50 | + var ( |
| 51 | + targetHash uint64 |
| 52 | + targetSig *token.Signature |
| 53 | + ) |
| 54 | + |
| 55 | + if pattern.Sample != nil && !pattern.Sample.IsEmpty() { |
| 56 | + sig := token.NewSignature(pattern.Sample) |
| 57 | + targetSig = &sig |
| 58 | + targetHash = sig.GetHashBucket() |
| 59 | + } else if pattern.Template != nil && !pattern.Template.IsEmpty() { |
| 60 | + sig := token.NewSignature(pattern.Template) |
| 61 | + targetSig = &sig |
| 62 | + targetHash = sig.GetHashBucket() |
| 63 | + } |
| 64 | + |
| 65 | + cm.mu.Lock() |
| 66 | + defer cm.mu.Unlock() |
| 67 | + |
| 68 | + removeFromBucket := func(hash uint64, clusters []*Cluster, verifySignature bool) bool { |
| 69 | + for ci := 0; ci < len(clusters); ci++ { |
| 70 | + cluster := clusters[ci] |
| 71 | + if verifySignature && targetSig != nil && !targetSig.Equals(cluster.Signature) { |
| 72 | + continue |
| 73 | + } |
| 74 | + |
| 75 | + for pi := 0; pi < len(cluster.Patterns); pi++ { |
| 76 | + if cluster.Patterns[pi] != pattern { |
| 77 | + continue |
| 78 | + } |
| 79 | + |
| 80 | + // Update counters before removing pattern |
| 81 | + cm.patternCount-- |
| 82 | + cm.estimatedBytes -= pattern.EstimatedBytes() |
| 83 | + |
| 84 | + // Remove pattern with swap delete for speed |
| 85 | + lastPattern := len(cluster.Patterns) - 1 |
| 86 | + cluster.Patterns[pi] = cluster.Patterns[lastPattern] |
| 87 | + cluster.Patterns = cluster.Patterns[:lastPattern] |
| 88 | + cluster.UpdatedAt = time.Now() |
| 89 | + |
| 90 | + if len(cluster.Patterns) == 0 { |
| 91 | + // Remove cluster from bucket |
| 92 | + lastCluster := len(clusters) - 1 |
| 93 | + clusters[ci] = clusters[lastCluster] |
| 94 | + clusters = clusters[:lastCluster] |
| 95 | + |
| 96 | + if len(clusters) == 0 { |
| 97 | + delete(cm.hashBuckets, hash) |
| 98 | + } else { |
| 99 | + cm.hashBuckets[hash] = clusters |
| 100 | + } |
| 101 | + } else { |
| 102 | + cm.hashBuckets[hash] = clusters |
| 103 | + } |
| 104 | + return true |
| 105 | + } |
| 106 | + } |
| 107 | + return false |
| 108 | + } |
| 109 | + |
| 110 | + // Try targeted bucket first |
| 111 | + if targetSig != nil { |
| 112 | + if clusters, ok := cm.hashBuckets[targetHash]; ok { |
| 113 | + if removeFromBucket(targetHash, clusters, true) { |
| 114 | + return |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + // Fallback: full scan if signature was missing or not found in target bucket |
| 120 | + for hash, clusters := range cm.hashBuckets { |
| 121 | + if removeFromBucket(hash, clusters, false) { |
| 122 | + return |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + log.Warnf("Pattern %d not found during eviction, may have been already removed", pattern.PatternID) |
| 127 | +} |
| 128 | + |
| 129 | +// EvictLowestScoringPatterns evicts up to numToEvict patterns with the lowest eviction scores. |
| 130 | +// Returns the list of evicted patterns. |
| 131 | +func (cm *ClusterManager) EvictLowestScoringPatterns(numToEvict int, decayFactor float64) []*Pattern { |
| 132 | + evictables := eviction.EvictLowestScoring(cm, numToEvict, decayFactor) |
| 133 | + if len(evictables) == 0 { |
| 134 | + return nil |
| 135 | + } |
| 136 | + patterns := make([]*Pattern, len(evictables)) |
| 137 | + for i, ev := range evictables { |
| 138 | + patterns[i] = ev.(*Pattern) |
| 139 | + } |
| 140 | + return patterns |
| 141 | +} |
| 142 | + |
| 143 | +// EvictToMemoryTarget evicts patterns until the target memory is freed. |
| 144 | +// It uses actual pattern sizes rather than averages for precision. |
| 145 | +func (cm *ClusterManager) EvictToMemoryTarget(targetBytesToFree int64, decayFactor float64) []*Pattern { |
| 146 | + evictables := eviction.EvictToMemoryTarget(cm, targetBytesToFree, decayFactor) |
| 147 | + if len(evictables) == 0 { |
| 148 | + return nil |
| 149 | + } |
| 150 | + patterns := make([]*Pattern, len(evictables)) |
| 151 | + for i, ev := range evictables { |
| 152 | + patterns[i] = ev.(*Pattern) |
| 153 | + } |
| 154 | + return patterns |
| 155 | +} |
0 commit comments