Files
mirror-3x-ui/internal/web/service/client_node_push_test.go
T
Hamed 2e0c751a80 fix(nodes): order client pushes outside inbound mutation locks (#7314)
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.
2026-12-02 14:23:45 +03:30

119 lines
4.6 KiB
Go

package service
import (
"os"
"path/filepath"
"testing"
"time"
"github.com/mhsanaei/3x-ui/v3/internal/database"
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
"github.com/mhsanaei/3x-ui/v3/internal/web/runtime"
)
// TestResetTrafficAcrossNodesPushesConcurrently covers the panel's per-client
// traffic reset, which propagated to nodes one round-trip after another.
func TestResetTrafficAcrossNodesPushesConcurrently(t *testing.T) {
setupBulkDB(t)
startSerializedWriter(t)
const nodes = inboundFanoutConcurrency + 1
const email = "reset@x"
bar := newApplyBarrier(inboundFanoutConcurrency)
seedClientAcrossNodes(t, bar, nodes, 46801, email, "88888888-1111-2222-3333-444444444444")
bar.arm()
if _, err := (&ClientService{}).ResetTrafficByEmail(&InboundService{}, email); err != nil {
t.Fatalf("ResetTrafficByEmail across %d node inbounds: %v", nodes, err)
}
if got := bar.maxPar.Load(); got != inboundFanoutConcurrency {
t.Fatalf("peak node pushes in flight = %d, want overlap at the %d cap (barrier timed out: %v)",
got, inboundFanoutConcurrency, bar.expired.Load())
}
}
// TestNodePushGivesUpBeforeTheRemoteTimeout pins that an edit gives up on a node
// that hangs on the push at the deadline, leaving it dirty for the reconcile.
func TestNodePushGivesUpBeforeTheRemoteTimeout(t *testing.T) {
setupBulkDB(t)
startSerializedWriter(t)
useTestRuntimeManager(t)
db := database.GetDB()
const uuid = "77777777-1111-2222-3333-444444444444"
f := newFakeNodeHTTP(t)
ib := realNodeInbound(t, f, 53100, []model.Client{{Email: "hung@x", ID: uuid, SubID: "sub-hung", Enable: true}})
rec := lookupClientRecord(t, "hung@x")
f.setHold(true)
start := time.Now()
if _, err := (&ClientService{}).Update(&InboundService{}, rec.Id, model.Client{
Email: "hung@x", ID: uuid, SubID: "sub-hung", Enable: true, Comment: "edited",
}, 0); err != nil {
t.Fatalf("Update against a hung node: %v", err)
}
elapsed := time.Since(start)
// Both bounds: no push at all would also finish fast and leave the node dirty.
if got := f.hitCount("/clients/update/"); got != 1 {
t.Fatalf("clients/update requests reaching the hung node = %d, want exactly 1", got)
}
if elapsed < nodeClientPushTimeout-200*time.Millisecond {
t.Fatalf("edit returned after %v, before the %v push deadline: the push was never awaited", elapsed, nodeClientPushTimeout)
}
if elapsed >= 2*nodeClientPushTimeout {
t.Fatalf("edit took %v against a hung node: it waited out the remote timeout instead of the %v push deadline", elapsed, nodeClientPushTimeout)
}
var node model.Node
if err := db.Where("id = ?", *ib.NodeID).First(&node).Error; err != nil {
t.Fatalf("read node: %v", err)
}
if !node.ConfigDirty {
t.Fatal("the node whose push timed out must stay dirty, or nothing ever converges it")
}
}
// TestBulkDeleteStopsPushingToAHungNode pins the batch circuit-break: once one
// push times out, the rest of the batch defers to the reconcile as well.
func TestBulkDeleteStopsPushingToAHungNode(t *testing.T) {
setupClientNodeOrderDB(t)
useTestRuntimeManager(t)
f := newFakeNodeHTTP(t)
ib := realNodeInbound(t, f, 53200, []model.Client{
{Email: "b1@x", ID: "66666666-1111-2222-3333-444444444441", SubID: "sub-b1", Enable: true},
{Email: "b2@x", ID: "66666666-1111-2222-3333-444444444442", SubID: "sub-b2", Enable: true},
{Email: "b3@x", ID: "66666666-1111-2222-3333-444444444443", SubID: "sub-b3", Enable: true},
})
f.setHold(true)
start := time.Now()
if _, _, err := (&ClientService{}).BulkDelete(&InboundService{}, []string{"b1@x", "b2@x", "b3@x"}, false); err != nil {
t.Fatalf("BulkDelete against a hung node: %v", err)
}
elapsed := time.Since(start)
if got := f.hitCount("/clients/del/"); got != 1 {
t.Fatalf("clients/del requests sent to the hung node = %d, want 1: the batch kept paying a deadline per client", got)
}
if elapsed >= 2*nodeClientPushTimeout {
t.Fatalf("deleting 3 clients took %v against a hung node, want a single %v deadline, not one per client", elapsed, nodeClientPushTimeout)
}
if err := database.CloseDB(); err != nil {
t.Fatal(err)
}
if err := database.InitDB(filepath.Join(os.Getenv("XUI_DB_FOLDER"), "x-ui.db")); err != nil {
t.Fatal(err)
}
f.setHold(false)
var node model.Node
if err := database.GetDB().First(&node, *ib.NodeID).Error; err != nil {
t.Fatal(err)
}
if err := (&InboundService{}).ReconcileNode(t.Context(), runtime.NewRemote(&node, nil), &node); err != nil {
t.Fatal(err)
}
if got := f.hitCount("/clients/del/"); got != 4 {
t.Fatalf("delete requests including replay after reopening = %d, want 4 (one timed out, then all three retried)", got)
}
}