diff --git a/internal/selfcompose/entries_test.go b/internal/selfcompose/entries_test.go index bc08d2b8..2aa09583 100644 --- a/internal/selfcompose/entries_test.go +++ b/internal/selfcompose/entries_test.go @@ -753,3 +753,64 @@ func TestTrunkAskBackoffAndCatalogRevision(t *testing.T) { } } } + +// A-487a, principle 3: a refused subscription to catalog-revision does not refuse Watch; it is said once with its subject and error, and +// tried again on laterAgain's steps until it holds, after which an event clears the waits. +func TestCatalogRevisionSubscriptionRefusedIsSaidAndRetried(t *testing.T) { + setFor(t, &laterAgain, []time.Duration{5 * time.Millisecond}) + b := newFakeBus() + env := b.env() + heard := make(chan []byte, 1) + var tries atomic.Int64 + env.Hear = func(subject string) (<-chan []byte, func(), error) { + if subject == CatalogRevisionEvents { + if tries.Add(1) < 3 { + return nil, nil, errors.New("permissions violation") + } + return heard, func() {}, nil + } + return make(chan []byte), func() {}, nil + } + c := New(env) + c.mu.Lock() + c.trunkRetry = map[string]trunkBackoff{"postgres": {tries: 1, next: time.Now().Add(time.Hour)}} + c.mu.Unlock() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + if _, err := c.Watch(ctx, "n1.>"); err != nil { + t.Fatalf("a refused catalog-revision subscription refused Watch: %v", err) + } + deadline := time.After(5 * time.Second) + for tries.Load() < 3 { + select { + case <-deadline: + t.Fatal("the subscription was not tried again") + case <-time.After(2 * time.Millisecond): + } + } + said := 0 + b.mu.Lock() + for _, s := range b.said { + if strings.Contains(s, CatalogRevisionEvents) && strings.Contains(s, "permissions violation") { + said++ + } + } + b.mu.Unlock() + if said != 1 { + t.Errorf("the refusal was said %d times, want once", said) + } + heard <- []byte(`{}`) + for { + c.mu.Lock() + z := c.trunkRetry["postgres"].next.IsZero() + c.mu.Unlock() + if z { + return + } + select { + case <-deadline: + t.Fatal("the event of the subscription that held did not clear the wait") + case <-time.After(2 * time.Millisecond): + } + } +} diff --git a/internal/selfcompose/selfcompose.go b/internal/selfcompose/selfcompose.go index c67e10a4..834671f8 100644 --- a/internal/selfcompose/selfcompose.go +++ b/internal/selfcompose/selfcompose.go @@ -547,29 +547,12 @@ func (c *Composer) Watch(ctx context.Context, _ string) (<-chan agent.Update, er }(i, ch) } if c.env.Hear != nil { - revisions, stopRevisions, err := c.env.Hear(CatalogRevisionEvents) - if err != nil { - return nil, fmt.Errorf("the node asks the trunk builds again when the catalog says a revision, and cannot hear it: %w", err) - } - go func() { - defer stopRevisions() - for { - select { - case <-ctx.Done(): - return - case _, ok := <-revisions: - if !ok { - return - } - c.trunkRetryCleared() - select { - case merged <- tagged{in: -1, ok: true}: - case <-ctx.Done(): - return - } - } + go c.hearCatalogRevisions(ctx, func() { + select { + case merged <- tagged{in: -1, ok: true}: + case <-ctx.Done(): } - }() + }) heard, stop, err := c.env.Hear(VaultSecretChanged) if err != nil { return nil, fmt.Errorf("the node composes again when a secret changes, and cannot hear the vault say so: %w", err) @@ -727,6 +710,41 @@ func (c *Composer) Watch(ctx context.Context, _ string) (<-chan agent.Update, er return out, nil } +// hearCatalogRevisions hears the seat mesh-catalog's event catalog-revision: each one clears the backoffs of the trunk builds the +// catalog gave none of and wakes a pass (A-487a). A wake-up only, the backoff bounds the asks without it: a subscription refused +// is said once with its subject and error (principle 3), the composer goes on composing, and the subscription is tried again on +// laterAgain's steps until it holds. +func (c *Composer) hearCatalogRevisions(ctx context.Context, wake func()) { + said := false + for try := 0; ; try++ { + revisions, stop, err := c.env.Hear(CatalogRevisionEvents) + if err == nil { + defer stop() + for { + select { + case <-ctx.Done(): + return + case _, ok := <-revisions: + if !ok { + return + } + c.trunkRetryCleared() + wake() + } + } + } + if !said { + said = true + c.env.Say(fmt.Sprintf("the node cannot hear %s: %v; it asks the trunk builds again on a backoff alone and tries to hear it again", CatalogRevisionEvents, err)) + } + select { + case <-ctx.Done(): + return + case <-time.After(LaterWait(try)): + } + } +} + func all(b []bool) bool { for _, v := range b { if !v {