Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,22 @@ All notable changes to this project are documented here. The format is based on

## [Unreleased]

### Added
- Per-engine lifecycle wrapper (`managedEngine`) carrying a lifetime context and
mutex. `Refresh()` now cancels the old engine's context and drains any
in-flight `Reconcile()` call before replacing or removing the engine, closing a
stale-checkpoint-write race that would have become exploitable once concurrent
ticks were enabled.
- Bounded concurrent tick support via `SHUNT_MAX_CONCURRENT_RECONCILES` (default
`1`, preserving today's serial behaviour exactly). Values greater than `1` let
independent repo queues reconcile in parallel within one poll cycle, bounded by
an errgroup semaphore. The at-most-one-concurrent-`Reconcile`-per-engine
invariant is enforced with a per-engine `sync.Mutex`; a skipped tick logs a
warning rather than blocking.
- Context propagation: each `Reconcile()` call derives its context from both the
process shutdown signal and the engine's own lifetime context so that either a
SIGTERM or an engine replacement cancels in-flight work promptly.

### Added
- Four new Prometheus histograms: `shunt_linger_seconds` (batch-linger window
duration), `shunt_gate_seconds{outcome}` (gate run duration),
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ is discovered and managed automatically. For a single repo, set
| `SHUNT_BATCH_TARGET` | `0` | Start a lingering batch early once this many ready PRs are present (0 = wait the full linger window). |
| `SHUNT_BISECT_FANOUT` | `1` | Maximum concurrent bisection staging runs per queue. `1` preserves serial bisection. |
| `SHUNT_QUEUE_COMMENTS` | `false` | When true, maintain one sticky queue-status comment on each queued PR. Disabled by default to avoid extra write traffic. |
| `SHUNT_MAX_CONCURRENT_RECONCILES` | `1` | Maximum number of managed repos reconciled concurrently within a single poll tick. `1` (the default) preserves serial per-repo behaviour exactly and is safe for all deployments. Values greater than `1` improve throughput when many repos are managed and their reconciles are I/O-bound, but increase peak forge API usage proportionally. |
| `SHUNT_STATE_PATH` | — | Optional path to a local bbolt database for durable queue checkpoints. Leave empty for in-memory state. |
| `SHUNT_POSTGRES_DSN` | — | Optional Postgres DSN for durable queue checkpoints and replica coordination. Mutually exclusive with `SHUNT_STATE_PATH`; migrations are applied at startup. |
| `SHUNT_QUEUE_LEASE_TTL` | `45s` | Postgres queue-ownership lease duration (at least `1µs`); renewed once per reconciliation tick, whose work is bounded to half this duration. |
Expand Down
9 changes: 7 additions & 2 deletions cmd/shunt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,15 +194,20 @@ func main() {
if err != nil {
fatal(logger, "forge client config error", "error", err)
}
maxConcurrentReconciles, err := strconv.Atoi(env("SHUNT_MAX_CONCURRENT_RECONCILES", "1"))
if err != nil || maxConcurrentReconciles < 1 {
fatal(logger, "bad SHUNT_MAX_CONCURRENT_RECONCILES: must be a positive integer")
}

if topic := os.Getenv("SHUNT_TOPIC"); topic != "" {
mgr := manager.New(fc, manager.Config{
Topic: topic, StatusCtx: statusCtx, MergeStyle: mergeStyle, MaxBatch: maxBatch, BatchLinger: batchLinger, BatchTarget: batchTarget, BisectFanout: bisectFanout, QueueComments: queueComments,
WebhookURL: webhookURL, WebhookSecret: webhookSecret,
InstanceURL: instance, PublicURL: publicURL, Token: token, BotUser: botUser, BotEmail: botEmail,
Metrics: metricsCollector, Checkpoint: checkpointStore, Lease: queueLease, LeaseHolderID: leaseHolderID, LeaseTTL: leaseTTL,
Logger: baseLogger.With("component", "manager"),
EngineLogger: baseLogger.With("component", "engine"),
Logger: baseLogger.With("component", "manager"),
EngineLogger: baseLogger.With("component", "engine"),
MaxConcurrentReconciles: maxConcurrentReconciles,
})
logger.Info("multi-repo mode", "topic", topic, "interval", interval)
if err := runDaemon(ctx, baseLogger, env("SHUNT_LISTEN", ":8080"), metricsCollector, webhook, interval, wake, func(ctx context.Context) {
Expand Down
116 changes: 102 additions & 14 deletions internal/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,25 @@ import (
"errors"
"log/slog"
"strings"
"sync"
"time"

"golang.org/x/sync/errgroup"

"github.com/rbtr/shunt/internal/engine"
"github.com/rbtr/shunt/internal/forge"
"github.com/rbtr/shunt/internal/gitops"
"github.com/rbtr/shunt/internal/metrics"
"github.com/rbtr/shunt/internal/repoconfig"
)

// reconciler is the subset of *engine.Engine used by the manager.
// Separating it as an interface keeps the manager unit-testable with
// lightweight fakes.
type reconciler interface {
Reconcile(ctx context.Context) error
}

type Config struct {
Topic string
StatusCtx string
Expand All @@ -39,19 +49,49 @@ type Config struct {
LeaseTTL time.Duration
Logger *slog.Logger
EngineLogger *slog.Logger
// MaxConcurrentReconciles caps how many engines may be reconciled
// concurrently in a single Tick pass. Values ≤ 0 or 1 preserve today's
// serial behaviour exactly. Values > 1 enable cross-repo parallelism.
MaxConcurrentReconciles int
}

type Manager struct {
fc *forge.Client
cfg Config
logger *slog.Logger
engineLogger *slog.Logger
engines map[string]*managedEngine
fc *forge.Client
cfg Config
logger *slog.Logger
engineLogger *slog.Logger
engines map[string]*managedEngine
maxConcurrent int
}

// managedEngine couples a reconcile engine to its per-instance lifecycle so
// that Tick and Refresh can safely coordinate:
//
// - lifetimeCtx / stop: Refresh cancels this context before replacing or
// removing the engine, giving any in-flight Reconcile a prompt exit signal.
// - mu: held for the duration of every Reconcile call. Tick uses TryLock to
// enforce the at-most-one-concurrent-call-per-engine invariant; Refresh uses
// Lock to drain any in-flight call before touching the engines map.
type managedEngine struct {
engine *engine.Engine
cfg engine.Config
engine reconciler
cfg engine.Config
lifetimeCtx context.Context
stop context.CancelFunc // cancels lifetimeCtx
mu sync.Mutex
}

func newManagedEngine(eng reconciler, cfg engine.Config) *managedEngine {
ctx, stop := context.WithCancel(context.Background())
return &managedEngine{engine: eng, cfg: cfg, lifetimeCtx: ctx, stop: stop}
}

// drain cancels the engine's lifetime context (giving any in-flight Reconcile
// a prompt exit signal) and then waits for the call to return.
// Must be called before replacing or removing this engine from the map.
func (me *managedEngine) drain() {
me.stop() // signal Reconcile to exit at its next ctx.Err() check
me.mu.Lock() // block until any in-flight Reconcile has returned
me.mu.Unlock() //nolint:staticcheck
}

func New(fc *forge.Client, cfg Config) *Manager {
Expand All @@ -63,7 +103,14 @@ func New(fc *forge.Client, cfg Config) *Manager {
if engineLogger == nil {
engineLogger = slog.Default().With("component", "engine")
}
return &Manager{fc: fc, cfg: cfg, logger: logger, engineLogger: engineLogger, engines: map[string]*managedEngine{}}
maxConcurrent := cfg.MaxConcurrentReconciles
if maxConcurrent <= 0 {
maxConcurrent = 1
}
return &Manager{
fc: fc, cfg: cfg, logger: logger, engineLogger: engineLogger,
engines: map[string]*managedEngine{}, maxConcurrent: maxConcurrent,
}
}

func keyOf(owner, repo, base string) string { return owner + "/" + repo + "@" + base }
Expand Down Expand Up @@ -122,11 +169,18 @@ func (m *Manager) Refresh(ctx context.Context) error {
queueLogger.Info("webhook configured")
}
st := gitops.NewStager(cloneURL(m.cfg.InstanceURL, r.Owner, r.Name), m.cfg.BotUser, m.cfg.Token, m.cfg.BotUser, m.cfg.BotEmail)
m.engines[k] = &managedEngine{engine: engine.New(cfg, m.fc, st), cfg: cfg}
// Drain before replacement: cancel any in-flight Reconcile on the old engine
// and wait for it to finish so stale checkpoint writes cannot race with the
// new engine loading state under the same (owner, repo, base) key.
if old, ok := m.engines[k]; ok {
old.drain()
}
m.engines[k] = newManagedEngine(engine.New(cfg, m.fc, st), cfg)
queueLogger.Info("repo managed")
}
for k := range m.engines {
if !seen[k] {
m.engines[k].drain() // wait for any in-flight Reconcile before removing
delete(m.engines, k)
m.cfg.Metrics.ForgetQueue(labelsOfKey(k))
m.logger.Info("repo no longer managed", "queue", k)
Expand All @@ -135,15 +189,49 @@ func (m *Manager) Refresh(ctx context.Context) error {
return nil
}

// Tick reconciles every managed queue once.
// Tick reconciles every managed queue, bounded to m.maxConcurrent concurrent
// engines at a time. The default (maxConcurrent == 1) is serial and preserves
// exactly the same behaviour as the original single-goroutine loop.
//
// Invariants:
// - At most one Reconcile call is ever in flight for a given engine at once.
// If a previous tick's Reconcile is still running (TryLock fails), this tick
// is skipped for that engine with a warning; this means SHUNT_POLL_INTERVAL
// is too short relative to the actual reconcile duration.
// - Tick blocks until every goroutine it started has returned, so the caller
// (the reconcile loop) can safely call Refresh immediately afterwards.
func (m *Manager) Tick(ctx context.Context) {
var eg errgroup.Group
eg.SetLimit(m.maxConcurrent)
for k, e := range m.engines {
if err := e.engine.Reconcile(ctx); err != nil {
if !errors.Is(err, forge.ErrUnavailable) {
m.logger.Error("reconcile failed", "queue", k, "error", err)
k, e := k, e
eg.Go(func() error {
// At-most-one-concurrent-Reconcile-per-engine invariant. A failure
// here indicates SHUNT_POLL_INTERVAL is shorter than this engine's
// actual reconcile duration, or Tick is being called concurrently
// (unsupported).
if !e.mu.TryLock() {
m.logger.Warn("engine tick skipped: previous reconcile still in flight", "queue", k)
return nil
}
}
defer e.mu.Unlock()
// Derive a context that is cancelled by either process shutdown (ctx)
// or this engine's replacement/removal (e.lifetimeCtx), whichever
// comes first. context.AfterFunc is non-blocking and cleans up after
// itself once the stop function returned by AfterFunc is called.
rctx, rcancel := context.WithCancel(ctx)
defer rcancel()
stop := context.AfterFunc(e.lifetimeCtx, rcancel)
defer stop()
if err := e.engine.Reconcile(rctx); err != nil {
if !errors.Is(err, forge.ErrUnavailable) {
m.logger.Error("reconcile failed", "queue", k, "error", err)
}
}
return nil
})
}
_ = eg.Wait()
}

func (m *Manager) repoSettings(ctx context.Context, r forge.RepoRef) (repoconfig.Settings, string, error) {
Expand Down
Loading
Loading