Mryange opened a new pull request, #70927:
URL: https://github.com/apache/doris/pull/70927

   ### Background
   
   Doris BE shares query resource contexts, the master FE endpoint, process 
memory samples, and Rowset state across worker and background threads. These 
values are accessed during statistics reporting, WAL replay, memory 
arbitration, and parallel scans.
   
   ### What problem does this PR solve?
   
   Issue Number: None
   Related PR: #70847 (fixed the RSS race)
   PR Type: Bugfix
   
   Problem Summary:
   
   A ThreadSanitizer P0 run reported races in these four shared values. This 
change fixes three of them. The RSS race was fixed by #70847 and its stack is 
included for reference. Most call-site changes migrate FE endpoint readers to 
accessors that return a consistent copy.
   
   ### Details
   
   - **Change map:** 168 additions and 145 deletions in production code, 7 
additions and 3 deletions in test setup, and no generated or documentation 
changes. The production changes cover query context publication, FE endpoint 
access and its callers, and Rowset lifecycle state.
   - **Frequency:** Intermittent. The stacks below show conflicting accesses 
captured in one P0 run.
   - **Root cause:** Statistics could copy the workload group before query 
initialization completed. Heartbeat processing updated the FE endpoint while 
WAL replay read its port. Parallel scanners read Rowset state outside the lock 
used by the state transition.
   - **Why existing tests missed it:** `ReportWorkerPool` and `WalManagerTest` 
exercise reporting and WAL behavior without running a concurrent real heartbeat 
update. `BetaRowsetTest.ReadTest` checks read results without asserting the 
parallel load interleaving. BE unit builds skip the query statistics 
registration path. P0 functional results alone do not detect conflicting 
accesses.
   - **Fix:** Publish the query resource context after workload-group setup. 
Return FE endpoint copies under a shared lock and update the endpoint under an 
exclusive lock. Use the existing Rowset mutex for lifecycle state reads and 
writes.
   - **Other paths and branches:** The synchronized endpoint accessors cover BE 
callers in both shared-nothing and cloud paths. The patch targets master.
   - **Behavior change:** Query contexts that fail before workload-group setup 
completes never enter the statistics map. Heartbeats rejected by the token 
check leave the previous endpoint and epoch in place.
   
   ### Testing
   
   - ASAN BE build passed with `./build.sh --be -j48` on the pre-rebase 
candidate at base `6f38079e391`. That candidate included a local RSS fix. The 
final branch picks up the upstream RSS fix from its base and has not been 
rebuilt.
   - The excerpts below are from the pre-fix TSan BE build `a371bd1428f`. 
Post-fix TSan and unit tests have not been run.
   
   ### Notes for Reviewers
   
   Each excerpt includes both sides of the conflicting access. Stack paths and 
