acbfeb5815
This change updates tests to honor `BootstrapExpect` exclusively when forming test clusters and removes test only knobs, e.g. `config.DevDisableBootstrap`. Background: Test cluster creation is fragile. Test servers don't follow the BootstapExpected route like production clusters. Instead they start as single node clusters and then get rejoin and may risk causing brain split or other test flakiness. The test framework expose few knobs to control those (e.g. `config.DevDisableBootstrap` and `config.Bootstrap`) that control whether a server should bootstrap the cluster. These flags are confusing and it's unclear when to use: their usage in multi-node cluster isn't properly documented. Furthermore, they have some bad side-effects as they don't control Raft library: If `config.DevDisableBootstrap` is true, the test server may not immediately attempt to bootstrap a cluster, but after an election timeout (~50ms), Raft may force a leadership election and win it (with only one vote) and cause a split brain. The knobs are also confusing as Bootstrap is an overloaded term. In BootstrapExpect, we refer to bootstrapping the cluster only after N servers are connected. But in tests and the knobs above, it refers to whether the server is a single node cluster and shouldn't wait for any other server. Changes: This commit makes two changes: First, it relies on `BootstrapExpected` instead of `Bootstrap` and/or `DevMode` flags. This change is relatively trivial. Introduce a `Bootstrapped` flag to track if the cluster is bootstrapped. This allows us to keep `BootstrapExpected` immutable. Previously, the flag was a config value but it gets set to 0 after cluster bootstrap completes.
95 lines
1.9 KiB
Go
95 lines
1.9 KiB
Go
package nomad
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/hashicorp/nomad/testutil"
|
|
)
|
|
|
|
func TestStatsFetcher(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
conf := func(c *Config) {
|
|
c.Region = "region-a"
|
|
c.BootstrapExpect = 3
|
|
}
|
|
|
|
s1, cleanupS1 := TestServer(t, conf)
|
|
defer cleanupS1()
|
|
|
|
s2, cleanupS2 := TestServer(t, conf)
|
|
defer cleanupS2()
|
|
|
|
s3, cleanupS3 := TestServer(t, conf)
|
|
defer cleanupS3()
|
|
|
|
TestJoin(t, s1, s2, s3)
|
|
testutil.WaitForLeader(t, s1.RPC)
|
|
|
|
members := s1.serf.Members()
|
|
if len(members) != 3 {
|
|
t.Fatalf("bad len: %d", len(members))
|
|
}
|
|
|
|
var servers []*serverParts
|
|
for _, member := range members {
|
|
ok, server := isNomadServer(member)
|
|
if !ok {
|
|
t.Fatalf("bad: %#v", member)
|
|
}
|
|
servers = append(servers, server)
|
|
}
|
|
|
|
// Do a normal fetch and make sure we get three responses.
|
|
func() {
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
|
defer cancel()
|
|
stats := s1.statsFetcher.Fetch(ctx, s1.Members())
|
|
if len(stats) != 3 {
|
|
t.Fatalf("bad: %#v", stats)
|
|
}
|
|
for id, stat := range stats {
|
|
switch id {
|
|
case s1.config.NodeID, s2.config.NodeID, s3.config.NodeID:
|
|
// OK
|
|
default:
|
|
t.Fatalf("bad: %s", id)
|
|
}
|
|
|
|
if stat == nil || stat.LastTerm == 0 {
|
|
t.Fatalf("bad: %#v", stat)
|
|
}
|
|
}
|
|
}()
|
|
|
|
// Fake an in-flight request to server 3 and make sure we don't fetch
|
|
// from it.
|
|
func() {
|
|
s1.statsFetcher.inflight[string(s3.config.NodeID)] = struct{}{}
|
|
defer delete(s1.statsFetcher.inflight, string(s3.config.NodeID))
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
|
defer cancel()
|
|
stats := s1.statsFetcher.Fetch(ctx, s1.Members())
|
|
if len(stats) != 2 {
|
|
t.Fatalf("bad: %#v", stats)
|
|
}
|
|
for id, stat := range stats {
|
|
switch id {
|
|
case s1.config.NodeID, s2.config.NodeID:
|
|
// OK
|
|
case s3.config.NodeID:
|
|
t.Fatalf("bad")
|
|
default:
|
|
t.Fatalf("bad: %s", id)
|
|
}
|
|
|
|
if stat == nil || stat.LastTerm == 0 {
|
|
t.Fatalf("bad: %#v", stat)
|
|
}
|
|
}
|
|
}()
|
|
}
|