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


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

Review Comment:
   This only updates the Nereids `ThriftPlansBuilder` path, but BE now switches 
every random-distribution sink to `FIND_TABLET_RANDOM_BUCKET` in both 
`VTabletWriter` implementations. The classic `Coordinator` path 
(`Coordinator.FragmentExecParams.toThrift()`) and the one-shot stream-load / 
group-commit path (`NereidsStreamLoadPlanner.plan()`) still serialize the old 
sink shape and never populate `local_bucket_seqs`. On those paths 
`local_bucket_seqs` stays empty in `vtablet_finder.cpp`, so the writer no 
longer does the old per-batch round-robin and collapses back to a single bucket 
for the whole load. This needs the same FE propagation on every 
`OLAP_TABLE_SINK` planning path, not just this one.



##########
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];

Review Comment:
   This charges each touched partition with the full block size. 
`_partition_to_tablet_map` has one entry per partition, but `bytes_per_row * 
rows` is just `block->bytes()` for the whole batch, not the bytes belonging to 
that partition's rows. If one block fans out to N partitions, each partition is 
overcounted by roughly N and the 200 MB rotation fires much earlier than 
intended. The accounting needs to be partition-local (for example by counting 
rows per partition or actual bytes sent per tablet).



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

Review Comment:
   The counter is never reset when we advance away from a bucket. After bucket 
0 crosses 200 MB once, wrapping back to bucket 0 later will hit this condition 
again on the very next batch, so large loads degenerate into 'rotate every 
batch after the first full cycle' instead of writing ~200 MB chunks as 
described in the PR. We need to subtract/reset the current bucket's counter 
when switching, or track bytes since the current bucket became active rather 
than lifetime bytes per tablet.



##########
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();

Review Comment:
   `fragmentParams.getFragment().getOutputSink()` is not BE-local here. 
`PlanFragment.toThrift()` calls `sink.toThrift()`, and 
`OlapTableSink.toThrift()` returns the shared `tDataSink` object, so every 
`TPipelineFragmentParams` for this fragment still points at the same 
`TDataSink`. Replacing `olap_table_sink` on that shared object means the last 
worker processed overwrites the sink seen by all earlier workers. To make the 
per-BE mutation real, you need to deep-copy the enclosing `TDataSink` / 
`TPlanFragment` before mutating it, not only the nested `TOlapTableSink`.



##########
be/src/storage/tablet_info.cpp:
##########
@@ -700,10 +700,15 @@ Status 
VOlapTablePartitionParam::generate_partition_from(const TOlapTablePartiti
     part_result = _obj_pool.add(new VOlapTablePartition(&_partition_block));
     part_result->id = t_part.id;
     part_result->is_mutable = t_part.is_mutable;
-    // only load_to_single_tablet = true will set load_tablet_idx
     if (t_part.__isset.load_tablet_idx) {
         part_result->load_tablet_idx = t_part.load_tablet_idx;
     }
+    if (t_part.__isset.bucket_be_id) {
+        part_result->bucket_be_id = t_part.bucket_be_id;
+    }
+    if (t_part.__isset.local_bucket_seqs) {
+        part_result->local_bucket_seqs = t_part.local_bucket_seqs;

Review Comment:
   This only covers the initial `generate_partition_from()` path. The runtime 
partition refresh paths still build `VOlapTablePartition` manually in 
`add_partitions()` and `replace_partitions()` without copying `load_tablet_idx` 
/ `local_bucket_seqs`. For random-distribution tables with auto partitioning or 
replace-partition during load, `FIND_TABLET_RANDOM_BUCKET` then sees 
`load_tablet_idx == -1` and line 96 in `vtablet_finder.cpp` indexes 
`indexes[0].tablets[cur_bucket]` with `cur_bucket = -1 % num_buckets`. Those 
paths need the same field propagation as the initial load path.



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