|
| 1 | +package com.ke.bella.openapi.worker; |
| 2 | + |
| 3 | +import com.ke.bella.openapi.protocol.limiter.LimiterManager; |
| 4 | +import com.ke.bella.openapi.script.LuaScriptExecutor; |
| 5 | +import com.ke.bella.openapi.script.ScriptType; |
| 6 | +import com.ke.bella.openapi.tables.pojos.ChannelDB; |
| 7 | +import com.ke.bella.openapi.utils.DateTimeUtils; |
| 8 | +import com.ke.bella.openapi.utils.JacksonUtils; |
| 9 | +import lombok.AllArgsConstructor; |
| 10 | +import lombok.Data; |
| 11 | +import lombok.extern.slf4j.Slf4j; |
| 12 | +import org.apache.commons.collections4.MapUtils; |
| 13 | +import org.redisson.api.RedissonClient; |
| 14 | + |
| 15 | +import java.util.Arrays; |
| 16 | +import java.util.List; |
| 17 | +import java.util.Map; |
| 18 | +import java.util.Objects; |
| 19 | +import java.util.stream.Collectors; |
| 20 | + |
| 21 | +@Slf4j |
| 22 | +@SuppressWarnings("all") |
| 23 | +public class CapacityCalculator { |
| 24 | + |
| 25 | + private String channelCode; |
| 26 | + private ChannelDB channel; |
| 27 | + private CapacityFittingAlgorithm fittingAlgorithm; |
| 28 | + private RedissonClient redissonClient; |
| 29 | + private LimiterManager limiterManager; |
| 30 | + private LuaScriptExecutor luaScriptExecutor; |
| 31 | + |
| 32 | + private volatile double cachedCapacity = -1.0; |
| 33 | + private volatile long cacheTimestamp = 0; |
| 34 | + private volatile long maxFinishRpm = 0; |
| 35 | + private static final long CACHE_DURATION_MS = 5 * 60 * 1000; |
| 36 | + private static final String RPM_429_HISTORY_METRIC = "rpm_429_history"; |
| 37 | + |
| 38 | + public CapacityCalculator(ChannelDB channelDB, RedissonClient redissonClient) { |
| 39 | + this.channel = channelDB; |
| 40 | + this.channelCode = channelDB.getChannelCode(); |
| 41 | + this.fittingAlgorithm = new EmaFittingAlgorithm(0.3); |
| 42 | + this.redissonClient = redissonClient; |
| 43 | + } |
| 44 | + |
| 45 | + public CapacityCalculator(ChannelDB channelDB, RedissonClient redissonClient, LuaScriptExecutor luaScriptExecutor) { |
| 46 | + this.channel = channelDB; |
| 47 | + this.channelCode = channelDB.getChannelCode(); |
| 48 | + this.fittingAlgorithm = new EmaFittingAlgorithm(0.3); |
| 49 | + this.redissonClient = redissonClient; |
| 50 | + this.luaScriptExecutor = luaScriptExecutor; |
| 51 | + } |
| 52 | + |
| 53 | + public double getRemainingCapacity() { |
| 54 | + double capacity = getCapacity(); |
| 55 | + if(capacity == 0) { |
| 56 | + capacity = 0.7 * getCurrentMaxRpm(); |
| 57 | + } |
| 58 | + if(capacity == 0) { |
| 59 | + return 1.0; |
| 60 | + } |
| 61 | + |
| 62 | + long requestCapacity = getCurrentRequests() + getCompletedRpm(); |
| 63 | + double remainingCapacity = 1.0 - (requestCapacity / capacity); |
| 64 | + return Math.max(0.0, Math.min(1.0, remainingCapacity)); |
| 65 | + } |
| 66 | + |
| 67 | + private long getCurrentMaxRpm() { |
| 68 | + long currentRpm = getCompletedRpm(); |
| 69 | + |
| 70 | + if(currentRpm > maxFinishRpm) { |
| 71 | + maxFinishRpm = currentRpm; |
| 72 | + } |
| 73 | + |
| 74 | + return Math.max(currentRpm, maxFinishRpm); |
| 75 | + } |
| 76 | + |
| 77 | + private long getCompletedRpm() { |
| 78 | + try { |
| 79 | + List<Object> keys = Arrays.asList(channelCode); |
| 80 | + List<Object> args = Arrays.asList(DateTimeUtils.getCurrentSeconds()); |
| 81 | + Object result = luaScriptExecutor.execute("/get_completed", ScriptType.metrics, keys, args); |
| 82 | + |
| 83 | + if(result != null) { |
| 84 | + return Long.parseLong(result.toString()); |
| 85 | + } |
| 86 | + } catch (Exception e) { |
| 87 | + log.warn("Failed to get completed RPM for channel {}: {}", channelCode, e.getMessage()); |
| 88 | + } |
| 89 | + return 0; |
| 90 | + } |
| 91 | + |
| 92 | + private long getCurrentRequests() { |
| 93 | + String concurrentKey = "bella-openapi-channel-concurrent:" + channel.getEntityCode(); |
| 94 | + Object count = redissonClient.getBucket(concurrentKey).get(); |
| 95 | + return count != null ? Long.parseLong(count.toString()) : 0L; |
| 96 | + } |
| 97 | + |
| 98 | + public double getCapacity() { |
| 99 | + long currentTime = System.currentTimeMillis(); |
| 100 | + |
| 101 | + if(cachedCapacity >= 0 && (currentTime - cacheTimestamp) < CACHE_DURATION_MS) { |
| 102 | + return cachedCapacity; |
| 103 | + } |
| 104 | + |
| 105 | + double capacity = computeCapacity(); |
| 106 | + cachedCapacity = capacity; |
| 107 | + cacheTimestamp = currentTime; |
| 108 | + return capacity; |
| 109 | + } |
| 110 | + |
| 111 | + private double computeCapacity() { |
| 112 | + String historyKey = "bella-openapi-channel-metrics:" + channelCode + ":" + RPM_429_HISTORY_METRIC; |
| 113 | + List<MetricsPoint> dataPoints = redissonClient.getList(historyKey).range(0, -1) |
| 114 | + .stream() |
| 115 | + .map(this::parseMetrics) |
| 116 | + .filter(Objects::nonNull) |
| 117 | + .collect(Collectors.toList()); |
| 118 | + |
| 119 | + if(dataPoints.isEmpty()) { |
| 120 | + return 0.0; |
| 121 | + } |
| 122 | + |
| 123 | + return fittingAlgorithm.calculateCapacity(dataPoints); |
| 124 | + } |
| 125 | + |
| 126 | + private MetricsPoint parseMetrics(Object metric) { |
| 127 | + try { |
| 128 | + Map<String, Object> data = JacksonUtils.toMap(metric.toString()); |
| 129 | + Long timestamp = MapUtils.getLong(data, "timestamp"); |
| 130 | + Double rpm = MapUtils.getDouble(data, "rpm"); |
| 131 | + Double avgResponseTime = MapUtils.getDouble(data, "avg_response_time", 60 * 1000.0); |
| 132 | + return new MetricsPoint(timestamp, rpm, avgResponseTime); |
| 133 | + } catch (Exception e) { |
| 134 | + log.warn("Failed to parse data point: {}", metric, e); |
| 135 | + return null; |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + @Data |
| 140 | + @AllArgsConstructor |
| 141 | + public static class MetricsPoint { |
| 142 | + private long timestamp; |
| 143 | + private double rpm; |
| 144 | + private double avgResponseTime; |
| 145 | + } |
| 146 | + |
| 147 | + public interface CapacityFittingAlgorithm { |
| 148 | + double calculateCapacity(List<MetricsPoint> historyData); |
| 149 | + |
| 150 | + String getAlgorithmName(); |
| 151 | + } |
| 152 | + |
| 153 | + public static class EmaFittingAlgorithm implements CapacityFittingAlgorithm { |
| 154 | + private final double alpha; |
| 155 | + |
| 156 | + private static final double DEFAULT_EMA_ALPHA = 0.3; |
| 157 | + private static final int DEFAULT_HISTORY_SIZE = 10; |
| 158 | + private static final int DEFAULT_RECORD_MAX_INTERVAL = 120; |
| 159 | + private static final double DEFAULT_RECORD_CHANGE_THRESHOLD = 0.15; |
| 160 | + private static final int DEFAULT_DECAY_HALF_LIFE = 180; |
| 161 | + private static final double DEFAULT_QUICK_CALC_CURRENT_WEIGHT = 0.3; |
| 162 | + private static final double DEFAULT_QUICK_CALC_DECAYED_WEIGHT = 0.7; |
| 163 | + |
| 164 | + public EmaFittingAlgorithm(double alpha) { |
| 165 | + this.alpha = alpha; |
| 166 | + } |
| 167 | + |
| 168 | + @Override |
| 169 | + public double calculateCapacity(List<MetricsPoint> historyData) { |
| 170 | + if(historyData.isEmpty()) { |
| 171 | + return 0.0; |
| 172 | + } |
| 173 | + |
| 174 | + double rpmEma = historyData.get(0).getRpm(); |
| 175 | + |
| 176 | + for (int i = 1; i < historyData.size(); i++) { |
| 177 | + MetricsPoint point = historyData.get(i); |
| 178 | + rpmEma = alpha * point.getRpm() + (1 - alpha) * rpmEma; |
| 179 | + } |
| 180 | + |
| 181 | + return Math.floor(rpmEma + 0.5); |
| 182 | + } |
| 183 | + |
| 184 | + @Override |
| 185 | + public String getAlgorithmName() { |
| 186 | + return "ema"; |
| 187 | + } |
| 188 | + } |
| 189 | +} |
0 commit comments