fix(nodes): bound ordered client dispatch and scope cache invalidation

Run independent inbounds in parallel while full reconciliation and permanent deletes retain node-wide ordering. Bound queue waits and live batches, and invalidate only cached payloads affected by a remote client deletion.
This commit is contained in:
Hamed Sanaei
2026-11-30 23:16:19 +03:30
parent e80af331eb
commit 4e0fb882bf
11 changed files with 266 additions and 114 deletions
+83 -33
View File
@@ -2,6 +2,7 @@ package service
import (
"container/list"
"context"
"sync"
"gorm.io/gorm"
@@ -16,17 +17,26 @@ var (
)
type nodeConfigQueue struct {
turns list.List
groups list.List
reconciles int
}
// Inbounds in the same group can push concurrently. A node-wide operation
// waits for the preceding group to drain before the next group can run.
type nodeConfigGroup struct {
position *list.Element
exclusive bool
inbounds map[int]*list.List
}
type nodeConfigTurn struct {
nodeID int
queue *nodeConfigQueue
position *list.Element
ready chan struct{}
work nodeConfigWork
behindReconcile bool
nodeID int
inboundID int
queue *nodeConfigQueue
group *nodeConfigGroup
position *list.Element
ready chan struct{}
work nodeConfigWork
}
type nodeConfigWork uint8
@@ -37,8 +47,7 @@ const (
nodeFullReconcile
)
// Per-node ordering also covers full client deletes, which can affect sibling inbounds.
func reserveNodeConfigTurn(nodeID int, work nodeConfigWork) *nodeConfigTurn {
func reserveNodeConfigTurn(nodeID, inboundID int, work nodeConfigWork) *nodeConfigTurn {
nodeConfigDispatchMu.Lock()
defer nodeConfigDispatchMu.Unlock()
q := nodeConfigDispatch[nodeID]
@@ -46,41 +55,81 @@ func reserveNodeConfigTurn(nodeID int, work nodeConfigWork) *nodeConfigTurn {
q = &nodeConfigQueue{}
nodeConfigDispatch[nodeID] = q
}
// A reconcile can see newer commits than its position in the queue. Let it
// converge overlapping deltas, retaining node-wide deletes it cannot replay.
// A reconcile can see later commits. It converges overlapping deltas,
// while node-wide deletes still run because they also remove client records.
if q.reconciles > 0 && work == nodeClientDelta {
return nil
}
turn := &nodeConfigTurn{
nodeID: nodeID, queue: q, ready: make(chan struct{}), work: work, behindReconcile: q.reconciles > 0,
exclusive := work != nodeClientDelta
if exclusive {
inboundID = 0
}
turn.position = q.turns.PushBack(turn)
var group *nodeConfigGroup
if last := q.groups.Back(); last != nil {
group = last.Value.(*nodeConfigGroup)
}
if group == nil || group.exclusive != exclusive {
group = &nodeConfigGroup{exclusive: exclusive, inbounds: make(map[int]*list.List)}
group.position = q.groups.PushBack(group)
}
lane := group.inbounds[inboundID]
if lane == nil {
lane = &list.List{}
group.inbounds[inboundID] = lane
}
turn := &nodeConfigTurn{
nodeID: nodeID, inboundID: inboundID, queue: q, group: group, ready: make(chan struct{}), work: work,
}
turn.position = lane.PushBack(turn)
if work == nodeFullReconcile {
q.reconciles++
}
if q.turns.Front() == turn.position {
if q.groups.Front() == group.position && lane.Front() == turn.position {
close(turn.ready)
}
return turn
}
func (t *nodeConfigTurn) wait(ctx context.Context) bool {
select {
case <-t.ready:
return ctx.Err() == nil
case <-ctx.Done():
return false
}
}
func (t *nodeConfigTurn) release() {
nodeConfigDispatchMu.Lock()
defer nodeConfigDispatchMu.Unlock()
if t.position == nil {
return
}
q := t.queue
wasFirst := t.position == q.turns.Front()
q.turns.Remove(t.position)
q, group := t.queue, t.group
lane := group.inbounds[t.inboundID]
wasReady := q.groups.Front() == group.position && lane.Front() == t.position
lane.Remove(t.position)
t.position = nil
if t.work == nodeFullReconcile {
q.reconciles--
}
if next := q.turns.Front(); next == nil {
if front := lane.Front(); front != nil {
if wasReady {
close(front.Value.(*nodeConfigTurn).ready)
}
return
}
delete(group.inbounds, t.inboundID)
if len(group.inbounds) > 0 {
return
}
q.groups.Remove(group.position)
if next := q.groups.Front(); next == nil {
delete(nodeConfigDispatch, t.nodeID)
} else if wasFirst {
close(next.Value.(*nodeConfigTurn).ready)
} else if wasReady {
for _, lane := range next.Value.(*nodeConfigGroup).inbounds {
close(lane.Front().Value.(*nodeConfigTurn).ready)
}
}
}
@@ -118,27 +167,28 @@ func (m *inboundClientMutation) commit(inbound *model.Inbound, work nodeConfigWo
}
return (&NodeService{}).MarkNodeDirtyTx(tx, nodeID)
}, func() {
m.turn = reserveNodeConfigTurn(nodeID, work)
m.turn = reserveNodeConfigTurn(nodeID, inbound.Id, work)
})
}
func (m *inboundClientMutation) push(rt runtime.Runtime, inbound *model.Inbound, apply func() bool) {
func (m *inboundClientMutation) push(rt runtime.Runtime, inbound *model.Inbound, apply func(context.Context) bool) {
m.unlock()
if m.turn == nil {
return
}
<-m.turn.ready
waitCtx, stopWaiting := nodePushContext()
ready := m.turn.wait(waitCtx)
stopWaiting()
if !ready {
return
}
ctx, cancel := nodePushContext()
defer cancel()
complete := false
defer func() {
if remote, ok := rt.(*runtime.Remote); ok {
// A node-wide delete may invalidate newer settings sent by an overlapping
// reconcile, including sibling inbounds that this turn did not edit.
if m.turn.work == nodeFullClientDelete && (!complete || m.turn.behindReconcile) {
remote.ForgetAllPushedInbounds()
} else if !complete {
remote.ForgetPushedInbound(inbound.Tag)
}
if remote, ok := rt.(*runtime.Remote); ok && !complete {
remote.ForgetPushedInbound(inbound.Tag)
}
}()
complete = apply()
complete = apply(ctx)
}