diff --git a/modules/mesh-delivery-contracts/go/clients/meshcatalogclient/catalog.go b/modules/mesh-delivery-contracts/go/clients/meshcatalogclient/catalog.go index c06d6453..8e6feb62 100644 --- a/modules/mesh-delivery-contracts/go/clients/meshcatalogclient/catalog.go +++ b/modules/mesh-delivery-contracts/go/clients/meshcatalogclient/catalog.go @@ -34,7 +34,7 @@ type Half struct { // ViewName is how the catalog half's revision is named in a part's first-view status (views.Status.View, WaitsFor). func ViewName(revision int64) string { return strconv.FormatInt(revision, 10) } -// catalogCreated stands for the view's Created in its follower: a revision view is never made again, so one fixed time, +// catalogCreated stands for the view's Created in its follower until its numbering starts again (H-A-141): one fixed time, // never zero, lets the follower tell a revision heard from one it holds. var catalogCreated = time.Unix(0, 0).UTC() @@ -57,6 +57,23 @@ type Catalog struct { paging *Half // waits is the revision waited for, 0 when none. waits int64 + // made is when the view's numbering last started again (H-A-141), the follower's Created; zero is catalogCreated. + made time.Time +} + +// created is madeLocked, taking mu. +func (c *Catalog) created() time.Time { + c.mu.Lock() + defer c.mu.Unlock() + return c.madeLocked() +} + +// madeLocked is the view's Created in its follower: catalogCreated until a numbering started again. The caller holds mu. +func (c *Catalog) madeLocked() time.Time { + if c.made.IsZero() { + return catalogCreated + } + return c.made } // NewCatalog is a Catalog of branch asking the seat through call. Nothing is asked before Start. @@ -146,21 +163,34 @@ func (c *Catalog) Hear(ctx context.Context, data []byte) error { if c.held != nil { held = c.held.Revision.Revision } - if ev.Revision > c.waits { - c.waits = ev.Revision - } - c.mu.Unlock() - if ev.Revision <= held { + if ev.Revision == held { + c.mu.Unlock() return nil } + restarted := ev.Revision < held + if restarted { + // A revision below the one the picture holds is the state made again: its numbering started anew (design 47 H + // H-A-141). The view's made time moves on, so the follower forgets the revisions heard of the earlier numbering, + // and what it waits for is this revision. + c.made = time.Now().UTC() + c.waits = ev.Revision + } else if ev.Revision > c.waits { + c.waits = ev.Revision + } + made := c.madeLocked() + c.mu.Unlock() // The follower keeps the newest revision heard, so an ask that was running when it was heard is asked once more. - c.Follower.Apply(views.Event{Owner: seat.Seat, State: c.Follower.State, Revision: uint64(ev.Revision), Created: catalogCreated, Gap: true}) + c.Follower.Apply(views.Event{Owner: seat.Seat, State: c.Follower.State, Revision: uint64(ev.Revision), Created: made, Gap: true}) c.Follower.WaitFor(ctx, ViewName(ev.Revision)) + why := fmt.Sprintf("catalog revision %d of %s said, the view holds %d", ev.Revision, c.Branch, held) + if restarted { + why = fmt.Sprintf("catalog revision %d of %s said, below the %d the view holds: the numbering started again", ev.Revision, c.Branch, held) + } if c.Follower.Logf != nil { - c.Follower.Logf("meshcatalogclient: catalog revision %d of %s said, the view holds %d: %s is asked once more", ev.Revision, c.Branch, held, RevisionVerb) + c.Follower.Logf("meshcatalogclient: %s: %s is asked once more", why, RevisionVerb) } go func() { - _ = c.Follower.Gap(ctx, fmt.Sprintf("catalog revision %d of %s said, the view holds %d", ev.Revision, c.Branch, held)) + _ = c.Follower.Gap(ctx, why) }() return nil } @@ -177,7 +207,7 @@ func (c *Catalog) ask(_ context.Context, after string) (views.Answer, error) { a, err := askRevision(c.call, in) if errors.Is(err, errNoneYet) { // A branch with no revision yet is ready and empty, and nothing waits for it (§479, §495). - return views.Answer{V: 1, Owner: seat.Seat, State: c.Follower.State, Created: catalogCreated, Values: map[string]json.RawMessage{}, At: time.Now()}, nil + return views.Answer{V: 1, Owner: seat.Seat, State: c.Follower.State, Created: c.created(), Values: map[string]json.RawMessage{}, At: time.Now()}, nil } if err != nil { return views.Answer{}, err @@ -212,7 +242,7 @@ func (c *Catalog) ask(_ context.Context, after string) (views.Answer, error) { if holds { c.Follower.Holds(ViewName(rev)) } - return views.Answer{V: 1, Owner: seat.Seat, State: c.Follower.State, Revision: uint64(rev), Created: catalogCreated, Values: values, Next: a.Next, At: time.Now()}, nil + return views.Answer{V: 1, Owner: seat.Seat, State: c.Follower.State, Revision: uint64(rev), Created: c.created(), Values: values, Next: a.Next, At: time.Now()}, nil } // errNoneYet is the seat's answer `none-yet`: the branch has no revision yet (design 47 G §530). diff --git a/modules/mesh-delivery-contracts/go/clients/meshcatalogclient/catalog_view_test.go b/modules/mesh-delivery-contracts/go/clients/meshcatalogclient/catalog_view_test.go index 760d8bfc..2b9ba612 100644 --- a/modules/mesh-delivery-contracts/go/clients/meshcatalogclient/catalog_view_test.go +++ b/modules/mesh-delivery-contracts/go/clients/meshcatalogclient/catalog_view_test.go @@ -188,3 +188,89 @@ func TestCandidatesReachTheHook(t *testing.T) { t.Errorf("the hook was handed %+v, want the candidate's revision 4 alone", hooked) } } + +// H-A-141 (design 47 H §750, §754): a catalog-revision below the revision the view holds is the state made again, its +// numbering started anew: said in the log with both revisions, and the first view asked again, so the view follows the new +// numbering and is behind nothing; one equal to the held revision asks nothing; one above is taken in as before. +func TestCatalogFollowsANumberingStartedAgain(t *testing.T) { + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + f := &fakeRevisions{newest: map[string]int64{"main": 528}, asks: map[string]int{}} + var logMu sync.Mutex + var logged []string + c, err := NewCatalog(f.call, "main") + if err != nil { + t.Fatal(err) + } + c.Follower.Wait = time.Nanosecond + c.Follower.Logf = func(format string, args ...any) { + logMu.Lock() + defer logMu.Unlock() + logged = append(logged, fmt.Sprintf(format, args...)) + } + came := make(chan string, 16) // the module of each entry that came, one per revision answered + c.Follower.Changed = func(module string, _ json.RawMessage, deleted bool, _ time.Time) { + if !deleted { + came <- module + } + } + if err := c.Start(ctx); err != nil { + t.Fatal(err) + } + <-c.Ready() + <-came // the first view's entry + for _, row := range []struct { + name string + newest int64 // what the seat answers from now + heard int64 + asks int // the asks of the first view after the event + holds int64 // the revision the view holds after it + said string // a line the log holds after it; "" none new + restarting bool + }{ + {name: "equal to the held revision: dropped", newest: 528, heard: 528, asks: 1, holds: 528}, + {name: "above: taken in", newest: 529, heard: 529, asks: 2, holds: 529, said: "catalog revision 529 of main said, the view holds 528"}, + {name: "below: the numbering started again", newest: 7, heard: 7, asks: 3, holds: 7, said: "catalog revision 7 of main said, below the 529 the view holds: the numbering started again", restarting: true}, + {name: "equal after the restart: dropped", newest: 7, heard: 7, asks: 3, holds: 7}, + {name: "above after the restart: taken in", newest: 8, heard: 8, asks: 4, holds: 8, said: "catalog revision 8 of main said, the view holds 7"}, + } { + t.Run(row.name, func(t *testing.T) { + logMu.Lock() + before := len(logged) + logMu.Unlock() + f.set("main", row.newest) + if err := c.Hear(ctx, revisionEvent("main", KindTrunk, row.heard)); err != nil { + t.Fatal(err) + } + if row.said != "" { + select { + case m := <-came: + if want := fmt.Sprintf("m-of-main-%d", row.holds); m != want { + t.Fatalf("the entry %s came, want %s", m, want) + } + case <-ctx.Done(): + t.Fatalf("the view never took revision %d in", row.holds) + } + } + if h, ok := c.View(); !ok || h.Revision.Revision != row.holds { + t.Fatalf("the view holds %+v, want revision %d", h.Revision, row.holds) + } + if n := f.asked("main"); n != row.asks { + t.Errorf("the first view was asked %d times, want %d", n, row.asks) + } + st := c.Follower.Status() + if st.Behind != "" || st.WaitsFor != "" { + t.Errorf("the view is behind %q, waits for %q, holding %d", st.Behind, st.WaitsFor, st.Revision) + } + logMu.Lock() + fresh := strings.Join(logged[before:], "\n") + logMu.Unlock() + if row.said == "" && strings.Contains(fresh, "meshcatalogclient:") { + t.Errorf("an event the view holds was said: %s", fresh) + } + if row.said != "" && !strings.Contains(fresh, row.said) { + t.Errorf("the log does not say %q: %s", row.said, fresh) + } + }) + } +}