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

[Enhancement] Avoid acquire multiple lock in the same stack for tablet report #55808

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

srlch
Copy link
Contributor

@srlch srlch commented Feb 11, 2025

Why I'm doing:

In current impl, the longest lock acquire chain in tablet report will be looks like: tablet shard lock -> tablet meta lock -> pk relative lock which may make other thread stuck easliy because too many lock acquire at the same time.

What I'm doing:

Remove the lock dependency.

Fixes #issue

What type of PR is this:

  • BugFix
  • Feature
  • Enhancement
  • Refactor
  • UT
  • Doc
  • Tool

Does this PR entail a change in behavior?

  • Yes, this PR will result in a change in behavior.
  • No, this PR will not result in a change in behavior.

If yes, please specify the type of change:

  • Interface/UI changes: syntax, type conversion, expression evaluation, display information
  • Parameter changes: default values, similar parameters but with different default values
  • Policy changes: use new policy to replace old one, functionality automatically enabled
  • Feature removed
  • Miscellaneous: upgrade & downgrade compatibility, etc.

Checklist:

  • I have added test cases for my bug fix or my new feature
  • This pr needs user documentation (for new or modified features or behaviors)
    • I have added documentation for my new feature or new function
  • This is a backport pr

Bugfix cherry-pick branch check:

  • I have checked the version labels which the pr will be auto-backported to the target branch
    • 3.4
    • 3.3
    • 3.2
    • 3.1
    • 3.0

…t report

## Why I'm doing:
In current impl, the longest lock acquire chain in tablet report will be looks like: tablet shard lock -> tablet meta lock -> pk relative lock
which may make other thread stuck easliy because too many lock acquire at the same time.

## What I'm doing:
Remove the lock dependency.

Signed-off-by: srlch <[email protected]>
@srlch srlch requested a review from a team as a code owner February 11, 2025 08:37
@mergify mergify bot assigned srlch Feb 11, 2025
}
}
all_tablets.clear();

LOG(INFO) << "Report all " << tablets_info->size()
<< " tablets info. max_tablet_rowset_num:" << max_tablet_rowset_num;
StarRocksMetrics::instance()->max_tablet_rowset_num.set_value(max_tablet_rowset_num);
Copy link

Choose a reason for hiding this comment

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

The most risky bug in this code is:
Potential deadlock when accessing _tablets_shards.

You can modify the code like this:

std::vector<TabletSharedPtr> all_tablets;
for (const auto& tablets_shard : _tablets_shards) {
    // Ensure lock acquisition is done inside a smaller scope to prevent prolonged holding of locks
    {
        std::shared_lock rlock(tablets_shard.lock);
        for (const auto& [_, tablet_ptr] : tablets_shard.tablet_map) {
            all_tablets.push_back(tablet_ptr);
        }
    }
}

size_t max_tablet_rowset_num = 0;
for (auto& tablet_ptr : all_tablets) {
    TTablet t_tablet;
    TTabletInfo tablet_info;
    tablet_ptr->build_tablet_report_info(&tablet_info);
    max_tablet_rowset_num = std::max(max_tablet_rowset_num, tablet_ptr->version_count());
    // find expired transaction corresponding to this tablet
    TabletInfo tinfo(tablet_ptr->tablet_id(), tablet_ptr->schema_hash(), tablet_ptr->tablet_uid());
    auto find = expire_txn_map.find(tinfo);
    if (find != expire_txn_map.end()) {
        tablet_info.__set_transaction_ids(find->second);
        expire_txn_map.erase(find);
    }
    t_tablet.tablet_infos.push_back(tablet_info);

    if (!t_tablet.tablet_infos.empty()) {
        tablets_info->emplace(tablet_ptr->tablet_id(), t_tablet);
    }
}
all_tablets.clear();

@srlch srlch changed the title [Enhancement] Avoid multiple lock acquire in the same stack for tablet report [Enhancement] Avoid acquire multiple lock in the same stack for tablet report Feb 12, 2025
Signed-off-by: srlch <[email protected]>
Copy link

[FE Incremental Coverage Report]

pass : 0 / 0 (0%)

Copy link

[Java-Extensions Incremental Coverage Report]

pass : 0 / 0 (0%)

Copy link

[BE Incremental Coverage Report]

pass : 20 / 22 (90.91%)

file detail

path covered_line new_line coverage not_covered_line_detail
🔵 be/src/storage/tablet_manager.cpp 17 19 89.47% [1004, 1005]
🔵 be/src/storage/tablet.cpp 3 3 100.00% []

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.

1 participant