Skip to content

Conversation

kfaraz
Copy link
Contributor

@kfaraz kfaraz commented Oct 22, 2024

Description

In clusters with a large number of datasources, task operations block each other owing to the giant lock in the TaskLockbox. This can become particularly problematic in cases of streaming ingestion being done into multiple datasources, where segment allocations become very slow since all of them must pass through a single queue.

None of the task operations share a critical section across datasources and it should suffice to perform the locking
at the datasource level.

This patch is an attempt to remediate this problem.

Changes

  • Dedicate TaskLockbox to a single datasource.
  • Add a GlobalTaskLockbox which delegates to the respective datasource for any operation.
  • The GlobalTaskLockbox.syncFromStorage() must be mutually exclusive from any operation
    being performed on any of the datasources.
    • Since syncFromStorage() is called only from TaskQueue.start() on becoming leader, using a ReadWriteLock didn't seem necessary
    • Instead, syncFromStorage() marks the GlobalTaskLockbox as "unsynced" at the start of the method
    • When "unsynced", all other lockbox operations fail.
    • Upon sync completion, the GlobalTaskLockbox is marked "synced" again and operations can proceed as normal
  • Add GlobalTaskLockbox.shutdown() and TaskLockbox.clear() to clean up unused resources upon
    loss of leadership.
  • The TaskLockbox of a datasource is removed when it is not needed anymore

Pending

  • Test out the patch in a cluster

Follow up

After this patch, the bottleneck for segment allocation will be the single-threaded SegmentAllocationQueue.
One approach could be to maintain a separate SegmentAllocationQueue for each datasource but that would
drastically increase the pressure on metadata store in case of multiple datasources.
A better alternative would be to Make segment allocation queue multithreaded

Release note

Improve concurrency on Overlord by ensuring that task actions on one datasource do not block actions on other datasources.


This PR has:

  • been self-reviewed.
  • added documentation for new or modified features or behaviors.
  • a release note entry in the PR description.
  • added Javadocs for most classes and all non-trivial methods. Linked related entities via Javadoc links.
  • added or updated version, license, or notice information in licenses.yaml
  • added comments explaining the "why" and the intent of the code wherever would not be obvious for an unfamiliar reader.
  • added unit tests or modified existing tests to cover new code paths, ensuring the threshold for code coverage is met.
  • added integration tests.
  • been tested in a test Druid cluster.

@kfaraz kfaraz changed the title Maintain TaskLockbox at datasource level for higher concurrency [WIP] Maintain TaskLockbox at datasource level for higher concurrency Oct 22, 2024
Copy link

This pull request has been marked as stale due to 60 days of inactivity.
It will be closed in 4 weeks if no further activity occurs. If you think
that's incorrect or this pull request should instead be reviewed, please simply
write any comment. Even if closed, you can still revive the PR at any time or
discuss it on the dev@druid.apache.org list.
Thank you for your contributions.

Copy link

github-actions bot commented Mar 8, 2025

This pull request has been marked as stale due to 60 days of inactivity.
It will be closed in 4 weeks if no further activity occurs. If you think
that's incorrect or this pull request should instead be reviewed, please simply
write any comment. Even if closed, you can still revive the PR at any time or
discuss it on the dev@druid.apache.org list.
Thank you for your contributions.

@github-actions github-actions bot added the stale label Mar 8, 2025
@kfaraz kfaraz removed the stale label Mar 11, 2025
@kfaraz kfaraz changed the title [WIP] Maintain TaskLockbox at datasource level for higher concurrency Maintain TaskLockbox at datasource level for higher concurrency Jun 4, 2025
dataSource,
(ds, resource) -> {
if (resource != null && resourcePredicate.test(resource)) {
// TODO: what if a bad runaway operation is holding the TaskLockbox.giant?
Copy link
Contributor

Choose a reason for hiding this comment

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

This is only a concern on shutdown, right? Since at other times, clear() will only be called when the refcount is zero, meaning no locks should be held. Just trying to make sure I understand.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, it's a concern only on shutdown(), which is invoked on loss of leadership.
We would want the leadership change listener to return quickly.

Copy link
Contributor

Choose a reason for hiding this comment

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

True, we do, although we also want the old leader to fully stand down before the new one stands up, to avoid races between the leaders. Ideal thing would be to interrupt other in flight operations, if that's feasible. If not feasible right now, then please update this comment to describe the concern and to not be a TODO comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated and moved the comment to shutdown() method as it seems more relevant there.

) throws T
{
// Verify that sync is complete
if (!syncComplete.get()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Check again in the critical section of getLockboxResource? This code seems potentially racey, since syncComplete could be set to false right after this check.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the tip!

I didn't initially put the check in getLockboxResource() because that method is called from syncFromStorage() as well before the syncComplete flag has been set.

I guess I can have two variants of the getLockboxResource() method or just pass a boolean
to decide whether to perform the check or not.

Copy link
Contributor

Choose a reason for hiding this comment

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

A parameter about performing the check sounds reasonable to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated.

@gianm gianm merged commit 5ff0020 into apache:master Jun 11, 2025
141 of 143 checks passed
@kfaraz
Copy link
Contributor Author

kfaraz commented Jun 11, 2025

Thanks for the review, @gianm !

@kfaraz kfaraz deleted the datasource_lockbox branch June 11, 2025 15:48
@capistrant capistrant added this to the 34.0.0 milestone Jul 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants