indexer: make a watched branch's source-moved durable when the bus holds none
The grants part composes no per-branch durable on the source-moved stream, so the indexer never heard a source revision after its start. Its bind now makes the durable itself, as a module's seat-event consumer is made (SeatEventConsumerConfig, named <node>_<indexer>_<branch>, filtered to the branch, one message at a time), and binds an existing one as it stands. Builds: design 47 H §743, H-A-134 Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy
This commit is contained in:
@@ -79,10 +79,19 @@ func sourceVia(call toolCaller) func(context.Context, string) (meshdelivery.Sour
|
||||
}
|
||||
}
|
||||
|
||||
// bindBranch binds the indexer's durable of one watched branch on the stream of source-moved through mesh-sdk.
|
||||
// bindBranch binds the indexer's durable of one watched branch on the stream of source-moved through mesh-sdk. One the bus does
|
||||
// not hold the indexer makes itself, as its own kept consumer, and then binds (design 47 H §743, H-A-134: the grants part
|
||||
// composes none until H4b); one the bus holds is bound as it stands, never made again.
|
||||
func bindBranch(js jetstream.JetStream, cred process.Account, logf func(string, ...any)) func(context.Context, string, func(jetstream.Msg)) (func(), error) {
|
||||
return func(ctx context.Context, branch string, each func(jetstream.Msg)) (func(), error) {
|
||||
cons, err := cred.NamedEventConsumer(ctx, js, meshdelivery.StreamSourceMoved, keys.EscapeName(branch))
|
||||
if errors.Is(err, jetstream.ErrConsumerNotFound) {
|
||||
cfg := branchConsumerConfig(cred, branch)
|
||||
if cons, err = js.CreateConsumer(ctx, meshdelivery.StreamSourceMoved, cfg); err != nil {
|
||||
return nil, fmt.Errorf("the consumer %s on %s is not there and is not made: %w", cfg.Durable, meshdelivery.StreamSourceMoved, err)
|
||||
}
|
||||
logf("source-moved: the consumer %s on %s was not there: the indexer made it (H-A-134)", cfg.Durable, meshdelivery.StreamSourceMoved)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -94,6 +103,17 @@ func bindBranch(js jetstream.JetStream, cred process.Account, logf func(string,
|
||||
}
|
||||
}
|
||||
|
||||
// branchConsumerConfig is the indexer's durable of one watched branch as the mesh gives a module's seat-event consumer
|
||||
// (mesh-sdk's SeatEventConsumerConfig), named <node>_<indexer>_<branch> as NamedEventConsumer binds it, filtered to the
|
||||
// branch's source-moved and delivering one message at a time (design 47 H §743, H-A-90, H-A-134).
|
||||
func branchConsumerConfig(cred process.Account, branch string) jetstream.ConsumerConfig {
|
||||
esc := keys.EscapeName(branch)
|
||||
cfg := cred.SeatEventConsumerConfig([]string{meshdelivery.SubjectSourceMoved(esc, "*")}, false)
|
||||
cfg.Durable = cred.EventConsumerName() + "_" + esc
|
||||
cfg.MaxAckPending = 1
|
||||
return cfg
|
||||
}
|
||||
|
||||
// refresh binds the consumer of every watched branch not bound yet: at the start and on each registration-changed. A consumer
|
||||
// not there yet is said and bound at the next registration-changed or start. The start's refresh reads every watched branch's
|
||||
// source revision, a later one each branch whose consumer could not be bound (H-A-129), after the binds.
|
||||
|
||||
@@ -12,6 +12,8 @@ import (
|
||||
"github.com/nats-io/nats.go/jetstream"
|
||||
|
||||
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdelivery"
|
||||
"git.novox.be/novox/mesh-sdk/go/keys"
|
||||
"git.novox.be/novox/mesh-sdk/go/process"
|
||||
|
||||
"mesh-delivery-indexer/internal/index"
|
||||
)
|
||||
@@ -152,3 +154,81 @@ func TestAStartWithNoConsumerTakesTheGatesSourceRevisionIn(t *testing.T) {
|
||||
t.Errorf("asked the gate for %v and said %v; want main asked at each and taken in once", asked, taken)
|
||||
}
|
||||
}
|
||||
|
||||
// fakeBranchJS is the bus as bindBranch uses it: the source-moved stream's consumers by name, and the consumers made.
|
||||
type fakeBranchJS struct {
|
||||
jetstream.JetStream
|
||||
held map[string]*fakeBranchConsumer
|
||||
made []jetstream.ConsumerConfig
|
||||
}
|
||||
|
||||
func (f *fakeBranchJS) Consumer(_ context.Context, stream, name string) (jetstream.Consumer, error) {
|
||||
if c, ok := f.held[stream+"/"+name]; ok {
|
||||
return c, nil
|
||||
}
|
||||
return nil, jetstream.ErrConsumerNotFound
|
||||
}
|
||||
|
||||
func (f *fakeBranchJS) CreateConsumer(_ context.Context, stream string, cfg jetstream.ConsumerConfig) (jetstream.Consumer, error) {
|
||||
f.made = append(f.made, cfg)
|
||||
c := &fakeBranchConsumer{}
|
||||
f.held[stream+"/"+cfg.Durable] = c
|
||||
return c, nil
|
||||
}
|
||||
|
||||
// fakeBranchConsumer hands one source-moved to the hearing as soon as it is consumed.
|
||||
type fakeBranchConsumer struct {
|
||||
jetstream.Consumer
|
||||
heard int
|
||||
}
|
||||
|
||||
type fakeConsumeContext struct{ jetstream.ConsumeContext }
|
||||
|
||||
func (fakeConsumeContext) Stop() {}
|
||||
|
||||
func (c *fakeBranchConsumer) Consume(handle jetstream.MessageHandler, _ ...jetstream.PullConsumeOpt) (jetstream.ConsumeContext, error) {
|
||||
c.heard++
|
||||
handle(msgOf{f: &fakeMsg{subject: "mesh.seat.mesh-delivery.event.source-moved.main.2"}})
|
||||
return fakeConsumeContext{}, nil
|
||||
}
|
||||
|
||||
// Design 47 H §743, H-A-134: a branch's durable the bus does not hold the indexer makes, as a module's seat-event consumer is
|
||||
// made (SeatEventConsumerConfig), named <node>_<indexer>_<branch>, filtered to the branch, one message at a time, and then
|
||||
// hears on it; one the bus holds is bound, never made again.
|
||||
func TestABranchDurableNotThereIsMadeAndHeard(t *testing.T) {
|
||||
cred := process.Account{Node: "n1", Module: "mesh-delivery-indexer"}
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
held bool
|
||||
}{{"not there: made", false}, {"there: bound", true}} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
js := &fakeBranchJS{held: map[string]*fakeBranchConsumer{}}
|
||||
name := "n1_mesh-delivery-indexer_" + keys.EscapeName("main")
|
||||
var existing *fakeBranchConsumer
|
||||
if tc.held {
|
||||
existing = &fakeBranchConsumer{}
|
||||
js.held[meshdelivery.StreamSourceMoved+"/"+name] = existing
|
||||
}
|
||||
var heard []string
|
||||
stop, err := bindBranch(js, cred, func(string, ...any) {})(context.Background(), "main", func(m jetstream.Msg) { heard = append(heard, m.Subject()) })
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
stop()
|
||||
if len(heard) != 1 {
|
||||
t.Fatalf("heard %v, want the one source-moved the consumer hands", heard)
|
||||
}
|
||||
if tc.held {
|
||||
if len(js.made) != 0 || existing.heard != 1 {
|
||||
t.Fatalf("made %v and heard the held one %d times; want it bound, not made again", js.made, existing.heard)
|
||||
}
|
||||
return
|
||||
}
|
||||
want := cred.SeatEventConsumerConfig([]string{meshdelivery.SubjectSourceMoved(keys.EscapeName("main"), "*")}, false)
|
||||
want.Durable, want.MaxAckPending = name, 1
|
||||
if len(js.made) != 1 || !reflect.DeepEqual(js.made[0], want) {
|
||||
t.Fatalf("made %+v, want once %+v", js.made, want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user