All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 14m13s
新增 000228 成对迁移 tb_polling_priority_item:卡、任务类型、状态、触发类型、来源订单/套餐使用记录、
触发次数与来源集合、尝试次数、失败原因、人工原因与操作者、店铺快照与各时间列;以活动项部分唯一索引
uq_polling_priority_item_active(仅 deleted_at IS NULL AND status IN ('pending','processing') 占键位)
表达「同卡同任务类型至多一条活动项」,另有状态/时间索引与全列注释;down 守卫在存在活动项或未终态行时
拒绝回滚并给出中文原因。
新增优先轮询请求可靠事件 polling.priority.requested(载荷版本 v1、事件键前缀 prio:)与消费者:只在原
业务事务内追加、幂等键稳定;消费者按卡 × 纳入任务类型(realname/carddata/card_status/package)逐条
建项并在提交后下发执行提示,重复投递只合并触发次数、来源集合与最近触发时间,不新建行也不重复调用。
触发点为四类自动场景 purchase_activated / renewal_activated(按同载体更早套餐使用记录判定)/
queue_activated / addon_activated 与「无有效套餐」no_valid_package(仅在普通套餐轮询来源且存在待生效
套餐使用记录时追加;事件通道显式拒绝 manual_trigger);入队对象恒为卡,绑定设备资产在触发事务内冻结
在用卡快照逐卡建项,不使用设备当前卡槽口径。
轮询共享基类新增认领接缝:四个 Handler(realname/carddata/card_status/package)在并发信号量之后、调用
上游之前探测活动项——待执行条件认领、执行中且 90 秒租约未到期则跳过并延后、无活动项时行为与既有完全
等价;超租约允许相邻执行接管,尝试次数只在真正发起执行后累加,未达上限(3)回到活动态按既有间隔重排,
达上限或业务校验类失败进入失败终态并保留可安全展示原因;执行前校验卡自身与绑定设备的轮询开关。未引入
通用卡级锁与 Redis 活动标记,分片队列的出队、入队与移除路径未改动。
提示通道按任务类型独立键(polling:priority:{taskType}),与既有手动触发队列分离;调度器在同一周期内先
排空优先提示、再排空手动触发队列,提示排空不受分片背压跳过影响;未新建调度设施或异步任务类型。
新增人工优先入队与只读查询三条路由 POST /api/admin/polling-priority-items、
GET /api/admin/polling-priority-items、GET /api/admin/polling-priority-items/:id:人工入队复用既有轮询
权限判定(抽取为同包共享函数),原因必填,不受每日 500 次上限与 24 小时去重约束,重复抑制由活动项合并
承担;读侧按店铺快照下推数据范围,越权与不存在不可区分,不提供优先级分级、有效期或人工重触发入口。
新增 7 个审计动作(enqueue/claim/fail/retry/complete/dequeue/manual_denied)与资源
polling_priority_item,并按(操作者类型,来源)注册,人工侧与 Worker 侧均通过来源校验。
同步 OpenAPI 文档装配三处与路由注册;归档 Change 至
openspec/changes/archive/2026-09-17-add-priority-polling-queue/ 并同步主 Spec(新增
priority-polling-queue、polling-operations 追加单次执行互斥 Requirement 与三条路由索引)与上下文健康
证据(requirement-evidence 150 行、入口矩阵 http 403 / async 56)。
本机验证:junhong_cmp_test 与隔离 Redis DB 15,未连生产、未启动 Worker/API、未调用运营商上游;迁移
up/down/up 与 down 守卫实测(含 dirty=true 记账口径与 force 恢复),A–F 批 94 PASS、接缝 63 PASS、
提示通道 12 PASS、清理零残留 20 PASS。成功路径 Complete、真并发互斥、尝试上限第 3 次判定、HTTP 层权限
矩阵、通道阈值持锁复机边界与三类生效触发点生产集成留待测试部署验证(见
docs/verification/add-priority-polling-queue-verification.md 第 4 节)。自动化测试按项目决策为 N/A,
未新增 *_test.go。
274 lines
9.6 KiB
Go
274 lines
9.6 KiB
Go
package polling
|
||
|
||
import (
|
||
"context"
|
||
"fmt"
|
||
"strconv"
|
||
"time"
|
||
|
||
"github.com/redis/go-redis/v9"
|
||
"go.uber.org/zap"
|
||
|
||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||
)
|
||
|
||
// allTaskTypes 轮询系统的全部任务类型(用于 RemoveFromAllQueues 遍历)
|
||
var allTaskTypes = []string{
|
||
constants.TaskTypePollingRealname,
|
||
constants.TaskTypePollingCarddata,
|
||
constants.TaskTypePollingPackage,
|
||
constants.TaskTypePollingProtect,
|
||
constants.TaskTypePollingCardStatus,
|
||
}
|
||
|
||
// dequeueScript Lua 脚本:原子出队(ZRANGEBYSCORE + ZREM 服务端原子执行)
|
||
// 保留时间过滤语义:只取 score ≤ now 的到期卡,不触碰未来项
|
||
// 分批 ZREM:Lua unpack() 受 LUAI_MAXCSTACK 约 8000 限制,按 7000 分批避免溢出
|
||
var dequeueScript = redis.NewScript(`
|
||
local results = redis.call('ZRANGEBYSCORE', KEYS[1], '-inf', ARGV[1], 'LIMIT', 0, tonumber(ARGV[2]))
|
||
for i = 1, #results, 7000 do
|
||
local j = math.min(i + 6999, #results)
|
||
redis.call('ZREM', KEYS[1], unpack(results, i, j))
|
||
end
|
||
return results
|
||
`)
|
||
|
||
// CardEntry 出队卡信息
|
||
type CardEntry struct {
|
||
CardID uint
|
||
}
|
||
|
||
// PollingQueueManager 统一 Redis 轮询队列操作
|
||
// 两个进程(API 进程和 Worker 进程)共享,仅依赖 Redis Client
|
||
// 支持分片 Sorted Set,实现千万级规模
|
||
type PollingQueueManager struct {
|
||
redis *redis.Client
|
||
shardCount int
|
||
logger *zap.Logger
|
||
}
|
||
|
||
// NewPollingQueueManager 创建轮询队列管理器
|
||
func NewPollingQueueManager(redisClient *redis.Client, shardCount int, logger *zap.Logger) *PollingQueueManager {
|
||
if shardCount <= 0 {
|
||
shardCount = constants.PollingShardCount
|
||
}
|
||
return &PollingQueueManager{
|
||
redis: redisClient,
|
||
shardCount: shardCount,
|
||
logger: logger,
|
||
}
|
||
}
|
||
|
||
// DequeueReady 原子出队到期卡(Lua 脚本:ZRANGEBYSCORE + ZREM 服务端原子执行)
|
||
// 只取 score ≤ now 的到期卡,不触碰未来项
|
||
// taskType: realname | carddata | package | protect
|
||
// shardID: 0 到 shardCount-1
|
||
func (m *PollingQueueManager) DequeueReady(ctx context.Context, shardID int, taskType string, batchSize int) ([]CardEntry, error) {
|
||
// 防御:batchSize 不超过 Lua unpack 栈限制
|
||
if batchSize <= 0 || batchSize > constants.PollingDequeueMaxBatchSize {
|
||
batchSize = constants.PollingDequeueMaxBatchSize
|
||
}
|
||
key := constants.RedisPollingShardQueueKey(shardID, taskType)
|
||
now := time.Now().Unix()
|
||
|
||
results, err := dequeueScript.Run(ctx, m.redis, []string{key}, now, batchSize).StringSlice()
|
||
if err != nil && err != redis.Nil {
|
||
return nil, err
|
||
}
|
||
|
||
entries := make([]CardEntry, 0, len(results))
|
||
for _, s := range results {
|
||
id, parseErr := strconv.ParseUint(s, 10, 64)
|
||
if parseErr != nil {
|
||
m.logger.Warn("解析卡ID失败", zap.String("value", s), zap.Error(parseErr))
|
||
continue
|
||
}
|
||
entries = append(entries, CardEntry{CardID: uint(id)})
|
||
}
|
||
return entries, nil
|
||
}
|
||
|
||
// Requeue 将卡重新入队(ZADD,score 为下次检查时间戳)
|
||
func (m *PollingQueueManager) Requeue(ctx context.Context, cardID uint, taskType string, nextCheckAt time.Time) error {
|
||
shardID := int(cardID) % m.shardCount
|
||
key := constants.RedisPollingShardQueueKey(shardID, taskType)
|
||
return m.redis.ZAdd(ctx, key, redis.Z{
|
||
Score: float64(nextCheckAt.Unix()),
|
||
Member: fmt.Sprintf("%d", cardID),
|
||
}).Err()
|
||
}
|
||
|
||
// EnsureQueued 仅在任务当前不在分片队列中时补入任务,不覆盖已有任务的执行时间。
|
||
func (m *PollingQueueManager) EnsureQueued(ctx context.Context, cardID uint, taskType string, nextCheckAt time.Time) (bool, error) {
|
||
shardID := int(cardID) % m.shardCount
|
||
key := constants.RedisPollingShardQueueKey(shardID, taskType)
|
||
added, err := m.redis.ZAddArgs(ctx, key, redis.ZAddArgs{
|
||
NX: true,
|
||
Members: []redis.Z{{
|
||
Score: float64(nextCheckAt.Unix()),
|
||
Member: fmt.Sprintf("%d", cardID),
|
||
}},
|
||
}).Result()
|
||
return added > 0, err
|
||
}
|
||
|
||
// RemoveFromAllQueues 从所有分片的所有5个队列(realname/carddata/package/protect/card_status)移除指定卡
|
||
// 修复 Bug3:旧实现漏掉 protect 队列
|
||
func (m *PollingQueueManager) RemoveFromAllQueues(ctx context.Context, cardID uint) error {
|
||
member := fmt.Sprintf("%d", cardID)
|
||
pipe := m.redis.Pipeline()
|
||
for i := 0; i < m.shardCount; i++ {
|
||
for _, taskType := range allTaskTypes {
|
||
key := constants.RedisPollingShardQueueKey(i, taskType)
|
||
pipe.ZRem(ctx, key, member)
|
||
}
|
||
}
|
||
_, err := pipe.Exec(ctx)
|
||
return err
|
||
}
|
||
|
||
// RemoveBatchFromCurrentShardQueues 批量移除卡在当前分片队列中的旧成员,并清理卡缓存。
|
||
// 迁移收尾重建队列前使用,避免重复执行时残留旧任务类型或旧缓存。
|
||
func (m *PollingQueueManager) RemoveBatchFromCurrentShardQueues(ctx context.Context, cardIDs []uint) error {
|
||
if len(cardIDs) == 0 {
|
||
return nil
|
||
}
|
||
pipe := m.redis.Pipeline()
|
||
for _, cardID := range cardIDs {
|
||
member := fmt.Sprintf("%d", cardID)
|
||
shardID := int(cardID) % m.shardCount
|
||
for _, taskType := range allTaskTypes {
|
||
key := constants.RedisPollingShardQueueKey(shardID, taskType)
|
||
pipe.ZRem(ctx, key, member)
|
||
}
|
||
pipe.Del(ctx, constants.RedisPollingCardInfoKey(cardID))
|
||
}
|
||
_, err := pipe.Exec(ctx)
|
||
return err
|
||
}
|
||
|
||
// EnqueueManual 手动触发入队(List RPUSH,调度器优先消费)
|
||
func (m *PollingQueueManager) EnqueueManual(ctx context.Context, cardID uint, taskType string) error {
|
||
key := constants.RedisPollingManualQueueKey(taskType)
|
||
return m.redis.RPush(ctx, key, fmt.Sprintf("%d", cardID)).Err()
|
||
}
|
||
|
||
// EnqueuePriority 优先轮询提示下发(List RPUSH,与手动触发队列分离)。
|
||
// 使用 RPUSH 与调度器的 LPopCount 组成先进先出;提示通道不是权威,丢失或缓存服务重启时
|
||
// 库内活动优先项仍在,后续普通轮询执行按条件认领仍会执行,只退化为延迟一个普通轮询周期。
|
||
func (m *PollingQueueManager) EnqueuePriority(ctx context.Context, cardID uint, taskType string) error {
|
||
key := constants.RedisPollingPriorityQueueKey(taskType)
|
||
return m.redis.RPush(ctx, key, fmt.Sprintf("%d", cardID)).Err()
|
||
}
|
||
|
||
// OnCardDeleted 卡删除事件处理(移除所有队列 + 清理卡信息缓存)
|
||
func (m *PollingQueueManager) OnCardDeleted(ctx context.Context, cardID uint) error {
|
||
// 从所有分片队列移除
|
||
if err := m.RemoveFromAllQueues(ctx, cardID); err != nil {
|
||
return err
|
||
}
|
||
// 清理轮询卡信息缓存
|
||
cacheKey := constants.RedisPollingCardInfoKey(cardID)
|
||
return m.redis.Del(ctx, cacheKey).Err()
|
||
}
|
||
|
||
// InvalidateCardCache 清理轮询卡信息缓存,强制下次轮询从 DB 重建
|
||
func (m *PollingQueueManager) InvalidateCardCache(ctx context.Context, cardID uint) {
|
||
cacheKey := constants.RedisPollingCardInfoKey(cardID)
|
||
if err := m.redis.Del(ctx, cacheKey).Err(); err != nil {
|
||
m.logger.Warn("清理轮询卡缓存失败", zap.Uint("card_id", cardID), zap.Error(err))
|
||
}
|
||
}
|
||
|
||
// GetQueueDepth 获取分片队列深度(用于背压检测)
|
||
func (m *PollingQueueManager) GetQueueDepth(ctx context.Context, shardID int, taskType string) (int64, error) {
|
||
key := constants.RedisPollingShardQueueKey(shardID, taskType)
|
||
return m.redis.ZCard(ctx, key).Result()
|
||
}
|
||
|
||
// GetTotalQueueDepth 获取指定任务类型的总队列深度(聚合所有分片)
|
||
// 供 MonitoringService 使用,替代直接读取旧的非分片 Redis Key
|
||
// 若任意分片查询失败,返回已累计的部分总量和第一个错误,调用方可据此判断数据完整性
|
||
func (m *PollingQueueManager) GetTotalQueueDepth(ctx context.Context, taskType string) (int64, error) {
|
||
var total int64
|
||
var firstErr error
|
||
for i := 0; i < m.shardCount; i++ {
|
||
depth, err := m.GetQueueDepth(ctx, i, taskType)
|
||
if err != nil {
|
||
m.logger.Warn("获取分片队列深度失败",
|
||
zap.Int("shard_id", i),
|
||
zap.String("task_type", taskType),
|
||
zap.Error(err))
|
||
if firstErr == nil {
|
||
firstErr = err
|
||
}
|
||
continue
|
||
}
|
||
total += depth
|
||
}
|
||
return total, firstErr
|
||
}
|
||
|
||
// GetTotalDueCount 获取指定任务类型所有分片中已到期的队列数量。
|
||
// 只统计 score <= now 的成员,用于后台监控展示真实待调度积压。
|
||
func (m *PollingQueueManager) GetTotalDueCount(ctx context.Context, taskType string, now time.Time) (int64, error) {
|
||
var total int64
|
||
var firstErr error
|
||
for i := 0; i < m.shardCount; i++ {
|
||
key := constants.RedisPollingShardQueueKey(i, taskType)
|
||
count, err := m.redis.ZCount(ctx, key, "-inf", fmt.Sprintf("%d", now.Unix())).Result()
|
||
if err != nil {
|
||
m.logger.Warn("获取分片到期数量失败",
|
||
zap.Int("shard_id", i),
|
||
zap.String("task_type", taskType),
|
||
zap.Error(err))
|
||
if firstErr == nil {
|
||
firstErr = err
|
||
}
|
||
continue
|
||
}
|
||
total += count
|
||
}
|
||
return total, firstErr
|
||
}
|
||
|
||
// GetAverageWaitTime 获取指定任务类型所有分片最早若干任务的平均等待秒数。
|
||
// 每个分片最多取 samplePerShard 条,避免监控接口扫描完整队列。
|
||
func (m *PollingQueueManager) GetAverageWaitTime(ctx context.Context, taskType string, now time.Time, samplePerShard int64) (float64, error) {
|
||
if samplePerShard <= 0 {
|
||
samplePerShard = 10
|
||
}
|
||
|
||
var totalWait float64
|
||
var sampleCount int64
|
||
var firstErr error
|
||
nowUnix := now.Unix()
|
||
|
||
for i := 0; i < m.shardCount; i++ {
|
||
key := constants.RedisPollingShardQueueKey(i, taskType)
|
||
earliest, err := m.redis.ZRangeWithScores(ctx, key, 0, samplePerShard-1).Result()
|
||
if err != nil {
|
||
m.logger.Warn("获取分片最早任务失败",
|
||
zap.Int("shard_id", i),
|
||
zap.String("task_type", taskType),
|
||
zap.Error(err))
|
||
if firstErr == nil {
|
||
firstErr = err
|
||
}
|
||
continue
|
||
}
|
||
for _, z := range earliest {
|
||
waitTime := float64(nowUnix) - z.Score
|
||
if waitTime > 0 {
|
||
totalWait += waitTime
|
||
}
|
||
sampleCount++
|
||
}
|
||
}
|
||
|
||
if sampleCount == 0 {
|
||
return 0, firstErr
|
||
}
|
||
return totalWait / float64(sampleCount), firstErr
|
||
}
|