Files
webui4frpc/internal/cluster/ring.go
JianFeeeee c69fb2caba feat(cluster): 负载均衡改为每节点自主「先撤销本地转发,再丢回环」
按用户要求重做:不再由 leader 单独调度,而是**每个节点对自己的转发跑同一条
规则**,超了就自己撤销一条、把任务丢回环上,由环原有的「最闲者认领」决定它
落到哪台。

## 为什么必须是 owner 自己动手(不是洁癖,是实测逼出来的)

真机探针(跑完已删)显示,真实 revoke 之后:

    TopologyOwner after revoke = "n1"     ← 条目还在(只标记不删除)
    n1 Forwards=3  score=310.0            ← 负载根本没降
    after step: revoking=true publishing=false

⇒ 一节点的负载**只有它自己的拓扑条目消失时才会下降**,而条目是本地簿记,没有任何
对端可以代它写。leader 从外面只能看见失衡、无法让失衡成为事实——它必须伸手去改
另一个节点的簿记,而这正是这个环到处都在避免的跨节点写(revoke / restart 定向往
owner 也是同一个理由)。

⇒ 而且旧实现**根本跑不起来**:phase 1 判据是「条目消失」,可 mark-don't-remove
让它永远不消失,于是每 120s 打一条 `revoking … timed out; abandoning`,
**phase 2 永不执行,一条转发都不会迁**。开 `W4F_BALANCE_MODE=on` 只会刷日志。

## 规则(替代原来的 ratio 阈值)

    当 Forwards >= 2 且 Forwards > ceil(总数/存活节点数) 时,撤销一条

- **自限**:3 条 / 3 台 → owner 2 > 1,卸一条 → 1,`1 > 1` 不成 → 停。
  没有上界的话 owner 会把转发**全部卸光**。
- **不会卸空**:低于下限(2)永不撤销。
- 平摊值向上取整(3/3=1,不是 0),否则 1 条 / 3 台的节点也算「超了」。
- **不再用 mem/net 参与判据**:实测 `.30` 空闲时 netPct 能到 55%,Score 会高于
  有转发的节点——用 Score 会把纯网络抖动当成负载。
- 节奏用**每节点冷却**(默认 600s)而不是「失衡持续多久」窗口:规则本身已经要求
  真实失衡,没有需要平滑的瞬态,只有需要限制的抖动频率。

## 为什么必须是新的 Release 任务类型,不能复用 Revoke

用户停止(Revoke)**保留拓扑条目**作为 disabled 标志的载体;均衡释放必须**删除
条目**,因为 (a) 负载由自己的条目数算出,条目不删负载不降;(b) 每个节点的
duplicate-guard 会拒绝「已有 owner 的转发」的创建任务——保留条目会让**所有节点
都拒领**,转发被永久搁死在刚放弃它的那台机器上。

Release 因此:`Handler.Release`(停 worker,但**不标记 stopped**)→ 删条目 →
`SubmitTask` 以**启用状态**重新发布。它需要自己的 Handler 方法,不能回落到
Revoke:Revoke 会顺手把 link 标成 stopped,而这条转发即将在别的节点上跑。

## 顺带修掉两个既有缺口(实测确认)

1. **`submitMigrate` 发布不出去**:`OnToken` 的 localPending 重合并只保留「从未
   publish 过」的本地 pending,而只有 `Add*` 系列会记 `published`。引擎里直接写
   `PendingTasks` 的任务会在下一次 adopt 时被静默丢弃。
   ⇒ 新 `ReleaseTask` 走 `AddPending`。
   (`SubmitRestart` 有同样的毛病,但它不在本次范围,未改动——见提交说明末尾。)
2. `Release` 需要绕过 duplicate-claim 守卫(同 Restart),否则「owner 本来就持有
   这条转发」这一前提会让每条 Release 都被丢掉。

## 测试(17 条,全绿)

规则:平摊值取整、超了才撤、撤到平摊就停、单节点不撤、低于下限不撤、不撤已停
转发、候选选择确定。
机制:Revoke 留载体 vs Release 删条目(这是两种语义的核心差异)、Release 只由
owner 执行且不消费他人的任务、owner 释放后环的普通认领把它接走。
模式:dry-run 不动任何东西、off 真的什么都不做、未知模式回落 dry-run、
冷却限制抖动频率、确认超时清理。
只读:Report 不变更任何状态、能解释「超了但不动」的原因、均衡节点给出真实答案。

