Caideyipi commented on code in PR #18627:
URL: https://github.com/apache/iotdb/pull/18627#discussion_r4043338380


##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/thrift/impl/DataNodeInternalRPCServiceImpl.java:
##########
@@ -629,10 +629,30 @@ public TSchemaFetchResponse fetchSchema(final 
TSchemaFetchRequest req) {
 
   @Override
   public TLoadResp sendTsFilePieceNode(final TTsFilePieceReq req) {
-    LOGGER.info(DataNodeMiscMessages.RECEIVE_LOAD_NODE, req.uuid);
+    if (!req.isSetSliceIndex() || req.sliceIndex == 0) {
+      LOGGER.info(DataNodeMiscMessages.RECEIVE_LOAD_NODE, req.uuid);
+    }
 
     final ConsensusGroupId groupId =
         
ConsensusGroupId.Factory.createFromTConsensusGroupId(req.consensusGroupId);
+    final boolean isSliced =
+        req.isSetSliceIndex() || req.isSetSliceCount() || 
req.isSetOriginBodySize();
+    if (isSliced) {
+      if (!req.isSetSliceIndex() || !req.isSetSliceCount() || 
!req.isSetOriginBodySize()) {
+        return createTLoadResp(
+            new 
TSStatus(TSStatusCode.DESERIALIZE_PIECE_OF_TSFILE_ERROR.getStatusCode()));
+      }

Review Comment:
   Addressed in 
[fdf4ac6dd41](https://github.com/apache/iotdb/commit/fdf4ac6dd412b205de1c92e39f38d93adab06e91).
 The rejection now lists every missing field (`sliceIndex`, `sliceCount`, 
and/or `originBodySize`) in both the returned status and `TLoadResp.message`. 
Added coverage for all six partially populated combinations, with EN/ZH 
messages.



##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/scheduler/load/LoadTsFileDispatcherImpl.java:
##########
@@ -143,26 +146,83 @@ public Future<FragInstanceDispatchResult> dispatch(
 
   private void dispatchOneInstance(FragmentInstance instance)
       throws FragmentInstanceDispatchException {
-    TTsFilePieceReq loadTsFileReq = null;
+    List<TTsFilePieceReq> loadTsFileReqs = null;
 
     for (TDataNodeLocation dataNodeLocation :
         instance.getRegionReplicaSet().getDataNodeLocations()) {
       TEndPoint endPoint = dataNodeLocation.getInternalEndPoint();
       if (isDispatchedToLocal(endPoint)) {
         dispatchLocally(instance);
       } else {
-        if (loadTsFileReq == null) {
-          loadTsFileReq =
-              new TTsFilePieceReq(
+        if (loadTsFileReqs == null) {
+          loadTsFileReqs =
+              splitTsFilePieceReq(
                   
instance.getFragment().getPlanNodeTree().serializeToByteBuffer(),
                   uuid,
-                  instance.getRegionReplicaSet().getRegionId());
+                  instance.getRegionReplicaSet().getRegionId(),
+                  getLoadTsFilePieceBodySizeLimit());
         }
-        dispatchRemote(loadTsFileReq, endPoint);
+        dispatchRemote(loadTsFileReqs, endPoint);
       }
     }
   }
 
+  private static int getLoadTsFilePieceBodySizeLimit() {
+    final int thriftMaxFrameSize =
+        IoTDBDescriptor.getInstance().getConfig().getThriftMaxFrameSize();
+    return Math.max(1, thriftMaxFrameSize - 
LOAD_TSFILE_PIECE_RPC_FRAME_RESERVED_BYTES);
+  }

Review Comment:
   Addressed in 
[fdf4ac6dd41](https://github.com/apache/iotdb/commit/fdf4ac6dd412b205de1c92e39f38d93adab06e91).
 The sender now queries each receiver's `getThriftMaxFrameSize()` and slices 
using `min(senderLimit, receiverLimit) - 1024`, caching the receiver limit for 
this dispatcher.
   
   I checked the exception path: `TElasticFramedTransport.validateFrame()` 
closes the connection before the request reaches the RPC handler, so the 
receiver's limit is not sent back in the sender's exception. The small 
discovery RPC avoids relying on that unavailable message. An `UNKNOWN_METHOD` 
response from an older receiver falls back to the existing limit and borrows 
another client, preserving small unsliced requests.
   
   Added tests that send the generated frames through sender/receiver 
transports with different limits, cover replicas with both smaller and larger 
limits, and check discovery caching, legacy fallback, and transport failure.



##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/load/LoadTsFilePieceNodeAssembler.java:
##########
@@ -0,0 +1,120 @@
+/*
+ * 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.storageengine.load;
+
+import org.apache.tsfile.utils.PublicBAOS;
+
+import java.nio.ByteBuffer;
+
+public final class LoadTsFilePieceNodeAssembler {
+
+  private final int sliceCount;
+  private final int originBodySize;
+  private final PublicBAOS assembledBody = new PublicBAOS();
+
+  private int nextSliceIndex;
+
+  LoadTsFilePieceNodeAssembler(final int sliceCount, final int originBodySize) 
{
+    this.sliceCount = sliceCount;
+    this.originBodySize = originBodySize;
+  }
+
+  synchronized Result append(
+      final ByteBuffer sliceBody,
+      final int sliceIndex,
+      final int requestSliceCount,
+      final int requestOriginBodySize) {
+    if (sliceBody == null
+        || !sliceBody.hasRemaining()
+        || sliceCount <= 1
+        || originBodySize <= 0
+        || sliceCount != requestSliceCount
+        || originBodySize != requestOriginBodySize
+        || sliceIndex != nextSliceIndex
+        || sliceIndex < 0
+        || sliceIndex >= sliceCount
+        || assembledBody.size() > originBodySize - sliceBody.remaining()) {
+      return Result.invalid();
+    }

Review Comment:
   Addressed in 
[fdf4ac6dd41](https://github.com/apache/iotdb/commit/fdf4ac6dd412b205de1c92e39f38d93adab06e91).
 Each validation failure now returns a specific reason with the relevant 
actual/expected values: empty body, invalid or changed metadata, unexpected 
slice index, overflow, premature completion, and final-size mismatch. A missing 
assembler also reports the UUID, DataRegion, and slice index. `StorageEngine` 
propagates the reason into the RPC error status, so it reaches the sender. 
Added regression coverage and matching EN/ZH messages.



##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/load/LoadTsFilePieceNodeAssembler.java:
##########
@@ -0,0 +1,120 @@
+/*
+ * 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.storageengine.load;
+
+import org.apache.tsfile.utils.PublicBAOS;
+
+import java.nio.ByteBuffer;
+
+public final class LoadTsFilePieceNodeAssembler {
+
+  private final int sliceCount;
+  private final int originBodySize;
+  private final PublicBAOS assembledBody = new PublicBAOS();
+
+  private int nextSliceIndex;
+
+  LoadTsFilePieceNodeAssembler(final int sliceCount, final int originBodySize) 
{
+    this.sliceCount = sliceCount;
+    this.originBodySize = originBodySize;
+  }

Review Comment:
   Applied in 
[fdf4ac6dd41](https://github.com/apache/iotdb/commit/fdf4ac6dd412b205de1c92e39f38d93adab06e91):
 `assembledBody` is initialized with `originBodySize`, so valid requests need 
no buffer growth while being assembled. Nonpositive sizes still follow the 
validation/error response path (`Math.max(0, originBodySize)` avoids throwing 
from the buffer constructor for a negative value).



##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/load/LoadTsFilePieceNodeAssembler.java:
##########
@@ -0,0 +1,120 @@
+/*
+ * 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.storageengine.load;
+
+import org.apache.tsfile.utils.PublicBAOS;
+
+import java.nio.ByteBuffer;
+
+public final class LoadTsFilePieceNodeAssembler {
+
+  private final int sliceCount;
+  private final int originBodySize;
+  private final PublicBAOS assembledBody = new PublicBAOS();
+
+  private int nextSliceIndex;
+
+  LoadTsFilePieceNodeAssembler(final int sliceCount, final int originBodySize) 
{
+    this.sliceCount = sliceCount;
+    this.originBodySize = originBodySize;
+  }
+
+  synchronized Result append(
+      final ByteBuffer sliceBody,
+      final int sliceIndex,
+      final int requestSliceCount,
+      final int requestOriginBodySize) {
+    if (sliceBody == null
+        || !sliceBody.hasRemaining()
+        || sliceCount <= 1
+        || originBodySize <= 0
+        || sliceCount != requestSliceCount
+        || originBodySize != requestOriginBodySize
+        || sliceIndex != nextSliceIndex
+        || sliceIndex < 0
+        || sliceIndex >= sliceCount
+        || assembledBody.size() > originBodySize - sliceBody.remaining()) {
+      return Result.invalid();
+    }
+
+    final ByteBuffer duplicatedBody = sliceBody.duplicate();
+    if (duplicatedBody.hasArray()) {
+      assembledBody.write(
+          duplicatedBody.array(),
+          duplicatedBody.arrayOffset() + duplicatedBody.position(),
+          duplicatedBody.remaining());
+    } else {
+      final byte[] bytes = new byte[Math.min(duplicatedBody.remaining(), 
8192)];
+      while (duplicatedBody.hasRemaining()) {
+        final int size = Math.min(duplicatedBody.remaining(), bytes.length);
+        duplicatedBody.get(bytes, 0, size);
+        assembledBody.write(bytes, 0, size);
+      }

Review Comment:
   Checked both implementations on Windows 11 / JDK 17.0.15 with the PR's 
TsFile dependency. Both use a preallocated `PublicBAOS`; the bulk variant 
includes the temporary buffer allocation and 8 KiB copy/write loop. After 5 
warmup batches, these are medians of 9 alternating measurement batches 
(milliseconds per copy):
   
   | Input | Size | Copy + write | Byte by byte |
   | --- | ---: | ---: | ---: |
   | Read-only heap | 1 MiB | 0.054 | 1.175 |
   | Direct | 1 MiB | 0.061 | 1.174 |
   | Read-only heap | 16 MiB | 2.457 | 22.506 |
   | Direct | 16 MiB | 2.384 | 21.438 |
   
   The bulk path was also faster at 4 KiB. I kept it in 
[fdf4ac6dd41](https://github.com/apache/iotdb/commit/fdf4ac6dd412b205de1c92e39f38d93adab06e91)
 and added tests covering direct/read-only buffers spanning multiple copy 
batches, exact reassembly, and unchanged input positions. These are local 
microbenchmark results, not an end-to-end load throughput claim.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to