fix(daemon): claim binding dispatch and enqueue its task atomically
EnqueueBindingTask now takes the capture and claims the ledger row, inserts the task, stamps its provenance and links the ledger row in one State Store transaction. A failure anywhere leaves the delivery undispatched for the next cycle; a replay or concurrent claim is refused by the ledger's unique index. SetDispatchTask is removed.
This commit is contained in:
11 files changed
+833
-837
No files matched your search
@@ -0,0 +1,132 @@
|
||||
package daemon
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/domain/binding"
|
||||
"github.com/samcharles93/archie-core/internal/domain/mapping"
|
||||
"github.com/samcharles93/archie-core/internal/domain/org"
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
)
|
||||
|
||||
// ledgerStore models the State Store's dispatch ledger: EnqueueBindingTask
|
||||
// claims and creates as one unit, and any ledger write the daemon makes for
|
||||
// a capture refuses a later claim of it, as the real unique index does.
|
||||
type ledgerStore struct {
|
||||
storecontract.TaskStore
|
||||
storecontract.BindingStore
|
||||
storecontract.MappingStore
|
||||
|
||||
mu sync.Mutex
|
||||
claimed map[string]bool
|
||||
tasks int
|
||||
markers int
|
||||
failures int // enqueues to fail before any commits
|
||||
listed *sync.WaitGroup // holds listers until every admitter has listed
|
||||
}
|
||||
|
||||
func (s *ledgerStore) ListBindings(context.Context) ([]binding.Binding, error) {
|
||||
return []binding.Binding{dispatchBinding}, nil
|
||||
}
|
||||
|
||||
func (s *ledgerStore) ArmedBindingsForSource(context.Context, string) ([]binding.Binding, error) {
|
||||
return []binding.Binding{dispatchBinding}, nil
|
||||
}
|
||||
|
||||
func (s *ledgerStore) ListUndispatchedCaptures(context.Context, []string, int) ([]storecontract.CapturedEvent, error) {
|
||||
s.mu.Lock()
|
||||
pending := !s.claimed["capture"]
|
||||
s.mu.Unlock()
|
||||
if s.listed != nil {
|
||||
s.listed.Done()
|
||||
s.listed.Wait()
|
||||
}
|
||||
if !pending {
|
||||
return nil, nil
|
||||
}
|
||||
return []storecontract.CapturedEvent{{ID: "capture", Source: "forge", EventType: "push", Body: "{}", Authenticated: true}}, nil
|
||||
}
|
||||
|
||||
func (s *ledgerStore) RecordDispatch(_ context.Context, _ string, _ int64, captureID string, _ int64, _ string) error {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
if s.claimed[captureID] {
|
||||
return storecontract.ErrAlreadyDispatched
|
||||
}
|
||||
s.claimed[captureID] = true
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *ledgerStore) EnqueueBindingTask(_ context.Context, _, _, _, _, _, _, _ string, _ int, captureID string, _ map[string]any) (*workflow.Task, error) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
if s.claimed[captureID] {
|
||||
return nil, storecontract.ErrAlreadyDispatched
|
||||
}
|
||||
if s.failures > 0 {
|
||||
s.failures--
|
||||
return nil, errors.New("state store unavailable")
|
||||
}
|
||||
s.claimed[captureID] = true
|
||||
s.tasks++
|
||||
return &workflow.Task{ID: int64(s.tasks)}, nil
|
||||
}
|
||||
|
||||
func (s *ledgerStore) GetMapping(context.Context, string) (*mapping.Mapping, error) {
|
||||
return &mapping.Mapping{ID: "push", EventTypeID: "push"}, nil
|
||||
}
|
||||
|
||||
func (s *ledgerStore) InsertEvent(context.Context, events.Event) (int64, error) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
s.markers++
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
var dispatchBinding = binding.Binding{
|
||||
ID: "on-push", Name: "on-push", Version: 1, MappingID: "push", Matcher: binding.Matcher{Source: "forge"},
|
||||
OrgID: org.DefaultOrgID, Workflow: "deploy", Status: binding.StatusArmed,
|
||||
}
|
||||
|
||||
// A delivery dispatches exactly one task however its admissions go: replayed
|
||||
// cycles, a failure after the claim, or concurrent dispatchers.
|
||||
func TestBindingDispatchCreatesOneTask(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
failures int
|
||||
cycles int
|
||||
concurrent int
|
||||
}{
|
||||
{name: "replay", cycles: 3, concurrent: 1},
|
||||
{name: "failure after the claim", failures: 1, cycles: 2, concurrent: 1},
|
||||
{name: "concurrent admission", cycles: 1, concurrent: 4},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
store := &ledgerStore{claimed: map[string]bool{}, failures: tc.failures}
|
||||
d := &Daemon{
|
||||
Cfg: config.NewHolder(config.Config{}), Store: store, Log: slog.New(slog.DiscardHandler),
|
||||
Bindings: store, BindingDispatcher: store, BindingTaskCreator: store, Mappings: store,
|
||||
WorkflowDefinitions: orgDefinitions{org.DefaultOrgID: "id: deploy\nrepository: none\nsteps:\n - type: workflow.finish\n"},
|
||||
}
|
||||
for range tc.cycles {
|
||||
var listed, done sync.WaitGroup
|
||||
listed.Add(tc.concurrent)
|
||||
store.listed = &listed
|
||||
for range tc.concurrent {
|
||||
done.Go(func() { d.dispatchBindings(t.Context()) })
|
||||
}
|
||||
done.Wait()
|
||||
}
|
||||
if store.tasks != 1 || store.markers != 1 {
|
||||
t.Fatalf("tasks %d, start markers %d; want exactly one of each", store.tasks, store.markers)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user