Files
archie/internal/daemon/task_bus.go
T

483 lines
15 KiB
Go

package daemon
import (
"context"
"errors"
"fmt"
"strings"
"sync"
"time"
"github.com/samcharles93/archie-core/internal/domain/identity"
"github.com/samcharles93/archie-core/internal/domain/workflow"
"github.com/samcharles93/archie-core/internal/domain/workintake"
"github.com/samcharles93/archie-core/internal/eventbus"
"github.com/samcharles93/archie-core/internal/events"
"github.com/samcharles93/archie-core/internal/natsrpc"
"github.com/samcharles93/archie-core/internal/taskstate"
)
// drainLoop is the only task drainer and capture dispatcher while Run is
// active. It runs apart from the poll tick so a captured event becomes a
// running task within about a second regardless of poll_interval or forge
// latency. Submit never blocks, so a full dispatcher
// just leaves tasks waiting on slots and the loop stays paced by the ticker.
func (d *Daemon) drainLoop(ctx context.Context) {
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
for {
d.markPass("drain")
d.BindingDispatch.dispatchBindings(ctx)
d.drainNATS(ctx)
select {
case <-ctx.Done():
return
case <-ticker.C:
}
}
}
// drainNATS processes tasks from NATS, falling back to the task store's ClaimNext for
// requeued tasks (waiting_approval approval, retry-parked) that didn't come
// through a NATS publish. It returns once the stream and the claim fallback
// are both empty: in-flight tasks keep running, so a task waiting on a callee
// does not hold up the pass that would claim it. Shutdown waits for them
// through WaitForTasks.
func (d *Daemon) drainNATS(ctx context.Context) {
dispatcher := d.taskDispatcher()
// A full dispatcher leaves work in the stream and the store, not parked
// on slots where it already reads as running.
for ctx.Err() == nil && dispatcher.hasRoom() {
msg, err := d.Tasks.Fetch(ctx)
if err != nil && !errors.Is(err, eventbus.ErrNoMessage) {
d.Log.ErrorContext(ctx, "nats fetch failed", "err", err)
break
}
if err == nil {
d.submitNATSTask(ctx, dispatcher, msg)
d.markPass("drain")
continue
}
// ErrNoMessage means the stream is drained. That is precisely when
// the SQLite fallback must run: requeued tasks (waiting_approval
// approval, retry-parked) never went through a NATS publish, so
// nothing will ever arrive on the stream to trigger them.
d.claimRequeued(ctx, dispatcher)
return
}
}
// claimRequeued submits tasks from the store's claim fallback while the
// dispatcher has room; the rest stay unclaimed for a later pass. It does
// not go back to the stream between claims: each empty fetch waits a full poll
// timeout, so a backlog left by a restart would outlast the systemd watchdog.
func (d *Daemon) claimRequeued(ctx context.Context, dispatcher *taskDispatcher) {
for ctx.Err() == nil && dispatcher.hasRoom() {
task, err := d.Store.ClaimNext(ctx)
if err != nil {
d.Log.ErrorContext(ctx, "sqlite claim failed", "err", err)
return
}
if task == nil {
return
}
dispatcher.Submit(ctx, task, d.process)
d.markPass("drain")
}
}
// taskDispatcher returns the daemon's long-lived global dispatcher, building
// it from the running config on first use. It outlives a single drain pass so
// ResizeTaskDispatcher can resize the running dispatcher.
func (d *Daemon) taskDispatcher() *taskDispatcher {
d.dispatcherOnce.Do(func() {
d.dispatcher = newTaskDispatcher(d.Cfg.Get().Containers.MaxConcurrency, d.allowConcurrentForTask)
})
return d.dispatcher
}
// WaitForTasks blocks until every submitted task's process has returned. A
// drain pass no longer waits for its own tasks, so shutdown is where in-flight
// work is waited for.
func (d *Daemon) WaitForTasks() {
d.taskDispatcher().Wait()
}
// ResizeTaskDispatcher applies a new containers.max_concurrency to the
// running global task dispatcher without a restart. Running tasks keep their
// slots; a lowered limit is honoured as they finish, and a raised limit
// admits queued work immediately.
func (d *Daemon) ResizeTaskDispatcher(maxConcurrency int) {
d.taskDispatcher().SetMaxConcurrency(maxConcurrency)
}
// submitNATSTask decodes a fetched message and queues it for processing. A
// message that cannot be decoded is acked rather than redelivered: it will
// never decode on a retry, so leaving it unacked would block the queue.
func (d *Daemon) submitNATSTask(ctx context.Context, dispatcher *taskDispatcher, msg eventbus.Message) {
envelope, err := workintake.DecodeTask(msg.Data())
if err != nil {
d.Log.ErrorContext(ctx, "nats decode failed", "subject", msg.Subject(), "err", err)
if err := msg.Ack(); err != nil {
d.Log.WarnContext(ctx, "nats ack failed", "subject", msg.Subject(), "err", err)
}
return
}
task := &workflow.Task{Owner: envelope.Owner, Repo: envelope.Repo, Identity: envelope.Identity}
dispatcher.Submit(ctx, task, func(ctx context.Context, _ *workflow.Task, hold *taskHold) {
d.processNATSTask(ctx, msg, hold)
})
}
// processNATSTask decodes a NATS message, writes it to SQLite, claims it,
// and runs the workflow. The message is ack'd on terminal (park is a valid
// outcome). Nak on transient errors so NATS redelivers.
func (d *Daemon) processNATSTask(ctx context.Context, msg eventbus.Message, hold *taskHold) {
ctx, span := natsrpc.StartConsumer(ctx, msg.Subject(), msg.Header())
defer span.End()
tm, err := workintake.DecodeTask(msg.Data())
if err != nil {
d.Log.ErrorContext(ctx, "nats decode failed", "err", err)
if err := msg.Ack(); err != nil {
d.Log.WarnContext(ctx, "ack failed", "err", err)
}
// bad message, don't retry
return
}
if !d.acceptTaskPublisher(ctx, msg, tm.Identity) {
return
}
inserted, err := d.Store.EnqueueIssue(ctx, tm.Owner, tm.Repo, tm.Number, tm.Title, tm.Body, strings.Join(tm.Labels, ","), tm.Identity)
if err != nil {
d.Log.ErrorContext(ctx, "nats enqueue failed", "err", err)
if err := msg.Nak(); err != nil {
d.Log.WarnContext(ctx, "nats nak failed", "err", err)
}
return
}
if !inserted {
// Already tracked -- dedup. Ack and move on.
if err := msg.Ack(); err != nil {
d.Log.WarnContext(ctx, "ack failed", "err", err)
}
return
}
task, err := d.Store.ClaimByIssue(ctx, tm.Owner, tm.Repo, tm.Number)
if err != nil {
d.Log.ErrorContext(ctx, "nats claim failed", "err", err)
if err := msg.Nak(); err != nil {
d.Log.WarnContext(ctx, "nats nak failed", "err", err)
}
return
}
if task == nil {
// Claimed by another consumer, or task is not queued. Ack.
if err := msg.Ack(); err != nil {
d.Log.WarnContext(ctx, "ack failed", "err", err)
}
return
}
d.emitTaskQueued(ctx, task)
d.process(ctx, task, hold)
if err := msg.Ack(); err != nil {
d.Log.WarnContext(ctx, "ack failed", "err", err)
}
}
func (d *Daemon) acceptTaskPublisher(ctx context.Context, msg eventbus.Message, name string) bool {
if ok, reason, class := d.identityMayAct(ctx, name); !ok {
d.Log.ErrorContext(ctx, "nats intake rejected: "+reason, "subject", msg.Subject())
if class == taskstate.ParkTransient {
if err := msg.Nak(); err != nil {
d.Log.WarnContext(ctx, "nak failed", "err", err)
}
return false
}
if err := msg.Ack(); err != nil {
d.Log.WarnContext(ctx, "ack failed", "err", err)
}
return false
}
return true
}
// emitTaskQueued records the queued task under the identity that polled it,
// which is both the actor and the authority the task was created with.
func (d *Daemon) emitTaskQueued(ctx context.Context, task *workflow.Task) {
e := events.Event{
Kind: events.KindTaskQueued, TaskID: task.ID, Repo: task.Owner + "/" + task.Repo,
Issue: task.IssueNumber, Detail: task.Title, Attempt: task.Attempt,
ActorID: task.Identity, PrincipalID: task.Identity,
}
if value, err := d.IdentityRepository.Get(ctx, identity.IdentityID(task.Identity)); err == nil {
e.ActorKind = string(value.Kind)
}
d.emit(ctx, e)
}
// PublishTask validates a task envelope, stamps the producing identity's
// org and publishes it. The idempotency key dedups rediscovered issues.
func (d *Daemon) PublishTask(ctx context.Context, task workintake.TaskEnvelope) error {
if err := task.Kind.Validate(); err != nil {
return fmt.Errorf("publish task %s: %w", task.Ref(), err)
}
if task.Identity == "" {
return fmt.Errorf("publish task %s: no identity", task.Ref())
}
resolved, err := d.identityOrg(ctx, identity.IdentityID(task.Identity))
if err != nil {
return fmt.Errorf("publish task %s: resolve org: %w", task.Ref(), err)
}
task.Org = resolved
payload, err := task.Encode()
if err != nil {
return err
}
return d.Tasks.PublishUnique(ctx, task.Subject(), task.IdempotencyKey(), payload)
}
// InFlight counts the same submitted work that graceful shutdown waits for,
// including tasks waiting for a repository or concurrency slot.
func (d *Daemon) InFlight() int {
dispatcher := d.taskDispatcher()
dispatcher.mu.Lock()
defer dispatcher.mu.Unlock()
return dispatcher.pending
}
// taskDispatcher limits concurrent tasks globally and runs one task per repo
// at a time, in submit order, unless the repo allows concurrency.
type taskDispatcher struct {
allowConcurrent func(task *workflow.Task) bool
mu sync.Mutex
limit int
active int
slotsFree *sync.Cond
repoTail map[string]chan struct{}
pending int
// waiting counts submitted tasks that hold no slot yet, so a drain pass
// stops claiming once queued work alone fills the limit.
waiting int
// holds maps a task id to the capacity its run holds, so a run waiting on
// a callee can give that capacity back and take it again by task id.
holds map[int64]*taskHold
}
// SetMaxConcurrency accepts a new global limit without a restart. Running
// tasks keep their slots; a lowered limit is enforced as they finish, and a
// raised limit admits queued work immediately.
func (d *taskDispatcher) SetMaxConcurrency(maxConcurrency int) {
d.mu.Lock()
d.limit = maxConcurrency
d.mu.Unlock()
d.slotsFree.Broadcast()
}
func newTaskDispatcher(maxConcurrency int, allowConcurrent func(task *workflow.Task) bool) *taskDispatcher {
if allowConcurrent == nil {
allowConcurrent = func(*workflow.Task) bool { return false }
}
d := &taskDispatcher{
limit: maxConcurrency,
allowConcurrent: allowConcurrent,
repoTail: make(map[string]chan struct{}),
holds: make(map[int64]*taskHold),
}
d.slotsFree = sync.NewCond(&d.mu)
return d
}
// joinRepoLocked appends baton to the tail of repo's chain and returns the
// baton ahead of it, which the joining task is blocked on. Callers hold d.mu.
func (d *taskDispatcher) joinRepoLocked(repo string, baton chan struct{}) chan struct{} {
previous := d.repoTail[repo]
d.repoTail[repo] = baton
return previous
}
// takeSlotLocked blocks until the global limit has room, then counts the run
// in. limit <= 0 means unlimited. The limit is re-read on every wake, so
// SetMaxConcurrency reaches waiting tasks without recreating the dispatcher.
// Callers hold d.mu.
func (d *taskDispatcher) takeSlotLocked() {
for d.limit > 0 && d.active >= d.limit {
d.slotsFree.Wait()
}
d.active++
}
// giveBackLocked gives back everything the run holds: its slot under the
// global limit, and its place in its repo's chain, which releases the task
// queued behind it. Callers hold d.mu.
func (d *taskDispatcher) giveBackLocked(hold *taskHold) {
if hold.slot {
d.active--
hold.slot = false
}
if hold.baton == nil {
return
}
close(hold.baton)
if d.repoTail[hold.repo] == hold.baton {
delete(d.repoTail, hold.repo)
}
hold.baton = nil
}
// finishRun retires a returned run: it holds no capacity any more, and no
// Resume may take any for it. Whatever an in-flight resume takes later is
// given back by reacquire, which sees finished.
func (d *taskDispatcher) finishRun(hold *taskHold) {
d.mu.Lock()
hold.finished = true
if hold.id != 0 && d.holds[hold.id] == hold {
delete(d.holds, hold.id)
}
d.giveBackLocked(hold)
d.pending--
d.mu.Unlock()
d.slotsFree.Broadcast()
}
// setCallWaits applies a durable snapshot without accumulating duplicate events.
func (d *taskDispatcher) setCallWaits(hold *taskHold, waits int) {
d.mu.Lock()
if d.holds[hold.id] != hold || hold.finished {
d.mu.Unlock()
return
}
previous := hold.waits
hold.waits = waits
if waits > 0 {
d.giveBackLocked(hold)
}
d.mu.Unlock()
if waits > 0 {
d.slotsFree.Broadcast()
} else if previous > 0 {
// Reacquiring may block behind another task; the caller keeps running.
go d.reacquire(hold)
}
}
// reacquire returns a resumed run's capacity: its place in its repo's chain
// and a slot under the global limit. The run keeps executing meanwhile -- a
// wait on a callee is not a pause. A hold records one slot and one place, so a
// re-acquire queued while another is still waiting gives back whichever of the
// two it finds already recorded instead of taking a second.
func (d *taskDispatcher) reacquire(hold *taskHold) {
d.mu.Lock()
if hold.finished || hold.waits > 0 {
// The run returned, or started waiting again, after the reconciliation that
// queued this: it needs no capacity.
d.mu.Unlock()
return
}
var previous chan struct{}
// A place another re-acquire already queued is the run's one place; joining
// behind it would queue a second, which the task behind it would wait on
// forever.
if hold.chain && hold.baton == nil {
hold.baton = make(chan struct{})
previous = d.joinRepoLocked(hold.repo, hold.baton)
}
d.mu.Unlock()
if previous != nil {
<-previous
}
d.mu.Lock()
d.takeSlotLocked()
if hold.slot {
// Another re-acquire took the run's one slot while this waited: give
// back the slot this one just took. The place is the other's, so it is
// left alone.
d.active--
d.mu.Unlock()
d.slotsFree.Broadcast()
return
}
hold.slot = true
if hold.finished || hold.waits > 0 {
// The run ended, or started waiting again, while this was queued for
// capacity: give back what it took, so no slot or place in the repo
// chain leaks.
d.giveBackLocked(hold)
d.mu.Unlock()
d.slotsFree.Broadcast()
return
}
d.mu.Unlock()
}
// Submit queues a task's process behind the global limit and, unless the repo
// allows concurrency, behind the task already holding that repo. process is
// handed the run's capacity so it can name it once it knows the task's id and
// give it back while it waits on a callee.
func (d *taskDispatcher) Submit(
ctx context.Context,
task *workflow.Task,
process func(context.Context, *workflow.Task, *taskHold),
) {
repo := task.Owner + "/" + task.Repo
hold := &taskHold{dispatcher: d, repo: repo}
var previous chan struct{}
if !d.allowConcurrent(task) {
hold.chain = true
hold.baton = make(chan struct{})
d.mu.Lock()
previous = d.joinRepoLocked(repo, hold.baton)
d.mu.Unlock()
}
d.mu.Lock()
d.pending++
d.waiting++
d.mu.Unlock()
go func() {
defer d.finishRun(hold)
if previous != nil {
<-previous
}
d.mu.Lock()
d.takeSlotLocked()
d.waiting--
hold.slot = true
d.mu.Unlock()
process(ctx, task, hold)
}()
}
// hasRoom reports whether a newly submitted task could take a slot without
// queueing behind work already submitted. A run waiting on a callee holds no
// slot, so its callee still finds room.
func (d *taskDispatcher) hasRoom() bool {
d.mu.Lock()
defer d.mu.Unlock()
return d.limit <= 0 || d.active+d.waiting < d.limit
}
// inFlight is how many submitted tasks have not returned yet.
func (d *taskDispatcher) inFlight() int {
d.mu.Lock()
defer d.mu.Unlock()
return d.pending
}
func (d *taskDispatcher) Wait() {
d.mu.Lock()
defer d.mu.Unlock()
for d.pending > 0 {
d.slotsFree.Wait()
}
}