diff --git a/cmd/moipctl/scout.go b/cmd/moipctl/scout.go index 5349612..8cb0e61 100644 --- a/cmd/moipctl/scout.go +++ b/cmd/moipctl/scout.go @@ -24,7 +24,7 @@ func runScout(args []string) error { controller := fs.String("controller", os.Getenv("MOIPCTL_CONTROLLER"), "base URL of the controller's scout listener, e.g. http://moipctl:8081 ($MOIPCTL_CONTROLLER)") source := fs.String("source", os.Getenv("MOIPCTL_SOURCE"), "name of this scout, e.g. the VLAN: av or ent ($MOIPCTL_SOURCE)") interval := fs.Duration("interval", 15*time.Second, "time between searches") - window := fs.Duration("window", 4*time.Second, "how long one search listens for answers") + window := fs.Duration("window", 6*time.Second, "how long one search listens for answers; it sends three queries within it") health := fs.String("health", ":8082", "address for GET /healthz (empty: disabled)") if err := fs.Parse(args); err != nil { return err diff --git a/internal/discovery/discovery.go b/internal/discovery/discovery.go index 5ac69d1..6496d84 100644 --- a/internal/discovery/discovery.go +++ b/internal/discovery/discovery.go @@ -52,7 +52,7 @@ func (o Options) withDefaults() Options { o.Interval = 15 * time.Second } if o.Window <= 0 { - o.Window = 4 * time.Second + o.Window = 6 * time.Second } if o.MissRounds <= 0 { o.MissRounds = 3 @@ -120,11 +120,64 @@ func (d *Discoverer) Round(ctx context.Context) { } } +// defaultPasses is how many separate queries one browse round sends. +const defaultPasses = 3 + // ZeroconfBrowser browses _moip._tcp on all multicast interfaces. -type ZeroconfBrowser struct{} +// +// The zeroconf library sends one query when a browse starts and the next one +// only after four seconds, so a long window does not help: against a real +// avahi-daemon, about a quarter of single queries went unanswered (the +// responder limits how often it repeats a record). A round is therefore split +// into several short browses, each sending a fresh query, and the answers are +// merged; with three queries a round misses a node only a few percent of the +// time instead of a quarter. +type ZeroconfBrowser struct { + // Passes is the number of queries per round (default 3). + Passes int + + // pass runs one short browse; tests replace it. + pass func(ctx context.Context, d time.Duration) ([]Entry, error) +} + +func (b ZeroconfBrowser) Browse(ctx context.Context, window time.Duration) ([]Entry, error) { + passes := b.Passes + if passes <= 0 { + passes = defaultPasses + } + pass := b.pass + if pass == nil { + pass = browseOnce + } + slice := window / time.Duration(passes) + + merged := map[string]Entry{} + var order []string + for i := 0; i < passes; i++ { + entries, err := pass(ctx, slice) + if err != nil { + return nil, err + } + for _, e := range entries { + if _, seen := merged[e.ID]; !seen { + order = append(order, e.ID) + } + merged[e.ID] = e // the latest answer wins + } + if ctx.Err() != nil { + break + } + } + out := make([]Entry, 0, len(order)) + for _, id := range order { + out = append(out, merged[id]) + } + return out, nil +} -func (ZeroconfBrowser) Browse(ctx context.Context, window time.Duration) ([]Entry, error) { - ctx, cancel := context.WithTimeout(ctx, window) +// browseOnce runs one zeroconf browse for d and returns what answered. +func browseOnce(ctx context.Context, d time.Duration) ([]Entry, error) { + ctx, cancel := context.WithTimeout(ctx, d) defer cancel() ch := make(chan *zeroconf.ServiceEntry, 32) diff --git a/internal/discovery/discovery_test.go b/internal/discovery/discovery_test.go index 065c2e9..a72df7c 100644 --- a/internal/discovery/discovery_test.go +++ b/internal/discovery/discovery_test.go @@ -134,3 +134,65 @@ func TestPicksTheAddressOnADirectlyAttachedSubnet(t *testing.T) { t.Fatalf("got %+v", e) } } + +func TestBrowseMergesPassesAndKeepsTheLatestAnswer(t *testing.T) { + moved := Entry{ID: "a", Hostname: "utone-a", Addr: "10.10.20.9:8000"} + other := Entry{ID: "b", Hostname: "utone-b", Addr: "10.10.20.4:8000"} + var calls []time.Duration + answers := [][]Entry{{nodeAForTest}, {}, {moved, other}} // a pass may come back empty + b := ZeroconfBrowser{pass: func(_ context.Context, d time.Duration) ([]Entry, error) { + calls = append(calls, d) + r := answers[len(calls)-1] + return r, nil + }} + got, err := b.Browse(context.Background(), 6*time.Second) + if err != nil { + t.Fatal(err) + } + if len(calls) != 3 || calls[0] != 2*time.Second { + t.Fatalf("want 3 passes of 2s, got %v", calls) + } + if len(got) != 2 || got[0] != moved || got[1] != other { + t.Fatalf("got %+v", got) + } +} + +func TestBrowseFindsANodeThatOnlyOnePassSees(t *testing.T) { + n := 0 + b := ZeroconfBrowser{pass: func(context.Context, time.Duration) ([]Entry, error) { + n++ + if n == 3 { // the first two queries went unanswered + return []Entry{nodeAForTest}, nil + } + return nil, nil + }} + got, _ := b.Browse(context.Background(), 6*time.Second) + if len(got) != 1 { + t.Fatalf("got %+v", got) + } +} + +func TestBrowseReturnsAPassError(t *testing.T) { + b := ZeroconfBrowser{pass: func(context.Context, time.Duration) ([]Entry, error) { + return nil, errors.New("no multicast interface") + }} + if _, err := b.Browse(context.Background(), time.Second); err == nil { + t.Fatal("want the error") + } +} + +func TestBrowseStopsWhenTheContextIsDone(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + n := 0 + b := ZeroconfBrowser{pass: func(context.Context, time.Duration) ([]Entry, error) { + n++ + cancel() + return nil, nil + }} + b.Browse(ctx, 6*time.Second) + if n != 1 { + t.Fatalf("want to stop after the cancelled pass, ran %d", n) + } +} + +var nodeAForTest = Entry{ID: "a", Hostname: "utone-a", Addr: "10.10.20.3:8000"} diff --git a/internal/scout/agent.go b/internal/scout/agent.go index e456e5f..7345d28 100644 --- a/internal/scout/agent.go +++ b/internal/scout/agent.go @@ -29,7 +29,7 @@ func (a *Agent) withDefaults() { a.Interval = 15 * time.Second } if a.Window <= 0 { - a.Window = 4 * time.Second + a.Window = 6 * time.Second } if a.Client == nil { a.Client = &http.Client{Timeout: 10 * time.Second}