Skip to content

Commit

Permalink
detect when kafkajs is stuck with stale broker metadata (#186)
Browse files Browse the repository at this point in the history
* detect when kafkajs is stuck with stale broker metadata and terminate application

* lint
  • Loading branch information
Sebastian Klose authored Sep 28, 2020
1 parent ffb5ecb commit 5001258
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/kafka/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@walmartlabs/cookie-cutter-kafka",
"version": "1.3.0-beta.3",
"version": "1.3.0-beta.4",
"license": "Apache-2.0",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
23 changes: 23 additions & 0 deletions packages/kafka/src/KafkaConsumer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export class KafkaConsumer implements IRequireInitialization, IDisposable {
private offsetCommitIntervalMs: number;
private timer: NodeJS.Timer;
private groupEpoch: number = 0;
private brokerMetadataErrors: number = 0;

constructor(config: KafkaConsumerConfig) {
this.config = config;
Expand Down Expand Up @@ -278,6 +279,7 @@ export class KafkaConsumer implements IRequireInitialization, IDisposable {
// tslint:disable-next-line:no-floating-promises
this.commitOffsetsIfNecessary();
}, 0);
this.timer.unref();
}

await this.consumer.run({
Expand Down Expand Up @@ -407,15 +409,36 @@ export class KafkaConsumer implements IRequireInitialization, IDisposable {
this.metrics.gauge(KafkaMetrics.Lag, lag.toNumber(), tags);
}
}

this.brokerMetadataErrors = 0;
} catch (e) {
this.logger.warn("Unable to retrieve watermarks", e);

// this is a workaround for https://github.com/walmartlabs/cookie-cutter/issues/185
// until a fix for kafkajs is available / the root cause of the problem is confirmed
if (
e &&
e.toString().contains("server is not the leader for that topic-partition")
) {
this.brokerMetadataErrors++;
}

if (this.brokerMetadataErrors > 10) {
this.logger.error("detected stale broker metadata in kafkajs");
this.brokerMetadataErrors = 0;

// throwing an error here will cause a UnhandledPromiseException
// and terminate the application
throw new Error("stale broker metadata");
}
}
}
} finally {
this.timer = setTimeout(() => {
// tslint:disable-next-line:no-floating-promises
this.commitOffsetsIfNecessary();
}, this.offsetCommitIntervalMs);
this.timer.unref();
}
}

Expand Down

0 comments on commit 5001258

Please sign in to comment.