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


##########
fe/fe-core/src/main/java/org/apache/doris/qe/runtime/ThriftPlansBuilder.java:
##########
@@ -242,11 +243,116 @@ private static void 
setParamsForOlapTableSink(List<PipelineDistributedPlan> dist
                     
fragmentParams.setNumLocalSink(fragmentParams.getLocalParams().size());
                     LOG.info("num local sink for backend {} is {}", 
fragmentParams.getBackendId(),
                             fragmentParams.getNumLocalSink());
+
+                    // Assign per-BE starting bucket and bucket_be_id for 
random distribution
+                    assignRandomBucketPerBe(fragmentParams, worker.id());

Review Comment:
   This runs for every `OLAP_TABLE_SINK`, including 
`load_to_single_tablet=true`. In that mode BE still chooses 
`FIND_TABLET_EVERY_SINK`, so it will honor the rewritten `load_tablet_idx` but 
ignore `local_bucket_seqs`. That changes the semantics from "one tablet for the 
whole load" to "one tablet per BE". For example, with two BEs and global 
`load_tablet_idx=0`, BE1 can be rewritten to bucket 0 while BE2 is rewritten to 
bucket 1. Please skip the per-BE rewrite when `load_to_single_tablet` is 
enabled.



##########
fe/fe-core/src/main/java/org/apache/doris/qe/runtime/ThriftPlansBuilder.java:
##########
@@ -242,11 +243,116 @@ private static void 
setParamsForOlapTableSink(List<PipelineDistributedPlan> dist
                     
fragmentParams.setNumLocalSink(fragmentParams.getLocalParams().size());
                     LOG.info("num local sink for backend {} is {}", 
fragmentParams.getBackendId(),
                             fragmentParams.getNumLocalSink());
+
+                    // Assign per-BE starting bucket and bucket_be_id for 
random distribution
+                    assignRandomBucketPerBe(fragmentParams, worker.id());
                 }
             }
         }
     }
 
+    /**
+     * For random-distribution partitions, override load_tablet_idx and set 
bucket_be_id /
+     * local_bucket_seqs so that each BE starts writing to one of its own 
local buckets and
+     * can rotate within them once per-tablet bytes exceed the threshold.
+     *
+     * <p>local_bucket_seqs always contains all local buckets for this BE.  
The starting bucket
+     * rotates across consecutive load tasks via tabletIndex; within a single 
load, the BE
+     * switches to the next entry in the list only when the 200 MB threshold 
is reached.
+     * Small loads (e.g. stream load) naturally stay on one bucket because the 
threshold is
+     * never triggered.
+     */
+    private static void assignRandomBucketPerBe(TPipelineFragmentParams 
fragmentParams, long beId) {
+        org.apache.doris.thrift.TDataSink outputSink = 
fragmentParams.getFragment().getOutputSink();
+        org.apache.doris.thrift.TOlapTableSink olapSink = 
outputSink.getOlapTableSink();
+        if (olapSink == null || olapSink.getPartition() == null
+                || !olapSink.getPartition().isSetPartitions()) {
+            return;
+        }
+
+        // Check whether any partition uses random distribution (identified by 
load_tablet_idx being set)
+        boolean hasRandomPartition = 
olapSink.getPartition().getPartitions().stream()
+                
.anyMatch(org.apache.doris.thrift.TOlapTablePartition::isSetLoadTabletIdx);
+        if (!hasRandomPartition) {
+            return;
+        }
+
+        // Build tablet_id -> BE IDs index from location
+        org.apache.doris.thrift.TOlapTableLocationParam location = 
olapSink.getLocation();
+        if (location == null || !location.isSetTablets()) {
+            return;
+        }
+        Map<Long, List<Long>> tabletToBeIds = new java.util.HashMap<>();
+        for (org.apache.doris.thrift.TTabletLocation tabletLoc : 
location.getTablets()) {
+            tabletToBeIds.put(tabletLoc.getTabletId(), tabletLoc.getNodeIds());
+        }
+
+        // Deep-copy only the OlapTableSink so that per-BE modifications are 
isolated
+        org.apache.doris.thrift.TOlapTableSink sinkCopy = olapSink.deepCopy();
+        outputSink.setOlapTableSink(sinkCopy);

Review Comment:
   `outputSink` is still shared across workers here. `PlanFragment.toThrift()` 
stores `sink.toThrift()`, and `OlapTableSink.toThrift()` just returns its 
cached `tDataSink`, so every worker's `TPlanFragment.outputSink` points to the 
same `TDataSink` instance. Replacing only the nested `TOlapTableSink` means the 
last worker that executes this code overwrites the previous workers' bucket 
assignments. To make the per-BE mutation real, the whole `TDataSink` (or 
enclosing `TPlanFragment`) needs to be deep-copied before mutating it.



