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
1 change: 1 addition & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ on:
push:
branches:
- main
- 'v0.*'

env:
GO_VERSION: 1.23
Expand Down
11 changes: 7 additions & 4 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ on:
pull_request:
branches:
- main
- 'v0.*'

env:
GO_VERSION: 1.23

jobs:
test:
runs-on: ubuntu-latest
permissions:
pull-requests: write
steps:
- uses: actions/setup-go@v2
with:
Expand All @@ -29,11 +32,11 @@ jobs:
if: success() || failure()
run: |
echo '```' >> results.comment

- name: results
if: success() || failure()
uses: thollander/actions-comment-pull-request@v2
uses: thollander/actions-comment-pull-request@v3
with:
comment_tag: results
filePath: results.comment
comment-tag: results
file-path: results.comment

97 changes: 62 additions & 35 deletions reconciler/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,28 +39,29 @@ func (r *reconciler[Obj]) reconcileLoop(ctx context.Context, health cell.Health)
// Create the change iterator to watch for inserts and deletes to the table.
wtxn := r.DB.WriteTxn(r.config.Table)
changeIterator, err := r.config.Table.Changes(wtxn)
txn := wtxn.Commit()
startTxn := wtxn.Commit()
if err != nil {
return fmt.Errorf("watching for changes failed: %w", err)
}

tableWatchChan := closedWatchChannel

externalPrune := false

tableInitialized := false
_, tableInitWatch := r.config.Table.Initialized(txn)

incremental := incremental[Obj]{
moduleID: r.ModuleID,
metrics: r.config.Metrics,
config: &r.config,
retries: r.retries,
primaryIndexer: r.primaryIndexer,
db: r.DB,
table: r.config.Table,
results: make(map[Obj]opResult),
}
var (
tableWatchChan = closedWatchChannel
pruneRequested = false
initialSetReconciled = false
initialTxn statedb.ReadTxn

incremental = incremental[Obj]{
moduleID: r.ModuleID,
metrics: r.config.Metrics,
config: &r.config,
retries: r.retries,
primaryIndexer: r.primaryIndexer,
db: r.DB,
table: r.config.Table,
results: make(map[Obj]opResult),
}
)
_, tableInitWatch := r.config.Table.Initialized(startTxn)

for {
// Throttle a bit before reconciliation to allow for a bigger batch to arrive and
Expand All @@ -69,8 +70,6 @@ func (r *reconciler[Obj]) reconcileLoop(ctx context.Context, health cell.Health)
return err
}

prune := false

// Wait for trigger
select {
case <-ctx.Done():
Expand All @@ -80,33 +79,61 @@ func (r *reconciler[Obj]) reconcileLoop(ctx context.Context, health cell.Health)
case <-tableWatchChan:
// Table has changed
case <-tableInitWatch:
tableInitialized = true
tableInitWatch = nil

// Do an immediate pruning now as the table has finished
// initializing and pruning is enabled.
prune = r.config.PruneInterval != 0
// Table has been initialized
case <-pruneTickerChan:
prune = true
pruneRequested = true
case <-r.externalPruneTrigger:
externalPrune = true
pruneRequested = true
}

// Grab a new snapshot and refresh the changes iterator to read
// in the new changes.
txn = r.DB.ReadTxn()
// Pin the snapshot when initialization is observed and drain it across
// rounds. Otherwise concurrent writes could move initial objects past
// the revision being reconciled and allow pruning before their first attempt.
txn := initialTxn
if txn == nil {
txn = r.DB.ReadTxn()
if !initialSetReconciled {
if initialized, _ := r.config.Table.Initialized(txn); initialized {
// Table has become initialized. Keep processing this snapshot
// until we've done enough rounds to exhaust it and we can
// perform the initial pruning.
initialTxn = txn
tableInitWatch = nil
pruneRequested = pruneRequested || r.config.PruneInterval != 0
}
}
}
var changes iter.Seq2[statedb.Change[Obj], statedb.Revision]
changes, tableWatchChan = changeIterator.Next(txn)
if initialTxn != nil {
// Wrap 'changes' to track when we've processed all changes in the
// snapshot after table initialization.
seq := changes
changes = func(yield func(statedb.Change[Obj], statedb.Revision) bool) {
for change, rev := range seq {
if !yield(change, rev) {
return
}
}
initialSetReconciled = true
}
}

// Perform incremental reconciliation and retries of previously failed
// objects.
errs := incremental.run(ctx, txn, changes)

if tableInitialized && (prune || externalPrune) {
if err := r.prune(ctx, txn); err != nil {
errs = append(errs, err)
if initialSetReconciled {
// Prune if it's requested and we've attempted reconciliation of all objects
// in the initial snapshot. Use that same snapshot for the initial prune
// so it does not include newer objects that have not been attempted yet.
if pruneRequested {
if err := r.prune(ctx, txn); err != nil {
errs = append(errs, err)
}
pruneRequested = false
}
externalPrune = false
initialTxn = nil
}

if len(errs) == 0 {
Expand Down
Loading
Loading