Repository navigation
Description
Two independent defects, found while fixing #269 and confirmed by a throwaway probe against a
single-node cluster rather than by reading. Both are the reason three tests in
tests/distributed_test.go (behind -tags=distributed) have been failing unobserved — see #240,
since no CI job builds that tag.
1. A single-node cluster never elects a leader
startElection (internal/distributed/consensus.go:365-384) sets voteCount = 1 and calls
sendVoteRequests. handleVoteResponse (:417-443) is the only place that compares the vote
count against a majority — and it is reached only when a peer replies. With one node there is no
peer, so sendVoteRequests sends nothing, nothing ever calls handleVoteResponse, and the
majority check that the node has already satisfied is never evaluated.
The election timer then fires again, the term increments, and the loop repeats forever. Probe
output, ElectionTimeout at 100 ms, observed after 600 ms:
IsLeader=false leader=""
The log shows starting election term=1, term=2, ... with no became leader.
Why it matters
A single-node cluster is not a corner case — it is the first thing anyone runs, and the shape a
deployment has while the second node is still being provisioned. OpTypeList routes to the leader
(coordinator.go:341-346) and falls back to load balancing when there is none, so it degrades
quietly; ProposeLeadershipChange refuses outright with only leader can propose changes. It also
means every operation that depends on leadership is untested at N=1.
Fix
Evaluate the majority at the point the vote count changes, not only on an inbound response.
Extracting the count-and-check from handleVoteResponse and calling it at the end of
startElection covers both paths with one rule. Note becomeLeader is called with ce.mu held in
the existing path, so the extracted helper must keep that contract.
Test: a one-node cluster becomes leader within a small multiple of ElectionTimeout. That test
fails today.
2. GetStats reports zero nodes until the first 5-second tick
ClusterManager.Start (cluster.go:284-292) adds the local node to cm.nodes — so GetNodes
is correct immediately. But ClusterStats is only ever populated by calculateClusterStats, which
runs from updateStats on a hardcoded 5-second ticker (cluster.go:531-545). Nothing computes
it at startup.
So for the first five seconds after Start returns successfully:
stats: total=0 alive=0 # while GetNodes() correctly reports 1
A cluster of one that reports zero alive nodes is indistinguishable from a cluster that failed to
start, and it is what any health check or cluster status command scraping this would see first.
The 5-second interval is also unrelated to HeartbeatInterval and not configurable.
Fix
Call calculateClusterStats once in Start, after the local node is registered, so the statistics
are consistent with the membership from the moment Start returns. Consider deriving the ticker
interval from configuration rather than hardcoding it.
Test: GetStats().AliveNodes == 1 immediately after Start returns, with no sleep. That test
fails today.
Where these show up
With -tags=distributed, all three of these failures are defect 1 or 2, not test bugs:
TestClusterManager_BasicOperations—Node should become leader after election timeout,
Expected 1 total node, got 0,Expected 1 alive node, got 0TestConsensusEngine_LeaderElection—Node should become leader after election, then
only leader can propose changesTestMultiNodeCluster—Expected exactly 1 leader, got 0; each of the three nodes also
sees 1 nodes in cluster, so seed-node discovery over loopback is a third thing to check
once leadership works. That part is not yet diagnosed and may be a test-configuration issue: the
configs set noSeedNodes.
Found alongside #269, which is fixed separately — that one is the accounting, this one is why the
operations had nothing to be accounted for.
Activity
fix: stop counting a failed distributed operation as a success (#276)