2bec257289
iotlcm-worker-svr:异步执行中枢(RabbitMQ 消费者 + 全量同步编排)。 - Go module:iotlcm-worker-svr → git.5iot.com/iotlcm-public/iotlcm-worker-svr(214 文件) - 凭据与内部标识脱敏:MySQL/Redis 凭据、私网与生产 IP、内网域名、 库名 xiujie_admin → iotlcm、哨兵名占位 - 159 处真实手机号 → 合法 11 位编造号(保留号段与后 4 位,维持互异, 不破坏「校验 MSISDN 格式」等测试断言) - 移除内部资产:CI 触发器、Jenkins 触发测试文档、预演门禁与镜像 digest 记录 - Dockerfile 改用公共基础镜像(golang:1.25-alpine → alpine:3.23), 移除写死的 git 凭据 - README 重写(架构/全量同步模型/配置/已知失败),补 Apache-2.0 LICENSE,LF 行尾 + .gitattributes 已知:3 个测试包在源仓同一提交上同样失败(client/upstream、 service/allocate、service/inbound),属既有缺陷,本仓未引入亦未修复。
173 lines
5.4 KiB
Go
173 lines
5.4 KiB
Go
package contract
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"strings"
|
|
"time"
|
|
"unicode"
|
|
"unicode/utf8"
|
|
)
|
|
|
|
const MaxIdempotencyKeyBytes = 256
|
|
|
|
// ReplayRequest combines the path, header and body. Only requestedBy is in the
|
|
// HTTP body. Idempotency keys are scoped to the source run; fingerprints cover
|
|
// the source and exact request body, not the key or a generated target run ID.
|
|
type ReplayRequest struct {
|
|
SourceRunID string `json:"-"`
|
|
IdempotencyKey string `json:"-"`
|
|
RequestedBy string `json:"requestedBy"`
|
|
}
|
|
|
|
func ValidateIdempotencyKey(key string) error {
|
|
if strings.TrimSpace(key) == "" || len(key) > MaxIdempotencyKeyBytes || !utf8.ValidString(key) || strings.ContainsFunc(key, unicode.IsControl) {
|
|
return invalid("invalid Idempotency-Key")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (r ReplayRequest) Validate() error {
|
|
if err := ValidateID(r.SourceRunID); err != nil {
|
|
return err
|
|
}
|
|
if err := ValidateIdempotencyKey(r.IdempotencyKey); err != nil {
|
|
return err
|
|
}
|
|
if strings.TrimSpace(r.RequestedBy) == "" || !utf8.ValidString(r.RequestedBy) {
|
|
return invalid("requestedBy is required")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (r ReplayRequest) Fingerprint() (string, error) {
|
|
if err := r.Validate(); err != nil {
|
|
return "", err
|
|
}
|
|
// JSON avoids ambiguous delimiter concatenation and fixes field ordering.
|
|
data, err := json.Marshal(struct {
|
|
SourceRunID string `json:"sourceRunId"`
|
|
RequestedBy string `json:"requestedBy"`
|
|
}{r.SourceRunID, r.RequestedBy})
|
|
if err != nil {
|
|
return "", invalid("cannot encode replay request: %v", err)
|
|
}
|
|
digest := sha256.Sum256(data)
|
|
return hex.EncodeToString(digest[:]), nil
|
|
}
|
|
|
|
// Counters describe distinct source failures, not scan visits or retry calls.
|
|
// ScanCursor alone is not an enqueue/cleanup checkpoint: repositories must
|
|
// retain sufficient durable per-batch evidence to resume partial writes.
|
|
type ReplayProgress struct {
|
|
ScanCursor Cursor `json:"scanCursor"`
|
|
DiscoveredCount int64 `json:"discoveredCount"`
|
|
EnqueuedCount int64 `json:"enqueuedCount"`
|
|
CleanedCount int64 `json:"cleanedCount"`
|
|
FailureCount int64 `json:"failureCount"`
|
|
}
|
|
|
|
func (p ReplayProgress) Validate() error {
|
|
if err := nonnegative(p.DiscoveredCount, p.EnqueuedCount, p.CleanedCount, p.FailureCount); err != nil {
|
|
return err
|
|
}
|
|
if p.EnqueuedCount > p.DiscoveredCount || p.CleanedCount > p.EnqueuedCount {
|
|
return invalid("replay progress must satisfy cleaned <= enqueued <= discovered")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
type ReplayAction struct {
|
|
ActionID string `json:"actionId"`
|
|
SourceRunID string `json:"sourceRunId"`
|
|
TargetRunID string `json:"targetRunId"`
|
|
RequestedBy string `json:"requestedBy"`
|
|
IdempotencyKey string `json:"-"`
|
|
Fingerprint string `json:"-"`
|
|
Status ActionStatus `json:"status"`
|
|
ReplayCycle int `json:"replayCycle"`
|
|
Progress ReplayProgress `json:"progress"`
|
|
LeaseOwner string `json:"-"`
|
|
LeaseToken string `json:"-"`
|
|
LeaseUntil time.Time `json:"-"`
|
|
Revision uint64 `json:"-"`
|
|
Error string `json:"error,omitempty"`
|
|
CreatedAt time.Time `json:"createdAt"`
|
|
UpdatedAt time.Time `json:"updatedAt"`
|
|
CompletedAt *time.Time `json:"completedAt,omitempty"`
|
|
Receipt *ArchiveReceipt `json:"receipt,omitempty"`
|
|
}
|
|
|
|
func (a ReplayAction) Validate() error {
|
|
for _, id := range []string{a.ActionID, a.SourceRunID, a.TargetRunID} {
|
|
if err := ValidateID(id); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
if a.SourceRunID == a.TargetRunID || a.ReplayCycle < 1 || !a.Status.Valid() || strings.TrimSpace(a.RequestedBy) == "" {
|
|
return invalid("invalid replay action identity or status")
|
|
}
|
|
if len(a.Error) > MaxErrorMessageBytes || !utf8.ValidString(a.Error) {
|
|
return invalid("action error exceeds message bounds")
|
|
}
|
|
if err := a.Progress.Validate(); err != nil {
|
|
return err
|
|
}
|
|
if a.Receipt != nil {
|
|
if err := a.Receipt.Validate(); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
if a.Status == ActionCompleted && (a.Receipt == nil || a.CompletedAt == nil || a.CompletedAt.IsZero()) {
|
|
return invalid("completed action requires a durable receipt and completedAt")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
type ReplayActionPage struct {
|
|
Items []ReplayAction `json:"items"`
|
|
NextCursor Cursor `json:"nextCursor"`
|
|
}
|
|
|
|
type ReplayActionCreation struct {
|
|
Action ReplayAction
|
|
Created bool
|
|
}
|
|
|
|
// Lease token fences a particular acquisition, even if the same owner name is
|
|
// reused. Repository methods check expiry against their authoritative clock.
|
|
type ReplayLease struct {
|
|
ActionID string
|
|
Owner string
|
|
Token string
|
|
Until time.Time
|
|
}
|
|
|
|
// ReplayActionUpdate is a compare-and-swap of mutable action state. Identity,
|
|
// provenance, request fingerprint, and target run must never be changed.
|
|
type ReplayActionUpdate struct {
|
|
Lease ReplayLease
|
|
ExpectedRevision uint64
|
|
Status ActionStatus
|
|
Progress ReplayProgress
|
|
Error string
|
|
Receipt *ArchiveReceipt
|
|
CompletedAt *time.Time
|
|
}
|
|
|
|
// Cleanup contains the exact source versions durably accepted by the target.
|
|
// The engine must not request cleanup until target enqueue is confirmed.
|
|
type ReplayCleanup struct {
|
|
Lease ReplayLease
|
|
SourceRunID string
|
|
TargetRunID string
|
|
Entries []FailureEntry
|
|
}
|
|
|
|
type ReplayCleanupResult struct {
|
|
CleanedCount int64
|
|
AlreadyCleanedCount int64
|
|
ConflictCount int64
|
|
}
|