Junjie
2026-04-27 dc1078bcd01c3b650163cf45553bd098a85a4c82
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
package com.zy.core.thread.impl.v5;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.core.common.Cools;
import com.zy.common.utils.RedisUtil;
import com.zy.core.enums.RedisKeyType;
import com.zy.system.entity.Config;
import com.zy.system.service.ConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.util.HashMap;
 
@Slf4j
@Component
public class StationV5RuntimeConfigProvider {
 
    private static final String CFG_SEGMENT_ADVANCE_RATIO = "stationCommandSegmentAdvanceRatio";
    private static final String CFG_SEGMENT_EXECUTOR_POOL_SIZE = "stationV5SegmentExecutorPoolSize";
    private static final String CFG_SEGMENT_EXECUTOR_QUEUE_CAPACITY = "stationV5SegmentExecutorQueueCapacity";
    private static final String CFG_CONFIG_REFRESH_SECONDS = "stationCommandConfigRefreshSeconds";
 
    private static final double DEFAULT_SEGMENT_ADVANCE_RATIO = 0.3d;
    private static final int DEFAULT_SEGMENT_EXECUTOR_POOL_SIZE = 128;
    private static final int DEFAULT_SEGMENT_EXECUTOR_QUEUE_CAPACITY = 512;
    private static final int DEFAULT_CONFIG_REFRESH_SECONDS = 30;
 
    @Autowired(required = false)
    private RedisUtil redisUtil;
    @Autowired(required = false)
    private ConfigService configService;
 
    private volatile CacheSnapshot cacheSnapshot = new CacheSnapshot(
            DEFAULT_SEGMENT_ADVANCE_RATIO,
            DEFAULT_SEGMENT_EXECUTOR_POOL_SIZE,
            DEFAULT_SEGMENT_EXECUTOR_QUEUE_CAPACITY,
            DEFAULT_CONFIG_REFRESH_SECONDS,
            0L
    );
 
    public double getSegmentAdvanceRatio() {
        return loadSnapshot().segmentAdvanceRatio;
    }
 
    public int getSegmentExecutorPoolSize() {
        return loadSnapshot().segmentExecutorPoolSize;
    }
 
    public int getSegmentExecutorQueueCapacity() {
        return loadSnapshot().segmentExecutorQueueCapacity;
    }
 
    private CacheSnapshot loadSnapshot() {
        CacheSnapshot snapshot = cacheSnapshot;
        long now = System.currentTimeMillis();
        if (now - snapshot.loadedAtMs <= snapshot.refreshSeconds * 1000L) {
            return snapshot;
        }
        synchronized (this) {
            snapshot = cacheSnapshot;
            now = System.currentTimeMillis();
            if (now - snapshot.loadedAtMs <= snapshot.refreshSeconds * 1000L) {
                return snapshot;
            }
            CacheSnapshot refreshed = refreshSnapshot(now);
            cacheSnapshot = refreshed;
            return refreshed;
        }
    }
 
    @SuppressWarnings("unchecked")
    private CacheSnapshot refreshSnapshot(long now) {
        HashMap<String, String> systemConfigMap = null;
        try {
            Object systemConfigMapObj = redisUtil == null ? null : redisUtil.get(RedisKeyType.SYSTEM_CONFIG_MAP.key);
            if (systemConfigMapObj instanceof HashMap) {
                systemConfigMap = (HashMap<String, String>) systemConfigMapObj;
            }
        } catch (Exception e) {
            log.warn("加载 V5 输送运行时配置缓存失败,fallback to db. reason=redis-read-error", e);
        }
 
        int refreshSeconds = normalizeRefreshSeconds(readConfigText(systemConfigMap, CFG_CONFIG_REFRESH_SECONDS));
        double segmentAdvanceRatio = normalizeSegmentAdvanceRatio(readConfigText(systemConfigMap, CFG_SEGMENT_ADVANCE_RATIO));
        int poolSize = normalizePoolSize(readConfigText(systemConfigMap, CFG_SEGMENT_EXECUTOR_POOL_SIZE));
        int queueCapacity = normalizeQueueCapacity(readConfigText(systemConfigMap, CFG_SEGMENT_EXECUTOR_QUEUE_CAPACITY));
 
        if (systemConfigMap == null) {
            segmentAdvanceRatio = normalizeSegmentAdvanceRatio(readConfigTextFromDb(CFG_SEGMENT_ADVANCE_RATIO), segmentAdvanceRatio);
            poolSize = normalizePoolSize(readConfigTextFromDb(CFG_SEGMENT_EXECUTOR_POOL_SIZE), poolSize);
            queueCapacity = normalizeQueueCapacity(readConfigTextFromDb(CFG_SEGMENT_EXECUTOR_QUEUE_CAPACITY), queueCapacity);
            refreshSeconds = normalizeRefreshSeconds(readConfigTextFromDb(CFG_CONFIG_REFRESH_SECONDS), refreshSeconds);
        }
        return new CacheSnapshot(segmentAdvanceRatio, poolSize, queueCapacity, refreshSeconds, now);
    }
 
