Files
webui4frpc/internal/cluster/ring_handler.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

104 lines
3.2 KiB
Go

// Runtime handlers the ring engine needs, provided by the app:
// - RuntimeLoad: sample this node's memory/network load;
// - Claim: create the frpc worker/forward for a claimed task.
package cluster
import (
"context"
"os"
"runtime"
)
// AppHandler is the app-provided bridge for ring load sampling + task claims.
type AppHandler struct {
// LoadFn returns (mem%, net%) — lower is more idle.
LoadFn func() (memPct, netPct float64)
// ClaimFn creates the forward on this node (persist + spawn worker).
ClaimFn func(ctx context.Context, tk *Task) error
// RevokeFn cancels the forward on this node (stop worker + drop from store).
RevokeFn func(ctx context.Context, tk *Task) error
// RestartFn re-enables an already-claimed forward on this node (clear the
// stopped flag + bring the worker back). Required because a stop keeps the
// topology entry, so the normal claim path will not re-create it.
RestartFn func(ctx context.Context, tk *Task) error
// ReleaseFn stops this node's worker for a forward being handed back to the
// ring, leaving the link ENABLED. When nil, Release falls back to RevokeFn —
// which also stops the worker, but additionally marks the link stopped; that
// is the pre-existing stop semantics and is wrong for a rebalance, so the app
// should provide this.
ReleaseFn func(ctx context.Context, tk *Task) error
}
// RuntimeLoad implements Handler.
func (a *AppHandler) RuntimeLoad() Load {
mem, net := 0.0, 0.0
if a.LoadFn != nil {
mem, net = a.LoadFn()
}
return Load{MemPct: mem, NetPct: net}
}
// Claim implements Handler.
func (a *AppHandler) Claim(ctx context.Context, tk *Task) error {
if a.ClaimFn == nil {
return nil
}
return a.ClaimFn(ctx, tk)
}
// Revoke implements Handler.
func (a *AppHandler) Revoke(ctx context.Context, tk *Task) error {
if a.RevokeFn == nil {
return nil
}
return a.RevokeFn(ctx, tk)
}
// Release implements Handler.
func (a *AppHandler) Release(ctx context.Context, tk *Task) error {
if a.ReleaseFn != nil {
return a.ReleaseFn(ctx, tk)
}
// Fall back to the stop path so a host that has not provided ReleaseFn still
// stops the worker (the forward keeps running otherwise). The link ends up
// flagged stopped, which the claim path may then refuse — the host is warned
// by the ring's own "re-publish was deduped" log line rather than silently
// leaving two workers up.
if a.RevokeFn != nil {
return a.RevokeFn(ctx, tk)
}
return nil
}
// Restart implements Handler.
func (a *AppHandler) Restart(ctx context.Context, tk *Task) error {
if a.RestartFn == nil {
return nil
}
return a.RestartFn(ctx, tk)
}
// SampleMemLoad returns a cheap memory-usage percentage (0..100).
func SampleMemLoad() float64 {
var m runtime.MemStats
runtime.ReadMemStats(&m)
total := m.Sys
if total == 0 {
return 0
}
return float64(m.Alloc) / float64(total) * 100
}
// SampleNetLoad is implemented in netload_linux.go for Linux and returns a
// safe zero fallback on other platforms. It is kept here as a package-level
// API because the ring handler injects it into RuntimeLoad.
// HostID returns a stable node id (hostname; empty fallback to pid).
func HostID() string {
h, _ := os.Hostname()
if h == "" {
return "node"
}
return h
}