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

[BugFix] Check the datacache usability before calling its function in case that the uninitialized members are called in some cases. #51158

Merged
merged 1 commit into from
Sep 19, 2024

Conversation

GavinMar
Copy link
Contributor

@GavinMar GavinMar commented Sep 19, 2024

Why I'm doing:

Now we call datacache update_mem_quota functions to release the memory resource before coredump. However, in some cases, especially for unittests, the datacache instance may not be initialized, which cause an unintialized datacache instance is used and result in unexpected error.

What I'm doing:

Check the datacache usability before calling its function.

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.3
    • 3.2
    • 3.1
    • 3.0
    • 2.5

if (_kv_cache) {
return _kv_cache->engine_type();
}
return DataCacheEngineType::STARCACHE;
}

void BlockCache::_refresh_quota() {
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:
Accessing _kv_cache without checking for its initialization state.

You can modify the code like this:

void BlockCache::record_read_remote(size_t size, int64_t latency_us) {
    if (_kv_cache) {
        _kv_cache->record_read_remote(size, latency_us);
    }
}

const DataCacheMetrics BlockCache::cache_metrics(int level) const {
    if (_kv_cache) {
        return _kv_cache->cache_metrics(level);
    }
    DataCacheMetrics dummy_metrics;
    return dummy_metrics;
}

void BlockCache::record_read_cache(size_t size, int64_t latency_us) {
    if (_kv_cache) {
        _kv_cache->record_read_cache(size, latency_us);
    }
}

DataCacheEngineType BlockCache::engine_type() {
    if (_kv_cache) {
        return _kv_cache->engine_type();
    }
    return DataCacheEngineType::STARCACHE;
}

Make sure additional functions also properly check _kv_cache before use to avoid potential issues.

@@ -66,6 +66,9 @@ Status BlockCache::init(const CacheOptions& options) {

Status BlockCache::write_buffer(const CacheKey& cache_key, off_t offset, const IOBuffer& buffer,
WriteCacheOptions* options) {
if (!_kv_cache) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Too many If

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Too many If

Changed it to check before calling datacache functions.

… case that the uninitialized members are called in some cases.

Signed-off-by: GavinMar <[email protected]>
@GavinMar GavinMar requested a review from a team as a code owner September 19, 2024 09:54
@GavinMar GavinMar changed the title [BugFix] Check the cache usability in datacache functions in case that the uninitialized members are called in some cases. [BugFix] Check the datacache usability before calling its function in case that the uninitialized members are called in some cases. Sep 19, 2024
Copy link

[Java-Extensions Incremental Coverage Report]

pass : 0 / 0 (0%)

Copy link

[FE Incremental Coverage Report]

pass : 0 / 0 (0%)

Copy link

[BE Incremental Coverage Report]

fail : 0 / 1 (00.00%)

file detail

path covered_line new_line coverage not_covered_line_detail
🔵 be/src/runtime/exec_env.cpp 0 1 00.00% [829]

@stephen-shelby stephen-shelby merged commit 20ed111 into StarRocks:main Sep 19, 2024
57 of 58 checks passed
@github-actions github-actions bot added the 3.3 label Sep 20, 2024
trueeyu pushed a commit that referenced this pull request Sep 23, 2024
… case that the uninitialized members are called in some cases. (backport #51158) (#51231)

Signed-off-by: GavinMar <[email protected]>
renzhimin7 pushed a commit to renzhimin7/starrocks that referenced this pull request Nov 7, 2024
… case that the uninitialized members are called in some cases. (StarRocks#51158)

Signed-off-by: GavinMar <[email protected]>
Signed-off-by: zhiminr.ren <[email protected]>
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