    private String readConfigText(HashMap<String, String> systemConfigMap, String code) {
        if (systemConfigMap == null || code == null) {
            return null;
        }
        return systemConfigMap.get(code);
    }
 
    private String readConfigTextFromDb(String code) {
        if (configService == null || code == null) {
            return null;
        }
        try {
            Config config = configService.getOne(new QueryWrapper<Config>().eq("code", code));
            return config == null ? null : config.getValue();
        } catch (Exception e) {
            log.warn("加载 V5 输送运行时配置数据库失败,code={}", code, e);
            return null;
        }
    }
 
    private double normalizeSegmentAdvanceRatio(String valueText) {
        return normalizeSegmentAdvanceRatio(valueText, DEFAULT_SEGMENT_ADVANCE_RATIO);
    }
 
    private double normalizeSegmentAdvanceRatio(String valueText, double defaultValue) {
        if (valueText == null) {
            return defaultValue;
        }
        String text = valueText.trim();
        if (text.isEmpty()) {
            return defaultValue;
        }
        if (text.endsWith("%")) {
            text = text.substring(0, text.length() - 1).trim();
        }
        try {
            double ratio = Double.parseDouble(text);
            if (ratio > 1d && ratio <= 100d) {
                ratio = ratio / 100d;
            }
            if (ratio < 0d) {
                return 0d;
            }
            if (ratio > 1d) {
                return 1d;
            }
            return ratio;
        } catch (Exception ignore) {
            return defaultValue;
        }
    }
 
    private int normalizePoolSize(String valueText) {
        return normalizePoolSize(valueText, DEFAULT_SEGMENT_EXECUTOR_POOL_SIZE);
    }
 
    private int normalizePoolSize(String valueText, int defaultValue) {
        int configured = parsePositiveInt(valueText, defaultValue);
        if (configured < 16) {
            return 16;
        }
        return Math.min(configured, 512);
    }
 
    private int normalizeQueueCapacity(String valueText) {
        return normalizeQueueCapacity(valueText, DEFAULT_SEGMENT_EXECUTOR_QUEUE_CAPACITY);
    }
 
    private int normalizeQueueCapacity(String valueText, int defaultValue) {
        int configured = parsePositiveInt(valueText, defaultValue);
        if (configured < 64) {
            return 64;
        }
        return Math.min(configured, 4096);
    }
 
    private int normalizeRefreshSeconds(String valueText) {
        return normalizeRefreshSeconds(valueText, DEFAULT_CONFIG_REFRESH_SECONDS);
    }
 
    private int normalizeRefreshSeconds(String valueText, int defaultValue) {
        int configured = parsePositiveInt(valueText, defaultValue);
        if (configured < 5) {
            return 5;
        }
        return Math.min(configured, 300);
    }
 
    private int parsePositiveInt(String valueText, int defaultValue) {
        if (Cools.isEmpty(valueText)) {
            return defaultValue;
        }
        try {
            return Integer.parseInt(valueText.trim());
        } catch (Exception ignore) {
            return defaultValue;
        }
    }
 
    private static class CacheSnapshot {
        private final double segmentAdvanceRatio;
        private final int segmentExecutorPoolSize;
        private final int segmentExecutorQueueCapacity;
        private final int refreshSeconds;
        private final long loadedAtMs;
 
        private CacheSnapshot(double segmentAdvanceRatio,
                              int segmentExecutorPoolSize,
                              int segmentExecutorQueueCapacity,
                              int refreshSeconds,
                              long loadedAtMs) {
            this.segmentAdvanceRatio = segmentAdvanceRatio;
            this.segmentExecutorPoolSize = segmentExecutorPoolSize;
            this.segmentExecutorQueueCapacity = segmentExecutorQueueCapacity;
            this.refreshSeconds = refreshSeconds;
            this.loadedAtMs = loadedAtMs;
        }
    }
}