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

Cosmos BulkExecutor: Fixing leak of threads with "cosmos-daemon-BulkExecutor-*" prefix #31082

Merged

Conversation

FabianMeiswinkel
Copy link
Member

@FabianMeiswinkel FabianMeiswinkel commented Sep 21, 2022

Description

A thread/memory leak was reported in this issue - #31077

When using a Spark structured streaming job to ingest data into Cosmos DB in micro batches there is a race condition that could cause BulkExecutor instances and their corresponding thread to not be cleaned-up properly. This can eventually reult in workers crashing with the following error: 'java.io.IOException: java.util.concurrent.ExecutionException: java.lang.OutOfMemoryError: unable to create new native thread'

The race condition described happens when the BulkWriter (in Spark connector) signals that all incoming operations have been consumed here -


The BulkExecutor (in Java SDK) would react to this signal and cleanly close/clean-up everything - except if the BulKWriter is disposing the subsription here - - before the completion has happened.

To fix this instead we make sure that clean-up is happening independent of how the subscription is terminated (complete, cancel or error).

All SDK Contribution checklist:

  • The pull request does not introduce [breaking changes]
  • CHANGELOG is updated for new features, bug fixes or other significant changes.
  • I have read the contribution guidelines.

General Guidelines and Best Practices

  • Title of the pull request is clear and informative.
  • There are a small number of commits, each of which have an informative message. This means that previously merged commits do not appear in the history of the PR. For more information on cleaning up the commits in your PR, see this page.

Testing Guidelines

  • Pull request includes test coverage for the included changes.

@azure-sdk
Copy link
Collaborator

API change check

API changes are not detected in this pull request.

@FabianMeiswinkel FabianMeiswinkel changed the title Cosmos BulkExecutor: Stopping the flush task to free up the daemon thread Cosmos BulkExecutor: Fixing Sep 22, 2022
@FabianMeiswinkel FabianMeiswinkel changed the title Cosmos BulkExecutor: Fixing Cosmos BulkExecutor: Fixing leak of threads with "cosmos-daemon-BulkExecutor-*" prefix Sep 22, 2022
@FabianMeiswinkel FabianMeiswinkel marked this pull request as ready for review September 22, 2022 14:52
@FabianMeiswinkel FabianMeiswinkel linked an issue Sep 22, 2022 that may be closed by this pull request
Copy link
Member

@kushagraThapar kushagraThapar left a comment

Choose a reason for hiding this comment

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

LGTM, thanks @FabianMeiswinkel
some minor comments, worth fixing the changelog.

Copy link
Member

@xinlian12 xinlian12 left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for the quick fix :)

Copy link
Member

@xinlian12 xinlian12 left a comment

Choose a reason for hiding this comment

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

LGTM, thanks

@FabianMeiswinkel FabianMeiswinkel merged commit 04b5196 into Azure:main Sep 22, 2022
fangjian0423 pushed a commit to fangjian0423/azure-sdk-for-java that referenced this pull request Sep 27, 2022
…xecutor-*" prefix (Azure#31082)

* Cosmos BulkExecutor: Stopping the flush task to free up the daemon thread

* Fixing thread leak due to race condition on bulk completion between BulkWriter and BulkExecutor

* Iterating on fix

* Adding unit test coverage

* Updating changelog

* Fixing changelog

* Reacted to code review feedback

* Update CosmosAsyncContainer.java

* Update CosmosAsyncContainer.java
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] Spark Cosmos connector does not close its threads
5 participants