github-actions[bot] commented on code in PR #62441:
URL: https://github.com/apache/doris/pull/62441#discussion_r3076967158


##########
be/test/exec/operator/spill_sort_source_operator_test.cpp:
##########
@@ -1196,4 +1200,71 @@ TEST_F(SpillSortSourceOperatorTest, 
EndToEndSinkAndSource) {
     ASSERT_TRUE(st.ok());
 }
 
+TEST_F(SpillSortSourceOperatorTest, SpillDataDirThreadSafety) {
+    // Test the thread safety of get_disk_usage and update_capacity (Bug 1)
+    std::atomic<bool> stop {false};
+
+    // Thread 1: Keep calling update_capacity
+    std::thread t1([&]() {
+        while (!stop) {
+            auto st = _helper.spill_data_dir_ptr->update_capacity();
+            EXPECT_TRUE(st.ok());
+            std::this_thread::yield();
+        }
+    });
+
+    // Thread 2: Keep calling get_disk_usage
+    std::thread t2([&]() {
+        while (!stop) {
+            double usage = _helper.spill_data_dir_ptr->get_disk_usage(1024);
+            EXPECT_GE(usage, 0.0);
+            EXPECT_LE(usage, 1.0);
+            std::this_thread::yield();
+        }
+    });
+
+    // Let them run concurrently for a short while
+    std::this_thread::sleep_for(std::chrono::milliseconds(50));
+    stop = true;
+    t1.join();
+    t2.join();
+}
+
+TEST_F(SpillSortSourceOperatorTest, SpillWriterMemoryTracking) {
+    // Test the accuracy of memory tracking in SpillFileWriter (Bug 2)
+    SpillFileSPtr spill_file;
+    auto st = 
ExecEnv::GetInstance()->spill_file_mgr()->create_spill_file("SpillWriterTest",
+                                                                          
spill_file);
+    ASSERT_TRUE(st.ok()) << "create_spill_file failed: " << st.to_string();
+
+    SpillFileWriterSPtr writer;
+    st = spill_file->create_writer(_helper.runtime_state.get(), 
_helper.operator_profile.get(),
+                                   writer);
+    ASSERT_TRUE(st.ok());
+
+    auto* memory_used_counter = 
_helper.common_profile->get_counter("MemoryUsage");
+    ASSERT_TRUE(memory_used_counter != nullptr);
+
+    // We use value() directly as it is safe for all Counter types.
+    // Downcasting to HighWaterMarkCounter caused ASAN heap-buffer-overflow
+    // because MemoryUsage might be a normal Counter in the latest Profile 
structure.
+    int64_t initial_memory = memory_used_counter->value();
+
+    std::vector<int32_t> data(1000);
+    std::iota(data.begin(), data.end(), 0);
+    auto block = ColumnHelper::create_block<DataTypeInt32>(data);
+

Review Comment:
   This does not actually prove the bug fixed in `spill_file_writer.cpp`. In 
the old implementation, `MemoryUsage` was decremented too early, but it was 
still back to `initial_memory` by the time `write_block()` returned, so this 
assertion would pass on both the buggy and fixed code. To make this a 
regression test, the test needs to observe the counter while 
`_file_writer->append(buff)` is still blocked/in flight (for example via a 
debug point or a controllable slow writer).



##########
be/src/exec/spill/spill_file_manager.h:
##########
@@ -79,8 +79,15 @@ class SpillDataDir {
 
     std::string debug_string();
 
+    // get current disk usage percent in a thread-safe way

Review Comment:
   This new accessor fixes one unlocked read, but the same `SpillDataDir` 
fields are still read without `_mutex` in `debug_string()` (called from the GC 
thread in `SpillFileManager::gc()`). That means the PR still leaves a data race 
between GC logging / `update_capacity()` and worker-thread updates like 
`update_spill_data_usage()` / `reach_capacity_limit()`. Please lock inside 
`debug_string()` or snapshot the protected fields under `_mutex` before 
formatting.



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