mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-12-06 16:04:24 +00:00
GetSnapshot 每次选号都 ZRANGE 全组账号 ID、分块 MGET 全组 sched:meta 并逐条 JSON 解码,Redis 出口、CPU 与 GC 都随分组号数线性增长。 - 每个 bucket 的解码结果常驻进程内存。每次读取用一次 MGET 取 ready、 active、全局修订号与 bucket 平台修订号(mixed 另加 antigravity),全部与 条目一致且条目未超过 10 秒才复用,否则重新加载。last_used 仍每次读取, 套用在本次调用独占的副本上,LRU 语义不变。 - 修订号是随机串:精简记录改写后写入所涉平台的修订号,与最后一批写入同一 pipeline 发出;删除记录、账号平台变化(SET ... GET 取回旧值比对)、 写入出错时写入全局修订号,出错路径在后台合并写入。 - 母本发布前预解析模型映射;副本在指针、长度与运行时版本都匹配时直接复用, 不计算签名。 - 未命中、空快照、记录缺失、解码失败、Redis 出错的返回与原实现一致;选中后 的整账号读取仍每次重新解码,转发路径对账号的原地修改不触及常驻对象。 真实 Redis 端到端(SelectAccountWithLoadAwareness,本机): 100 号:3.83ms → 0.73ms,Redis 出口 251KB → 7.0KB,分配 2.03MB → 0.23MB 1000 号:27.8ms → 1.92ms,Redis 出口 2.47MB → 25KB,分配 20.2MB → 1.97MB 每次都未命中:100 号 3.83ms → 3.10ms,1000 号 27.6ms → 21.9ms 常驻内存约为每号 7KB × 活跃 bucket 数;闲置超过 100 秒的条目在写入新 条目时清除。
124 lines
5.1 KiB
Go
124 lines
5.1 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
SchedulerModeSingle = "single"
|
|
SchedulerModeMixed = "mixed"
|
|
SchedulerModeForced = "forced"
|
|
)
|
|
|
|
var (
|
|
ErrSchedulerBucketRetired = errors.New("scheduler bucket retired")
|
|
ErrSchedulerBucketWriteFenced = errors.New("scheduler bucket write fenced")
|
|
ErrSchedulerGroupLifecycleLeaseInvalid = errors.New("scheduler group lifecycle lease invalid")
|
|
ErrSchedulerGroupLifecycleLeaseLost = errors.New("scheduler group lifecycle lease lost")
|
|
)
|
|
|
|
// SchedulerBucketWriteToken fences a snapshot writer to one bucket epoch.
|
|
// Tokens must be captured before any database load or queued rebuild work.
|
|
type SchedulerBucketWriteToken struct {
|
|
Bucket SchedulerBucket
|
|
Epoch int64
|
|
}
|
|
|
|
func (t SchedulerBucketWriteToken) ValidFor(bucket SchedulerBucket) bool {
|
|
return t.Epoch > 0 && t.Bucket == bucket
|
|
}
|
|
|
|
// SchedulerGroupLifecycleLease identifies one owner of a group's short-lived
|
|
// retirement/reopen critical section.
|
|
type SchedulerGroupLifecycleLease struct {
|
|
GroupID int64
|
|
OwnerToken string
|
|
}
|
|
|
|
func (l SchedulerGroupLifecycleLease) ValidFor(groupID int64) bool {
|
|
return groupID > 0 && l.GroupID == groupID && l.OwnerToken != ""
|
|
}
|
|
|
|
type SchedulerBucket struct {
|
|
GroupID int64
|
|
Platform string
|
|
Mode string
|
|
}
|
|
|
|
func (b SchedulerBucket) String() string {
|
|
return fmt.Sprintf("%d:%s:%s", b.GroupID, b.Platform, b.Mode)
|
|
}
|
|
|
|
func ParseSchedulerBucket(raw string) (SchedulerBucket, bool) {
|
|
parts := strings.Split(raw, ":")
|
|
if len(parts) != 3 {
|
|
return SchedulerBucket{}, false
|
|
}
|
|
groupID, err := strconv.ParseInt(parts[0], 10, 64)
|
|
if err != nil {
|
|
return SchedulerBucket{}, false
|
|
}
|
|
if parts[1] == "" || parts[2] == "" {
|
|
return SchedulerBucket{}, false
|
|
}
|
|
return SchedulerBucket{
|
|
GroupID: groupID,
|
|
Platform: parts[1],
|
|
Mode: parts[2],
|
|
}, true
|
|
}
|
|
|
|
// SchedulerCache 负责调度快照与账号快照的缓存读写。
|
|
type SchedulerCache interface {
|
|
// GetSnapshot 读取快照并返回命中与否(ready + active + 数据完整)。
|
|
// 返回的账号对象由本次调用独占,可以修改其顶层字段;map、切片底层数组与指针指向的值
|
|
// 与其它调用共享,不得原地修改。
|
|
GetSnapshot(ctx context.Context, bucket SchedulerBucket) ([]*Account, bool, error)
|
|
// CaptureBucketWriteToken captures the current open epoch without changing
|
|
// retirement state. A tombstoned bucket returns ErrSchedulerBucketRetired.
|
|
CaptureBucketWriteToken(ctx context.Context, bucket SchedulerBucket) (SchedulerBucketWriteToken, error)
|
|
// SetSnapshot 写入快照并切换激活版本。token 必须在 DB load/任务排队前取得。
|
|
SetSnapshot(ctx context.Context, bucket SchedulerBucket, token SchedulerBucketWriteToken, accounts []Account) error
|
|
// RetireBucket persistently tombstones a bucket and fences every older writer.
|
|
// Readers that captured the active version before retirement may finish; new
|
|
// readers observe ready/active as absent.
|
|
RetireBucket(ctx context.Context, bucket SchedulerBucket) error
|
|
// ReopenBucket is the only operation allowed to clear a tombstone. It returns
|
|
// the retirement generation established by RetireBucket; repeated calls for
|
|
// the same generation are idempotent. Callers must serialize a fresh authority
|
|
// check through ReopenBucket with RetireBucket under the same group lifecycle
|
|
// lease; ordinary rebuild paths never call ReopenBucket.
|
|
ReopenBucket(ctx context.Context, bucket SchedulerBucket) (SchedulerBucketWriteToken, error)
|
|
// TryAcquireGroupLifecycleLease serializes authoritative retirement/reopen
|
|
// decisions for one non-zero group across instances.
|
|
TryAcquireGroupLifecycleLease(ctx context.Context, groupID int64, ttl time.Duration) (SchedulerGroupLifecycleLease, bool, error)
|
|
// ReleaseGroupLifecycleLease releases the lease only if its owner token still
|
|
// matches, so an expired holder cannot delete a successor's lease. Missing,
|
|
// expired, mismatched, and already released leases return
|
|
// ErrSchedulerGroupLifecycleLeaseLost.
|
|
ReleaseGroupLifecycleLease(ctx context.Context, lease SchedulerGroupLifecycleLease) error
|
|
// GetAccount 获取单账号快照。
|
|
GetAccount(ctx context.Context, accountID int64) (*Account, error)
|
|
// SetAccount 写入单账号快照(包含不可调度状态)。
|
|
SetAccount(ctx context.Context, account *Account) error
|
|
// DeleteAccount 删除单账号快照。
|
|
DeleteAccount(ctx context.Context, accountID int64) error
|
|
// UpdateLastUsed 批量更新账号的最后使用时间。
|
|
UpdateLastUsed(ctx context.Context, updates map[int64]time.Time) error
|
|
// TryLockBucket 尝试获取分桶重建锁。
|
|
TryLockBucket(ctx context.Context, bucket SchedulerBucket, ttl time.Duration) (bool, error)
|
|
// UnlockBucket 释放分桶重建锁。
|
|
UnlockBucket(ctx context.Context, bucket SchedulerBucket) error
|
|
// ListBuckets 返回已注册的分桶集合。
|
|
ListBuckets(ctx context.Context) ([]SchedulerBucket, error)
|
|
// GetOutboxWatermark 读取 outbox 水位。
|
|
GetOutboxWatermark(ctx context.Context) (int64, error)
|
|
// SetOutboxWatermark 保存 outbox 水位。
|
|
SetOutboxWatermark(ctx context.Context, id int64) error
|
|
}
|