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

Restore consumer OptStartSeq clipping behaviour #6014

Merged
merged 3 commits into from
Oct 17, 2024
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
16 changes: 6 additions & 10 deletions server/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5031,16 +5031,12 @@ func (o *consumer) selectStartingSeqNo() {
o.sseq = o.cfg.OptStartSeq
}

// Only clip the sseq if the OptStartSeq is not provided, otherwise
// it's possible that the stream just doesn't contain OptStartSeq yet.
if o.cfg.OptStartSeq == 0 {
if state.FirstSeq == 0 {
o.sseq = 1
} else if o.sseq < state.FirstSeq {
o.sseq = state.FirstSeq
} else if o.sseq > state.LastSeq {
o.sseq = state.LastSeq + 1
}
if state.FirstSeq == 0 {
o.sseq = 1
} else if o.sseq < state.FirstSeq {
o.sseq = state.FirstSeq
} else if o.sseq > state.LastSeq {
o.sseq = state.LastSeq + 1
}
}

Expand Down
38 changes: 21 additions & 17 deletions server/jetstream_cluster_4_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3402,6 +3402,8 @@ func TestJetStreamClusterConsumeWithStartSequence(t *testing.T) {
// This is the success condition for all sub-tests below
var ExpectedMsgId = ""
checkMessage := func(t *testing.T, msg *nats.Msg) {
t.Helper()

msgMeta, err := msg.Metadata()
require_NoError(t, err)

Expand All @@ -3414,6 +3416,8 @@ func TestJetStreamClusterConsumeWithStartSequence(t *testing.T) {
}

checkRawMessage := func(t *testing.T, msg *nats.RawStreamMsg) {
t.Helper()

// Check sequence number
require_Equal(t, msg.Sequence, ChosenSeq)

Expand Down Expand Up @@ -3447,7 +3451,23 @@ func TestJetStreamClusterConsumeWithStartSequence(t *testing.T) {
})
require_NoError(t, err)

// Setup: create subscriptions before stream is populated
// Setup: populate stream
buf := make([]byte, 100)
for i := uint64(1); i <= NumMessages; i++ {
msgId := nuid.Next()
pubAck, err := js.Publish(StreamSubjectPrefix+strconv.Itoa(int(i)), buf, nats.MsgId(msgId))
require_NoError(t, err)

// Verify assumption made in tests below
require_Equal(t, pubAck.Sequence, i)

if i == ChosenSeq {
// Save the expected message id for the chosen message
ExpectedMsgId = msgId
}
}

// Setup: create subscriptions, needs to be after stream creation or OptStartSeq could be clipped
var preCreatedSub, preCreatedSubDurable *nats.Subscription
{
preCreatedSub, err = js.PullSubscribe(
Expand Down Expand Up @@ -3483,22 +3503,6 @@ func TestJetStreamClusterConsumeWithStartSequence(t *testing.T) {
}()
}

// Setup: populate stream
buf := make([]byte, 100)
for i := uint64(1); i <= NumMessages; i++ {
msgId := nuid.Next()
pubAck, err := js.Publish(StreamSubjectPrefix+strconv.Itoa(int(i)), buf, nats.MsgId(msgId))
require_NoError(t, err)

// Verify assumption made in tests below
require_Equal(t, pubAck.Sequence, i)

if i == ChosenSeq {
// Save the expected message id for the chosen message
ExpectedMsgId = msgId
}
}

// Tests various ways to consume the stream starting at the ChosenSeq sequence

t.Run(
Expand Down
140 changes: 84 additions & 56 deletions server/jetstream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24000,62 +24000,6 @@ func TestJetStreamConsumerInfoNumPending(t *testing.T) {
require_Equal(t, ci.NumPending, 100)
}

func TestJetStreamConsumerStartSequenceNotInStream(t *testing.T) {
// This test is checking that we still correctly set the start
// sequence of a consumer if that start sequence doesn't appear
// in the stream yet. Previously this would have been clipped
// back to between the first and last seq from the stream state.

s := RunBasicJetStreamServer(t)
defer s.Shutdown()

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

_, err := js.AddStream(&nats.StreamConfig{
Name: "TEST",
Subjects: []string{"test"},
})
require_NoError(t, err)

sub, err := js.PullSubscribe("test", "test_consumer", nats.StartSequence(10))
require_NoError(t, err)

stream, err := s.gacc.lookupStream("TEST")
require_NoError(t, err)
consumer := stream.lookupConsumer("test_consumer")

func() {
consumer.mu.RLock()
defer consumer.mu.RUnlock()

require_Equal(t, consumer.dseq, 1)
require_Equal(t, consumer.sseq, 10)
}()

for i := 1; i <= 10; i++ {
_, err = js.Publish("test", []byte{byte(i)})
require_NoError(t, err)
}

msgs, err := sub.Fetch(1)
require_NoError(t, err)
require_Len(t, len(msgs), 1)
require_Equal(t, msgs[0].Data[0], 10)

require_NoError(t, msgs[0].AckSync())

func() {
consumer.mu.RLock()
defer consumer.mu.RUnlock()

require_Equal(t, consumer.dseq, 2)
require_Equal(t, consumer.adflr, 1)
require_Equal(t, consumer.sseq, 11)
require_Equal(t, consumer.asflr, 10)
}()
}

func TestJetStreamInterestStreamWithDuplicateMessages(t *testing.T) {
s := RunBasicJetStreamServer(t)
defer s.Shutdown()
Expand Down Expand Up @@ -24427,3 +24371,87 @@ func TestIsJSONObjectOrArray(t *testing.T) {
})
}
}

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

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

_, err := js.AddStream(&nats.StreamConfig{
Name: "ORIGIN",
Subjects: []string{"test"},
})
require_NoError(t, err)

for i := 0; i < 10; i++ {
_, err := js.Publish("test", nil)
require_NoError(t, err)
}

_, err = js.AddStream(&nats.StreamConfig{
Name: "SOURCING",
Sources: []*nats.StreamSource{
{
Name: "ORIGIN",
OptStartSeq: 20,
},
},
})
require_NoError(t, err)

// Wait for sourcing consumer to be created.
time.Sleep(time.Second)

mset, err := s.GlobalAccount().lookupStream("ORIGIN")
require_NoError(t, err)
require_True(t, mset != nil)
require_Len(t, len(mset.consumers), 1)
for _, o := range mset.consumers {
// Should have been clipped back to below 20 as only
// 10 messages in the origin stream.
require_Equal(t, o.sseq, 11)
}
}

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

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

_, err := js.AddStream(&nats.StreamConfig{
Name: "ORIGIN",
Subjects: []string{"test"},
})
require_NoError(t, err)

for i := 0; i < 10; i++ {
_, err := js.Publish("test", nil)
require_NoError(t, err)
}

_, err = js.AddStream(&nats.StreamConfig{
Name: "MIRRORING",
Mirror: &nats.StreamSource{
Name: "ORIGIN",
OptStartSeq: 20,
},
})
require_NoError(t, err)

// Wait for mirroring consumer to be created.
time.Sleep(time.Second)

mset, err := s.GlobalAccount().lookupStream("ORIGIN")
require_NoError(t, err)
require_True(t, mset != nil)
require_Len(t, len(mset.consumers), 1)
for _, o := range mset.consumers {
// Should have been clipped back to below 20 as only
// 10 messages in the origin stream.
require_Equal(t, o.sseq, 11)
}
}