mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-12-05 16:07:33 +00:00
分组账号数多时,进程内快照条目失效后每个并发请求各自 ZRANGE + MGET 全部精简记录并 逐个解码;Redis 读失败时每个请求又各自回源数据库并回写整份快照。每次选号还要把整组 账号按值复制三遍,并分块串行 MGET 全部 last_used。 - 快照条目加载按 (bucket, active, 修订号) 合并为一次,加载与调用方取消解耦; (active, 修订号) 未变、仅超过 10s 直接复用时长的条目在 20s 宽限期内先返回, 同时由一次后台加载刷新,修订号或 active 变化时仍等待新数据 - 重新加载时,精简记录原文未变且模型映射预解析对应当前运行时版本的账号沿用已解码母本 - last_used 读取结果在条目内复用 200ms,本进程写入 last_used 后立即失效; 多个分块的 MGET 放进同一个 pipeline 一次往返 - 新增 GetSnapshotValues 直接返回值切片,服务层优先使用,省掉 derefAccounts 的复制; 调度阈值过滤改为原地压缩 - 数据库回源按 bucket 合并:取发布令牌、读库与回写在一次共享调用里完成, 未配置 db_fallback_timeout_seconds 时上限 30s,各调用方得到独立的切片 压测(单用户 Bedrock、关闭在途预留):300 号 57.7K→61.6K RPM,500 号 42.3K→50.0K RPM。
131 lines
5.5 KiB
Go
131 lines
5.5 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
|
|
}
|
|
|
|
// schedulerSnapshotValuesReader 由能直接返回账号值切片的 SchedulerCache 实现。
|
|
// GetSnapshotValues 与 GetSnapshot 读取同一份快照,返回的切片与其中的账号由本次调用独占,
|
|
// 账号的使用约束与 GetSnapshot 相同。
|
|
type schedulerSnapshotValuesReader interface {
|
|
GetSnapshotValues(ctx context.Context, bucket SchedulerBucket) ([]Account, bool, error)
|
|
}
|
|
|
|
// 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
|
|
}
|