nats-io / nats.deno

Deno client for NATS, the cloud native messaging system
https://www.nats.io
Apache License 2.0
158 stars 50 forks source link

KV watch functionality fails when NATS server restarts #688

Closed ThalesValentim closed 5 months ago

ThalesValentim commented 5 months ago

Observed behavior

When watching for KV updates and the NATS server restarts, the client reconnects successfully but API throws the error:

{
   code: 400,
   err_code: 10013,
   description: 'consumer name already in use'
 }

Debug logs show that it tries to reconnect twice, with the same request data, when the watch method has been called.

Expected behavior

It should reconnect and continue receiving KV updates

Server and client version

nats-server 2.10.14 nats.js 2.22.0

Host environment

Unix OS

Steps to reproduce

From the KV example, the code to reproduce looks like:

const nats = require('./lib/src/mod.js');
const nc = await nats.connect();
const sc = nats.StringCodec();
const js = nc.jetstream();
const kv = await js.views.kv("testing");
await kv.create("hello.world", sc.encode("hi"));

const watch = await kv.watch();
(async () => {
  for await (const e of watch) {
    // do something with the change
    console.log(
      `watch: ${e.key}: ${e.operation} ${e.value ? sc.decode(e.value) : ""}`,
    );
  }
})().then();
  1. Run the example app
  2. Restart NATS server
  3. The client reconnects but the watch functionality fails with the response:

{"type":"io.nats.jetstream.api.v1.consumer_create_response","error":{"code":400,"err_code":10013,"description":"consumer name already in use"}}

Any ideas? Thanks!

aricart commented 5 months ago

The watcher is R1, so when your server restarts it is gone. There's no option for listening on the idle heartbeats - so that means that the watcher just stalls.... Let me see what I can do

ThalesValentim commented 5 months ago

The watcher is R1, so when your server restarts it is gone. There's no option for listening on the idle heartbeats - so that means that the watcher just stalls.... Let me see what I can do

Thanks for your reply!

Let me give you some context. For every NATS server release, we expect to follow the changes and upgrade the NATS to the latest release, but currently, it affects clients that watch updates on KV. It would be great to handle that situation. Would you have a workaround in mind?

aricart commented 5 months ago

but of course - the question is how to onboard the change - looking at it.