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

Cherry picks for v2.10.11 release #5090

Merged
merged 2 commits into from
Feb 15, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions server/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4783,6 +4783,23 @@ func (o *consumer) selectStartingSeqNo() {
// If we are here we are time based.
// TODO(dlc) - Once clustered can't rely on this.
o.sseq = o.mset.store.GetSeqFromTime(*o.cfg.OptStartTime)
// Here we want to see if we are filtered, and if so possibly close the gap
// to the nearest first given our starting sequence from time. This is so we do
// not force the system to do a linear walk between o.sseq and the real first.
if len(o.subjf) > 0 {
nseq := state.LastSeq
for _, filter := range o.subjf {
// Use first sequence since this is more optimized atm.
ss := o.mset.store.FilteredState(state.FirstSeq, filter.subject)
if ss.First > o.sseq && ss.First < nseq {
nseq = ss.First
}
}
// Skip ahead if possible.
if nseq > o.sseq && nseq < state.LastSeq {
o.sseq = nseq
}
}
} else {
// DeliverNew
o.sseq = state.LastSeq + 1
Expand Down
30 changes: 24 additions & 6 deletions server/filestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -2246,22 +2246,33 @@ func (mb *msgBlock) firstMatching(filter string, wc bool, start uint64, sm *Stor
if !isAll && len(mb.fss) == 1 {
_, isAll = mb.fss[filter]
}
// Skip scan of mb.fss if number of messages in the block are less than
// 1/2 the number of subjects in mb.fss. Or we have a wc and lots of fss entries.
const linearScanMaxFSS = 32
// Make sure to start at mb.first.seq if fseq < mb.first.seq
if seq := atomic.LoadUint64(&mb.first.seq); seq > fseq {
fseq = seq
}
lseq := atomic.LoadUint64(&mb.last.seq)
doLinearScan := isAll || 2*int(lseq-fseq) < len(mb.fss) || (wc && len(mb.fss) > linearScanMaxFSS)

// Optionally build the isMatch for wildcard filters.
tsa := [32]string{}
fsa := [32]string{}
var fts []string
var isMatch func(subj string) bool
// Decide to build.
if wc {
fts = tokenizeSubjectIntoSlice(fsa[:0], filter)
isMatch = func(subj string) bool {
tts := tokenizeSubjectIntoSlice(tsa[:0], subj)
return isSubsetMatchTokenized(tts, fts)
}
}
// Only do linear scan if isAll or we are wildcarded and have to traverse more fss than actual messages.
doLinearScan := isAll || (wc && len(mb.fss) > int(lseq-fseq))
if !doLinearScan {
// If we have a wildcard match against all tracked subjects we know about.
if wc {
subs = subs[:0]
for subj := range mb.fss {
if subjectIsSubsetMatch(subj, filter) {
if isMatch(subj) {
subs = append(subs, subj)
}
}
Expand All @@ -2283,6 +2294,13 @@ func (mb *msgBlock) firstMatching(filter string, wc bool, start uint64, sm *Stor
}
}

// If we guess to not do a linear scan, but the above resulted in alot of subs that will
// need to be checked for every scanned message, revert.
// TODO(dlc) - we could memoize the subs across calls.
if len(subs) > int(lseq-fseq) {
doLinearScan = true
}

if fseq > lseq {
return nil, didLoad, ErrStoreMsgNotFound
}
Expand Down Expand Up @@ -2310,7 +2328,7 @@ func (mb *msgBlock) firstMatching(filter string, wc bool, start uint64, sm *Stor
return fsm, expireOk, nil
}
if doLinearScan {
if wc && subjectIsSubsetMatch(fsm.subj, filter) {
if wc && isMatch(sm.subj) {
return fsm, expireOk, nil
} else if !wc && fsm.subj == filter {
return fsm, expireOk, nil
Expand Down