Skip to content

branch-4.0: [fix](cloud) Use bthread-aware shared mutex for tablet header lock - #66019

Merged
liaoxin01 merged 1 commit into
apache:branch-4.0from
liaoxin01:codex/pick-64574-to-branch-4.0
Jul 28, 2026
Merged

branch-4.0: [fix](cloud) Use bthread-aware shared mutex for tablet header lock#66019
liaoxin01 merged 1 commit into
apache:branch-4.0from
liaoxin01:codex/pick-64574-to-branch-4.0

Conversation

@liaoxin01

Copy link
Copy Markdown
Contributor

Pick #64574

### What problem does this PR solve?
Issue Number: None
Related PR: apache#64574
Problem Summary: The tablet header lock used a thread-affine shared mutex. In cloud mode, a bthread could suspend while holding the write lock and resume on another worker pthread, leaving the lock permanently wedged. Replace the tablet header lock with BthreadSharedMutex, which uses bthread-aware synchronization and supports cross-worker resume safely. Keep unrelated shared mutexes unchanged.
### Release note
Fix a potential permanent tablet header lock wedge after bthread migration in cloud mode.
### Check List (For Author)
- Test: Unit Test
- ./run-be-ut.sh --run --filter=BthreadSharedMutexTest.* -j 32 (7 tests passed)
- Behavior changed: Yes. The tablet header lock is now bthread-aware.
- Does this need documentation: No
CopilotAI review requested due to automatic review settings July 24, 2026 13:17
@hello-stephen

Copy link
Copy Markdown
Contributor

Thank you for your contribution to Apache Doris.
Don't know what should be done next? See How to process your PR.

