From fe7edea1a3955c8b012e0080013feb920fb0ebe4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zhero=20=26=20=E5=8A=A9=E7=90=86?= <836283459@qq.com> Date: Tue, 1 Dec 2026 19:47:00 +0800 Subject: [PATCH] =?UTF-8?q?fix(reconciliation):=20=E5=AF=B9=E8=B4=A6?= =?UTF-8?q?=E4=BB=BB=E5=8A=A1=E5=85=A5=E9=98=9F=20id=20=E8=B6=85=E9=95=BF?= =?UTF-8?q?=E5=AF=BC=E8=87=B4=E6=AF=8F=E6=89=B9=20queue=5Funavailable?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit enqueueRun 用 "job-" + run_id 当 jobs.id,而 jobs.id 是 VARCHAR(36);run_id 本身 已是 36 字符(rec- + 32 位 hex),拼出 40 字符,INSERT 必然失败,批次在 claim 之后 立刻被记成 failed/queue_unavailable——候选库里 19:37 起所有批次都是这个状态,C-101 起的用例全部无法开始。 改为 job 自带 "job-"+32 位 hex 的 id(36 字符),幂等仍由唯一 idempotency_key (reconciliation-run:)保证。补单测:CreateRun 入队请求 id 必须正好 36 字符、 type/idempotency_key/payload 与 run 一致。 验证:go test ./internal/reconciliation/ 通过;make verify 17 项 = 16 success + identity skipped。 --- internal/reconciliation/service.go | 9 +++++- internal/reconciliation/service_test.go | 42 +++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/internal/reconciliation/service.go b/internal/reconciliation/service.go index a3020d0..e1c8e69 100644 --- a/internal/reconciliation/service.go +++ b/internal/reconciliation/service.go @@ -214,8 +214,15 @@ func (s *Service) enqueueRun(ctx context.Context, runID string, now time.Time) e if err != nil { return err } + // jobs.id is VARCHAR(36) and the run id alone already fills it ("rec-" + 32 hex), + // so the job carries its own id; idempotency is guaranteed by the unique + // idempotency_key below, not by deriving the id from the run. + jobID, err := newID("job-") + if err != nil { + return err + } _, err = s.queue.Enqueue(ctx, jobs.EnqueueRequest{ - ID: "job-" + runID, + ID: jobID, Type: "reconciliation.run", Payload: payload, Priority: 5, diff --git a/internal/reconciliation/service_test.go b/internal/reconciliation/service_test.go index 6a64242..b5028eb 100644 --- a/internal/reconciliation/service_test.go +++ b/internal/reconciliation/service_test.go @@ -13,6 +13,7 @@ import ( "time" "knowledge-governance/internal/governance" + "knowledge-governance/internal/jobs" "knowledge-governance/internal/platform/weknora" "knowledge-governance/internal/readiness" "knowledge-governance/internal/weknoraaccess" @@ -60,6 +61,7 @@ type fakeReconciliationStore struct { removeHit bool requeueDoc string orphan bool + enqueued []jobs.EnqueueRequest } func (f *fakeReconciliationStore) ReapStaleReconciliationRuns(context.Context, time.Time, time.Duration) (int64, error) { @@ -133,6 +135,11 @@ func (f *fakeReconciliationStore) MarkReconciliationOrphanRemoved(context.Contex return nil } +func (f *fakeReconciliationStore) Enqueue(_ context.Context, request jobs.EnqueueRequest, _ time.Time) (*jobs.Job, error) { + f.enqueued = append(f.enqueued, request) + return &jobs.Job{ID: request.ID}, nil +} + type fakeReconciliationRemote struct { items []weknora.Knowledge missing map[string]bool @@ -538,3 +545,38 @@ func TestExecuteRunR6UsesRemovalCandidatesViewOnly(t *testing.T) { t.Fatalf("run counts R6 = %#v", store.completedRun) } } + +// TestCreateRunEnqueuesJobWithinJobIDLimit freezes the queue contract: jobs.id is +// VARCHAR(36), and the run id ("rec-" + 32 hex) already fills it. Deriving the job +// id from the run id overflowed the column, so every batch was created `failed` +// with queue_unavailable before a worker ever saw it. +func TestCreateRunEnqueuesJobWithinJobIDLimit(t *testing.T) { + now := time.Date(2026, 12, 1, 12, 0, 0, 0, time.UTC) + store := &fakeReconciliationStore{ + run: governance.ReconciliationRun{ID: "rec-unused", KBID: "kb-1", State: "running", Trigger: "manual", StartedAt: now}, + } + service, err := New(Config{Store: store, Queue: store, Remote: &fakeReconciliationRemote{}, + Now: func() time.Time { return now }, Grace: 30 * time.Minute, StallAfter: 2 * time.Hour, + StaleAfter: 6 * time.Hour, ConfirmConcurrency: 1, KeepRuns: 30, MaxAge: 90 * 24 * time.Hour, + WeKnoraWebBase: func(context.Context) string { return "https://test.example/" }}) + if err != nil { + t.Fatal(err) + } + run, err := service.CreateRun(context.Background(), "kb-1", "manual") + if err != nil { + t.Fatalf("CreateRun error = %v", err) + } + if len(store.enqueued) != 1 { + t.Fatalf("enqueued %d jobs", len(store.enqueued)) + } + request := store.enqueued[0] + if len(request.ID) != 36 { + t.Fatalf("job id %q length = %d, want exactly 36 (VARCHAR(36))", request.ID, len(request.ID)) + } + if request.Type != "reconciliation.run" || request.IdempotencyKey != "reconciliation-run:"+run.ID { + t.Fatalf("enqueue request = %#v", request) + } + if string(request.Payload) != `{"run_id":"`+run.ID+`"}` { + t.Fatalf("payload = %s", request.Payload) + } +} -- 2.52.0