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

[ADDED] StartRevision option to KV watcher #1489

Merged
merged 8 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from 7 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
5 changes: 5 additions & 0 deletions jetstream/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ type (
updatesOnly bool
// retrieve only the meta data of the entry
metaOnly bool
// resumeFromSeq is the sequence to resume from
resumeFromSeq uint64
}

KVDeleteOpt interface {
Expand Down Expand Up @@ -875,6 +877,9 @@ func (kv *kvs) Watch(ctx context.Context, keys string, opts ...WatchOpt) (KeyWat
if o.metaOnly {
subOpts = append(subOpts, nats.HeadersOnly())
}
if o.resumeFromSeq > 0 {
subOpts = append(subOpts, nats.StartSequence(o.resumeFromSeq))
}
subOpts = append(subOpts, nats.Context(ctx))
// Create the sub and rest of initialization under the lock.
// We want to prevent the race between this code and the
Expand Down
8 changes: 8 additions & 0 deletions jetstream/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,14 @@ func MetaOnly() WatchOpt {
})
}

// ResumeFromSeq instructs the key watcher to resume from a specific sequence number.
func ResumeFromSeq(revision uint64) WatchOpt {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As this is KV abstraction over Stream, we should probably use KV naming convetions.

This should become ResumeFromRevision.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Related Sequence names have been updated to Revision.

Please take a look, thanks!

return watchOptFn(func(opts *watchOpts) error {
opts.resumeFromSeq = revision
return nil
})
}

// DeleteMarkersOlderThan indicates that delete or purge markers older than that
// will be deleted as part of PurgeDeletes() operation, otherwise, only the data
// will be removed but markers that are recent will be kept.
Expand Down
47 changes: 47 additions & 0 deletions jetstream/test/kv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,53 @@ func TestKeyValueWatch(t *testing.T) {
expectOk(t, err)
expectUpdate("t.age", "66", 12)
})

t.Run("watcher with start revision", func(t *testing.T) {
s := RunBasicJetStreamServer()
defer shutdownJSServerAndRemoveStorage(t, s)

nc, js := jsClient(t, s)
defer nc.Close()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

kv, err := js.CreateKeyValue(ctx, jetstream.KeyValueConfig{Bucket: "WATCH"})
expectOk(t, err)

_, err = kv.Create(ctx, "name", []byte("derek"))
expectOk(t, err)
_, err = kv.Put(ctx, "name", []byte("rip"))
expectOk(t, err)
_, err = kv.Put(ctx, "age", []byte("22"))
expectOk(t, err)

watcher, err := kv.WatchAll(ctx, jetstream.ResumeFromSeq(2))
expectOk(t, err)
defer watcher.Stop()

expectUpdate := expectUpdateF(t, watcher)

// check that we get only updates after revision 2
expectUpdate("name", "rip", 2)
expectUpdate("age", "22", 3)

// stop first watcher
watcher.Stop()

_, err = kv.Put(ctx, "name2", []byte("ik"))
expectOk(t, err)

// create a new watcher with start revision 3
watcher, err = kv.WatchAll(ctx, jetstream.ResumeFromSeq(3))
expectOk(t, err)
defer watcher.Stop()

expectUpdate = expectUpdateF(t, watcher)

// check that we get only updates after revision 3
expectUpdate("age", "22", 3)
expectUpdate("name2", "ik", 4)
})
}

func TestKeyValueWatchContext(t *testing.T) {
Expand Down