Skip to content

Commit 393ece7

Browse files
authored
Adjust Kafka message estimator on MESSAGE_TOO_LARGE (#6374)
1 parent 0e4be14 commit 393ece7

File tree

4 files changed

+22
-2
lines changed

4 files changed

+22
-2
lines changed

.changeset/ka-f-ka.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'hive': patch
3+
---
4+
5+
Adjust the Kafka message size estimation only when Kafka gives back `MESSAGE_TOO_LARGE` error

packages/services/usage/__tests__/buffer.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ test('increase the defaultBytesPerOperation estimation by 5% when over 100 calls
3434
limitInBytes: eventHubLimitInBytes,
3535
useEstimator: true,
3636
onRetry,
37+
isTooLargePayloadError() {
38+
return true;
39+
},
3740
calculateReportSize(report) {
3841
return report.size;
3942
},
@@ -150,6 +153,9 @@ test('buffer should split the report into multiple reports when the estimated si
150153
interval,
151154
limitInBytes: eventHubLimitInBytes,
152155
useEstimator: true,
156+
isTooLargePayloadError() {
157+
return true;
158+
},
153159
calculateReportSize(report) {
154160
return report.size;
155161
},
@@ -262,6 +268,9 @@ test('buffer create two chunks out of one buffer when actual buffer size is too
262268
interval,
263269
limitInBytes: eventHubLimitInBytes,
264270
useEstimator: true,
271+
isTooLargePayloadError() {
272+
return true;
273+
},
265274
calculateReportSize(report) {
266275
return report.size;
267276
},

packages/services/usage/src/buffer.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ export function createKVBuffer<T>(config: {
131131
calculateReportSize(report: T): number;
132132
split(report: T, numOfChunks: number): readonly T[];
133133
onRetry(reports: readonly T[]): void;
134+
isTooLargePayloadError(error: unknown): boolean;
134135
sender(
135136
reports: readonly T[],
136137
estimatedSizeInBytes: number,
@@ -254,8 +255,10 @@ export function createKVBuffer<T>(config: {
254255
await flushBuffer(reports, size, batchId);
255256
} catch (error) {
256257
logger.error(error);
257-
// the payload size was most likely too big
258-
estimator.overflowed(batchId);
258+
if (config.isTooLargePayloadError(error)) {
259+
// the payload size was most likely too big
260+
estimator.overflowed(batchId);
261+
}
259262
}
260263
}
261264

packages/services/usage/src/usage.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,9 @@ export function createUsage(config: {
156156
interval: config.kafka.buffer.interval,
157157
limitInBytes: 990_000, // 1MB is the limit of a single request to EventHub, let's keep it below that
158158
useEstimator: config.kafka.buffer.dynamic,
159+
isTooLargePayloadError(error) {
160+
return error instanceof Error && 'type' in error && error.type === 'MESSAGE_TOO_LARGE';
161+
},
159162
calculateReportSize(report) {
160163
return Object.keys(report.map).length;
161164
},

0 commit comments

Comments
 (0)