yujun777 commented on code in PR #48704:
URL: https://github.com/apache/doris/pull/48704#discussion_r1986738133


##########
fe/fe-core/src/main/java/org/apache/doris/alter/Alter.java:
##########
@@ -1056,11 +1073,103 @@ public void modifyPartitionsProperty(Database db,
         Env.getCurrentEnv().getEditLog().logBatchModifyPartition(info);
     }
 
+    public void setReplicasToDrop(Partition partition,
+                                 ReplicaAllocation oldReplicaAlloc,
+                                 ReplicaAllocation newReplicaAlloc,
+                                 Map<Long, Long> tableBeToReplicaNumMap) {
+        Set<Tag> scaleInTags = getScaleInTags(oldReplicaAlloc, 
newReplicaAlloc);
+        SystemInfoService systemInfoService = Env.getCurrentSystemInfo();
+        List<Long> aliveBes = systemInfoService.getAllBackendIds(true);
+
+        for (Tag tag : scaleInTags) {
+            int replicasToDrop = oldReplicaAlloc.getReplicaNumByTag(tag) - 
newReplicaAlloc.getReplicaNumByTag(tag);
+            if (replicasToDrop <= 0) {
+                return;
+            }
+
+            processReplicasInPartition(partition, tag, replicasToDrop,
+                    tableBeToReplicaNumMap, systemInfoService, 
oldReplicaAlloc, aliveBes);
+        }
+    }
+
+    private void processReplicasInPartition(Partition partition, Tag tag, int 
replicasToDrop,
+                                            Map<Long, Long> 
tableBeToReplicaNumMap, SystemInfoService systemInfoService,
+                                            ReplicaAllocation oldReplicaAlloc,
+                                            List<Long> aliveBes) {
+        for (MaterializedIndex index : 
partition.getMaterializedIndices(MaterializedIndex.IndexExtState.VISIBLE)) {
+            for (Tablet tablet : index.getTablets()) {
+                if (!isTabletHealthy(tablet, systemInfoService, partition, 
oldReplicaAlloc, aliveBes)) {
+                    continue;
+                }
+
+                List<Replica> toDealReplicas = getReplicasByTag(tablet, tag);
+                if (toDealReplicas == null) {
+                    continue;
+                }
+
+                sortReplicasByBackendCount(toDealReplicas, 
tableBeToReplicaNumMap);
+
+                markReplicasForDropping(toDealReplicas, replicasToDrop, 
tableBeToReplicaNumMap);
+            }
+        }
+    }
+
+    private boolean isTabletHealthy(Tablet tablet, SystemInfoService 
systemInfoService,
+                                    Partition partition, ReplicaAllocation 
oldReplicaAlloc,
+                                    List<Long> aliveBes) {
+        return tablet.getHealth(systemInfoService, 
partition.getVisibleVersion(), oldReplicaAlloc, aliveBes)
+                     .status == Tablet.TabletStatus.HEALTHY;
+    }
+
+    private Set<Tag> getScaleInTags(ReplicaAllocation oldReplicaAlloc, 
ReplicaAllocation newReplicaAlloc) {
+        return oldReplicaAlloc.getAllocMap().entrySet().stream()
+            .filter(entry -> entry.getValue() > newReplicaAlloc.getAllocMap()
+                .getOrDefault(entry.getKey(), (short) 0))
+            .map(Map.Entry::getKey)
+            .collect(Collectors.toSet());
+    }
+
+    private List<Replica> getReplicasByTag(Tablet tablet, Tag tag) {
+        Map<Tag, List<Replica>> replicasByTag = tablet.getReplicas().stream()
+                .collect(Collectors.groupingBy(replica -> 
Env.getCurrentSystemInfo()
+                
.getBackend(replica.getBackendIdWithoutException()).getLocationTag()));
+        return replicasByTag.get(tag);
+    }
+
+    private void sortReplicasByBackendCount(List<Replica> replicas, Map<Long, 
Long> tableBeToReplicaNumMap) {
+        replicas.sort((Replica r1, Replica r2) -> {

Review Comment:
   sort need two factor:
   1. tablet num of this partition  on this backend;
   2. tablet num of this table on this backend;



-- 
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: commits-unsubscr...@doris.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to