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

[Kafka Connector]Fix BadRequest exception when using serverless database account #43125

Merged
merged 6 commits into from
Jan 3, 2025
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
1 change: 1 addition & 0 deletions sdk/cosmos/azure-cosmos-kafka-connect/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#### Breaking Changes

#### Bugs Fixed
* Fixed `BadRequestException` when customer using Serverless CosmosDB database account and metadata container does not exists. - See [PR 43125](https://github.com/Azure/azure-sdk-for-java/pull/43125)

#### Other Changes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,12 +215,28 @@ private boolean shouldCreateMetadataContainerIfNotExists() {
}

private Mono<CosmosContainerResponse> createMetadataContainer() {
return this.createMetadataContainer(METADATA_CONTAINER_DEFAULT_RU_CONFIG)
.onErrorResume(throwable -> {
// for serverless database account, creating container with throughput configured will get 400/0 exceptions
if (KafkaCosmosExceptionsHelper.isBadRequestException(throwable)) {
tvaron3 marked this conversation as resolved.
Show resolved Hide resolved
LOGGER.info(
"Getting exception '{}' when creating metadata container with throughput, retrying creating without throughput.",
throwable.getMessage());
return this.createMetadataContainer(null);
}

return Mono.error(throwable);
});
}

private Mono<CosmosContainerResponse> createMetadataContainer(Integer throughput) {
return this.cosmosClient
.getDatabase(this.config.getContainersConfig().getDatabaseName())
.createContainer(
this.config.getMetadataConfig().getStorageName(),
"/id",
ThroughputProperties.createAutoscaledThroughput(METADATA_CONTAINER_DEFAULT_RU_CONFIG));
throughput == null
? null : ThroughputProperties.createAutoscaledThroughput(throughput));
}

private void updateMetadataRecordsInCosmos(MetadataTaskUnit metadataTaskUnit) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,11 @@ public static boolean isOwnerResourceNotExistsException(Throwable throwable) {
&& cosmosException.getStatusCode() == HttpConstants.StatusCodes.NOTFOUND
&& cosmosException.getSubStatusCode() == HttpConstants.SubStatusCodes.OWNER_RESOURCE_NOT_EXISTS;
}

public static boolean isBadRequestException(Throwable throwable) {
CosmosException cosmosException = Utils.as(Exceptions.unwrap(throwable), CosmosException.class);
return cosmosException != null
&& cosmosException.getStatusCode() == HttpConstants.StatusCodes.BADREQUEST
&& cosmosException.getSubStatusCode() == HttpConstants.SubStatusCodes.UNKNOWN;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ public class CosmosSourceConnectorITest extends KafkaCosmosIntegrationTestSuiteB
@BeforeClass(groups = { "kafka-integration" })
public void before_CosmosSourceConnectorITest() {
this.client = new CosmosClientBuilder()
.key(TestConfigurations.MASTER_KEY)
.endpoint(TestConfigurations.HOST)
.key(KafkaCosmosTestConfigurations.MASTER_KEY)
.endpoint(KafkaCosmosTestConfigurations.HOST)
.endpointDiscoveryEnabled(true)
.buildAsyncClient();
}
Expand Down
Loading