1717
1818package org .apache .doris .catalog ;
1919
20+ import com .google .common .collect .HashMultimap ;
21+ import com .google .common .collect .Lists ;
22+ import com .google .common .collect .Multimap ;
23+ import com .google .common .collect .Sets ;
24+ import com .google .gson .annotations .SerializedName ;
2025import org .apache .doris .catalog .Replica .ReplicaState ;
2126import org .apache .doris .clone .TabletSchedCtx ;
2227import org .apache .doris .clone .TabletSchedCtx .Priority ;
2530import org .apache .doris .common .io .Writable ;
2631import org .apache .doris .system .Backend ;
2732import org .apache .doris .system .SystemInfoService ;
28-
29- import com .google .common .collect .HashMultimap ;
30- import com .google .common .collect .Lists ;
31- import com .google .common .collect .Multimap ;
32- import com .google .common .collect .Sets ;
33- import com .google .gson .annotations .SerializedName ;
34-
3533import org .apache .logging .log4j .LogManager ;
3634import org .apache .logging .log4j .Logger ;
3735
4240import java .util .Iterator ;
4341import java .util .List ;
4442import java .util .Set ;
43+ import java .util .stream .Collectors ;
4544
4645/**
4746 * This class represents the olap tablet related metadata.
@@ -407,7 +406,7 @@ public long getDataSize(boolean singleReplica) {
407406 public Pair <TabletStatus , TabletSchedCtx .Priority > getHealthStatusWithPriority (
408407 SystemInfoService systemInfoService , String clusterName ,
409408 long visibleVersion , long visibleVersionHash , int replicationNum ,
410- int availableBackendsNum ) {
409+ List < Long > aliveBeIdsInCluster ) {
411410
412411 int alive = 0 ;
413412 int aliveAndVersionComplete = 0 ;
@@ -453,15 +452,16 @@ public Pair<TabletStatus, TabletSchedCtx.Priority> getHealthStatusWithPriority(
453452 }
454453
455454 // 1. alive replicas are not enough
456- if (alive < replicationNum && replicas .size () >= availableBackendsNum
457- && availableBackendsNum >= replicationNum && replicationNum > 1 ) {
455+ int aliveBackendsNum = aliveBeIdsInCluster .size ();
456+ if (alive < replicationNum && replicas .size () >= aliveBackendsNum
457+ && aliveBackendsNum >= replicationNum && replicationNum > 1 ) {
458458 // there is no enough backend for us to create a new replica, so we have to delete an existing replica,
459459 // so there can be available backend for us to create a new replica.
460460 // And if there is only one replica, we will not handle it(maybe need human interference)
461461 // condition explain:
462462 // 1. alive < replicationNum: replica is missing or bad
463- // 2. replicas.size() >= availableBackendsNum : the existing replicas occupies all available backends
464- // 3. availableBackendsNum >= replicationNum: make sure after deleting, there will be at least one backend for new replica.
463+ // 2. replicas.size() >= aliveBackendsNum : the existing replicas occupies all available backends
464+ // 3. aliveBackendsNum >= replicationNum: make sure after deleting, there will be at least one backend for new replica.
465465 // 4. replicationNum > 1: if replication num is set to 1, do not delete any replica, for safety reason
466466 return Pair .create (TabletStatus .FORCE_REDUNDANT , TabletSchedCtx .Priority .VERY_HIGH );
467467 } else if (alive < (replicationNum / 2 ) + 1 ) {
@@ -484,10 +484,23 @@ public Pair<TabletStatus, TabletSchedCtx.Priority> getHealthStatusWithPriority(
484484 }
485485
486486 // 3. replica is under relocating
487- if (stable < (replicationNum / 2 ) + 1 ) {
488- return Pair .create (TabletStatus .REPLICA_RELOCATING , TabletSchedCtx .Priority .NORMAL );
489- } else if (stable < replicationNum ) {
490- return Pair .create (TabletStatus .REPLICA_RELOCATING , TabletSchedCtx .Priority .LOW );
487+ if (stable < replicationNum ) {
488+ List <Long > replicaBeIds = replicas .stream ()
489+ .map (Replica ::getBackendId ).collect (Collectors .toList ());
490+ List <Long > availableBeIds = aliveBeIdsInCluster .stream ()
491+ .filter (systemInfoService ::checkBackendAvailable )
492+ .collect (Collectors .toList ());
493+ if (replicaBeIds .containsAll (availableBeIds )
494+ && availableBeIds .size () >= replicationNum
495+ && replicationNum > 1 ) { // No BE can be choose to create a new replica
496+ return Pair .create (TabletStatus .FORCE_REDUNDANT ,
497+ stable < (replicationNum / 2 ) + 1 ? TabletSchedCtx .Priority .NORMAL : TabletSchedCtx .Priority .LOW );
498+ }
499+ if (stable < (replicationNum / 2 ) + 1 ) {
500+ return Pair .create (TabletStatus .REPLICA_RELOCATING , TabletSchedCtx .Priority .NORMAL );
501+ } else if (stable < replicationNum ) {
502+ return Pair .create (TabletStatus .REPLICA_RELOCATING , TabletSchedCtx .Priority .LOW );
503+ }
491504 }
492505
493506 // 4. healthy replicas in cluster are not enough
0 commit comments