**双向验证 3 条**(改坏→变红→还原→变绿):
- 去掉 Release 的 duplicate-guard 例外 → `the owner released 0 times, want 1`
- 去掉「不超过平摊值」上界 → `2 owned against share 2 must not release`
- Release 改成只标记不删除 → `n1 should now own 2, got 3`

go build / go vet / go test ./... 全绿(go clean -testcache 后逐包实跑)。

## 未做 / 待办

- `SubmitRestart` 的同类发布缺口未修(同样会被 adopt 丢弃),需要单独确认它
  在实机上是否真的丢过 restart。
- UI 尚未跟进新报告结构(下一步)。
2026-10-06 14:03:03 +08:00

498 lines
16 KiB
Go

// Package cluster implements the token-ring cooperative network.
// Authoritative design: plan.md §M6 (令牌环网拓扑).
//
// Token payload semantics (per the design):
// - PendingTasks: forward requests (中间转发意图) riding in the token,
// waiting to be claimed by the lowest-load node. Once claimed, the task
// disappears from PendingTasks.
// - Topology: the cluster's ACTIVE forward map — every established forward
// with its owning node. The claiming node appends it back into the token
// so every member converges on a complete picture (who runs what).
package cluster
import (
"fmt"
"sort"
"time"
"webui4frpc/internal/store"
)
// ---- 数据面 ---- //
// Node is one cluster member in the ring.
type Node struct {
ID string `json:"id"`
Addr string `json:"addr"`
IsLeader bool `json:"isLeader,omitempty"`
Alive bool `json:"alive"`
Load Load `json:"load"`
Version string `json:"version,omitempty"`
Cache []string `json:"cache,omitempty"`
LastSeen int64 `json:"lastSeen,omitempty"`
// NodeKey is this member's cluster admission key. It travels in the
// token so every node knows every peer's key — a crashed node can
// rejoin via ANY cached peer by presenting that peer's key. Without
// this, a rejoiner would only know its original sponsor's key (from
// the -join-key flag) and couldn't rejoin through a different peer.
NodeKey string `json:"nodeKey,omitempty"`
}
// Load is the combined load metric used to pick the task claimer.
type Load struct {
MemPct float64 `json:"memPct"`
NetPct float64 `json:"netPct"`
Forwards int `json:"forwards,omitempty"` // active forwards owned by this node (primary signal)
}
// Score weights owned forwards heavily so the node with the fewest active
// forwards is picked first; mem/net only break ties at equal forward count.
func (l Load) Score() float64 { return float64(l.Forwards)*100 + l.MemPct + l.NetPct }
// Task is a PENDING forward request circulated in the token. It carries the
// intermediate forwarding intent (local/remote/link) — NOT a rendered frpc
// config. The claiming node renders its local frpc config and spawns the worker.
type Task struct {
ID string `json:"id"`
Local store.Local `json:"local"`
Remote store.Remote `json:"remote"`
Link store.Link `json:"link"`
Created int64 `json:"created"`
// Revoke marks a REVOCATION task: instead of creating the forward, the
// owning node cancels it (stop worker, drop from topology). Reuses the
// same publish channel as creation (round-1 inject, round-2 apply).
Revoke bool `json:"revoke,omitempty"`
// Restart marks a RE-ENABLE task: the forward is already claimed and its
// topology entry still exists (a stop keeps the entry as the flag's
// carrier), so the creation path would dedupe the submission and the
// duplicate-claim guard would discard it. The owner applies this one
// unconditionally: re-mark the entry enabled and (re)spawn the worker.
Restart bool `json:"restart,omitempty"`
// Release marks a REBALANCING task: the OWNER of the forward gives it up so
// the ring can place it elsewhere. Only the owning node may act on it — it is
// that node's own load which has to fall, and only it can make that true (its
// topology entry is local bookkeeping no peer may write).
//
// It is deliberately NOT the same thing as a user stop (Revoke). A stop keeps
// the topology entry as the carrier of the disabled flag and flags the link
// stopped; a release REMOVES the entry and leaves the link enabled, because
// the forward is about to run somewhere else. Marking it stopped instead would
// make the re-published creation task look like a duplicate of an existing
// forward, and every node would refuse to claim it — stranding the forward on
// the node that just gave it up.
Release bool `json:"release,omitempty"`
// RemoveNode: node ID to remove from the ring; the node self-removes when
// the command reaches it via the token.
RemoveNode string `json:"removeNode,omitempty"`
}
// TopoEntry is an ESTABLISHED forward in the cluster topology: who (OwnerID)
// runs which forward (rendered from the same intermediate intent).
type TopoEntry struct {
TaskID string `json:"taskId"`
OwnerID string `json:"ownerId"`
Local store.Local `json:"local"`
Remote store.Remote `json:"remote"`
Link store.Link `json:"link"`
Active bool `json:"active"`
}
// State is the full cluster picture, replicated on every node:
// ring membership + pending tasks + active forward topology.
type State struct {
LeaderID string `json:"leaderId"`
Nodes []Node `json:"nodes"`
Cycle int64 `json:"cycle"`
// RoundDelay rides the token so every node converges on the same ring
// cadence (serialized as nanoseconds; the leader refreshes it each round
// via tkDelaySince). MUST be serialized — otherwise a token round-trip
// zeroed it and LossTimeout collapsed to the floor.
RoundDelay time.Duration `json:"roundDelay,omitempty"`
// PendingTasks: not yet claimed (disappear when claimed).
PendingTasks map[string]*Task `json:"pendingTasks,omitempty"`
// Topology: active forwards owned by members (full cluster view).
Topology map[string]*TopoEntry `json:"topology,omitempty"`
}
// Token is the circulating message: one physical token per cycle (single
// round — adopt + append + execute in one pass, no phase split).
type Token struct {
Cycle int64 `json:"cycle"`
State State `json:"state"`
Log []LogEntry `json:"logDelta,omitempty"`
SentAt int64 `json:"sentAt,omitempty"`
}
// ---- 环图操作 ---- //
func (s *State) Find(id string) int {
for i, n := range s.Nodes {
if n.ID == id {
return i
}
}
return -1
}
func (s *State) AliveSuccessor(id string) (string, bool) {
i := s.Find(id)
if i < 0 {
return "", false
}
n := len(s.Nodes)
for k := 1; k <= n; k++ {
j := (i + k) % n
if s.Nodes[j].Alive {
return s.Nodes[j].ID, true
}
}
return "", false
}
func (s *State) AlivePredecessor(id string) (string, bool) {
i := s.Find(id)
if i < 0 {
return "", false
}
n := len(s.Nodes)
for k := 1; k <= n; k++ {
j := ((i-k)%n + n) % n
if s.Nodes[j].Alive {
return s.Nodes[j].ID, true
}
}
return "", false
}
func (s *State) UpsertNode(n Node) {
i := s.Find(n.ID)
if i < 0 {
if n.IsLeader {
s.LeaderID = n.ID
}
n.Alive = true
n.LastSeen = time.Now().Unix()
s.Nodes = append(s.Nodes, n)
return
}
old := s.Nodes[i]
n.IsLeader = old.IsLeader || n.IsLeader
if n.IsLeader {
s.LeaderID = n.ID
}
n.Alive = true
n.LastSeen = time.Now().Unix()
s.Nodes[i] = n
}
func (s *State) MarkOffline(id string) int {
i := s.Find(id)
if i < 0 {
return -1
}
s.Nodes[i].Alive = false
s.Nodes[i].LastSeen = time.Now().Unix()
return i
}
// InsertAfter inserts newNode right after anchor (join: newNode lands behind
// its sponsor; the former successor moves after the newcomer).
func (s *State) InsertAfter(anchor string, n Node) {
i := s.Find(n.ID)
if i >= 0 {
s.Nodes[i].Alive = true
s.Nodes[i].LastSeen = time.Now().Unix()
return
}
n.Alive = true
n.LastSeen = time.Now().Unix()
j := s.Find(anchor)
if j < 0 {
s.Nodes = append(s.Nodes, n)
return
}
out := make([]Node, 0, len(s.Nodes)+1)
out = append(out, s.Nodes[:j+1]...)
out = append(out, n)
out = append(out, s.Nodes[j+1:]...)
s.Nodes = out
}
// LowestAlive returns alive node with lowest combined load (nil if none).
func (s *State) LowestAlive() *Node {
var best *Node
for i := range s.Nodes {
if !s.Nodes[i].Alive {
continue
}
if best == nil || s.Nodes[i].Load.Score() < best.Load.Score() {
b := s.Nodes[i]
best = &b
}
}
return best
}
func (s *State) NextTaskID() string {
// Collision-free id allocation by scanning the ids actually in flight
// (pending + topology) and taking max+1. The ring is mutually exclusive
// (one token holder at a time), so when a node mints an id it has the
// authoritative full view — scanning existing ids guarantees a fresh id.
// n is tiny (handful of forwards). No Seq counter is needed: a cross-node
// Seq was previously adopted wholesale on every OnToken (e.state = tk.State),
// which dropped local increments and could regress below an id still in use,
// recycling it and overwriting an active topology entry.
max := int64(0)
for id := range s.PendingTasks {
if n := taskIDNum(id); n > max {
max = n
}
}
for _, e := range s.Topology {
if n := taskIDNum(e.TaskID); n > max {
max = n
}
}
return fmt.Sprintf("t%d", max+1)
}
// taskIDNum extracts the numeric suffix of a task id "t12" -> 12 (0 if it does
// not parse). Used only to keep NextTaskID collision-free.
func taskIDNum(id string) int64 {
if len(id) < 2 || id[0] != 't' {
return 0
}
var n int64
for _, c := range id[1:] {
if c < '0' || c > '9' {
return 0
}
n = n*10 + int64(c-'0')
}
return n
}
// AddRemoveNode publishes a node-removal command via the token; the target
// node self-removes when it receives the command (re-queue forwards, drop
// ring position, pass token to former successor).
func (s *State) AddRemoveNode(nodeID string) *Task {
t := &Task{
ID: s.NextTaskID(), Created: time.Now().Unix(), RemoveNode: nodeID,
}
if s.PendingTasks == nil {
s.PendingTasks = map[string]*Task{}
}
s.PendingTasks[t.ID] = t
return t
}
// AddRevoke publishes a REVOCATION task (same channel as creation): when a
// node wants to cancel an established forward, it injects a Revoke task;
// the owning node cancels the worker and drops it from topology.
func (s *State) AddRevoke(local store.Local, remote store.Remote, link store.Link) *Task {
t := &Task{
ID: s.NextTaskID(),
Local: local,
Remote: remote,
Link: link,
Created: time.Now().Unix(),
Revoke: true,
}
if s.PendingTasks == nil {
s.PendingTasks = map[string]*Task{}
}
s.PendingTasks[t.ID] = t
return t
}
// AddPending attaches a NEW forward request to the token (round-1 inject).
func (s *State) AddPending(local store.Local, remote store.Remote, link store.Link) *Task {
t := &Task{
ID: s.NextTaskID(),
Local: local,
Remote: remote,
Link: link,
Created: time.Now().Unix(),
}
if s.PendingTasks == nil {
s.PendingTasks = map[string]*Task{}
}
s.PendingTasks[t.ID] = t
return t
}
// PendingList returns pending tasks, stable by id.
func (s *State) PendingList() []*Task {
out := make([]*Task, 0, 4)
for _, t := range s.PendingTasks {
out = append(out, t)
}
sort.Slice(out, func(i, j int) bool { return out[i].ID < out[j].ID })
return out
}
// ClaimPending removes a pending task and returns it (the claimer then renders
// its local frpc config and spawns the worker). Returns nil if absent.
func (s *State) ClaimPending(id string) *Task {
if s.PendingTasks == nil {
return nil
}
t := s.PendingTasks[id]
if t == nil {
return nil
}
delete(s.PendingTasks, id)
return t
}
// AddTopology records an ESTABLISHED forward into the cluster topology.
// Called by the claiming node right after it spawns the worker: the task
// disappears from pending and lives on as active topology.
func (s *State) AddTopology(t *Task, ownerID string) *TopoEntry {
if s.Topology == nil {
s.Topology = map[string]*TopoEntry{}
}
// Active mirrors the task's disabled flag rather than being unconditionally
// true. A claim can legitimately carry a stopped forward (e.g. a node
// re-claiming from a departed peer), and forcing Active=true there would
// resurrect it: OfflineReassign only re-queues Active entries, and every
// reader that filters on Active would treat it as running again.
e := &TopoEntry{
TaskID: t.ID, OwnerID: ownerID,
Local: t.Local, Remote: t.Remote, Link: t.Link,
Active: !t.Link.Disabled,
}
s.Topology[t.ID] = e
return e
}
// SelfRemove removes this node from the ring: re-queues its own forwards
// as pending (so another member claims them), drops its topology entries
// and removes its ring position. Returns the re-queued task ids.
func (s *State) SelfRemove(nodeID string) []string {
reattached := s.OfflineReassign(nodeID)
i := s.Find(nodeID)
if i >= 0 {
s.Nodes = append(s.Nodes[:i], s.Nodes[i+1:]...)
}
if s.LeaderID == nodeID {
s.LeaderID = ""
}
return reattached
}
// RemoveTopology drops the active forward for the given local/remote/port
// (returns true if removed — the owning node revokes it).
func (s *State) RemoveTopology(local, remote string, port int) bool {
for id, e := range s.Topology {
if e.Local.Name == local && e.Remote.Name == remote && e.Link.RemotePort == port {
delete(s.Topology, id)
return true
}
}
return false
}
// TopologyOwner returns the node that owns the active forward matching the
// given task (by local/remote/port), or "" if no such entry exists. Used to
// route a revocation to the OWNING node (plan §任务撤销: "持有该转发的节点
// 收到撤销任务后取消") instead of letting any lowest-load node claim it.
func (s *State) TopologyOwner(t *Task) string {
for _, e := range s.Topology {
if e.Local.Name == t.Local.Name && e.Remote.Name == t.Remote.Name && e.Link.RemotePort == t.Link.RemotePort {
return e.OwnerID
}
}
return ""
}
// TopologyList returns active forwards, stable by task id.
func (s *State) TopologyList() []*TopoEntry {
out := make([]*TopoEntry, 0, len(s.Topology))
for _, e := range s.Topology {
out = append(out, e)
}
sort.Slice(out, func(i, j int) bool { return out[i].TaskID < out[j].TaskID })
return out
}
// ForwardsOwnedBy returns topology entries owned by a node (for offline
// reassignment and UI per-node views).
func (s *State) ForwardsOwnedBy(owner string) []*TopoEntry {
var out []*TopoEntry
for _, e := range s.Topology {
if e.OwnerID == owner {
out = append(out, e)
}
}
return out
}
// UpdateTopologyGroup sets the group label on the topology entry matching the
// (local, remote, port) triple. Returns true if found. The updated entry
// propagates to all nodes via the next token cycle — group changes sync
// through the ring without a dedicated command.
// UpdateTopologyDisabled sets the disabled flag on the topology entry matching
// the forward's natural key. Mirror of UpdateTopologyGroup: it makes a locally
// decided stop/start part of the topology so the next token cycle carries it to
// every other member (the alternative — a one-shot revoke task addressed at the
// owner — only converges if that owner happens to be online right then).
//
// Returns false when the forward is not in the topology, which is not an error:
// a stopped forward may legitimately have no entry yet.
func (s *State) UpdateTopologyDisabled(local, remote string, port int, disabled bool) bool {
for _, e := range s.Topology {
if e.Local.Name == local && e.Remote.Name == remote && e.Link.RemotePort == port {
e.Link.Disabled = disabled
// Keep Active consistent with the flag so readers that key off it
// (status page, load accounting) agree with Link.Disabled.
e.Active = !disabled
return true
}
}
return false
}
// TopologyDisabled returns the disabled flag the cluster currently holds for a
// forward, and whether such an entry exists. Used by the adoption path to learn
// a peer's decision into the local store.
func (s *State) TopologyDisabled(local, remote string, port int) (bool, bool) {
for _, e := range s.Topology {
if e.Local.Name == local && e.Remote.Name == remote && e.Link.RemotePort == port {
return e.Link.Disabled, true
}
}
return false, false
}
func (s *State) UpdateTopologyGroup(local, remote string, port int, group string) bool {
for _, e := range s.Topology {
if e.Local.Name == local && e.Remote.Name == remote && e.Link.RemotePort == port {
e.Link.Group = group
return true
}
}
return false
}
// OfflineReassign moves all active forwards owned by an offline node back into
// PendingTasks (they become new tasks for the next lowest-load member). Returns
// the reassigned task ids.
func (s *State) OfflineReassign(offlineID string) []string {
var reattached []string
for id, e := range s.Topology {
if e.OwnerID != offlineID || !e.Active {
continue
}
delete(s.Topology, id)
t := &Task{ID: id, Local: e.Local, Remote: e.Remote, Link: e.Link,
Created: time.Now().Unix()}
if s.PendingTasks == nil {
s.PendingTasks = map[string]*Task{}
}
s.PendingTasks[id] = t
reattached = append(reattached, id)
}
return reattached
}