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 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
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
// resumeFromRevision is the revision to resume from.
resumeFromRevision 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.resumeFromRevision > 0 {
subOpts = append(subOpts, nats.StartSequence(o.resumeFromRevision))
}
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 {
})
}

// ResumeFromRevision instructs the key watcher to resume from a specific revision number.
func ResumeFromRevision(revision uint64) WatchOpt {
return watchOptFn(func(opts *watchOpts) error {
opts.resumeFromRevision = 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.ResumeFromRevision(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.ResumeFromRevision(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