line numbers refer to the TSan build.
   
   <details>
   <summary>Workload Group pointer</summary>
   
   ```text
   SUMMARY: ThreadSanitizer: data race 
be/src/runtime/workload_management/resource_context.h:83:68 in 
set_workload_group
   
   Write of size 8
     #0 operator= libstdc++/bits/shared_ptr_base.h:1530:69
     #1 operator= libstdc++/bits/shared_ptr.h:413:65
     #2 set_workload_group 
be/src/runtime/workload_management/resource_context.h:83:68
     #3 
doris::QueryContext::set_workload_group(std::shared_ptr<doris::WorkloadGroup>&) 
be/src/runtime/query_context.cpp:471:20
     #4 operator() be/src/runtime/fragment_mgr.cpp:503:13
     #5 __invoke_impl<doris::Status, (lambda at 
./be/src/runtime/fragment_mgr.cpp:503:13) &, 
phmap::flat_hash_map<doris::TUniqueId, std::weak_ptr<doris::QueryContext>, 
phmap::Hash<doris::TUniqueId>, phmap::EqualTo<doris::TUniqueId>, 
std::allocator<std::pair<const doris::TUniqueId, 
std::weak_ptr<doris::QueryContext> > > > &> libstdc++/bits/invoke.h:63:14
   
   Previous read of size 8
     #0 __shared_ptr libstdc++/bits/shared_ptr_base.h:1529:7
     #1 shared_ptr libstdc++/bits/shared_ptr.h:203:7
     #2 workload_group 
be/src/runtime/workload_management/resource_context.h:65:54
     #3 
doris::ResourceContext::to_thrift_query_statistics(doris::TQueryStatistics*) 
const be/src/runtime/workload_management/resource_context.cpp:182:9
     #4 doris::RuntimeQueryStatisticsMgr::report_runtime_query_statistics() 
be/src/runtime/runtime_query_statistics_mgr.cpp:371:35
     #5 doris::Daemon::report_runtime_query_statistics_thread() 
be/src/common/daemon.cpp:459:65
   
   Location is heap block of size 104
   ```
   
   </details>
   
   <details>
   <summary>Master FE address</summary>
   
   ```text
   SUMMARY: ThreadSanitizer: data race be/src/agent/heartbeat_server.cpp:204:48 
in doris::HeartbeatServer::_heartbeat(doris::TMasterInfo const&)
   
   Write of size 4
     #0 doris::HeartbeatServer::_heartbeat(doris::TMasterInfo const&) 
be/src/agent/heartbeat_server.cpp:204:48
     #1 doris::HeartbeatServer::heartbeat(doris::THeartbeatResult&, 
doris::TMasterInfo const&) be/src/agent/heartbeat_server.cpp:87:17
     #2 doris::HeartbeatServiceProcessor::process_heartbeat(int, 
apache::thrift::protocol::TProtocol*, apache::thrift::protocol::TProtocol*, 
void*) gensrc/build/gen_cpp/HeartbeatService.cpp:302:13
     #3 
doris::HeartbeatServiceProcessor::dispatchCall(apache::thrift::protocol::TProtocol*,
 apache::thrift::protocol::TProtocol*, std::__cxx11::basic_string<char, 
std::char_traits<char>, std::allocator<char>> const&, int, void*) 
gensrc/build/gen_cpp/HeartbeatService.cpp:275:3
     #4 
apache::thrift::TDispatchProcessor::process(std::shared_ptr<apache::thrift::protocol::TProtocol>,
 std::shared_ptr<apache::thrift::protocol::TProtocol>, void*) 
thirdparty/installed/include/thrift/TDispatchProcessor.h:121:12
     #5 apache::thrift::server::TConnectedClient::run() <null>
   
   Previous read of size 4
     #0 doris::WalManager::_replay_background() 
be/src/load/group_commit/wal/wal_manager.cpp:402:55
     #1 operator() be/src/load/group_commit/wal/wal_manager.cpp:88:72
     #2 __invoke_impl<void, (lambda at 
./be/src/load/group_commit/wal/wal_manager.cpp:88:37) &> 
libstdc++/bits/invoke.h:63:14
     #3 __invoke_r<void, (lambda at 
./be/src/load/group_commit/wal/wal_manager.cpp:88:37) &> 
libstdc++/bits/invoke.h:113:2
     #4 std::_Function_handler<void (), 
doris::WalManager::init()::$_0>::_M_invoke(std::_Any_data const&) 
libstdc++/bits/std_function.h:292:9
     #5 operator() libstdc++/bits/std_function.h:593:9
   
   Location is heap block of size 176
   ```
   
   </details>
   
   <details>
   <summary>Process RSS (fixed upstream in #70847)</summary>
   
   ```text
   SUMMARY: ThreadSanitizer: data race be/src/util/perf_counters.cpp:585:13 in 
doris::PerfCounters::refresh_proc_status()
   
   Write of size 8
     #0 doris::PerfCounters::refresh_proc_status() 
be/src/util/perf_counters.cpp:585:13
     #1 doris::refresh_process_memory_metrics() be/src/common/daemon.cpp:208:5
     #2 doris::Daemon::memory_maintenance_thread() 
be/src/common/daemon.cpp:336:9
     #3 operator() be/src/common/daemon.cpp:605:69
     #4 __invoke_impl<void, (lambda at ./be/src/common/daemon.cpp:605:52) &> 
libstdc++/bits/invoke.h:63:14
     #5 __invoke_r<void, (lambda at ./be/src/common/daemon.cpp:605:52) &> 
libstdc++/bits/invoke.h:113:2
   
   Previous read of size 8
     #0 get_vm_rss be/src/util/perf_counters.h:120:49
     #1 process_memory_usage 
be/src/runtime/memory/global_memory_arbitrator.h:41:16
     #2 doris::get_concurrent_per_disk(long, int) 
be/src/storage/olap_server.cpp:867:33
     #3 doris::StorageEngine::_generate_compaction_tasks(doris::CompactionType, 
std::vector<doris::DataDir*, std::allocator<doris::DataDir*>>&, bool) 
be/src/storage/olap_server.cpp:945:30
     #4 doris::StorageEngine::_changelog_compaction_tasks_producer_callback() 
be/src/storage/olap_server.cpp:796:21
     #5 operator() be/src/storage/olap_server.cpp:291:5
   
   Location is global 'doris::PerfCounters::_vm_rss' of size 8
   ```
   
   </details>
   
   <details>
   <summary>Rowset state</summary>
   
   ```text
   SUMMARY: ThreadSanitizer: data race be/src/storage/rowset/rowset.h:121:41 in 
rowset_state
   
   Read of size 4
     #0 rowset_state be/src/storage/rowset/rowset.h:121:41
     #1 doris::Rowset::load(bool) be/src/storage/rowset/rowset.cpp:68:31
     #2 doris::ParallelScannerBuilder::_load() 
be/src/exec/scan/parallel_scanner_builder.cpp:295:13
     #3 
doris::ParallelScannerBuilder::build_scanners(std::__cxx11::list<std::shared_ptr<doris::Scanner>,
 std::allocator<std::shared_ptr<doris::Scanner>>>&) 
be/src/exec/scan/parallel_scanner_builder.cpp:70:5
     #4 
doris::OlapScanLocalState::_init_scanners(std::__cxx11::list<std::shared_ptr<doris::Scanner>,
 std::allocator<std::shared_ptr<doris::Scanner>>>*) 
be/src/exec/operator/olap_scan_operator.cpp:872:9
     #5 doris::ScanLocalState<doris::OlapScanLocalState>::_prepare_scanners() 
be/src/exec/operator/scan_operator.cpp:1000:5
   
   Previous write of size 4
     #0 on_load be/src/storage/rowset/rowset.h:81:27
     #1 doris::Rowset::load(bool) be/src/storage/rowset/rowset.cpp:76:13
     #2 doris::ParallelScannerBuilder::_load() 
be/src/exec/scan/parallel_scanner_builder.cpp:295:13
     #3 
doris::ParallelScannerBuilder::build_scanners(std::__cxx11::list<std::shared_ptr<doris::Scanner>,
 std::allocator<std::shared_ptr<doris::Scanner>>>&) 
be/src/exec/scan/parallel_scanner_builder.cpp:70:5
     #4 
doris::OlapScanLocalState::_init_scanners(std::__cxx11::list<std::shared_ptr<doris::Scanner>,
 std::allocator<std::shared_ptr<doris::Scanner>>>*) 
be/src/exec/operator/olap_scan_operator.cpp:872:9
     #5 doris::ScanLocalState<doris::OlapScanLocalState>::_prepare_scanners() 
be/src/exec/operator/scan_operator.cpp:1000:5
   
   Location is heap block of size 296
   ```
   
   </details>
   
   ### Release note
   
   Fix BE data races in workload-group publication, master FE address updates, 
and Rowset lifecycle state.
   
   ### Check List (For Author)
   
   - Behavior changed: Yes (see Details)
   - Does this need documentation: No
   - Pre-submission status: Partially verified: ASAN BE build passed before 
rebasing. The final revision has not been rebuilt or rerun under TSan. Unit 
tests have not been run.
   
   ### AI Disclosure
   
   - Tool: Codex (GPT-6)
   - Generated: Code changes and PR description
   - Human review: The author discussed the fixes and approved the approaches 
for local implementation.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to