3838import org .apache .doris .catalog .Column ;
3939import org .apache .doris .catalog .Database ;
4040import org .apache .doris .catalog .DatabaseIf ;
41+ import org .apache .doris .catalog .DistributionInfo ;
4142import org .apache .doris .catalog .Env ;
4243import org .apache .doris .catalog .MaterializedIndex ;
4344import org .apache .doris .catalog .OlapTable ;
340341import java .util .concurrent .TimeUnit ;
341342import java .util .concurrent .TimeoutException ;
342343import java .util .concurrent .atomic .AtomicInteger ;
344+ import java .util .concurrent .atomic .AtomicLong ;
343345import java .util .concurrent .locks .ReentrantLock ;
344346import java .util .stream .Collectors ;
345347
@@ -3856,6 +3858,7 @@ public TCreatePartitionResult createPartition(TCreatePartitionRequest request) t
38563858 List <TTabletLocation > tablets = new ArrayList <>();
38573859 List <TTabletLocation > slaveTablets = new ArrayList <>();
38583860 List <TOlapTablePartition > partitions = Lists .newArrayList ();
3861+ boolean loadToSingleTablet = request .isSetLoadToSingleTablet () && request .isLoadToSingleTablet ();
38593862 for (String partitionName : addPartitionClauseMap .keySet ()) {
38603863 Partition partition = table .getPartition (partitionName );
38613864 // For thread safety, we preserve the tablet distribution information of each partition
@@ -3879,17 +3882,36 @@ public TCreatePartitionResult createPartition(TCreatePartitionRequest request) t
38793882 tPartition .setNumBuckets (index .getTablets ().size ());
38803883 }
38813884 tPartition .setIsMutable (olapTable .getPartitionInfo ().getIsMutable (partition .getId ()));
3885+ boolean randomDistribution =
3886+ partition .getDistributionInfo ().getType () == DistributionInfo .DistributionInfoType .RANDOM ;
3887+ boolean cacheLoadTabletIdx = loadToSingleTablet && randomDistribution ;
38823888 partitions .add (tPartition );
38833889 // tablet
3890+ AtomicLong cachedLoadTabletIdx = new AtomicLong (-1 );
38843891 if (needUseCache
38853892 && Env .getCurrentGlobalTransactionMgr ().getAutoPartitionCacheMgr ()
38863893 .getAutoPartitionInfo (txnId , partition .getId (), partitionTablets ,
3887- partitionSlaveTablets )) {
3894+ partitionSlaveTablets , cachedLoadTabletIdx )) {
3895+ if (cacheLoadTabletIdx ) {
3896+ tPartition .setLoadTabletIdx (cachedLoadTabletIdx .get ());
3897+ }
38883898 // fast path, if cached
38893899 tablets .addAll (partitionTablets );
38903900 slaveTablets .addAll (partitionSlaveTablets );
38913901 continue ;
38923902 }
3903+ if (cacheLoadTabletIdx ) {
3904+ try {
3905+ int tabletIndex = Env .getCurrentEnv ().getTabletLoadIndexRecorderMgr ()
3906+ .getCurrentTabletLoadIndex (dbId , olapTable .getId (), partition );
3907+ tPartition .setLoadTabletIdx (tabletIndex );
3908+ } catch (UserException ex ) {
3909+ errorStatus .setErrorMsgs (Lists .newArrayList (ex .getMessage ()));
3910+ result .setStatus (errorStatus );
3911+ LOG .warn ("send create partition error status: {}" , result );
3912+ return result ;
3913+ }
3914+ }
38933915 int quorum = olapTable .getPartitionInfo ().getReplicaAllocation (partition .getId ()).getTotalReplicaNum () / 2
38943916 + 1 ;
38953917 for (MaterializedIndex index : partition .getMaterializedIndices (MaterializedIndex .IndexExtState .ALL )) {
@@ -3953,9 +3975,13 @@ public TCreatePartitionResult createPartition(TCreatePartitionRequest request) t
39533975 }
39543976
39553977 if (needUseCache ) {
3956- Env .getCurrentGlobalTransactionMgr ().getAutoPartitionCacheMgr ()
3978+ long loadTabletIdx = cacheLoadTabletIdx ? tPartition .getLoadTabletIdx () : -1 ;
3979+ long cachedTabletIdx = Env .getCurrentGlobalTransactionMgr ().getAutoPartitionCacheMgr ()
39573980 .getOrSetAutoPartitionInfo (txnId , partition .getId (), partitionTablets ,
3958- partitionSlaveTablets );
3981+ partitionSlaveTablets , loadTabletIdx );
3982+ if (cacheLoadTabletIdx ) {
3983+ tPartition .setLoadTabletIdx (cachedTabletIdx );
3984+ }
39593985 }
39603986
39613987 tablets .addAll (partitionTablets );
@@ -4173,6 +4199,7 @@ public TReplacePartitionResult replacePartition(TReplacePartitionRequest request
41734199 List <TTabletLocation > tablets = new ArrayList <>();
41744200 List <TTabletLocation > slaveTablets = new ArrayList <>();
41754201 PartitionInfo partitionInfo = olapTable .getPartitionInfo ();
4202+ boolean loadToSingleTablet = request .isSetLoadToSingleTablet () && request .isLoadToSingleTablet ();
41764203 for (long partitionId : resultPartitionIds ) {
41774204 Partition partition = olapTable .getPartition (partitionId );
41784205 // For thread safety, we preserve the tablet distribution information of each partition
@@ -4198,17 +4225,36 @@ public TReplacePartitionResult replacePartition(TReplacePartitionRequest request
41984225 tPartition .setNumBuckets (index .getTablets ().size ());
41994226 }
42004227 tPartition .setIsMutable (olapTable .getPartitionInfo ().getIsMutable (partition .getId ()));
4228+ boolean randomDistribution =
4229+ partition .getDistributionInfo ().getType () == DistributionInfo .DistributionInfoType .RANDOM ;
4230+ boolean cacheLoadTabletIdx = loadToSingleTablet && randomDistribution ;
42014231 partitions .add (tPartition );
42024232 // tablet
4233+ AtomicLong cachedLoadTabletIdx = new AtomicLong (-1 );
42034234 if (needUseCache && txnId != 0
42044235 && Env .getCurrentGlobalTransactionMgr ().getAutoPartitionCacheMgr ()
42054236 .getAutoPartitionInfo (txnId , partition .getId (), partitionTablets ,
4206- partitionSlaveTablets )) {
4237+ partitionSlaveTablets , cachedLoadTabletIdx )) {
4238+ if (cacheLoadTabletIdx ) {
4239+ tPartition .setLoadTabletIdx (cachedLoadTabletIdx .get ());
4240+ }
42074241 // fast path, if cached
42084242 tablets .addAll (partitionTablets );
42094243 slaveTablets .addAll (partitionSlaveTablets );
42104244 continue ;
42114245 }
4246+ if (cacheLoadTabletIdx ) {
4247+ try {
4248+ int tabletIndex = Env .getCurrentEnv ().getTabletLoadIndexRecorderMgr ()
4249+ .getCurrentTabletLoadIndex (dbId , olapTable .getId (), partition );
4250+ tPartition .setLoadTabletIdx (tabletIndex );
4251+ } catch (UserException ex ) {
4252+ errorStatus .setErrorMsgs (Lists .newArrayList (ex .getMessage ()));
4253+ result .setStatus (errorStatus );
4254+ LOG .warn ("send replace partition error status: {}" , result );
4255+ return result ;
4256+ }
4257+ }
42124258 int quorum = olapTable .getPartitionInfo ().getReplicaAllocation (partition .getId ()).getTotalReplicaNum () / 2
42134259 + 1 ;
42144260 for (MaterializedIndex index : partition .getMaterializedIndices (MaterializedIndex .IndexExtState .ALL )) {
@@ -4276,9 +4322,13 @@ public TReplacePartitionResult replacePartition(TReplacePartitionRequest request
42764322 }
42774323
42784324 if (needUseCache ) {
4279- Env .getCurrentGlobalTransactionMgr ().getAutoPartitionCacheMgr ()
4325+ long loadTabletIdx = cacheLoadTabletIdx ? tPartition .getLoadTabletIdx () : -1 ;
4326+ long cachedTabletIdx = Env .getCurrentGlobalTransactionMgr ().getAutoPartitionCacheMgr ()
42804327 .getOrSetAutoPartitionInfo (txnId , partition .getId (), partitionTablets ,
4281- partitionSlaveTablets );
4328+ partitionSlaveTablets , loadTabletIdx );
4329+ if (cacheLoadTabletIdx ) {
4330+ tPartition .setLoadTabletIdx (cachedTabletIdx );
4331+ }
42824332 if (LOG .isDebugEnabled ()) {
42834333 LOG .debug ("Cache auto partition info, txnId: {}, partitionId: {}, "
42844334 + "tablets: {}, slaveTablets: {}" , txnId , partition .getId (),
0 commit comments