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

Add ordering key size validation to validatePubsubMessageSize #31645

Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,20 @@ static int validatePubsubMessageSize(PubsubMessage message, int maxPublishBatchS
}
int totalSize = payloadSize;

@Nullable String orderingKey = message.getOrderingKey();
if (orderingKey != null) {
int orderingKeySize = orderingKey.getBytes(StandardCharsets.UTF_8).length;
if (orderingKeySize > PUBSUB_MESSAGE_ATTRIBUTE_MAX_VALUE_BYTES) {
throw new SizeLimitExceededException(
"Pubsub message ordering key of length "
+ orderingKeySize
+ " exceeds maximum of "
+ PUBSUB_MESSAGE_ATTRIBUTE_MAX_VALUE_BYTES
+ " bytes. See https://cloud.google.com/pubsub/quotas#resource_limits");
}
totalSize += orderingKeySize;
}

@Nullable Map<String, String> attributes = message.getAttributeMap();
if (attributes != null) {
if (attributes.size() > PUBSUB_MESSAGE_MAX_ATTRIBUTES) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,19 @@ public void testValidatePubsubMessageSizeOnlyPayload() throws SizeLimitExceededE
assertEquals(data.length, messageSize);
}

@Test
public void testValidatePubsubMessageSizePayloadAndOrderingKey()
throws SizeLimitExceededException {
byte[] data = new byte[1024];
String orderingKey = "key";
PubsubMessage message = new PubsubMessage(data, null, null, orderingKey);

int messageSize =
PreparePubsubWriteDoFn.validatePubsubMessageSize(message, PUBSUB_MESSAGE_MAX_TOTAL_SIZE);

assertEquals(data.length + orderingKey.getBytes(StandardCharsets.UTF_8).length, messageSize);
}

@Test
public void testValidatePubsubMessageSizePayloadAndAttributes()
throws SizeLimitExceededException {
Expand Down Expand Up @@ -76,6 +89,19 @@ public void testValidatePubsubMessageSizePayloadTooLarge() {
message, PUBSUB_MESSAGE_MAX_TOTAL_SIZE));
}

@Test
public void testValidatePubsubMessageSizePayloadPlusOrderingKeyTooLarge() {
byte[] data = new byte[(10 << 20)];
String orderingKey = "key";
PubsubMessage message = new PubsubMessage(data, null, null, orderingKey);

assertThrows(
SizeLimitExceededException.class,
() ->
PreparePubsubWriteDoFn.validatePubsubMessageSize(
message, PUBSUB_MESSAGE_MAX_TOTAL_SIZE));
}

@Test
public void testValidatePubsubMessageSizePayloadPlusAttributesTooLarge() {
byte[] data = new byte[(10 << 20)];
Expand Down Expand Up @@ -121,6 +147,19 @@ public void testValidatePubsubMessageSizeAttributeValueTooLarge() {
message, PUBSUB_MESSAGE_MAX_TOTAL_SIZE));
}

@Test
public void testValidatePubsubMessageSizeOrderingKeyTooLarge() {
byte[] data = new byte[1024];
String orderingKey = RandomStringUtils.randomAscii(1025);
PubsubMessage message = new PubsubMessage(data, null, null, orderingKey);

assertThrows(
SizeLimitExceededException.class,
() ->
PreparePubsubWriteDoFn.validatePubsubMessageSize(
message, PUBSUB_MESSAGE_MAX_TOTAL_SIZE));
}

@Test
public void testValidatePubsubMessagePayloadTooLarge() {
byte[] data = new byte[(10 << 20) + 1];
Expand Down
Loading