Skip to content

Commit

Permalink
[CHANGED] JetStream: accept "Nats-Expected-Last-Sequence" with "0"
Browse files Browse the repository at this point in the history
We use to ignore if the seq was 0, but now would treat it as
a requirement that the stream be empty if header is present but
set to 0.

This relates to client PR: nats-io/nats.go#958

Signed-off-by: Ivan Kozlovic <ivan@synadia.com>
  • Loading branch information
kozlovic committed Jul 7, 2022
1 parent b4aaec3 commit be8734e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
10 changes: 10 additions & 0 deletions server/jetstream_test.go
Expand Up @@ -3478,6 +3478,16 @@ func TestJetStreamPublishExpect(t *testing.T) {
t.Fatalf("Expected an error, got %q", resp.Data)
}

// Or if we expect that there are no messages by setting "0" as the expected last seq
m.Header.Set(JSExpectedLastSeq, "0")
resp, err = nc.RequestMsg(m, 100*time.Millisecond)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if pa := getPubAckResponse(resp.Data); pa == nil || pa.Error == nil {
t.Fatalf("Expected an error, got %q", resp.Data)
}

// Now send a message with a message ID and make sure we can match that.
m = nats.NewMsg("foo.bar")
m.Data = []byte("HELLO")
Expand Down
8 changes: 4 additions & 4 deletions server/stream.go
Expand Up @@ -3187,12 +3187,12 @@ func getExpectedStream(hdr []byte) string {
}

// Fast lookup of expected stream.
func getExpectedLastSeq(hdr []byte) uint64 {
func getExpectedLastSeq(hdr []byte) (uint64, bool) {
bseq := getHeader(JSExpectedLastSeq, hdr)
if len(bseq) == 0 {
return 0
return 0, false
}
return uint64(parseInt64(bseq))
return uint64(parseInt64(bseq)), true
}

// Fast lookup of rollups.
Expand Down Expand Up @@ -3472,7 +3472,7 @@ func (mset *stream) processJetStreamMsg(subject, reply string, hdr, msg []byte,
return errors.New("expected stream does not match")
}
// Expected last sequence.
if seq := getExpectedLastSeq(hdr); seq > 0 && seq != mset.lseq {
if seq, exists := getExpectedLastSeq(hdr); exists && seq != mset.lseq {
mlseq := mset.lseq
mset.clfs++
mset.mu.Unlock()
Expand Down

0 comments on commit be8734e

Please sign in to comment.