Please clearly describe your PR:

  1. What problem was fixed (it's best to include specific error reporting information). How it was fixed.
  2. Which behaviors were modified. What was the previous behavior, what is it now, why was it modified, and what possible impacts might there be.
  3. What features were added. Why was this function added?
  4. Which code was refactored and why was this part of the code refactored?
  5. Which functions were optimized and what is the difference before and after the optimization?

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Note

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

This PR backports #64574 to replace the tablet header/meta lock with a bthread-migration-safe shared mutex, and updates call sites to avoid self-deadlock from recursive shared acquisition under writer-preferring semantics.

Changes:

  • Introduces BthreadSharedMutex (bthread-based std::shared_mutex analogue) and adds dedicated unit tests.
  • Replaces std::shared_mutex tablet header locks with BthreadSharedMutex and updates locking at key metadata mutation/read sites.
  • Adds/uses _unlocked APIs in tablet/cloud-tablet paths to prevent recursive lock acquisition under a shared lock.

Reviewed changes

Copilot reviewed 23 out of 23 changed files in this pull request and generated 2 comments.

Show a summary per file
FileDescription
be/src/util/bthread_shared_mutex.hAdds bthread-aware shared mutex implementation.
be/test/util/bthread_shared_mutex_test.cppAdds correctness/regression/stress coverage for the new mutex.
be/src/olap/base_tablet.hSwitches meta lock type to BthreadSharedMutex; adds “_unlocked” score API declaration.
be/src/olap/base_tablet.cppRefactors compaction score calculation into locked + unlocked variants.
be/src/olap/tablet.hSwitches header lock return type; adds unlocked rowset-pick APIs to avoid recursive locking.
be/src/olap/tablet.cppUpdates lock usage and routes through unlocked helper paths under existing shared locks.
be/src/cloud/cloud_tablet.hUpdates APIs taking header locks to use BthreadSharedMutex; introduces unlocked pick APIs.
be/src/cloud/cloud_tablet.cppRemoves inner re-locking in “caller already holds lock” paths; updates lock types and pick APIs.
be/src/cloud/cloud_meta_mgr.hUpdates fill_version_holes lock parameter type.
be/src/cloud/cloud_meta_mgr.cppUpdates lock type usage related to tablet header lock.
be/src/olap/task/index_builder.cppUpdates header lock guard type to match new mutex.
be/src/olap/task/engine_clone_task.cppUpdates header lock guard type to match new mutex.
be/src/olap/tablet_manager.cppUpdates header lock guard type to match new mutex.
be/src/olap/schema_change.cppUpdates header lock guard type to match new mutex.
be/src/olap/rowset_builder.cppUpdates header lock guard type to match new mutex.
be/src/olap/full_compaction.cppUpdates header lock guard type to match new mutex.
be/src/olap/compaction.cppUpdates header lock guard type to match new mutex.
be/src/olap/cumulative_compaction_time_series_policy.cppUses unlocked tablet API when caller already holds _meta_lock.
be/src/cloud/cloud_full_compaction.cppUses unlocked cloud-tablet API under an existing shared header lock.
be/src/cloud/cloud_base_compaction.cppUses unlocked cloud-tablet API under an existing shared header lock.
be/src/cloud/cloud_cumulative_compaction.cppSwitches traversal call to unlocked variant in locked context.
be/test/cloud/cloud_meta_mgr_test.cppUpdates tests to lock with new header lock type (via CTAD).
be/test/cloud/cloud_empty_rowset_compaction_test.cppUpdates tests to lock with new header lock type (via CTAD).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment threadbe/src/util/bthread_shared_mutex.h
Comment threadbe/src/cloud/cloud_meta_mgr.cpp
@liaoxin01

Copy link
Copy Markdown
ContributorAuthor

run buildall

@hello-stephen

Copy link
Copy Markdown
Contributor

BE UT Coverage Report

Increment line coverage 81.20% (95/117) 🎉

Increment coverage report
Complete coverage report

CategoryCoverage
Function Coverage53.56% (19592/36579)
Line Coverage36.62% (183333/500618)
Region Coverage33.17% (142487/429546)
Branch Coverage34.04% (61625/181046)

@liaoxin01

Copy link
Copy Markdown
ContributorAuthor

run buildall

@github-actionsgithub-actionsBot added the approved Indicates a PR has been approved by one committer. label Jul 27, 2026
@github-actions

Copy link
Copy Markdown
Contributor

PR approved by at least one committer and no changes requested.

@github-actions

Copy link
Copy Markdown
Contributor

PR approved by anyone and no changes requested.

@hello-stephen

Copy link
Copy Markdown
Contributor

BE Regression && UT Coverage Report

Increment line coverage 92.93% (92/99) 🎉

Increment coverage report
Complete coverage report

CategoryCoverage
Function Coverage71.62% (25628/35784)
Line Coverage54.51% (271966/498945)
Region Coverage52.05% (225366/432964)
Branch Coverage53.57% (97212/181462)

@hello-stephen

Copy link
Copy Markdown
Contributor

BE UT Coverage Report

Increment line coverage 81.20% (95/117) 🎉

Increment coverage report
Complete coverage report

CategoryCoverage
Function Coverage53.57% (19597/36579)
Line Coverage36.63% (183371/500647)
Region Coverage33.20% (142635/429583)
Branch Coverage34.04% (61637/181056)

@hello-stephen

Copy link
Copy Markdown
Contributor

BE Regression && UT Coverage Report

Increment line coverage 92.93% (92/99) 🎉

Increment coverage report
Complete coverage report

CategoryCoverage
Function Coverage71.62% (25629/35784)
Line Coverage54.47% (271801/498974)
Region Coverage51.95% (224945/433001)
Branch Coverage53.52% (97130/181472)

1 similar comment
@hello-stephen

Copy link
Copy Markdown
Contributor

BE Regression && UT Coverage Report

Increment line coverage 92.93% (92/99) 🎉

Increment coverage report
Complete coverage report

CategoryCoverage
Function Coverage71.62% (25629/35784)
Line Coverage54.47% (271801/498974)
Region Coverage51.95% (224945/433001)
Branch Coverage53.52% (97130/181472)

@hello-stephen

Copy link
Copy Markdown
Contributor

BE Regression && UT Coverage Report

Increment line coverage 92.93% (92/99) 🎉

Increment coverage report
Complete coverage report

CategoryCoverage
Function Coverage71.62% (25629/35784)
Line Coverage54.47% (271812/498974)
Region Coverage51.96% (225007/433001)
Branch Coverage53.53% (97134/181472)

@hello-stephen

Copy link
Copy Markdown
Contributor

BE Regression && UT Coverage Report

Increment line coverage 92.93% (92/99) 🎉

Increment coverage report
Complete coverage report

CategoryCoverage
Function Coverage71.62% (25630/35784)
Line Coverage54.48% (271838/498974)
Region Coverage52.00% (225166/433001)
Branch Coverage53.54% (97153/181472)

@liaoxin01

Copy link
Copy Markdown
ContributorAuthor

run p0

@hello-stephen

Copy link
Copy Markdown
Contributor

BE Regression && UT Coverage Report

Increment line coverage 92.93% (92/99) 🎉

Increment coverage report
Complete coverage report

CategoryCoverage
Function Coverage71.64% (25637/35784)
Line Coverage54.51% (271983/498974)
Region Coverage52.03% (225290/433001)
Branch Coverage53.56% (97193/181472)

@liaoxin01
liaoxin01 merged commit 88db7a4 into apache:branch-4.0Jul 28, 2026
28 of 31 checks passed
@liaoxin01
liaoxin01 deleted the codex/pick-64574-to-branch-4.0 branch July 28, 2026 09:11
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approvedIndicates a PR has been approved by one committer.reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

@liaoxin01@hello-stephen@morningman