异常记录检测修改

This commit is contained in:
super
2026-05-04 19:04:55 +08:00
parent f46c231323
commit 111f30d150
70 changed files with 7182 additions and 252 deletions
@@ -22,4 +22,6 @@ public class ZiniaoShopIndexEntryDto {
private List<Long> sampleUserIds = new ArrayList<>();
private Long lastSeenAt;
private Long lastRefreshedAt;
private Long lastRefreshBlockedAt;
private String refreshBlockedReason;
}
@@ -52,6 +52,7 @@ public class ZiniaoShopIndexService {
private static final Duration DEFAULT_CURSOR_TTL = Duration.ofHours(24);
private static final int DEFAULT_REFRESH_BATCH_SIZE = 100;
private static final int SHOP_INDEX_LIST_LIMIT = 10000;
private static final String REFRESH_BLOCKED_REASON_IP_WHITELIST = "IP_WHITELIST";
private final ZiniaoMemoryStoreService ziniaoMemoryStoreService;
private final ZiniaoTransientCacheService ziniaoTransientCacheService;
@@ -339,6 +340,9 @@ public class ZiniaoShopIndexService {
cursor.setStatus("FAILED");
cursor.setMessage(ex.getMessage());
cursor.setLastFinishedAt(now);
if (isIpWhitelistRefreshFailure(ex)) {
markExistingEntriesRefreshBlocked(now, REFRESH_BLOCKED_REASON_IP_WHITELIST, ex.getMessage());
}
ziniaoTransientCacheService.put(CACHE_TYPE_SHOP_INDEX_REFRESH_CURSOR, "global", cursor, DEFAULT_CURSOR_TTL);
log.warn("[ziniao-index] refresh failed status=FAILED msg={}", ex.getMessage());
if (ex instanceof BusinessException businessException) {
@@ -559,6 +563,11 @@ public class ZiniaoShopIndexService {
if (entry.getLastRefreshedAt() == null || entry.getLastRefreshedAt() <= 0) {
return false;
}
if (REFRESH_BLOCKED_REASON_IP_WHITELIST.equals(entry.getRefreshBlockedReason())
&& entry.getLastRefreshBlockedAt() != null
&& entry.getLastRefreshBlockedAt() >= entry.getLastRefreshedAt()) {
return true;
}
ZiniaoShopIndexRefreshCursorDto cursor = getRefreshCursor();
if (cursor == null || !"FAILED".equals(cursor.getStatus())) {
return false;
@@ -571,6 +580,49 @@ public class ZiniaoShopIndexService {
return lastFinishedAt != null && lastFinishedAt >= entry.getLastRefreshedAt();
}
private boolean isIpWhitelistRefreshFailure(Exception ex) {
if (ex instanceof BusinessException businessException
&& ziniaoAuthService.isIpWhitelistError(businessException)) {
return true;
}
String message = ex == null ? null : ex.getMessage();
return message != null && message.contains("白名单");
}
private void markExistingEntriesRefreshBlocked(long now, String reason, String message) {
List<ZiniaoMemoryStoreEntity> entities = ziniaoMemoryStoreService.listAliveEntitiesByType(
ZiniaoMemoryStoreService.CACHE_TYPE_SHOP_INDEX_ENTRY,
SHOP_INDEX_LIST_LIMIT
);
if (entities.isEmpty()) {
return;
}
int updated = 0;
for (ZiniaoMemoryStoreEntity entity : entities) {
String rowKey = entity.getCacheKey();
ZiniaoShopIndexEntryDto existingEntry;
try {
existingEntry = objectMapper.readValue(entity.getPayloadJson(), ZiniaoShopIndexEntryDto.class);
} catch (Exception ex) {
log.warn("[ziniao-index] skip refresh-block mark corrupt shop_index row cacheKey={}", rowKey);
continue;
}
if (existingEntry == null || !STATUS_ACTIVE.equals(existingEntry.getStatus())) {
continue;
}
existingEntry.setLastRefreshBlockedAt(now);
existingEntry.setRefreshBlockedReason(reason);
if (message != null && !message.isBlank()) {
existingEntry.setMessage(message);
}
ziniaoMemoryStoreService.put(ZiniaoMemoryStoreService.CACHE_TYPE_SHOP_INDEX_ENTRY, rowKey, existingEntry, resolveEntryTtl());
updated++;
}
if (updated > 0) {
log.warn("[ziniao-index] refresh blocked reason={} existing active rows kept usable count={}", reason, updated);
}
}
private Duration resolveEntryTtl() {
Integer ttlHours = ziniaoProperties.getShopIndexEntryTtlHours();
if (ttlHours == null || ttlHours <= 0) {