Skip to content

A1-5: Populate NodeInfo stats via gossip heartbeat #132

Closed

Description

@scttfrdmn

Context

NodeInfo in cluster.go has CPUUsage, MemoryUsage, DiskUsage, and CacheHitRate fields that are never populated. The StrategyLeastLoad load balancer therefore always sees zero load on all nodes and makes random selections rather than informed ones. Real stats are needed for cache-warming decisions in v0.12.0.

What to implement

Add to internal/distributed/gossip.go:

MessageTypeNodeStats MessageType = "node_stats"

type NodeStatsMessage struct {
    NodeID       string  `json:"node_id"`
    CPUUsage     float64 `json:"cpu_usage"`     // 0.0–1.0 from runtime
    MemoryUsage  float64 `json:"memory_usage"`  // HeapInuse / HeapSys
    CacheHitRate float64 `json:"cache_hit_rate"` // from cache.Stats().HitRate
}

In the gossip heartbeat loop (sendHeartbeats or equivalent), build a NodeStatsMessage for the local node:

  • CPUUsage: use runtime.ReadMemStats as a proxy (HeapInuse/HeapSys) — a real CPU sample via os is optional
  • MemoryUsage: ms.HeapInuse / ms.HeapSys
  • CacheHitRate: call gp.cluster.cache.Stats().HitRate if cache is non-nil, else 0

Broadcast alongside the heartbeat. Handle MessageTypeNodeStats in handleIncomingMessage by calling gp.cluster.UpdateNodeInfo with the received values.

Acceptance criteria

  • NodeStatsMessage struct and MessageTypeNodeStats constant defined
  • Gossip heartbeat sends stats for the local node every heartbeat period
  • Receiving node updates NodeInfo.CacheHitRate, MemoryUsage via UpdateNodeInfo
  • Integration test: start two nodes, wait 2× heartbeat interval, verify node B's view of node A has CacheHitRate >= 0 and MemoryUsage > 0
  • go test -race ./internal/distributed/... passes

Sequencing

Independent — can land any time in v0.11.0.

Activity

  1. added
    type: technical-debt
    priority: high
    area: distributed
    on Apr 7, 2026
  2. added
    effort: small
    on Aug 3, 2026
  3. scttfrdmn commented on Aug 3, 2026

    @scttfrdmn
    OwnerAuthor

    Verified this issue's premises before implementing. Two of them do not hold, and there is a
    prerequisite defect the plan does not mention — implementing as written would produce a feature that
    appears to work in a two-node test and reports a frozen value forever in production.

    The transport already exists

    performGossip (gossip.go:756-773) already sends gp.localNode — the whole NodeInfo, including
    CPUUsage, MemoryUsage, DiskUsage, CacheSize, CacheHitRate, and Operations — inside every
    AliveMessage, and handleAliveMessage (gossip.go:531-533) already applies it via
    UpdateNodeInfo, which copies all six fields (cluster.go:575-582).

    So a new MessageTypeNodeStats and NodeStatsMessage would be a second channel carrying a subset of
    what the first one already carries, and a second thing to remember to authenticate, count, and
    version. What is actually missing is narrower: nothing ever writes those fields on gp.localNode.
    It is built once at gossip.go:194-201 with the six stats fields at their zero values and never
    updated, so the transport faithfully delivers zeros.

    Prerequisite: stats would freeze at the first value received

    handleAliveMessage only applies an update when aliveMsg.Incarnation > gossipNode.Incarnation
    (gossip.go:522). Nothing in the package ever increments an incarnation — the only two writes to the
    field (gossip.go:523, gossip.go:672) both copy a received value, and getCurrentIncarnation
    returns the stored 1. So after a node is first discovered, every later alive message from it is
    dropped
    , and its stats hold whatever they were at discovery.

    Proven by execution rather than read off the source, with a throwaway probe driving
    handleIncomingMessage twice with sealed alive messages carrying different values:

    after first  alive: CPUUsage=0.1
    after second alive: CPUUsage=0.1 (sender said 0.9)
    --- FAIL: stats did not update on the second alive message: got 0.1, want 0.9
    

    This matters for the acceptance criteria as written. "Start two nodes, wait 2× heartbeat interval,
    verify node B's view of node A has MemoryUsage > 0" passes on the broken code: the first alive
    message is applied, so if the local node populates its stats before gossip starts, the first value
    lands and the assertion is satisfied. The test would then be green while the metric is frozen for the
    lifetime of the process — and a stale cache hit rate is worse than an absent one, because a
    cache-warming decision made on it looks informed.

    The test has to assert that a changed value propagates, not that a non-zero one arrived.

    Two premises that are wrong

    StrategyLeastLoad does not read these fields. selectLeastLoad (coordinator.go:906-925)
    sorts on lb.stats.NodeLoad, which is populated — coordinator.go:297 increments it per routed
    operation. So least-load is not making random selections for want of these fields; it is balancing on
    locally-observed request counts. That may be the wrong signal, but it is a different issue from this
    one, and fixing this one will not change its behaviour.

    What actually reads the fields is calculateStats (cluster.go:544-562), which averages
    CacheHitRate across alive nodes into ClusterStats.CacheHitRate — reported through GetStats and
    currently always 0.

    CPUUsage cannot be HeapInuse/HeapSys. That ratio is memory, which is what the issue also
    assigns to MemoryUsage — so the two fields would carry the same number under different names, and a
    load-balancing decision reading CPUUsage would be reading heap fragmentation. Better to populate
    MemoryUsage from runtime.ReadMemStats and leave CPUUsage at zero, documented as
    unpopulated, than to fill it with a plausible-looking number from an unrelated quantity. A field that
    is obviously zero prompts someone to implement it; a field carrying the wrong measurement does not.

    Adjacent defect found in the same function

    calculateStats accumulates totalCacheSize across alive nodes and then discards it —
    _ = totalCacheSize at cluster.go:562. ClusterStats has no cache-size field to put it in. So the
    loop does the work and drops the result. Worth fixing in the same change, since it is three lines from
    the code that has to be touched anyway: either add the field or delete the accumulation.

    Revised scope

    1. Increment the local incarnation each gossip round (or on each stats refresh), so alive messages
      after the first are not dropped. This is the prerequisite; without it nothing else in this issue
      has an observable effect after startup.
    2. Refresh gp.localNode's stats before broadcasting, from runtime.ReadMemStats and, when a cache
      is set via SetCache, cache.Stats(). No new message type — the alive message already carries
      them.
    3. Populate or delete totalCacheSize.
    4. Leave CPUUsage, DiskUsage, and NetworkBandwidth at zero with a comment saying so and why,
      rather than filling them with proxies from unrelated quantities.
    5. Test that a changed value propagates, since asserting a non-zero one passes on the frozen-stats
      bug.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: distributedeffort: smallpriority: hightype: technical-debt

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions