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

JackieTien97 pushed a commit to branch rc/2.0.12
in repository https://gitbox.apache.org/repos/asf/iotdb.git


The following commit(s) were added to refs/heads/rc/2.0.12 by this push:
     new 7434765cb57 Fix table-model pipe batch leader redirects (#18690)
7434765cb57 is described below

commit 7434765cb571194cefae5c1dfba267646c26891c
Author: Caideyipi <[email protected]>
AuthorDate: Wed Nov 11 14:10:59 2026 +0800

    Fix table-model pipe batch leader redirects (#18690)
---
 .../IoTDBRemoveConfigNodeSchemaPartitionIT.java    |  31 ++++--
 .../protocol/thrift/IoTDBDataNodeReceiver.java     |  25 +++--
 .../db/pipe/sink/util/cacher/LeaderCacheUtils.java |  19 ++--
 .../protocol/thrift/IoTDBDataNodeReceiverTest.java |  70 ++++++++++++++
 .../sink/util/cacher/LeaderCacheUtilsTest.java     | 104 +++++++++++++++++++++
 5 files changed, 224 insertions(+), 25 deletions(-)

diff --git 
a/integration-test/src/test/java/org/apache/iotdb/confignode/it/removeconfignode/IoTDBRemoveConfigNodeSchemaPartitionIT.java
 
b/integration-test/src/test/java/org/apache/iotdb/confignode/it/removeconfignode/IoTDBRemoveConfigNodeSchemaPartitionIT.java
index 8e2c6b957cb..73686358a3f 100644
--- 
a/integration-test/src/test/java/org/apache/iotdb/confignode/it/removeconfignode/IoTDBRemoveConfigNodeSchemaPartitionIT.java
+++ 
b/integration-test/src/test/java/org/apache/iotdb/confignode/it/removeconfignode/IoTDBRemoveConfigNodeSchemaPartitionIT.java
@@ -35,6 +35,7 @@ import org.apache.iotdb.it.framework.IoTDBTestRunner;
 import org.apache.iotdb.itbase.category.TableClusterIT;
 import org.apache.iotdb.rpc.TSStatusCode;
 
+import org.apache.thrift.TException;
 import org.apache.thrift.transport.TTransport;
 import org.apache.tsfile.enums.ColumnCategory;
 import org.apache.tsfile.enums.TSDataType;
@@ -125,16 +126,7 @@ public class IoTDBRemoveConfigNodeSchemaPartitionIT {
           .filter(endpoint -> !endpoint.equals(removedEndpoint))
           .forEach(staleEndpoints::add);
       // Use the actual DN retry client against real CN processes. This client 
is not pooled.
-      try (ConfigNodeClient retryClient =
-          new ConfigNodeClient(
-              staleEndpoints,
-              new 
ThriftClientProperty.Builder().setConnectionTimeoutMs(30_000).build(),
-              null) {
-            @Override
-            public void close() {
-              invalidate();
-            }
-          }) {
+      try (TestConfigNodeClient retryClient = new 
TestConfigNodeClient(staleEndpoints)) {
         assertEquals(removedEndpoint, retryClient.getConfigNode());
         final TTransport oldTransport = retryClient.getTransport();
         oldConnection.setTimeout(30_000);
@@ -259,4 +251,23 @@ public class IoTDBRemoveConfigNodeSchemaPartitionIT {
       assertFalse(result.hasNext());
     }
   }
+
+  private static class TestConfigNodeClient extends ConfigNodeClient {
+
+    private TestConfigNodeClient(List<TEndPoint> configNodes) throws 
TException {
+      super(
+          configNodes,
+          new 
ThriftClientProperty.Builder().setConnectionTimeoutMs(30_000).build(),
+          null);
+    }
+
+    private TEndPoint getConfigNode() {
+      return configNode;
+    }
+
+    @Override
+    public void close() {
+      invalidate();
+    }
+  }
 }
diff --git 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/receiver/protocol/thrift/IoTDBDataNodeReceiver.java
 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/receiver/protocol/thrift/IoTDBDataNodeReceiver.java
index e7d4053ea64..c6b95699e22 100644
--- 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/receiver/protocol/thrift/IoTDBDataNodeReceiver.java
+++ 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/receiver/protocol/thrift/IoTDBDataNodeReceiver.java
@@ -1129,18 +1129,27 @@ public class IoTDBDataNodeReceiver extends 
IoTDBFileReceiver {
   }
 
   /**
-   * For {@link InsertRowsStatement} and {@link InsertMultiTabletsStatement}, 
the returned {@link
-   * TSStatus} will use sub-status to record the endpoint for redirection. 
Each sub-status records
-   * the redirection endpoint for one device path, and the order is the same 
as the order of the
-   * device paths in the statement. However, this order is not guaranteed to 
be the same as in the
-   * request. So for each sub-status which needs to redirect, we record the 
device path using the
-   * message field.
+   * For tree-model {@link InsertRowsStatement} and {@link 
InsertMultiTabletsStatement}, the
+   * returned {@link TSStatus} uses sub-statuses to record redirection 
endpoints. Their order is the
+   * same as the device paths in the statement, but not necessarily the 
request, so attach the
+   * device path to each redirected sub-status.
    */
   private TSStatus executeBatchStatementAndAddRedirectInfo(final 
InsertBaseStatement statement) {
     final TSStatus result = executeStatementAndClassifyExceptions(statement, 
5);
+    return addRedirectInfoForBatch(statement, result, receiverId.get());
+  }
 
+  static TSStatus addRedirectInfoForBatch(
+      final InsertBaseStatement statement, final TSStatus result, final long 
receiverId) {
     if (result.getCode() == TSStatusCode.REDIRECTION_RECOMMEND.getStatusCode()
         && result.getSubStatusSize() > 0) {
+      // A table-model batch may contain rows for multiple devices. The pipe 
sink currently routes
+      // the entire event by its device ID, so caching a row's endpoint for 
the whole tablet/table
+      // could misroute later writes. Keep the successful write status without 
cache hints.
+      if (statement.isWriteToTable()) {
+        return result;
+      }
+
       final List<PartialPath> devicePaths;
       if (statement instanceof InsertRowsStatement) {
         devicePaths = ((InsertRowsStatement) statement).getDevicePaths();
@@ -1149,7 +1158,7 @@ public class IoTDBDataNodeReceiver extends 
IoTDBFileReceiver {
       } else {
         LOGGER.warn(
             
DataNodePipeMessages.RECEIVER_ID_UNSUPPORTED_STATEMENT_TYPE_FOR_REDIRECTION,
-            receiverId.get(),
+            receiverId,
             statement);
         return result;
       }
@@ -1163,7 +1172,7 @@ public class IoTDBDataNodeReceiver extends 
IoTDBFileReceiver {
       } else {
         LOGGER.warn(
             DataNodePipeMessages.RECEIVER_ID_THE_NUMBER_OF_DEVICE_PATHS,
-            receiverId.get(),
+            receiverId,
             statement,
             result);
       }
diff --git 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/sink/util/cacher/LeaderCacheUtils.java
 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/sink/util/cacher/LeaderCacheUtils.java
index 754b406dcd7..bb0357d3b90 100644
--- 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/sink/util/cacher/LeaderCacheUtils.java
+++ 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/sink/util/cacher/LeaderCacheUtils.java
@@ -41,23 +41,28 @@ public class LeaderCacheUtils {
    * @return a list of pairs, each pair contains a device path and its 
redirect endpoint.
    */
   public static List<Pair<String, TEndPoint>> 
parseRecommendedRedirections(TSStatus status) {
-    // If there is no exception, there should be 2 sub-statuses, one for 
InsertRowsStatement and one
-    // for InsertMultiTabletsStatement (see 
IoTDBDataNodeReceiver#handleTransferTabletBatch).
+    // Each top-level sub-status corresponds to one statement constructed by 
the receiver. V2 batch
+    // requests may contain any number of statements because rows are grouped 
by database and table.
     final List<Pair<String, TEndPoint>> redirectList = new ArrayList<>();
 
-    if (status.getSubStatusSize() != 2) {
+    if (status == null || !status.isSetSubStatus()) {
       return redirectList;
     }
 
     for (final TSStatus subStatus : status.getSubStatus()) {
-      if (subStatus.getCode() != 
TSStatusCode.REDIRECTION_RECOMMEND.getStatusCode()) {
+      if (subStatus == null
+          || subStatus.getCode() != 
TSStatusCode.REDIRECTION_RECOMMEND.getStatusCode()
+          || !subStatus.isSetSubStatus()) {
         continue;
       }
 
       for (final TSStatus innerSubStatus : subStatus.getSubStatus()) {
-        if (innerSubStatus.isSetRedirectNode()) {
-          // We assume that innerSubStatus.getMessage() is a device path.
-          // The message field should be a device path.
+        if (innerSubStatus != null
+            && innerSubStatus.isSetRedirectNode()
+            && innerSubStatus.isSetMessage()
+            && !innerSubStatus.getMessage().isEmpty()) {
+          // The receiver sets the message to a device path only when it can 
safely associate the
+          // redirection with a single tree-model device.
           redirectList.add(
               new Pair<>(innerSubStatus.getMessage(), 
innerSubStatus.getRedirectNode()));
         }
diff --git 
a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/pipe/receiver/protocol/thrift/IoTDBDataNodeReceiverTest.java
 
b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/pipe/receiver/protocol/thrift/IoTDBDataNodeReceiverTest.java
index 66c50b37939..752370c8c80 100644
--- 
a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/pipe/receiver/protocol/thrift/IoTDBDataNodeReceiverTest.java
+++ 
b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/pipe/receiver/protocol/thrift/IoTDBDataNodeReceiverTest.java
@@ -19,7 +19,9 @@
 
 package org.apache.iotdb.db.pipe.receiver.protocol.thrift;
 
+import org.apache.iotdb.common.rpc.thrift.TEndPoint;
 import org.apache.iotdb.common.rpc.thrift.TSStatus;
+import org.apache.iotdb.commons.path.PartialPath;
 import org.apache.iotdb.db.conf.IoTDBDescriptor;
 import 
org.apache.iotdb.db.pipe.sink.payload.evolvable.request.PipeTransferTsFileSealWithModReq;
 import 
org.apache.iotdb.db.queryengine.plan.statement.crud.InsertMultiTabletsStatement;
@@ -43,6 +45,74 @@ import java.util.Map;
 
 public class IoTDBDataNodeReceiverTest {
 
+  @Test
+  public void testTableModelTabletRedirectDoesNotCachePerRowLeader() {
+    final InsertTabletStatement statement = new InsertTabletStatement();
+    statement.setWriteToTable(true);
+    statement.setRowCount(2);
+
+    final TSStatus firstRow =
+        new TSStatus(TSStatusCode.SUCCESS_STATUS.getStatusCode())
+            .setRedirectNode(new TEndPoint("127.0.0.2", 6667));
+    final TSStatus secondRow =
+        new TSStatus(TSStatusCode.SUCCESS_STATUS.getStatusCode())
+            .setRedirectNode(new TEndPoint("127.0.0.3", 6667));
+    final TSStatus result =
+        new TSStatus(TSStatusCode.REDIRECTION_RECOMMEND.getStatusCode())
+            .setSubStatus(Arrays.asList(firstRow, secondRow));
+
+    Assert.assertSame(result, 
IoTDBDataNodeReceiver.addRedirectInfoForBatch(statement, result, 1));
+    Assert.assertFalse(firstRow.isSetMessage());
+    Assert.assertFalse(secondRow.isSetMessage());
+  }
+
+  @Test
+  public void testTableModelRowsRedirectDoesNotCachePerRowLeader() throws 
Exception {
+    final InsertRowStatement firstRow = new InsertRowStatement();
+    firstRow.setDevicePath(new PartialPath("table1"));
+    final InsertRowStatement secondRow = new InsertRowStatement();
+    secondRow.setDevicePath(new PartialPath("table1"));
+    final InsertRowsStatement statement = new InsertRowsStatement();
+    statement.setWriteToTable(true);
+    statement.setInsertRowStatementList(Arrays.asList(firstRow, secondRow));
+
+    final TSStatus firstRowStatus =
+        new TSStatus(TSStatusCode.SUCCESS_STATUS.getStatusCode())
+            .setRedirectNode(new TEndPoint("127.0.0.2", 6667));
+    final TSStatus secondRowStatus =
+        new TSStatus(TSStatusCode.SUCCESS_STATUS.getStatusCode())
+            .setRedirectNode(new TEndPoint("127.0.0.3", 6667));
+    final TSStatus result =
+        new TSStatus(TSStatusCode.REDIRECTION_RECOMMEND.getStatusCode())
+            .setSubStatus(Arrays.asList(firstRowStatus, secondRowStatus));
+
+    Assert.assertSame(result, 
IoTDBDataNodeReceiver.addRedirectInfoForBatch(statement, result, 1));
+    Assert.assertFalse(firstRowStatus.isSetMessage());
+    Assert.assertFalse(secondRowStatus.isSetMessage());
+  }
+
+  @Test
+  public void testTreeModelBatchRedirectHasDevicePath() throws Exception {
+    final InsertRowStatement firstRow = new InsertRowStatement();
+    firstRow.setDevicePath(new PartialPath("root.sg.d1"));
+    final InsertRowStatement secondRow = new InsertRowStatement();
+    secondRow.setDevicePath(new PartialPath("root.sg.d2"));
+    final InsertRowsStatement statement = new InsertRowsStatement();
+    statement.setInsertRowStatementList(Arrays.asList(firstRow, secondRow));
+
+    final TSStatus local = new 
TSStatus(TSStatusCode.SUCCESS_STATUS.getStatusCode());
+    final TSStatus redirected =
+        new TSStatus(TSStatusCode.SUCCESS_STATUS.getStatusCode())
+            .setRedirectNode(new TEndPoint("127.0.0.2", 6667));
+    final TSStatus result =
+        new TSStatus(TSStatusCode.REDIRECTION_RECOMMEND.getStatusCode())
+            .setSubStatus(Arrays.asList(local, redirected));
+
+    Assert.assertSame(result, 
IoTDBDataNodeReceiver.addRedirectInfoForBatch(statement, result, 1));
+    Assert.assertFalse(local.isSetMessage());
+    Assert.assertEquals("root.sg.d2", redirected.getMessage());
+  }
+
   @Test
   public void 
testLoadTsFileSyncStatementUsesTreeDatabaseLevelFromDatabaseName() throws 
Exception {
     final Path tsFile = Files.createTempFile("pipe-load-tree-database-level", 
".tsfile");
diff --git 
a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/pipe/sink/util/cacher/LeaderCacheUtilsTest.java
 
b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/pipe/sink/util/cacher/LeaderCacheUtilsTest.java
new file mode 100644
index 00000000000..62d5b57cedf
--- /dev/null
+++ 
b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/pipe/sink/util/cacher/LeaderCacheUtilsTest.java
@@ -0,0 +1,104 @@
+/*
+ * 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.db.pipe.sink.util.cacher;
+
+import org.apache.iotdb.common.rpc.thrift.TEndPoint;
+import org.apache.iotdb.common.rpc.thrift.TSStatus;
+import org.apache.iotdb.rpc.RpcUtils;
+import org.apache.iotdb.rpc.TSStatusCode;
+
+import org.apache.tsfile.utils.Pair;
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+
+public class LeaderCacheUtilsTest {
+
+  @Test
+  public void testParseRecommendedRedirectionsFromVariableStatementCount() {
+    final TEndPoint redirectEndPoint = new TEndPoint("127.0.0.2", 6667);
+    final TSStatus redirectedRowStatus =
+        RpcUtils.getStatus(TSStatusCode.SUCCESS_STATUS)
+            .setMessage("table1.device1")
+            .setRedirectNode(redirectEndPoint);
+    final TSStatus redirectedStatementStatus =
+        RpcUtils.getStatus(TSStatusCode.REDIRECTION_RECOMMEND)
+            .setSubStatus(Collections.singletonList(redirectedRowStatus));
+    final TSStatus batchStatus =
+        RpcUtils.getStatus(TSStatusCode.REDIRECTION_RECOMMEND)
+            .setSubStatus(
+                Arrays.asList(
+                    RpcUtils.getStatus(TSStatusCode.SUCCESS_STATUS),
+                    redirectedStatementStatus,
+                    RpcUtils.getStatus(TSStatusCode.SUCCESS_STATUS)));
+
+    final List<Pair<String, TEndPoint>> redirects =
+        LeaderCacheUtils.parseRecommendedRedirections(batchStatus);
+
+    Assert.assertEquals(1, redirects.size());
+    Assert.assertEquals("table1.device1", redirects.get(0).getLeft());
+    Assert.assertEquals(redirectEndPoint, redirects.get(0).getRight());
+  }
+
+  @Test
+  public void testIgnoreRedirectsWithoutDevicePath() {
+    final TEndPoint redirectEndPoint = new TEndPoint("127.0.0.2", 6667);
+    final TSStatus tableRowWithoutPath =
+        
RpcUtils.getStatus(TSStatusCode.SUCCESS_STATUS).setRedirectNode(redirectEndPoint);
+    final TSStatus rowWithEmptyPath =
+        RpcUtils.getStatus(TSStatusCode.SUCCESS_STATUS)
+            .setMessage("")
+            .setRedirectNode(redirectEndPoint);
+    final TSStatus treeRowWithPath =
+        RpcUtils.getStatus(TSStatusCode.SUCCESS_STATUS)
+            .setMessage("root.sg.d1")
+            .setRedirectNode(redirectEndPoint);
+    final TSStatus batchStatus =
+        RpcUtils.getStatus(TSStatusCode.REDIRECTION_RECOMMEND)
+            .setSubStatus(
+                Arrays.asList(
+                    RpcUtils.getStatus(TSStatusCode.REDIRECTION_RECOMMEND)
+                        .setSubStatus(Arrays.asList(tableRowWithoutPath, 
rowWithEmptyPath)),
+                    RpcUtils.getStatus(TSStatusCode.REDIRECTION_RECOMMEND)
+                        
.setSubStatus(Collections.singletonList(treeRowWithPath))));
+
+    final List<Pair<String, TEndPoint>> redirects =
+        LeaderCacheUtils.parseRecommendedRedirections(batchStatus);
+
+    Assert.assertEquals(1, redirects.size());
+    Assert.assertEquals("root.sg.d1", redirects.get(0).getLeft());
+    Assert.assertEquals(redirectEndPoint, redirects.get(0).getRight());
+  }
+
+  @Test
+  public void testIgnoreMalformedRedirectStatus() {
+    final TSStatus redirectWithoutSubStatus =
+        RpcUtils.getStatus(TSStatusCode.REDIRECTION_RECOMMEND);
+    final TSStatus batchStatus =
+        RpcUtils.getStatus(TSStatusCode.REDIRECTION_RECOMMEND)
+            .setSubStatus(Arrays.asList(null, redirectWithoutSubStatus));
+
+    
Assert.assertTrue(LeaderCacheUtils.parseRecommendedRedirections(batchStatus).isEmpty());
+    
Assert.assertTrue(LeaderCacheUtils.parseRecommendedRedirections(null).isEmpty());
+  }
+}

Reply via email to