Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use new consumer create subject when single subject filter specified in SubjectFilters #4564

Merged
merged 2 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions server/jetstream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21853,3 +21853,78 @@ func TestJetStreamSyncInterval(t *testing.T) {
})
}
}

func TestJetStreamFilteredSubjectUsesNewConsumerCreateSubject(t *testing.T) {
s := RunBasicJetStreamServer(t)
defer s.Shutdown()

nc, _ := jsClientConnect(t, s)
defer nc.Close()

extEndpoint := make(chan *nats.Msg, 1)
normalEndpoint := make(chan *nats.Msg, 1)

_, err := nc.ChanSubscribe(JSApiConsumerCreateEx, extEndpoint)
require_NoError(t, err)

_, err = nc.ChanSubscribe(JSApiConsumerCreate, normalEndpoint)
require_NoError(t, err)

testStreamSource := func(name string, shouldBeExtended bool, ss StreamSource) {
t.Run(name, func(t *testing.T) {
req := StreamConfig{
Name: name,
Storage: MemoryStorage,
Subjects: []string{fmt.Sprintf("foo.%s", name)},
Sources: []*StreamSource{&ss},
}
reqJson, err := json.Marshal(req)
require_NoError(t, err)

_, err = nc.Request(fmt.Sprintf(JSApiStreamCreateT, name), reqJson, time.Second)
require_NoError(t, err)

select {
case <-time.After(time.Second * 5):
t.Fatalf("Timed out waiting for receive consumer create")
case <-extEndpoint:
if !shouldBeExtended {
t.Fatalf("Expected normal consumer create, got extended")
}
case <-normalEndpoint:
if shouldBeExtended {
t.Fatalf("Expected extended consumer create, got normal")
}
}
})
}

testStreamSource("OneFilterSubject", true, StreamSource{
Name: "source",
FilterSubject: "bar.>",
})

testStreamSource("OneTransform", true, StreamSource{
Name: "source",
SubjectTransforms: []SubjectTransformConfig{
{
Source: "bar.one.>",
Destination: "bar.two.>",
},
},
})

testStreamSource("TwoTransforms", false, StreamSource{
Name: "source",
SubjectTransforms: []SubjectTransformConfig{
{
Source: "bar.one.>",
Destination: "bar.two.>",
},
{
Source: "baz.one.>",
Destination: "baz.two.>",
},
},
})
}
7 changes: 7 additions & 0 deletions server/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -2893,6 +2893,13 @@ func (mset *stream) setSourceConsumer(iname string, seq uint64, startTime time.T
if req.Config.FilterSubject != _EMPTY_ {
req.Config.Name = fmt.Sprintf("src-%s", createConsumerName())
subject = fmt.Sprintf(JSApiConsumerCreateExT, si.name, req.Config.Name, req.Config.FilterSubject)
} else if len(req.Config.FilterSubjects) == 1 {
req.Config.Name = fmt.Sprintf("src-%s", createConsumerName())
// It is necessary to switch to using FilterSubject here as the extended consumer
// create API checks for it, so as to not accidentally allow multiple filtered subjects.
req.Config.FilterSubject = req.Config.FilterSubjects[0]
req.Config.FilterSubjects = nil
subject = fmt.Sprintf(JSApiConsumerCreateExT, si.name, req.Config.Name, req.Config.FilterSubject)
} else {
subject = fmt.Sprintf(JSApiConsumerCreateT, si.name)
}
Expand Down