|
| 1 | +package ingester |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "sync" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/prometheus/common/model" |
| 9 | + |
| 10 | + phlaremodel "github.com/grafana/phlare/pkg/model" |
| 11 | + "github.com/grafana/phlare/pkg/validation" |
| 12 | +) |
| 13 | + |
| 14 | +var ( |
| 15 | + activeSeriesTimeout = 10 * time.Minute |
| 16 | + activeSeriesCleanup = time.Minute |
| 17 | +) |
| 18 | + |
| 19 | +type RingCount interface { |
| 20 | + HealthyInstancesCount() int |
| 21 | +} |
| 22 | + |
| 23 | +type Limits interface { |
| 24 | + MaxLocalSeriesPerTenant(tenantID string) int |
| 25 | + MaxGlobalSeriesPerTenant(tenantID string) int |
| 26 | +} |
| 27 | + |
| 28 | +type Limiter interface { |
| 29 | + // AllowProfile returns an error if the profile is not allowed to be ingested. |
| 30 | + // The error is a validation error and can be out of order or max series limit reached. |
| 31 | + AllowProfile(fp model.Fingerprint, lbs phlaremodel.Labels, tsNano int64) error |
| 32 | + Stop() |
| 33 | +} |
| 34 | + |
| 35 | +type limiter struct { |
| 36 | + limits Limits |
| 37 | + ring RingCount |
| 38 | + replicationFactor int |
| 39 | + tenantID string |
| 40 | + |
| 41 | + activeSeries map[model.Fingerprint]int64 |
| 42 | + lastTimestamp map[model.Fingerprint]int64 |
| 43 | + |
| 44 | + mtx sync.Mutex // todo: may be shard the lock to avoid latency spikes. |
| 45 | + |
| 46 | + ctx context.Context |
| 47 | + cancel context.CancelFunc |
| 48 | + wg sync.WaitGroup |
| 49 | +} |
| 50 | + |
| 51 | +func NewLimiter(tenantID string, limits Limits, ring RingCount, replicationFactor int) Limiter { |
| 52 | + ctx, cancel := context.WithCancel(context.Background()) |
| 53 | + |
| 54 | + l := &limiter{ |
| 55 | + tenantID: tenantID, |
| 56 | + limits: limits, |
| 57 | + ring: ring, |
| 58 | + replicationFactor: replicationFactor, |
| 59 | + activeSeries: map[model.Fingerprint]int64{}, |
| 60 | + lastTimestamp: map[model.Fingerprint]int64{}, |
| 61 | + cancel: cancel, |
| 62 | + ctx: ctx, |
| 63 | + } |
| 64 | + |
| 65 | + l.wg.Add(1) |
| 66 | + go l.loop() |
| 67 | + |
| 68 | + return l |
| 69 | +} |
| 70 | + |
| 71 | +func (l *limiter) Stop() { |
| 72 | + l.cancel() |
| 73 | + l.wg.Wait() |
| 74 | +} |
| 75 | + |
| 76 | +func (l *limiter) loop() { |
| 77 | + defer l.wg.Done() |
| 78 | + |
| 79 | + ticker := time.NewTicker(activeSeriesCleanup) |
| 80 | + defer ticker.Stop() |
| 81 | + |
| 82 | + for { |
| 83 | + select { |
| 84 | + case <-ticker.C: |
| 85 | + l.cleanup() |
| 86 | + case <-l.ctx.Done(): |
| 87 | + return |
| 88 | + } |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +// cleanup removes the series that have not been used for a while. |
| 93 | +func (l *limiter) cleanup() { |
| 94 | + now := time.Now().UnixNano() |
| 95 | + l.mtx.Lock() |
| 96 | + defer l.mtx.Unlock() |
| 97 | + |
| 98 | + for fp, lastUsed := range l.activeSeries { |
| 99 | + if now-lastUsed > int64(activeSeriesTimeout) { |
| 100 | + delete(l.activeSeries, fp) |
| 101 | + } |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +func (l *limiter) AllowProfile(fp model.Fingerprint, lbs phlaremodel.Labels, tsNano int64) error { |
| 106 | + l.mtx.Lock() |
| 107 | + defer l.mtx.Unlock() |
| 108 | + if err := l.allowNewProfile(fp, lbs, tsNano); err != nil { |
| 109 | + return err |
| 110 | + } |
| 111 | + return l.allowNewSeries(fp) |
| 112 | +} |
| 113 | + |
| 114 | +func (l *limiter) allowNewProfile(fp model.Fingerprint, lbs phlaremodel.Labels, tsNano int64) error { |
| 115 | + max, ok := l.lastTimestamp[fp] |
| 116 | + if ok { |
| 117 | + // profile is before the last timestamp |
| 118 | + if tsNano < max { |
| 119 | + return validation.NewErrorf(validation.OutOfOrder, "profile for series %s out of order (received %s last %s)", phlaremodel.LabelPairsString(lbs), time.Unix(0, tsNano), time.Unix(0, max)) |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + // set the last timestamp |
| 124 | + l.lastTimestamp[fp] = tsNano |
| 125 | + return nil |
| 126 | +} |
| 127 | + |
| 128 | +func (l *limiter) allowNewSeries(fp model.Fingerprint) error { |
| 129 | + _, ok := l.activeSeries[fp] |
| 130 | + series := len(l.activeSeries) |
| 131 | + if !ok { |
| 132 | + // can this series be added? |
| 133 | + if err := l.assertMaxSeriesPerUser(l.tenantID, series); err != nil { |
| 134 | + return err |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + // update time or add it |
| 139 | + l.activeSeries[fp] = time.Now().UnixNano() |
| 140 | + return nil |
| 141 | +} |
| 142 | + |
| 143 | +func (l *limiter) assertMaxSeriesPerUser(tenantID string, series int) error { |
| 144 | + // Start by setting the local limit either from override or default |
| 145 | + localLimit := l.limits.MaxLocalSeriesPerTenant(tenantID) |
| 146 | + |
| 147 | + // We can assume that series are evenly distributed across ingesters |
| 148 | + // so we do convert the global limit into a local limit |
| 149 | + globalLimit := l.limits.MaxGlobalSeriesPerTenant(tenantID) |
| 150 | + adjustedGlobalLimit := convertGlobalToLocalLimit(globalLimit, l.ring, l.replicationFactor) |
| 151 | + |
| 152 | + // Set the calculated limit to the lesser of the local limit or the new calculated global limit |
| 153 | + calculatedLimit := minNonZero(localLimit, adjustedGlobalLimit) |
| 154 | + |
| 155 | + // If both the local and global limits are disabled, we just |
| 156 | + // use the largest int value |
| 157 | + if calculatedLimit == 0 { |
| 158 | + return nil |
| 159 | + } |
| 160 | + |
| 161 | + if series < calculatedLimit { |
| 162 | + return nil |
| 163 | + } |
| 164 | + return validation.NewErrorf(validation.SeriesLimit, validation.SeriesLimitErrorMsg, series, calculatedLimit) |
| 165 | +} |
| 166 | + |
| 167 | +func convertGlobalToLocalLimit(globalLimit int, ringCount RingCount, replicationFactor int) int { |
| 168 | + if globalLimit == 0 { |
| 169 | + return 0 |
| 170 | + } |
| 171 | + |
| 172 | + // Given we don't need a super accurate count (ie. when the ingesters |
| 173 | + // topology changes) and we prefer to always be in favor of the tenant, |
| 174 | + // we can use a per-ingester limit equal to: |
| 175 | + // (global limit / number of ingesters) * replication factor |
| 176 | + numIngesters := ringCount.HealthyInstancesCount() |
| 177 | + |
| 178 | + // May happen because the number of ingesters is asynchronously updated. |
| 179 | + // If happens, we just temporarily ignore the global limit. |
| 180 | + if numIngesters > 0 { |
| 181 | + return int((float64(globalLimit) / float64(numIngesters)) * float64(replicationFactor)) |
| 182 | + } |
| 183 | + |
| 184 | + return 0 |
| 185 | +} |
| 186 | + |
| 187 | +func minNonZero(first, second int) int { |
| 188 | + if first == 0 || (second != 0 && first > second) { |
| 189 | + return second |
| 190 | + } |
| 191 | + |
| 192 | + return first |
| 193 | +} |
0 commit comments