mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-12-03 16:04:50 +03:00
91d5ff400d
Group compatible client creates by destination and effective payload via the existing clients/add API. Resolve node-local inbound IDs, preserve distinct flow/address/credential payloads, bound delivery concurrency and use a shared four-second remote delivery budget after local commits.
Advance reconciliation fingerprints only for acknowledged intended settings, retaining previous-fingerprint CAS rather than stamping unrelated rebased changes. Preserve FIFO turns, node-wide barriers, dirty generations, durable delete supersession, partial and uncertain delivery recovery and downstream outcomes. Protect later reattachments in final Delete cleanup and complete Create metadata before remote delivery.
Validation: all 12 CI jobs passed on 63e70cda38 (run 46935177853). PostgreSQL logs confirm 21 ordering/grouped-create tests and prior reporter-lock, reconciliation and traffic contention gates; node E2E confirms all four version pairings. Author reports SQLite/PostgreSQL race tests, make verify and isolated full make race.
Author-reported paired PostgreSQL 18 benchmark: 11 -> 1 RPC, fixed 200 ms peer delay 11-target p95 1552.0 -> 731.9 ms, timeout probe 12.41–12.42 -> 4.41–4.50 s. Size-dependent peer cost can instead cause pending delivery (1.74 -> 4.51 s with 0.74 s recovery); four stalled nodes can defer a fifth. Monitor production convergence and pending rate. No general production latency or allocation improvement claimed.
Refs #7299
198 lines
6.9 KiB
Go
198 lines
6.9 KiB
Go
package nodee2e
|
|
|
|
import (
|
|
"bytes"
|
|
"compress/gzip"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"net/http/httputil"
|
|
"net/url"
|
|
"os"
|
|
"slices"
|
|
"strconv"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
|
|
"github.com/mhsanaei/3x-ui/v3/internal/web/runtime"
|
|
)
|
|
|
|
func TestNodeSnapshotVersionCompatibility(t *testing.T) {
|
|
current := panelBinary(t)
|
|
old := os.Getenv("XUI_NODE_E2E_OLD_BINARY")
|
|
for _, version := range []struct {
|
|
name string
|
|
master, node string
|
|
compact bool
|
|
}{
|
|
{"new-master-new-node", current, current, true},
|
|
{"new-master-old-node", current, old, false},
|
|
{"old-master-new-node", old, current, false},
|
|
{"old-master-old-node", old, old, false},
|
|
} {
|
|
t.Run(version.name, func(t *testing.T) {
|
|
if version.master == "" || version.node == "" {
|
|
t.Skip("set XUI_NODE_E2E_OLD_BINARY to a baseline panel binary")
|
|
}
|
|
t.Parallel()
|
|
master := newPanel(t, version.master, "master")
|
|
node := newPanel(t, version.node, "node")
|
|
token := node.mintToken("snapshot-link", "node-sync")
|
|
master.start()
|
|
node.start()
|
|
var lists, compact, full, unchanged, clientAdds atomic.Int32
|
|
target, err := url.Parse(node.url())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
proxy := httputil.NewSingleHostReverseProxy(target)
|
|
proxy.ModifyResponse = func(response *http.Response) error {
|
|
if response.Request.URL.Path != "/panel/api/inbounds/trafficSnapshot" || response.StatusCode != http.StatusOK {
|
|
return nil
|
|
}
|
|
data, err := io.ReadAll(response.Body)
|
|
_ = response.Body.Close()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
response.Body = io.NopCloser(bytes.NewReader(data))
|
|
if response.Header.Get("Content-Encoding") == "gzip" {
|
|
reader, err := gzip.NewReader(bytes.NewReader(data))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
decoded, readErr := io.ReadAll(reader)
|
|
_ = reader.Close()
|
|
if readErr != nil {
|
|
return readErr
|
|
}
|
|
data = decoded
|
|
}
|
|
var env struct {
|
|
Obj struct {
|
|
Structure json.RawMessage `json:"structure"`
|
|
} `json:"obj"`
|
|
}
|
|
if err := json.Unmarshal(data, &env); err != nil {
|
|
return err
|
|
}
|
|
if len(env.Obj.Structure) > 0 && string(env.Obj.Structure) != "null" {
|
|
full.Add(1)
|
|
} else {
|
|
unchanged.Add(1)
|
|
}
|
|
return nil
|
|
}
|
|
front := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
|
switch req.URL.Path {
|
|
case "/panel/api/inbounds/list":
|
|
lists.Add(1)
|
|
case "/panel/api/inbounds/trafficSnapshot":
|
|
compact.Add(1)
|
|
case "/panel/api/clients/add":
|
|
clientAdds.Add(1)
|
|
}
|
|
proxy.ServeHTTP(w, req)
|
|
}))
|
|
t.Cleanup(front.Close)
|
|
address, err := url.Parse(front.URL)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
port, err := strconv.Atoi(address.Port())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
inboundPort := freePort(t)
|
|
secondaryPort := freePort(t)
|
|
var source inboundView
|
|
if err := json.Unmarshal(node.call(http.MethodPost, "/panel/api/inbounds/add", vlessInbound("matrix", inboundPort, nil, vlessClient("matrix-first"))), &source); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if version.master == current {
|
|
node.call(http.MethodPost, "/panel/api/inbounds/add", vlessInbound("matrix-secondary", secondaryPort, nil))
|
|
}
|
|
master.call(http.MethodPost, "/panel/api/nodes/add", map[string]any{"name": "matrix-node", "scheme": "http", "address": address.Hostname(), "port": port, "basePath": "/", "apiToken": token, "enable": true, "allowPrivateAddress": true})
|
|
eventually(t, settleTimeout, "version pair synchronizes and reaches steady state", func() (bool, string) {
|
|
ib, ok := master.inboundOnPort(inboundPort)
|
|
ready := lists.Load() >= 3
|
|
if version.compact {
|
|
ready = unchanged.Load() >= 2
|
|
}
|
|
return ok && hasAll(ib.emails(), "matrix-first") && ready, fmt.Sprintf("adopted=%v lists=%d compact=%d unchanged=%d", ok, lists.Load(), compact.Load(), unchanged.Load())
|
|
})
|
|
if !version.compact && compact.Load() != 0 {
|
|
t.Fatalf("legacy peer received compact calls: %d", compact.Load())
|
|
}
|
|
before := full.Load()
|
|
node.call(http.MethodPost, "/panel/api/clients/add", map[string]any{"client": map[string]any{"email": "matrix-added", "enable": true}, "inboundIds": []int{source.Id}})
|
|
eventually(t, settleTimeout, "node edit after unchanged polls reaches master", func() (bool, string) {
|
|
ib, ok := master.inboundOnPort(inboundPort)
|
|
return ok && hasAll(ib.emails(), "matrix-first", "matrix-added"), fmt.Sprint(ib.emails())
|
|
})
|
|
if version.compact && full.Load() <= before {
|
|
t.Fatal("node edit did not refresh catalog")
|
|
}
|
|
if version.compact {
|
|
before = full.Load()
|
|
node.stop()
|
|
node.start()
|
|
eventually(t, settleTimeout, "node restart invalidates the old catalog", func() (bool, string) { return full.Load() > before, fmt.Sprint(full.Load()) })
|
|
before = full.Load()
|
|
master.stop()
|
|
master.start()
|
|
eventually(t, settleTimeout, "master restart reacquires complete structure", func() (bool, string) { return full.Load() > before, fmt.Sprint(full.Load()) })
|
|
}
|
|
if version.master == current {
|
|
first, hasFirst := master.inboundOnPort(inboundPort)
|
|
second, hasSecond := master.inboundOnPort(secondaryPort)
|
|
if !hasFirst || !hasSecond {
|
|
t.Fatal("master did not adopt both inbounds")
|
|
}
|
|
const email = "matrix-shared-create"
|
|
master.call(http.MethodPost, "/panel/api/clients/add", map[string]any{
|
|
"client": map[string]any{"email": email, "enable": true}, "inboundIds": []int{first.Id, second.Id},
|
|
})
|
|
if got := clientAdds.Load(); got != 1 {
|
|
t.Fatalf("node-sync received %d add requests, want one", got)
|
|
}
|
|
var tags []string
|
|
for _, port := range []int{inboundPort, secondaryPort} {
|
|
if ib, ok := node.inboundOnPort(port); !ok || ib.client(email) == nil {
|
|
t.Fatalf("node did not apply the grouped client on port %d", port)
|
|
} else {
|
|
tags = append(tags, ib.Tag)
|
|
}
|
|
}
|
|
admin := runtime.NewRemote(&model.Node{
|
|
Name: "matrix-admin", Scheme: "http", Address: address.Hostname(), Port: port,
|
|
BasePath: "/", ApiToken: node.token, Enable: true, AllowPrivateAddress: true,
|
|
}, nil)
|
|
const adminEmail = "matrix-admin-create"
|
|
raw, err := json.Marshal(vlessClient(adminEmail))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ctx, cancel := context.WithTimeout(t.Context(), 5*time.Second)
|
|
defer cancel()
|
|
if _, ack, err := admin.AddClientGroup(ctx, tags, raw); err != nil || !slices.Equal(ack, []bool{true, true}) {
|
|
t.Fatalf("admin grouped add acknowledged %v, err=%v", ack, err)
|
|
}
|
|
if got := clientAdds.Load(); got != 2 {
|
|
t.Fatalf("admin sent %d add requests, want one", got-1)
|
|
}
|
|
for _, port := range []int{inboundPort, secondaryPort} {
|
|
if ib, ok := node.inboundOnPort(port); !ok || ib.client(adminEmail) == nil {
|
|
t.Fatalf("admin grouped client absent on port %d", port)
|
|
}
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|