This is an automated email from the ASF dual-hosted git repository.

Caideyipi pushed a commit to branch dev/1.3
in repository https://gitbox.apache.org/repos/asf/iotdb.git


The following commit(s) were added to refs/heads/dev/1.3 by this push:
     new 3e0ec1c375d Fall back to a current replica when the cached region 
leader is stale (#19135) (#19146)
3e0ec1c375d is described below

commit 3e0ec1c375daeec1c0193dda97dc2ed658501162
Author: Caideyipi <[email protected]>
AuthorDate: Fri Nov 6 10:03:43 2026 +0800

    Fall back to a current replica when the cached region leader is stale 
(#19135) (#19146)
    
    (cherry picked from commit 6a3e9d214a74e6b57bc8927953c00a3ce9b4dcae)
---
 .../impl/schema/DataNodeRegionGroupUtil.java       |  7 ++-
 .../impl/schema/DataNodeRegionGroupUtilTest.java   | 54 ++++++++++++++++++++++
 2 files changed, 57 insertions(+), 4 deletions(-)

diff --git 
a/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/procedure/impl/schema/DataNodeRegionGroupUtil.java
 
b/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/procedure/impl/schema/DataNodeRegionGroupUtil.java
index a741d536ff2..8369483cc51 100644
--- 
a/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/procedure/impl/schema/DataNodeRegionGroupUtil.java
+++ 
b/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/procedure/impl/schema/DataNodeRegionGroupUtil.java
@@ -43,10 +43,9 @@ class DataNodeRegionGroupUtil {
     regionReplicaSetMap.forEach(
         (consensusGroupId, regionReplicaSet) -> {
           Integer leaderId = leaderMap.get(consensusGroupId);
-          TDataNodeLocation leaderDataNodeLocation = null;
-          if (leaderId == null || leaderId == -1) {
-            leaderDataNodeLocation = 
regionReplicaSet.getDataNodeLocations().get(0);
-          } else {
+          TDataNodeLocation leaderDataNodeLocation = 
regionReplicaSet.getDataNodeLocations().get(0);
+          // The leader cache can still reference a removed replica during 
region migration.
+          if (leaderId != null && leaderId != -1) {
             for (TDataNodeLocation dataNodeLocation : 
regionReplicaSet.getDataNodeLocations()) {
               if (dataNodeLocation.getDataNodeId() == leaderId) {
                 leaderDataNodeLocation = dataNodeLocation;
diff --git 
a/iotdb-core/confignode/src/test/java/org/apache/iotdb/confignode/procedure/impl/schema/DataNodeRegionGroupUtilTest.java
 
b/iotdb-core/confignode/src/test/java/org/apache/iotdb/confignode/procedure/impl/schema/DataNodeRegionGroupUtilTest.java
new file mode 100644
index 00000000000..547544f41d2
--- /dev/null
+++ 
b/iotdb-core/confignode/src/test/java/org/apache/iotdb/confignode/procedure/impl/schema/DataNodeRegionGroupUtilTest.java
@@ -0,0 +1,54 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iotdb.confignode.procedure.impl.schema;
+
+import org.apache.iotdb.common.rpc.thrift.TConsensusGroupId;
+import org.apache.iotdb.common.rpc.thrift.TConsensusGroupType;
+import org.apache.iotdb.common.rpc.thrift.TDataNodeLocation;
+import org.apache.iotdb.common.rpc.thrift.TRegionReplicaSet;
+
+import org.junit.Test;
+
+import java.util.Arrays;
+import java.util.Collections;
+
+import static org.junit.Assert.assertEquals;
+
+public class DataNodeRegionGroupUtilTest {
+  @Test
+  public void testLeaderSelectionAfterReplicaMigration() {
+    final TConsensusGroupId region = new 
TConsensusGroupId(TConsensusGroupType.SchemaRegion, 1);
+    final TDataNodeLocation firstReplica = new 
TDataNodeLocation().setDataNodeId(1);
+    final TDataNodeLocation secondReplica = new 
TDataNodeLocation().setDataNodeId(2);
+    final TRegionReplicaSet replicas =
+        new TRegionReplicaSet(region, Arrays.asList(firstReplica, 
secondReplica));
+
+    // A stale leader outside the replica set must fall back to a current 
replica.
+    for (final Integer leaderId : Arrays.asList(null, -1, 0, 1, 2)) {
+      assertEquals(
+          Collections.singletonMap(
+              Integer.valueOf(2).equals(leaderId) ? secondReplica : 
firstReplica,
+              Collections.singletonList(region)),
+          DataNodeRegionGroupUtil.getLeaderDataNodeRegionGroupMap(
+              Collections.singletonMap(region, leaderId),
+              Collections.singletonMap(region, replicas)));
+    }
+  }
+}

Reply via email to