mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-12-04 00:15:09 +03:00
0db9503228
Isolate traffic synchronization with bounded round-robin per-node workers, independent maintenance and publication, cancellation and ordered shutdown drain. Recheck online eligibility inside the serialized writer before merging snapshots and retain the latest valid speed sample per node, with expiry and explicit empty publication.
Report direct per-node applied/pending/failed outcomes without changing local-save success semantics. Propagate downstream pending separately from direct ACKs so acknowledged batch items do not abort subsequent work. Integrate live admission into the existing #7314 ordered dispatcher with separate four-second queue and batch budgets, retaining monotonic dirty generations, exclusive full-delete/reconcile turns, durable deletion outbox, generation-matched settlement, cross-node recreation safeguards and targeted fingerprint invalidation. Preserve legacy peer compatibility and update generated API documentation.
Validation: all 12 CI jobs and Docs CI passed on reviewed head d13850474f (runs 46871519847 and 46871519831). PostgreSQL logs confirm eight ordering regressions, including saturation, lost ACK, cross-node recreation and bulk recovery. Author reports make verify, focused SQLite/PostgreSQL race tests, four mixed-version node E2E pairings and Xray E2E. No independent production-scale latency measurement or universal HTTP timeout guarantee.
Residual follow-ups: possible conservative false-pending result when a final full delete settles an intermediate superseded intent on the same node; add per-logical-operation settlement regression. Verify shutdown drain under a blocked writer/maintenance worker. These static-review concerns remain open for follow-up, not demonstrated data loss or a new deadlock.
Refs #7293
89 lines
3.0 KiB
Go
89 lines
3.0 KiB
Go
package job
|
|
|
|
import (
|
|
"path/filepath"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
|
|
"github.com/op/go-logging"
|
|
"gorm.io/gorm"
|
|
|
|
"github.com/mhsanaei/3x-ui/v3/internal/database"
|
|
"github.com/mhsanaei/3x-ui/v3/internal/database/dbtest"
|
|
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
|
|
xuilogger "github.com/mhsanaei/3x-ui/v3/internal/logger"
|
|
"github.com/mhsanaei/3x-ui/v3/internal/web/runtime"
|
|
"github.com/mhsanaei/3x-ui/v3/internal/xray"
|
|
)
|
|
|
|
func TestBackgroundNodeJobs_SkipNodeDashboardStatQueries(t *testing.T) {
|
|
xuilogger.InitLogger(logging.ERROR)
|
|
dbtest.InitDB(t, filepath.Join(t.TempDir(), "x-ui.db"))
|
|
runtime.SetManager(runtime.NewManager(runtime.LocalDeps{APIPort: func() int { return 0 }, SetNeedRestart: func() {}}))
|
|
t.Cleanup(func() { runtime.SetManager(nil) })
|
|
|
|
db := database.GetDB()
|
|
disabled := &model.Node{Name: "disabled", Address: "127.0.0.1", Port: 2053, ApiToken: "tok", Enable: true, Status: "offline"}
|
|
if err := db.Create(disabled).Error; err != nil {
|
|
t.Fatalf("create node: %v", err)
|
|
}
|
|
if err := db.Model(disabled).Update("enable", false).Error; err != nil {
|
|
t.Fatalf("disable node: %v", err)
|
|
}
|
|
nid := disabled.Id
|
|
ib := &model.Inbound{Tag: "n-in", Enable: true, Port: 443, Protocol: model.VLESS, Settings: `{"clients":[]}`, NodeID: &nid}
|
|
if err := db.Create(ib).Error; err != nil {
|
|
t.Fatalf("create inbound: %v", err)
|
|
}
|
|
if err := db.Create(&model.ClientRecord{Id: 1, Email: "c@example.com", Enable: true}).Error; err != nil {
|
|
t.Fatalf("create client: %v", err)
|
|
}
|
|
if err := db.Create(&model.ClientInbound{ClientId: 1, InboundId: ib.Id}).Error; err != nil {
|
|
t.Fatalf("create client_inbound: %v", err)
|
|
}
|
|
if err := db.Create(&xray.ClientTraffic{InboundId: ib.Id, Email: "c@example.com", Enable: true}).Error; err != nil {
|
|
t.Fatalf("create client_traffic: %v", err)
|
|
}
|
|
|
|
var mu sync.Mutex
|
|
var queries []string
|
|
recordSQL := func(tx *gorm.DB) {
|
|
if sql := tx.Statement.SQL.String(); sql != "" {
|
|
mu.Lock()
|
|
queries = append(queries, sql)
|
|
mu.Unlock()
|
|
}
|
|
}
|
|
const cbName = "test:job-node-enum-sql"
|
|
if err := db.Callback().Query().After("gorm:query").Register(cbName, recordSQL); err != nil {
|
|
t.Fatalf("register query callback: %v", err)
|
|
}
|
|
if err := db.Callback().Row().After("gorm:row").Register(cbName, recordSQL); err != nil {
|
|
t.Fatalf("register row callback: %v", err)
|
|
}
|
|
if err := db.Callback().Raw().After("gorm:raw").Register(cbName, recordSQL); err != nil {
|
|
t.Fatalf("register raw callback: %v", err)
|
|
}
|
|
t.Cleanup(func() {
|
|
_ = db.Callback().Query().Remove(cbName)
|
|
_ = db.Callback().Row().Remove(cbName)
|
|
_ = db.Callback().Raw().Remove(cbName)
|
|
})
|
|
|
|
job := NewNodeTrafficSyncJob()
|
|
job.Run()
|
|
job.workers.Wait()
|
|
job.Close()
|
|
NewNodeHeartbeatJob().Run()
|
|
|
|
for _, q := range queries {
|
|
lower := strings.ToLower(q)
|
|
if strings.Contains(lower, "inbounds.node_id is not null") ||
|
|
strings.Contains(lower, "count(distinct client_inbounds.client_id)") ||
|
|
strings.Contains(lower, "join client_traffics ct") {
|
|
t.Errorf("background node job executed dashboard node-stats query: %s", q)
|
|
}
|
|
}
|
|
}
|