Complete batch import v2 runtime and host capability recovery

This commit is contained in:
phamnazage-jpg
2026-05-23 09:18:02 +08:00
parent e50c292c7f
commit cfa1eaa904
60 changed files with 3718 additions and 530 deletions

View File

@@ -3,6 +3,8 @@ package batch
import (
"context"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
@@ -160,6 +162,50 @@ func TestConfirmationWorker(t *testing.T) {
}
})
t.Run("concurrent workers do not both call confirmer before lease is persisted", func(t *testing.T) {
t.Parallel()
now := time.Date(2026, 5, 22, 13, 3, 30, 0, time.UTC)
store := newFakeConfirmationStore([]sqlite.ImportRunItem{
{ItemID: "shared", RunID: "run-1", CurrentStage: "confirm", ConfirmationStatus: "pending"},
})
started := make(chan struct{}, 2)
release := make(chan struct{})
var calls atomic.Int32
confirmer := func(ctx context.Context, item sqlite.ImportRunItem) (ConfirmationResult, error) {
calls.Add(1)
started <- struct{}{}
<-release
return ConfirmationResult{StatusCode: 200}, nil
}
workerA := ConfirmationWorker{WorkerID: "worker-a", ItemStore: store, EventStore: store, LeaseDuration: time.Minute, RetryDelay: time.Second, Confirmer: confirmer}
workerB := ConfirmationWorker{WorkerID: "worker-b", ItemStore: store, EventStore: store, LeaseDuration: time.Minute, RetryDelay: time.Second, Confirmer: confirmer}
errCh := make(chan error, 2)
go func() { errCh <- workerA.Tick(context.Background(), now) }()
go func() { errCh <- workerB.Tick(context.Background(), now) }()
<-started
select {
case <-started:
t.Fatal("second worker reached confirmer before lease was acquired")
case <-time.After(50 * time.Millisecond):
}
close(release)
for range 2 {
if err := <-errCh; err != nil {
t.Fatalf("Tick() error = %v", err)
}
}
if got := calls.Load(); got != 1 {
t.Fatalf("confirmer calls = %d, want 1", got)
}
})
t.Run("reactivated account metadata is preserved", func(t *testing.T) {
t.Parallel()
@@ -200,6 +246,7 @@ func TestConfirmationWorker(t *testing.T) {
}
type fakeConfirmationStore struct {
mu sync.Mutex
items map[string]sqlite.ImportRunItem
processed []string
events []sqlite.ImportRunItemEvent
@@ -217,6 +264,9 @@ func newFakeConfirmationStore(items []sqlite.ImportRunItem) *fakeConfirmationSto
}
func (f *fakeConfirmationStore) List(ctx context.Context) ([]sqlite.ImportRunItem, error) {
f.mu.Lock()
defer f.mu.Unlock()
items := make([]sqlite.ImportRunItem, 0, len(f.items))
for _, item := range f.items {
items = append(items, item)
@@ -225,12 +275,36 @@ func (f *fakeConfirmationStore) List(ctx context.Context) ([]sqlite.ImportRunIte
}
func (f *fakeConfirmationStore) Upsert(ctx context.Context, item sqlite.ImportRunItem) error {
f.mu.Lock()
defer f.mu.Unlock()
f.items[item.ItemID] = item
f.processed = append(f.processed, item.ItemID)
return nil
}
func (f *fakeConfirmationStore) TryAcquireLease(ctx context.Context, itemID, workerID string, now time.Time, leaseDuration time.Duration) (sqlite.ImportRunItem, bool, error) {
f.mu.Lock()
defer f.mu.Unlock()
item, ok := f.items[itemID]
if !ok {
return sqlite.ImportRunItem{}, false, nil
}
if !isConfirmationCandidate(item, now) {
return sqlite.ImportRunItem{}, false, nil
}
item.ConfirmationAttempts++
item.LeaseOwner = workerID
item.LeaseUntil = now.Add(leaseDuration).Format(time.RFC3339)
f.items[itemID] = item
return item, true, nil
}
func (f *fakeConfirmationStore) Append(ctx context.Context, event sqlite.ImportRunItemEvent) error {
f.mu.Lock()
defer f.mu.Unlock()
f.events = append(f.events, event)
return nil
}
@@ -238,6 +312,9 @@ func (f *fakeConfirmationStore) Append(ctx context.Context, event sqlite.ImportR
func (f *fakeConfirmationStore) mustItem(t *testing.T, itemID string) sqlite.ImportRunItem {
t.Helper()
f.mu.Lock()
defer f.mu.Unlock()
item, ok := f.items[itemID]
if !ok {
t.Fatalf("item %q not found", itemID)