hello-stephen commented on code in PR #71336:
URL: https://github.com/apache/doris/pull/71336#discussion_r4784232538


##########
be/test/runtime/fragment_mgr_cross_cluster_cancel_test.cpp:
##########
@@ -287,6 +303,177 @@ TEST_F(FragmentMgrCrossClusterCancelTest, 
RebuildRestoresFragmentExecutingCount)
     EXPECT_EQ(get_fragment_executing_count(), count_before_rebuild);
 }
 
+TEST_F(FragmentMgrCrossClusterCancelTest, DumpPipelineTasksWithGlobalRfReset) {
+    auto* fragment_mgr = _exec_env.fragment_mgr();
+    EXPECT_EQ(fragment_mgr->dump_pipeline_tasks(0),
+              "0 pipeline fragment contexts are still running! 
duration_limit=0\n");
+
+    TUniqueId query_id;
+    query_id.__set_hi(707);
+    query_id.__set_lo(808);
+    constexpr int fragment_id = 1;
+    constexpr int filter_id = 7;
+    TNetworkAddress coord;
+    coord.hostname = "127.0.0.1";
+    coord.port = 9030;
+    auto query_ctx = QueryContext::create(query_id, &_exec_env, 
_make_min_query_options(789), coord,
+                                          true, coord, 
QuerySource::INTERNAL_FRONTEND);
+    SCOPED_ATTACH_TASK(query_ctx->resource_ctx());
+    auto handler = std::make_shared<RuntimeFilterMergeControllerEntity>();
+    auto rf_params =
+            TRuntimeFilterParamsBuilder()
+                    .add_rid_to_runtime_filter(
+                            filter_id, 
TRuntimeFilterDescBuilder(filter_id).set_mode(false).build())
+                    .add_runtime_filter_builder_num(filter_id, 3)
+                    .add_rid_to_target_paramv2(filter_id)
+                    .build();
+    ASSERT_TRUE(handler->init(query_ctx, rf_params).ok());
+    query_ctx->set_merge_controller_handler(handler);
+    TPipelineFragmentParams params;
+    params.__set_fragment_id(fragment_id);
+    params.__set_need_notify_close(false);
+    auto pfc = std::make_shared<PipelineFragmentContext>(query_id, params, 
query_ctx, &_exec_env,
+                                                         
FragmentMgr::FinishCallback());
+    const auto key = std::make_pair(query_id, fragment_id);
+    // This diagnostic-only PFC has no tasks or execution-count increment. 
Erase directly
+    // rather than calling remove_pipeline_context(), which decrements that 
count.
+    fragment_mgr->_pipeline_map.insert(key, pfc);
+    Defer cleanup([&] { fragment_mgr->_pipeline_map.erase(key); });
+
+    const std::string query_heading = "QueryId: " + print_id(query_id);
+    auto initial_dump = fragment_mgr->dump_pipeline_tasks(0);
+    EXPECT_NE(initial_dump.find(query_heading), std::string::npos);
+    EXPECT_NE(initial_dump.find("global_runtime_filter_mgr:"), 
std::string::npos);
+    EXPECT_NE(initial_dump.find("RuntimeFilterMergeControllerEntity Info:"), 
std::string::npos);
+    EXPECT_NE(initial_dump.find("filter_id: 7, stage: 0, Merger:"), 
std::string::npos);
+    EXPECT_NE(initial_dump.find("No.0 (elapse_second="), std::string::npos);
+    EXPECT_NE(initial_dump.find("PipelineFragmentContext Info:"), 
std::string::npos);
+    auto filtered_dump = fragment_mgr->dump_pipeline_tasks(3600);
+    EXPECT_EQ(filtered_dump.find(query_heading), std::string::npos);
+    EXPECT_EQ(filtered_dump.find("PipelineFragmentContext Info:"), 
std::string::npos);
+
+    google::protobuf::RepeatedField<int32_t> filter_ids;
+    filter_ids.Add(filter_id);
+    constexpr int rounds = 32;
+    std::barrier rendezvous(2);
+    std::vector<Status> reset_results(rounds);
+    std::vector<std::string> dumps(rounds);
+    std::thread resetter([&] {
+        SCOPED_ATTACH_TASK(query_ctx->resource_ctx());
+        for (int round = 0; round < rounds; ++round) {
+            rendezvous.arrive_and_wait();
+            reset_results[round] = query_ctx->reset_global_rf(filter_ids);
+            rendezvous.arrive_and_wait();
+        }
+    });
+    std::thread dumper([&] {
+        for (int round = 0; round < rounds; ++round) {
+            rendezvous.arrive_and_wait();
+            dumps[round] = fragment_mgr->dump_pipeline_tasks(0);
+            rendezvous.arrive_and_wait();
+        }
+    });
+    resetter.join();
+    dumper.join();
+
+    const std::regex rf_line(
+            "filter_id: 7, stage: ([0-9]+), Merger: \\([^\\n]*mode: GLOBAL, 
stage: ([0-9]+)");
+    for (int round = 0; round < rounds; ++round) {
+        ASSERT_TRUE(reset_results[round].ok()) << 
reset_results[round].to_string();
+        std::smatch match;
+        ASSERT_TRUE(std::regex_search(dumps[round], match, rf_line)) << 
dumps[round];
+        int stage = std::stoi(match[1]);
+        EXPECT_EQ(stage, std::stoi(match[2]));
+        EXPECT_GE(stage, round);
+        EXPECT_LE(stage, round + 1);
+    }
+    auto final_dump = fragment_mgr->dump_pipeline_tasks(0);
+    EXPECT_NE(final_dump.find("filter_id: 7, stage: 32, Merger:"), 
std::string::npos);
+    EXPECT_NE(final_dump.find("mode: GLOBAL, stage: 32"), std::string::npos);
+    EXPECT_TRUE(fragment_mgr->_pipeline_map.erase(key));
+    EXPECT_EQ(fragment_mgr->dump_pipeline_tasks(0).find(query_heading), 
std::string::npos);
+}
+
+TEST_F(FragmentMgrCrossClusterCancelTest, 
DumpPipelineTasksReleasesLocalMergerUnderQueryTracker) {
+    class TrackerCheckingMerger final : public RuntimeFilterMerger {
+    public:
+        TrackerCheckingMerger(const TRuntimeFilterDesc* desc, 
RuntimeFilterMgr* mgr,
+                              MemTrackerLimiter* tracker, bool* destroyed)
+                : RuntimeFilterMerger(nullptr, desc),
+                  _mgr(mgr),
+                  _tracker(tracker),
+                  _destroyed(destroyed) {}
+
+        ~TrackerCheckingMerger() override {
+            
EXPECT_EQ(thread_context()->thread_mem_tracker_mgr->limiter_mem_tracker(), 
_tracker);
+            EXPECT_FALSE(thread_context()->is_attach_task());
+            *_destroyed = true;
+        }
+
+        std::string debug_string() override {
+            
EXPECT_EQ(thread_context()->thread_mem_tracker_mgr->limiter_mem_tracker(), 
_tracker);
+            EXPECT_FALSE(thread_context()->is_attach_task());
+            // Drop the manager's ownership while debug_string's snapshot 
still owns this
+            // test merger. No Bloom buffer or concurrent recursive round is 
needed.
+            LockGuard lock(_mgr->_lock);
+            _mgr->_local_merge_map.clear();
+            return "tracked local merger";
+        }
+
+    private:
+        RuntimeFilterMgr* _mgr;
+        MemTrackerLimiter* _tracker;
+        bool* _destroyed;
+    };
+
+    TUniqueId query_id;
+    query_id.__set_hi(909);
+    query_id.__set_lo(1010);
+    TNetworkAddress coord;
+    coord.hostname = "127.0.0.1";
+    coord.port = 9030;
+    auto query_ctx = QueryContext::create(query_id, &_exec_env, 
_make_min_query_options(789), coord,
+                                          true, coord, 
QuerySource::INTERNAL_FRONTEND);
+    constexpr int filter_id = 7;
+    auto desc = TRuntimeFilterDescBuilder(filter_id).build();
+    auto* mgr = query_ctx->runtime_filter_mgr();
+    bool destroyed = false;

Review Comment:
   [P3] Keep the destruction flag alive through query teardown
   
   If a regression skips local RF rendering, the fake merger stays in 
`_local_merge_map` and the nonfatal expectations let cleanup continue. 
`QueryContext::~QueryContext()` then destroys the merger after `destroyed` has 
gone out of scope, and the merger's destructor writes through the stale 
pointer. Declare `destroyed` before `query_ctx` so the test can report the 
failed assertions safely.



-- 
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