mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-12-02 23:45:11 +03:00
2e0c751a80
Release inbound mutation locks after durable local commit and reserve conflicting remote dispatch turns in writer commit order. Allow independent inbounds to progress concurrently while reconciliation and full client deletes retain node-wide ordering. Bound queue waits and live batches separately, advance dirty revisions strictly, and invalidate only affected remote fingerprints.
Persist permanent-delete intents transactionally per node/email and replay bounded pages during reconciliation. Match acknowledgements to the queued generation and retain failed/ambiguous deliveries. Supersede pending deletes only when the client is desired on the target node, so recreation on another node does not leave an old orphan record. Preserve pending rows during database migration and accept legacy backups without the outbox table.
Validation: reproduced the cross-node orphan on the previous head; fixed regression passed locally on Lenovo with focused SQLite (9/9) and PostgreSQL 16 (7/7). All 12 CI jobs passed on 5459316fbd (run 46862646487), including race, PostgreSQL migration and durable-first, and node/Xray E2E. PostgreSQL logs confirm all seven ordering tests including cross-node recreation, lost acknowledgement and bulk recovery after database reopen. Branch is up to date with main. No measured production latency claim; requests still wait for their own bounded ordered remote apply. Coordinate overlapping #7308 before its integration.
Fixes #7304.
195 lines
4.5 KiB
Go
195 lines
4.5 KiB
Go
package service
|
|
|
|
import (
|
|
"container/list"
|
|
"context"
|
|
"sync"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
|
|
"github.com/mhsanaei/3x-ui/v3/internal/web/runtime"
|
|
)
|
|
|
|
var (
|
|
nodeConfigDispatchMu sync.Mutex
|
|
nodeConfigDispatch = map[int]*nodeConfigQueue{}
|
|
)
|
|
|
|
type nodeConfigQueue struct {
|
|
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
|
|
inboundID int
|
|
queue *nodeConfigQueue
|
|
group *nodeConfigGroup
|
|
position *list.Element
|
|
ready chan struct{}
|
|
work nodeConfigWork
|
|
}
|
|
|
|
type nodeConfigWork uint8
|
|
|
|
const (
|
|
nodeClientDelta nodeConfigWork = iota
|
|
nodeFullClientDelete
|
|
nodeFullReconcile
|
|
)
|
|
|
|
func reserveNodeConfigTurn(nodeID, inboundID int, work nodeConfigWork) *nodeConfigTurn {
|
|
nodeConfigDispatchMu.Lock()
|
|
defer nodeConfigDispatchMu.Unlock()
|
|
q := nodeConfigDispatch[nodeID]
|
|
if q == nil {
|
|
q = &nodeConfigQueue{}
|
|
nodeConfigDispatch[nodeID] = q
|
|
}
|
|
// 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
|
|
}
|
|
exclusive := work != nodeClientDelta
|
|
if exclusive {
|
|
inboundID = 0
|
|
}
|
|
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.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, 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 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 wasReady {
|
|
for _, lane := range next.Value.(*nodeConfigGroup).inbounds {
|
|
close(lane.Front().Value.(*nodeConfigTurn).ready)
|
|
}
|
|
}
|
|
}
|
|
|
|
type inboundClientMutation struct {
|
|
lock *inboundMutationLock
|
|
turn *nodeConfigTurn
|
|
}
|
|
|
|
func beginInboundClientMutation(inboundID int) *inboundClientMutation {
|
|
return &inboundClientMutation{lock: lockInbound(inboundID)}
|
|
}
|
|
|
|
func (m *inboundClientMutation) unlock() {
|
|
if m.lock != nil {
|
|
m.lock.Unlock()
|
|
m.lock = nil
|
|
}
|
|
}
|
|
|
|
func (m *inboundClientMutation) close() {
|
|
m.unlock()
|
|
if m.turn != nil {
|
|
m.turn.release()
|
|
}
|
|
}
|
|
|
|
func (m *inboundClientMutation) commit(inbound *model.Inbound, work nodeConfigWork, write func(*gorm.DB) error) error {
|
|
if inbound.NodeID == nil {
|
|
return runSerializedTx(write)
|
|
}
|
|
nodeID := *inbound.NodeID
|
|
return runSerializedTxThen(func(tx *gorm.DB) error {
|
|
if err := write(tx); err != nil {
|
|
return err
|
|
}
|
|
return (&NodeService{}).MarkNodeDirtyTx(tx, nodeID)
|
|
}, func() {
|
|
m.turn = reserveNodeConfigTurn(nodeID, inbound.Id, work)
|
|
})
|
|
}
|
|
|
|
func (m *inboundClientMutation) push(rt runtime.Runtime, inbound *model.Inbound, apply func(context.Context) bool) {
|
|
m.unlock()
|
|
if m.turn == nil {
|
|
return
|
|
}
|
|
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 && !complete && m.turn.work != nodeFullClientDelete {
|
|
remote.ForgetPushedInbound(inbound.Tag)
|
|
}
|
|
}()
|
|
complete = apply(ctx)
|
|
}
|