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

[ServiceBus] add message batch size limit #33300

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions sdk/servicebus/service-bus/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

### Bugs Fixed

- `MessageSender.createBatch()` now report error if `maxSizeInBytes` option is greater than maximum batch size.

### Other Changes

- Upgrade dependency `@azure/abort-controller` version to `^2.1.2`.
Expand Down
10 changes: 9 additions & 1 deletion sdk/servicebus/service-bus/src/core/messageSender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import type { AbortSignalLike } from "@azure/abort-controller";
import { ServiceBusError, translateServiceBusError } from "../serviceBusError.js";
import { isDefined } from "@azure/core-util";
import { defaultDataTransformer } from "../dataTransformer.js";
import { maxBatchSizePremium, maxBatchSizeStandard } from "../util/constants.js";

/**
* @internal
Expand Down Expand Up @@ -401,10 +402,17 @@ export class MessageSender extends LinkEntity<AwaitableSender> {
retryOptions: this._retryOptions,
abortSignal: options?.abortSignal,
});

if (maxMessageSize > maxBatchSizePremium) {
maxMessageSize = maxBatchSizePremium;
} else {
maxMessageSize = maxBatchSizeStandard;
}
Comment on lines +406 to +410
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we speculating here whether we're connected to a premium resource? Could this be done in a more robust way?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The maxMessageSize is from the service. It is a good indicator since for standard it's 256 KB and for premium it is at least 1 MB.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the service introduces a new tier with a different batch size, would this code still work?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we will need to update the code if that happens. Currently I am not aware of any way to retrieve the batch limit from the service. Let me ask the service team.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We hope that existing libraries can continue to work. Does the service API use API versions to guard behavioral changes? If not, this code could be easily broken by a future change.


if (options?.maxSizeInBytes) {
if (options.maxSizeInBytes > maxMessageSize!) {
const error = new Error(
`Max message size (${options.maxSizeInBytes} bytes) is greater than maximum message size (${maxMessageSize} bytes) on the AMQP sender link.`,
`Max message size (${options.maxSizeInBytes} bytes) is greater than maximum batch size (${maxMessageSize} bytes).`,
);
throw error;
}
Expand Down
10 changes: 10 additions & 0 deletions sdk/servicebus/service-bus/src/util/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ export const receiveDrainTimeoutInMs = 200;
*/
export const max32BitNumber = Math.pow(2, 31) - 1;

/**
* @internal
*/
export const maxBatchSizeStandard = 256 * 1024;

/**
* @internal
*/
export const maxBatchSizePremium = 1024 * 1024;

/**
* Queue name identifier
* @internal
Expand Down
4 changes: 2 additions & 2 deletions sdk/servicebus/service-bus/test/internal/sendBatch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { delay } from "@azure/core-util";
import { createTestCredential } from "@azure-tools/test-credential";
import { afterAll, afterEach, beforeAll, describe, it } from "vitest";
import { assert, should } from "../public/utils/chai.js";
import { maxBatchSizeStandard } from "../../src/util/constants.js";

describe("Send Batch", () => {
let sender: ServiceBusSender;
Expand Down Expand Up @@ -407,10 +408,9 @@ describe("Send Batch", () => {
try {
await sender.createMessageBatch({ maxSizeInBytes });
} catch (error: any) {
const maxSize = await (sender as ServiceBusSenderImpl)["_sender"].getMaxMessageSize();
should.equal(
error.message,
`Max message size (${maxSizeInBytes} bytes) is greater than maximum message size (${maxSize} bytes) on the AMQP sender link.`,
`Max message size (${maxSizeInBytes} bytes) is greater than maximum batch size (${maxBatchSizeStandard} bytes).`,
"Unexpected error message when tried to create a batch of size > maximum message size.",
);
errorIsThrown = true;
Expand Down
Loading