20
20
import org .apache .doris .catalog .Env ;
21
21
import org .apache .doris .catalog .Partition ;
22
22
import org .apache .doris .catalog .Replica ;
23
- import org .apache .doris .catalog .Replica .ReplicaContext ;
24
23
import org .apache .doris .cloud .system .CloudSystemInfoService ;
25
24
import org .apache .doris .common .Config ;
26
25
import org .apache .doris .common .DdlException ;
@@ -49,7 +48,7 @@ public class CloudReplica extends Replica {
49
48
50
49
// In the future, a replica may be mapped to multiple BEs in a cluster,
51
50
// so this value is be list
52
- private Map <String , List <Long >> clusterToBackends = new ConcurrentHashMap <String , List <Long >>();
51
+ private Map <String , List <Long >> primaryClusterToBackends = new ConcurrentHashMap <String , List <Long >>();
53
52
@ SerializedName (value = "dbId" )
54
53
private long dbId = -1 ;
55
54
@ SerializedName (value = "tableId" )
@@ -65,6 +64,9 @@ public class CloudReplica extends Replica {
65
64
66
65
private Map <String , List <Long >> memClusterToBackends = new ConcurrentHashMap <String , List <Long >>();
67
66
67
+ // clusterId, secondaryBe, changeTimestamp
68
+ private Map <String , List <Long >> secondaryClusterToBackends = new ConcurrentHashMap <String , List <Long >>();
69
+
68
70
public CloudReplica () {
69
71
}
70
72
@@ -186,8 +188,8 @@ private long getBackendIdImpl(String cluster) {
186
188
backendId = memClusterToBackends .get (clusterId ).get (indexRand );
187
189
}
188
190
189
- if (!replicaEnough && !allowColdRead && clusterToBackends .containsKey (clusterId )) {
190
- backendId = clusterToBackends .get (clusterId ).get (0 );
191
+ if (!replicaEnough && !allowColdRead && primaryClusterToBackends .containsKey (clusterId )) {
192
+ backendId = primaryClusterToBackends .get (clusterId ).get (0 );
191
193
}
192
194
193
195
if (backendId > 0 ) {
@@ -212,21 +214,47 @@ private long getBackendIdImpl(String cluster) {
212
214
}
213
215
}
214
216
215
- if (clusterToBackends .containsKey (clusterId )) {
216
- long backendId = clusterToBackends .get (clusterId ).get (0 );
217
- Backend be = Env .getCurrentSystemInfo ().getBackend (backendId );
218
- if (be != null && be .isQueryAvailable ()) {
219
- if (LOG .isDebugEnabled ()) {
220
- LOG .debug ("backendId={} " , backendId );
221
- }
222
- return backendId ;
217
+ // use primaryClusterToBackends, if find be normal
218
+ long pickBeId = getAvaliableBeId (clusterId , primaryClusterToBackends );
219
+ if (pickBeId != -1 ) {
220
+ return pickBeId ;
221
+ }
222
+
223
+ if (!Config .enable_immediate_be_assign ) {
224
+ // use secondaryClusterToBackends, if find be normal
225
+ pickBeId = getAvaliableBeId (clusterId , secondaryClusterToBackends );
226
+ if (pickBeId != -1 ) {
227
+ return pickBeId ;
223
228
}
224
229
}
225
- if (DebugPointUtil .isEnable ("CloudReplica.getBackendIdImpl.clusterToBackends" )) {
226
- LOG .info ("Debug Point enable CloudReplica.getBackendIdImpl.clusterToBackends" );
230
+
231
+ if (DebugPointUtil .isEnable ("CloudReplica.getBackendIdImpl.primaryClusterToBackends" )) {
232
+ LOG .info ("Debug Point enable CloudReplica.getBackendIdImpl.primaryClusterToBackends" );
233
+ return -1 ;
234
+ }
235
+
236
+ // be abnormal, rehash it. configure settings to different maps
237
+ pickBeId = hashReplicaToBe (clusterId , false );
238
+ updateClusterToBe (clusterId , pickBeId , Config .enable_immediate_be_assign );
239
+ return pickBeId ;
240
+ }
241
+
242
+ private long getAvaliableBeId (String clusterId , Map <String , List <Long >> clusterToBackends ) {
243
+ List <Long > backendIds = clusterToBackends .get (clusterId );
244
+ if (backendIds == null || backendIds .isEmpty ()) {
227
245
return -1 ;
228
246
}
229
- return hashReplicaToBe (clusterId , false );
247
+
248
+ long backendId = backendIds .get (0 );
249
+ Backend be = Env .getCurrentSystemInfo ().getBackend (backendId );
250
+ if (be != null && be .isQueryAvailable ()) {
251
+ // be normal
252
+ if (LOG .isDebugEnabled ()) {
253
+ LOG .debug ("backendId={} " , backendId );
254
+ }
255
+ return backendId ;
256
+ }
257
+ return -1 ;
230
258
}
231
259
232
260
public long hashReplicaToBe (String clusterId , boolean isBackGround ) {
@@ -270,14 +298,10 @@ public long hashReplicaToBe(String clusterId, boolean isBackGround) {
270
298
pickedBeId , getId (), partitionId , availableBes .size (), idx , index ,
271
299
hashCode == null ? -1 : hashCode .asLong ());
272
300
273
- // save to clusterToBackends map
274
- List <Long > bes = new ArrayList <Long >();
275
- bes .add (pickedBeId );
276
- clusterToBackends .put (clusterId , bes );
277
-
278
301
return pickedBeId ;
279
302
}
280
303
304
+
281
305
public List <Long > hashReplicaToBes (String clusterId , boolean isBackGround , int replicaNum ) {
282
306
// TODO(luwei) list should be sorted
283
307
List <Backend > clusterBes = ((CloudSystemInfoService ) Env .getCurrentSystemInfo ())
@@ -375,7 +399,7 @@ public void readFields(DataInput in) throws IOException {
375
399
long beId = in .readLong ();
376
400
List <Long > bes = new ArrayList <Long >();
377
401
bes .add (beId );
378
- clusterToBackends .put (clusterId , bes );
402
+ primaryClusterToBackends .put (clusterId , bes );
379
403
}
380
404
}
381
405
@@ -399,14 +423,19 @@ public long getIdx() {
399
423
return idx ;
400
424
}
401
425
402
- public Map <String , List <Long >> getClusterToBackends () {
403
- return clusterToBackends ;
426
+ public Map <String , List <Long >> getprimaryClusterToBackends () {
427
+ return primaryClusterToBackends ;
404
428
}
405
429
406
- public void updateClusterToBe (String cluster , long beId ) {
430
+ // save to primaryClusterToBackends or secondaryClusterToBackends map
431
+ public void updateClusterToBe (String cluster , long beId , boolean isUpdatePrimary ) {
407
432
// write lock
408
433
List <Long > bes = new ArrayList <Long >();
409
434
bes .add (beId );
410
- clusterToBackends .put (cluster , bes );
435
+ if (isUpdatePrimary ) {
436
+ primaryClusterToBackends .put (cluster , bes );
437
+ } else {
438
+ secondaryClusterToBackends .put (cluster , bes );
439
+ }
411
440
}
412
441
}
0 commit comments