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

[FIX] Kv Watcher reliability during server restarts #689

Merged
merged 1 commit into from
Apr 22, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions jetstream/jsclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ import {
PullOptions,
ReplayPolicy,
} from "./jsapi_types.ts";
import { nuid } from "../nats-base-client/nuid.ts";

export enum PubHeaders {
MsgIdHdr = "Nats-Msg-Id",
Expand Down Expand Up @@ -775,6 +776,7 @@ export class JetStreamSubscriptionImpl extends TypedSubscription<JsMsg>
const nci = this.js.nc;
nci._resub(this.sub, newDeliver);
const info = this.info;
info.config.name = nuid.next();
info.ordered_consumer_sequence.delivery_seq = 0;
info.flow_control.heartbeat_count = 0;
info.flow_control.fc_count = 0;
Expand Down Expand Up @@ -842,6 +844,7 @@ export class JetStreamSubscriptionImpl extends TypedSubscription<JsMsg>
// reset the consumer
const seq = this.info?.ordered_consumer_sequence?.stream_seq || 0;
this._resetOrderedConsumer(seq + 1);
this.monitor?.restart();
// if we are ordered, we will reset the consumer and keep
// feeding the iterator or callback - we are not stopping
return false;
Expand Down
30 changes: 30 additions & 0 deletions jetstream/tests/kv_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2134,3 +2134,33 @@ Deno.test("kv - honors checkAPI option", async () => {

await cleanup(ns, nc);
});

Deno.test("kv - watcher on server restart", async () => {
let { ns, nc } = await setup(
jetstreamServerConf({}),
);
const js = nc.jetstream();
const kv = await js.views.kv("A");
const iter = await kv.watch();
const d = deferred<KvEntry>();
(async () => {
for await (const e of iter) {
d.resolve(e);
break;
}
})().then();

ns = await ns.restart();
console.log("server restarted");
for (let i = 0; i < 10; i++) {
try {
await kv.put("hello", "world");
break;
} catch {
await delay(500);
}
}

await d;
await cleanup(ns, nc);
});
1 change: 1 addition & 0 deletions nats-base-client/idleheartbeat_monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export class IdleHeartbeatMonitor {
}
this.timer = 0;
this.autoCancelTimer = 0;
this.missed = 0;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/deno_transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export class DenoTransport implements Transport {
const sto = await (this.loadTlsOptions(hostname));
this.conn = await Deno.startTls(
//@ts-ignore: just the conn
this.conn,
this.conn as Deno.TcpConn,
sto,
);
// this is necessary because the startTls process doesn't
Expand Down
Loading