##########
be/src/exec/sink/writer/vtablet_writer.cpp:
##########
@@ -1546,7 +1546,7 @@ Status VTabletWriter::_init(RuntimeState* state, 
RuntimeProfile* profile) {
         if (table_sink.__isset.load_to_single_tablet && 
table_sink.load_to_single_tablet) {
             find_tablet_mode = 
OlapTabletFinder::FindTabletMode::FIND_TABLET_EVERY_SINK;
         } else {
-            find_tablet_mode = 
OlapTabletFinder::FindTabletMode::FIND_TABLET_EVERY_BATCH;
+            find_tablet_mode = 
OlapTabletFinder::FindTabletMode::FIND_TABLET_RANDOM_BUCKET;

Review Comment:
   Switching every random-distribution sink to `FIND_TABLET_RANDOM_BUCKET` is 
not backward-compatible yet. The new mode only works when FE has filled 
`local_bucket_seqs`, but that happens only in `ThriftPlansBuilder` today. 
Classic `Coordinator` and `NereidsStreamLoadPlanner` still serialize 
`fragment.toThrift()` directly, and mixed FE/BE versions also will not send the 
new fields. In all of those cases `seqs` stays empty, so we silently lose the 
old per-batch round-robin and pin the whole sink to a single bucket. This needs 
a compatibility fallback until every FE path populates the new fields.`



##########
be/src/exec/sink/vtablet_finder.cpp:
##########
@@ -82,8 +83,39 @@ Status OlapTabletFinder::find_tablets(RuntimeState* state, 
Block* block, int row
 
     if (_find_tablet_mode == FindTabletMode::FIND_TABLET_EVERY_ROW) {
         _vpartition->find_tablets(block, qualified_rows, partitions, 
tablet_index);
+    } else if (_find_tablet_mode == FindTabletMode::FIND_TABLET_RANDOM_BUCKET) 
{
+        // Use per-batch caching same as FIND_TABLET_EVERY_BATCH
+        _vpartition->find_tablets(block, qualified_rows, partitions, 
tablet_index,
+                                  &_partition_to_tablet_map);
+
+        int64_t bytes_per_row = rows > 0 ? block->bytes() / rows : 0;
+        for (auto& [partition, _] : _partition_to_tablet_map) {
+            if (partition->indexes.empty()) {
+                continue;
+            }
+            int64_t cur_bucket = partition->load_tablet_idx % 
partition->num_buckets;
+            int64_t cur_tablet_id = partition->indexes[0].tablets[cur_bucket];
+            _tablet_written_bytes[cur_tablet_id] += bytes_per_row * rows;

Review Comment:
   This charges the full block size to every touched partition. If a batch 
contains rows for partitions A and B, both counters get `block->bytes()`, so 
the 200 MB threshold is reached 2x too fast; with N touched partitions it is N 
times too fast. The rotation threshold needs to use bytes (or at least row 
counts) for the rows that actually routed to this partition, not the whole 
batch.



##########
be/src/exec/sink/vtablet_finder.cpp:
##########
@@ -82,8 +83,39 @@ Status OlapTabletFinder::find_tablets(RuntimeState* state, 
Block* block, int row
 
     if (_find_tablet_mode == FindTabletMode::FIND_TABLET_EVERY_ROW) {
         _vpartition->find_tablets(block, qualified_rows, partitions, 
tablet_index);
+    } else if (_find_tablet_mode == FindTabletMode::FIND_TABLET_RANDOM_BUCKET) 
{
+        // Use per-batch caching same as FIND_TABLET_EVERY_BATCH
+        _vpartition->find_tablets(block, qualified_rows, partitions, 
tablet_index,
+                                  &_partition_to_tablet_map);
+
+        int64_t bytes_per_row = rows > 0 ? block->bytes() / rows : 0;
+        for (auto& [partition, _] : _partition_to_tablet_map) {
+            if (partition->indexes.empty()) {
+                continue;
+            }
+            int64_t cur_bucket = partition->load_tablet_idx % 
partition->num_buckets;
+            int64_t cur_tablet_id = partition->indexes[0].tablets[cur_bucket];
+            _tablet_written_bytes[cur_tablet_id] += bytes_per_row * rows;
+
+            // Switch to the next local bucket when the threshold is reached.
+            // local_bucket_seqs carries the exact set of buckets assigned to 
this BE by the FE,
+            // which handles both single-replica and multi-replica correctly.
+            const auto& seqs = partition->local_bucket_seqs;
+            if (_tablet_written_bytes[cur_tablet_id] >= 
BUCKET_SWITCH_THRESHOLD_BYTES &&
+                !seqs.empty()) {
+                auto it = std::find(seqs.begin(), seqs.end(), 
static_cast<int32_t>(cur_bucket));
+                if (it != seqs.end()) {
+                    ++it;
+                    if (it == seqs.end()) {
+                        it = seqs.begin();
+                    }
+                    partition->load_tablet_idx = *it;

Review Comment:
   When we advance to the next bucket, the counter for `cur_tablet_id` is left 
above the threshold forever. After the rotation wraps back to this tablet 
later, the next small batch immediately switches away again instead of allowing 
another ~200 MB on that bucket. For example, with local buckets `[0, 2]`, after 
writing 200 MB to 0 and 200 MB to 2, the first small batch back on 0 will 
rotate away again. The counter needs to be reset or have the threshold 
subtracted when the bucket advances.



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