Copilot commented on code in PR #62496:
URL: https://github.com/apache/doris/pull/62496#discussion_r3080800934


##########
be/src/exec/scan/scanner_context.h:
##########
@@ -115,7 +115,7 @@ class ScannerContext : public 
std::enable_shared_from_this<ScannerContext>,
                    const TupleDescriptor* output_tuple_desc,
                    const RowDescriptor* output_row_descriptor,
                    const std::list<std::shared_ptr<ScannerDelegate>>& 
scanners, int64_t limit_,
-                   std::shared_ptr<Dependency> dependency
+                   std::shared_ptr<Dependency> dependency, 
std::atomic<int64_t>* shared_scan_limit

Review Comment:
   Passing a raw pointer for `shared_scan_limit` makes lifetime requirements 
implicit and easy to violate (dangling pointer risk), especially since 
`ScannerContext` instances can outlive some surrounding scaffolding during 
shutdown/cancellation. Consider making the API express ownership/lifetime 
explicitly (e.g., `std::shared_ptr<std::atomic<int64_t>>`, or a small shared 
state object), or accept/store a reference with a clearly documented guarantee 
that the referenced atomic outlives `ScannerContext`.
   ```suggestion
                      std::shared_ptr<Dependency> dependency,
                      std::shared_ptr<std::atomic<int64_t>> shared_scan_limit
   ```



##########
be/test/exec/scan/scanner_context_test.cpp:
##########
@@ -122,6 +122,7 @@ class ScannerContextTest : public testing::Test {
     std::shared_ptr<CgroupCpuCtl> cgroup_cpu_ctl = 
std::make_shared<CgroupV2CpuCtl>(1);
     std::unique_ptr<ScannerScheduler> scan_scheduler =
             std::make_unique<ThreadPoolSimplifiedScanScheduler>("ForTest", 
cgroup_cpu_ctl);
+    std::atomic<int64_t> shared_limit {-1};

Review Comment:
   Tests now pass a `shared_limit` pointer but keep it at `-1` (no limit). This 
means the newly added shared-limit enforcement paths (quota acquisition, 
truncation/discard-on-exhaustion, early finish when remaining==0) are not 
exercised by these unit tests. To cover the new behavior, initialize 
`shared_limit` from `limit` in tests that set a positive `limit`, and add at 
least one case asserting: (1) blocks are truncated when the remaining quota is 
smaller than a block, and (2) scanning stops/finishes when quota reaches 0.



##########
be/src/exec/scan/scanner_context.h:
##########
@@ -221,6 +221,12 @@ class ScannerContext : public 
std::enable_shared_from_this<ScannerContext>,
     int _batch_size;
     // The limit from SQL's limit clause
     int64_t limit;
+    // Points to the shared remaining limit on ScanOperatorX, shared across all
+    // parallel instances and their scanners. -1 means no limit.
+    std::atomic<int64_t>* _shared_scan_limit = nullptr;

Review Comment:
   `remaining_limit()` unconditionally dereferences `_shared_scan_limit`, which 
is default-initialized to `nullptr` and is also used throughout the 
implementation (e.g., quota acquisition / finish checks). If any call path 
constructs a `ScannerContext` without a valid shared limit pointer (or with a 
pointer whose lifetime ends early), this will crash. Make `_shared_scan_limit` 
a non-null invariant (e.g., store a reference, or enforce non-null via 
constructor + `DCHECK(_shared_scan_limit != nullptr)`), and document/guarantee 
that the pointed-to atomic outlives the `ScannerContext`.
   ```suggestion
       // Default shared remaining limit used to preserve a non-null invariant 
for
       // _shared_scan_limit. A value of -1 means "no limit".
       inline static std::atomic<int64_t> _default_shared_scan_limit {-1};
       // Points to the shared remaining limit on ScanOperatorX, shared across 
all
       // parallel instances and their scanners. This pointer must always be 
non-null;
       // if a caller does not provide one explicitly, it falls back to
       // _default_shared_scan_limit. Any externally supplied atomic must 
outlive this
       // ScannerContext. A value of -1 means no limit.
       std::atomic<int64_t>* _shared_scan_limit = &_default_shared_scan_limit;
   ```



##########
be/test/exec/scan/scanner_context_test.cpp:
##########
@@ -150,7 +151,7 @@ TEST_F(ScannerContextTest, test_init) {
 
     std::shared_ptr<ScannerContext> scanner_context = 
ScannerContext::create_shared(
             state.get(), olap_scan_local_state.get(), output_tuple_desc, 
output_row_descriptor,
-            scanners, limit, scan_dependency, parallel_tasks);
+            scanners, limit, scan_dependency, &shared_limit, parallel_tasks);

Review Comment:
   Tests now pass a `shared_limit` pointer but keep it at `-1` (no limit). This 
means the newly added shared-limit enforcement paths (quota acquisition, 
truncation/discard-on-exhaustion, early finish when remaining==0) are not 
exercised by these unit tests. To cover the new behavior, initialize 
`shared_limit` from `limit` in tests that set a positive `limit`, and add at 
least one case asserting: (1) blocks are truncated when the remaining quota is 
smaller than a block, and (2) scanning stops/finishes when quota reaches 0.



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