|
| 1 | +package ingester |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "math" |
| 6 | + |
| 7 | + "github.com/cortexproject/cortex/pkg/util/validation" |
| 8 | +) |
| 9 | + |
| 10 | +const ( |
| 11 | + errMaxSeriesPerMetricLimitExceeded = "per-metric series limit (local: %d global: %d actual local: %d) exceeded" |
| 12 | + errMaxSeriesPerUserLimitExceeded = "per-user series limit (local: %d global: %d actual local: %d) exceeded" |
| 13 | +) |
| 14 | + |
| 15 | +// RingCount is the interface exposed by a ring implementation which allows |
| 16 | +// to count members |
| 17 | +type RingCount interface { |
| 18 | + HealthyInstancesCount() int |
| 19 | +} |
| 20 | + |
| 21 | +// SeriesLimiter implements primitives to get the maximum number of series |
| 22 | +// an ingester can handle for a specific tenant |
| 23 | +type SeriesLimiter struct { |
| 24 | + limits *validation.Overrides |
| 25 | + ring RingCount |
| 26 | + replicationFactor int |
| 27 | + shardByAllLabels bool |
| 28 | +} |
| 29 | + |
| 30 | +// NewSeriesLimiter makes a new in-memory series limiter |
| 31 | +func NewSeriesLimiter(limits *validation.Overrides, ring RingCount, replicationFactor int, shardByAllLabels bool) *SeriesLimiter { |
| 32 | + return &SeriesLimiter{ |
| 33 | + limits: limits, |
| 34 | + ring: ring, |
| 35 | + replicationFactor: replicationFactor, |
| 36 | + shardByAllLabels: shardByAllLabels, |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +// AssertMaxSeriesPerMetric limit has not been reached compared to the current |
| 41 | +// number of series in input and returns an error if so. |
| 42 | +func (l *SeriesLimiter) AssertMaxSeriesPerMetric(userID string, series int) error { |
| 43 | + actualLimit := l.maxSeriesPerMetric(userID) |
| 44 | + if series < actualLimit { |
| 45 | + return nil |
| 46 | + } |
| 47 | + |
| 48 | + localLimit := l.limits.MaxLocalSeriesPerMetric(userID) |
| 49 | + globalLimit := l.limits.MaxGlobalSeriesPerMetric(userID) |
| 50 | + |
| 51 | + return fmt.Errorf(errMaxSeriesPerMetricLimitExceeded, localLimit, globalLimit, actualLimit) |
| 52 | +} |
| 53 | + |
| 54 | +// AssertMaxSeriesPerUser limit has not been reached compared to the current |
| 55 | +// number of series in input and returns an error if so. |
| 56 | +func (l *SeriesLimiter) AssertMaxSeriesPerUser(userID string, series int) error { |
| 57 | + actualLimit := l.maxSeriesPerUser(userID) |
| 58 | + if series < actualLimit { |
| 59 | + return nil |
| 60 | + } |
| 61 | + |
| 62 | + localLimit := l.limits.MaxLocalSeriesPerUser(userID) |
| 63 | + globalLimit := l.limits.MaxGlobalSeriesPerUser(userID) |
| 64 | + |
| 65 | + return fmt.Errorf(errMaxSeriesPerUserLimitExceeded, localLimit, globalLimit, actualLimit) |
| 66 | +} |
| 67 | + |
| 68 | +// MaxSeriesPerQuery returns the maximum number of series a query is allowed to hit. |
| 69 | +func (l *SeriesLimiter) MaxSeriesPerQuery(userID string) int { |
| 70 | + return l.limits.MaxSeriesPerQuery(userID) |
| 71 | +} |
| 72 | + |
| 73 | +func (l *SeriesLimiter) maxSeriesPerMetric(userID string) int { |
| 74 | + localLimit := l.limits.MaxLocalSeriesPerMetric(userID) |
| 75 | + globalLimit := l.limits.MaxGlobalSeriesPerMetric(userID) |
| 76 | + |
| 77 | + if globalLimit > 0 { |
| 78 | + if l.shardByAllLabels { |
| 79 | + // We can assume that series are evenly distributed across ingesters |
| 80 | + // so we do convert the global limit into a local limit |
| 81 | + localLimit = l.minNonZero(localLimit, l.convertGlobalToLocalLimit(globalLimit)) |
| 82 | + } else { |
| 83 | + // Given a metric is always pushed to the same set of ingesters (based on |
| 84 | + // the replication factor), we can configure the per-ingester local limit |
| 85 | + // equal to the global limit. |
| 86 | + localLimit = l.minNonZero(localLimit, globalLimit) |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + // If both the local and global limits are disabled, we just |
| 91 | + // use the largest int value |
| 92 | + if localLimit == 0 { |
| 93 | + localLimit = math.MaxInt32 |
| 94 | + } |
| 95 | + |
| 96 | + return localLimit |
| 97 | +} |
| 98 | + |
| 99 | +func (l *SeriesLimiter) maxSeriesPerUser(userID string) int { |
| 100 | + localLimit := l.limits.MaxLocalSeriesPerUser(userID) |
| 101 | + |
| 102 | + // The global limit is supported only when shard-by-all-labels is enabled, |
| 103 | + // otherwise we wouldn't get an even split of series across ingesters and |
| 104 | + // can't take a "local decision" without any centralized coordination. |
| 105 | + if l.shardByAllLabels { |
| 106 | + // We can assume that series are evenly distributed across ingesters |
| 107 | + // so we do convert the global limit into a local limit |
| 108 | + globalLimit := l.limits.MaxGlobalSeriesPerUser(userID) |
| 109 | + localLimit = l.minNonZero(localLimit, l.convertGlobalToLocalLimit(globalLimit)) |
| 110 | + } |
| 111 | + |
| 112 | + // If both the local and global limits are disabled, we just |
| 113 | + // use the largest int value |
| 114 | + if localLimit == 0 { |
| 115 | + localLimit = math.MaxInt32 |
| 116 | + } |
| 117 | + |
| 118 | + return localLimit |
| 119 | +} |
| 120 | + |
| 121 | +func (l *SeriesLimiter) convertGlobalToLocalLimit(globalLimit int) int { |
| 122 | + if globalLimit == 0 { |
| 123 | + return 0 |
| 124 | + } |
| 125 | + |
| 126 | + // Given we don't need a super accurate count (ie. when the ingesters |
| 127 | + // topology changes) and we prefer to always be in favor of the tenant, |
| 128 | + // we can use a per-ingester limit equal to: |
| 129 | + // (global limit / number of ingesters) * replication factor |
| 130 | + numIngesters := l.ring.HealthyInstancesCount() |
| 131 | + |
| 132 | + // May happen because the number of ingesters is asynchronously updated. |
| 133 | + // If happens, we just temporarily ignore the global limit. |
| 134 | + if numIngesters > 0 { |
| 135 | + return int((float64(globalLimit) / float64(numIngesters)) * float64(l.replicationFactor)) |
| 136 | + } |
| 137 | + |
| 138 | + return 0 |
| 139 | +} |
| 140 | + |
| 141 | +func (l *SeriesLimiter) minNonZero(first, second int) int { |
| 142 | + if first == 0 || (second != 0 && first > second) { |
| 143 | + return second |
| 144 | + } |
| 145 | + |
| 146 | + return first |
| 147 | +} |
0 commit comments