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


##########
iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/pipe/sink/protocol/IoTDBAirGapSink.java:
##########
@@ -334,6 +478,141 @@ protected boolean sendBytes(final AirGapSocket socket, 
byte[] bytes) throws IOEx
     return Arrays.equals(AirGapOneByteResponse.OK, response);
   }
 
+  private boolean sendBytesByUdp(final AirGapSocket socket, final byte[] 
bytes) throws IOException {
+    for (final byte[] requestBytes : sliceIfNeededForUdp(bytes)) {
+      if (!sendOneDatagram(socket, requestBytes)) {
+        return false;
+      }
+    }
+    return true;
+  }
+
+  private boolean sendOneDatagram(final AirGapSocket socket, final byte[] 
requestBytes)
+      throws IOException {
+    final long requestId = udpRequestIdGenerator.incrementAndGet();
+    final byte[] framedRequestBytes =
+        eLanguageEnable
+            ? enrichWithELanguage(enrichWithLengthAndChecksum(requestBytes))
+            : enrichWithLengthAndChecksum(requestBytes);
+    final byte[] datagramBytes =
+        ByteBuffer.allocate(UDP_REQUEST_ID_SIZE + framedRequestBytes.length)
+            .putLong(requestId)
+            .put(framedRequestBytes)
+            .array();
+    if (datagramBytes.length > udpPacketSizeInBytes) {
+      throw new IOException(
+          String.format(
+              PipeMessages
+                  
.EXCEPTION_AIR_GAP_UDP_DATAGRAM_SIZE_ARG_EXCEEDS_CONFIGURED_PACKET_SIZE_ARG_559D0654,
+              datagramBytes.length,
+              udpPacketSizeInBytes));
+    }
+
+    final DatagramSocket datagramSocket = socket.getDatagramSocket();
+    datagramSocket.send(
+        new DatagramPacket(
+            datagramBytes,
+            datagramBytes.length,
+            socket.getDatagramAddress(),
+            socket.getEndPoint().getPort()));
+
+    final int originalTimeoutMs = datagramSocket.getSoTimeout();
+    final long deadlineNanos =
+        originalTimeoutMs == 0
+            ? Long.MAX_VALUE
+            : System.nanoTime() + 
TimeUnit.MILLISECONDS.toNanos(originalTimeoutMs);
+    final byte[] response = new byte[UDP_REQUEST_ID_SIZE + 1];
+    final DatagramPacket responsePacket = new DatagramPacket(response, 
response.length);
+    try {
+      while (true) {
+        if (deadlineNanos != Long.MAX_VALUE) {
+          final long remainingNanos = deadlineNanos - System.nanoTime();
+          if (remainingNanos <= 0) {
+            throw new SocketTimeoutException(
+                
PipeMessages.EXCEPTION_TIMED_OUT_WAITING_FOR_THE_MATCHING_AIR_GAP_ACK_DD2F3706);
+          }
+          datagramSocket.setSoTimeout(
+              (int) Math.max(1, 
TimeUnit.NANOSECONDS.toMillis(remainingNanos)));
+        }
+
+        responsePacket.setLength(response.length);
+        datagramSocket.receive(responsePacket);
+        if (responsePacket.getLength() != UDP_REQUEST_ID_SIZE + 1) {

Review Comment:
   UDP ACKs are not sliced. The receiver accepts only the exact 
request-ID-plus-one-byte-status length. Reading one extra byte ensures an 
oversized datagram cannot be silently truncated into a valid-looking ACK; it is 
discarded and the sender times out.



##########
iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/pipe/sink/protocol/IoTDBAirGapSink.java:
##########
@@ -334,6 +478,141 @@ protected boolean sendBytes(final AirGapSocket socket, 
byte[] bytes) throws IOEx
     return Arrays.equals(AirGapOneByteResponse.OK, response);
   }
 
+  private boolean sendBytesByUdp(final AirGapSocket socket, final byte[] 
bytes) throws IOException {
+    for (final byte[] requestBytes : sliceIfNeededForUdp(bytes)) {
+      if (!sendOneDatagram(socket, requestBytes)) {
+        return false;
+      }
+    }
+    return true;
+  }
+
+  private boolean sendOneDatagram(final AirGapSocket socket, final byte[] 
requestBytes)
+      throws IOException {
+    final long requestId = udpRequestIdGenerator.incrementAndGet();
+    final byte[] framedRequestBytes =
+        eLanguageEnable
+            ? enrichWithELanguage(enrichWithLengthAndChecksum(requestBytes))
+            : enrichWithLengthAndChecksum(requestBytes);
+    final byte[] datagramBytes =
+        ByteBuffer.allocate(UDP_REQUEST_ID_SIZE + framedRequestBytes.length)
+            .putLong(requestId)
+            .put(framedRequestBytes)
+            .array();

Review Comment:
   Yes. UDP framing now writes the request ID, envelope, request header, and 
body into one exact-sized datagram buffer. This removes the intermediate framed 
byte array and the subsequent copy into a second datagram array.



##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/receiver/protocol/airgap/IoTDBAirGapReceiverAgent.java:
##########
@@ -26,30 +26,46 @@
 import org.apache.iotdb.commons.service.IService;
 import org.apache.iotdb.commons.service.ServiceType;
 import org.apache.iotdb.db.i18n.DataNodePipeMessages;
+import org.apache.iotdb.db.pipe.agent.PipeDataNodeAgent;
+import org.apache.iotdb.db.protocol.session.ClientSession;
+import org.apache.iotdb.db.protocol.session.SessionManager;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import java.io.IOException;
+import java.net.DatagramPacket;
+import java.net.DatagramSocket;
+import java.net.InetAddress;
 import java.net.ServerSocket;
 import java.net.Socket;
+import java.net.SocketTimeoutException;
+import java.util.Map;
 import java.util.Objects;
+import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ExecutorService;
+import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicLong;
 
 public class IoTDBAirGapReceiverAgent implements IService {
 
   private static final Logger LOGGER = 
LoggerFactory.getLogger(IoTDBAirGapReceiverAgent.class);
+  private static final int UDP_PACKET_MAX_SIZE_IN_BYTES = 65_507;

Review Comment:
   Oversized requests are split with the existing `PipeTransferSliceReqBuilder` 
into `TRANSFER_SLICE` requests. Each datagram is sized against the configured 
UDP packet limit, including the request ID, envelope, and optional E-Language 
framing. The receiver sends the slices through the existing handler, which 
reassembles the original request.



##########
iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/pipe/sink/protocol/IoTDBAirGapSink.java:
##########
@@ -334,6 +478,141 @@ protected boolean sendBytes(final AirGapSocket socket, 
byte[] bytes) throws IOEx
     return Arrays.equals(AirGapOneByteResponse.OK, response);
   }
 
+  private boolean sendBytesByUdp(final AirGapSocket socket, final byte[] 
bytes) throws IOException {
+    for (final byte[] requestBytes : sliceIfNeededForUdp(bytes)) {
+      if (!sendOneDatagram(socket, requestBytes)) {
+        return false;
+      }
+    }
+    return true;
+  }
+
+  private boolean sendOneDatagram(final AirGapSocket socket, final byte[] 
requestBytes)
+      throws IOException {
+    final long requestId = udpRequestIdGenerator.incrementAndGet();
+    final byte[] framedRequestBytes =
+        eLanguageEnable
+            ? enrichWithELanguage(enrichWithLengthAndChecksum(requestBytes))
+            : enrichWithLengthAndChecksum(requestBytes);
+    final byte[] datagramBytes =
+        ByteBuffer.allocate(UDP_REQUEST_ID_SIZE + framedRequestBytes.length)
+            .putLong(requestId)
+            .put(framedRequestBytes)
+            .array();
+    if (datagramBytes.length > udpPacketSizeInBytes) {
+      throw new IOException(
+          String.format(
+              PipeMessages
+                  
.EXCEPTION_AIR_GAP_UDP_DATAGRAM_SIZE_ARG_EXCEEDS_CONFIGURED_PACKET_SIZE_ARG_559D0654,
+              datagramBytes.length,
+              udpPacketSizeInBytes));
+    }
+
+    final DatagramSocket datagramSocket = socket.getDatagramSocket();
+    datagramSocket.send(
+        new DatagramPacket(
+            datagramBytes,
+            datagramBytes.length,
+            socket.getDatagramAddress(),
+            socket.getEndPoint().getPort()));
+
+    final int originalTimeoutMs = datagramSocket.getSoTimeout();
+    final long deadlineNanos =
+        originalTimeoutMs == 0
+            ? Long.MAX_VALUE
+            : System.nanoTime() + 
TimeUnit.MILLISECONDS.toNanos(originalTimeoutMs);
+    final byte[] response = new byte[UDP_REQUEST_ID_SIZE + 1];
+    final DatagramPacket responsePacket = new DatagramPacket(response, 
response.length);
+    try {
+      while (true) {
+        if (deadlineNanos != Long.MAX_VALUE) {
+          final long remainingNanos = deadlineNanos - System.nanoTime();
+          if (remainingNanos <= 0) {
+            throw new SocketTimeoutException(
+                
PipeMessages.EXCEPTION_TIMED_OUT_WAITING_FOR_THE_MATCHING_AIR_GAP_ACK_DD2F3706);
+          }
+          datagramSocket.setSoTimeout(
+              (int) Math.max(1, 
TimeUnit.NANOSECONDS.toMillis(remainingNanos)));
+        }
+
+        responsePacket.setLength(response.length);
+        datagramSocket.receive(responsePacket);
+        if (responsePacket.getLength() != UDP_REQUEST_ID_SIZE + 1) {
+          continue;
+        }
+
+        final ByteBuffer responseBuffer = ByteBuffer.wrap(response, 0, 
responsePacket.getLength());
+        if (responseBuffer.getLong() != requestId) {
+          continue;
+        }
+        return Arrays.equals(AirGapOneByteResponse.OK, new byte[] 
{responseBuffer.get()});

Review Comment:
   The response is now checked directly with `responseBuffer.get() == 
AirGapOneByteResponse.OK[0]`, avoiding the temporary one-byte array.



##########
iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/pipe/sink/protocol/IoTDBAirGapSink.java:
##########
@@ -334,6 +478,141 @@ protected boolean sendBytes(final AirGapSocket socket, 
byte[] bytes) throws IOEx
     return Arrays.equals(AirGapOneByteResponse.OK, response);
   }
 
+  private boolean sendBytesByUdp(final AirGapSocket socket, final byte[] 
bytes) throws IOException {
+    for (final byte[] requestBytes : sliceIfNeededForUdp(bytes)) {
+      if (!sendOneDatagram(socket, requestBytes)) {
+        return false;
+      }
+    }
+    return true;
+  }
+
+  private boolean sendOneDatagram(final AirGapSocket socket, final byte[] 
requestBytes)
+      throws IOException {
+    final long requestId = udpRequestIdGenerator.incrementAndGet();
+    final byte[] framedRequestBytes =
+        eLanguageEnable
+            ? enrichWithELanguage(enrichWithLengthAndChecksum(requestBytes))
+            : enrichWithLengthAndChecksum(requestBytes);
+    final byte[] datagramBytes =
+        ByteBuffer.allocate(UDP_REQUEST_ID_SIZE + framedRequestBytes.length)
+            .putLong(requestId)
+            .put(framedRequestBytes)
+            .array();
+    if (datagramBytes.length > udpPacketSizeInBytes) {
+      throw new IOException(
+          String.format(
+              PipeMessages
+                  
.EXCEPTION_AIR_GAP_UDP_DATAGRAM_SIZE_ARG_EXCEEDS_CONFIGURED_PACKET_SIZE_ARG_559D0654,
+              datagramBytes.length,
+              udpPacketSizeInBytes));
+    }
+
+    final DatagramSocket datagramSocket = socket.getDatagramSocket();
+    datagramSocket.send(
+        new DatagramPacket(
+            datagramBytes,
+            datagramBytes.length,
+            socket.getDatagramAddress(),
+            socket.getEndPoint().getPort()));
+
+    final int originalTimeoutMs = datagramSocket.getSoTimeout();
+    final long deadlineNanos =
+        originalTimeoutMs == 0
+            ? Long.MAX_VALUE
+            : System.nanoTime() + 
TimeUnit.MILLISECONDS.toNanos(originalTimeoutMs);
+    final byte[] response = new byte[UDP_REQUEST_ID_SIZE + 1];
+    final DatagramPacket responsePacket = new DatagramPacket(response, 
response.length);
+    try {
+      while (true) {
+        if (deadlineNanos != Long.MAX_VALUE) {
+          final long remainingNanos = deadlineNanos - System.nanoTime();
+          if (remainingNanos <= 0) {
+            throw new SocketTimeoutException(
+                
PipeMessages.EXCEPTION_TIMED_OUT_WAITING_FOR_THE_MATCHING_AIR_GAP_ACK_DD2F3706);
+          }
+          datagramSocket.setSoTimeout(
+              (int) Math.max(1, 
TimeUnit.NANOSECONDS.toMillis(remainingNanos)));
+        }
+
+        responsePacket.setLength(response.length);
+        datagramSocket.receive(responsePacket);
+        if (responsePacket.getLength() != UDP_REQUEST_ID_SIZE + 1) {
+          continue;
+        }
+
+        final ByteBuffer responseBuffer = ByteBuffer.wrap(response, 0, 
responsePacket.getLength());
+        if (responseBuffer.getLong() != requestId) {
+          continue;
+        }
+        return Arrays.equals(AirGapOneByteResponse.OK, new byte[] 
{responseBuffer.get()});
+      }
+    } finally {
+      datagramSocket.setSoTimeout(originalTimeoutMs);
+    }
+  }
+
+  private List<byte[]> sliceIfNeededForUdp(final byte[] requestBytes) throws 
IOException {

Review Comment:
   Yes. `request.body` is a slice of the original request bytes, and sending 
reads it through a duplicate directly into the final datagram buffer, avoiding 
an intermediate serialized request array. `TRANSFER_SLICE` payloads still 
require per-slice serialization.



##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/receiver/protocol/airgap/IoTDBAirGapReceiverAgent.java:
##########
@@ -61,39 +77,160 @@ public void listen() {
           ThreadName.PIPE_AIR_GAP_RECEIVER.getName() + "-" + airGapReceiverId);
       airGapReceiverThread.start();
     } catch (final IOException e) {
-      
LOGGER.warn(DataNodePipeMessages.UNHANDLED_EXCEPTION_DURING_PIPE_AIR_GAP_RECEIVER,
 e);
+      if (allowSubmitListen.get()) {
+        
LOGGER.warn(DataNodePipeMessages.UNHANDLED_EXCEPTION_DURING_PIPE_AIR_GAP_RECEIVER,
 e);
+      }
     }
 
     if (allowSubmitListen.get()) {
       listenExecutor.submit(this::listen);
     }
   }
 
+  public void listenUdp() {
+    while (allowSubmitListen.get() && !datagramSocket.isClosed()) {
+      removeExpiredUdpClientSessions(System.nanoTime());
+      final byte[] buffer = new byte[UDP_PACKET_MAX_SIZE_IN_BYTES];
+      final DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
+      try {
+        datagramSocket.receive(packet);
+
+        final long airGapReceiverId = receiverId.incrementAndGet();
+        final IoTDBAirGapReceiver receiver =
+            new IoTDBAirGapReceiver(new Socket(), airGapReceiverId);
+        final String receiverKey = packet.getSocketAddress().toString();
+        final boolean[] registeredSession = {false};
+        try {
+          receiver.receiveUdp(
+              datagramSocket,
+              packet,
+              receiverKey,
+              buffer,
+              () -> registeredSession[0] = 
registerUdpSessionIfNecessary(packet));
+        } finally {
+          if (registeredSession[0]) {
+            SessionManager.getInstance().removeCurrSession();
+          }
+        }
+      } catch (final SocketTimeoutException e) {
+        removeExpiredUdpClientSessions(System.nanoTime());
+      } catch (final IOException e) {
+        if (allowSubmitListen.get()) {
+          
LOGGER.warn(DataNodePipeMessages.UNHANDLED_EXCEPTION_DURING_PIPE_AIR_GAP_RECEIVER,
 e);
+        }
+      } catch (final Exception e) {
+        
LOGGER.warn(DataNodePipeMessages.UNHANDLED_EXCEPTION_DURING_PIPE_AIR_GAP_RECEIVER,
 e);
+      }
+    }
+  }
+
+  boolean registerUdpSessionIfNecessary(final DatagramPacket packet) {
+    final String receiverKey = packet.getSocketAddress().toString();
+    final long now = System.nanoTime();
+    removeExpiredUdpClientSessions(now);
+
+    UdpClientSession clientSession = udpClientSessions.get(receiverKey);
+    if (clientSession == null) {
+      ensureUdpClientSessionCapacity();
+      clientSession =
+          new UdpClientSession(
+              new ClientSession(new DatagramClientSocket(packet.getAddress(), 
packet.getPort())),
+              now);
+      final UdpClientSession existing = 
udpClientSessions.putIfAbsent(receiverKey, clientSession);
+      if (existing != null) {
+        clientSession = existing;
+      }
+    }
+    clientSession.lastAccessTimeNanos = now;
+    return SessionManager.getInstance().registerSession(clientSession.session);
+  }
+
+  private void ensureUdpClientSessionCapacity() {
+    if (udpClientSessions.size() < UDP_MAX_CLIENT_SESSIONS) {
+      return;
+    }
+
+    String leastRecentlyUsedKey = null;
+    UdpClientSession leastRecentlyUsedSession = null;
+    for (final Map.Entry<String, UdpClientSession> entry : 
udpClientSessions.entrySet()) {
+      if (leastRecentlyUsedSession == null
+          || entry.getValue().lastAccessTimeNanos < 
leastRecentlyUsedSession.lastAccessTimeNanos) {
+        leastRecentlyUsedKey = entry.getKey();
+        leastRecentlyUsedSession = entry.getValue();
+      }
+    }
+    if (leastRecentlyUsedKey != null) {
+      removeUdpClientSession(leastRecentlyUsedKey, leastRecentlyUsedSession);
+    }
+  }
+
+  void removeExpiredUdpClientSessions(final long nowNanos) {
+    final long idleTimeoutNanos =
+        TimeUnit.MILLISECONDS.toNanos(
+            Math.max(1, 
PipeConfig.getInstance().getPipeSinkTransferTimeoutMs()));
+    udpClientSessions.forEach(
+        (key, session) -> {
+          if (nowNanos - session.lastAccessTimeNanos >= idleTimeoutNanos) {
+            removeUdpClientSession(key, session);
+          }
+        });
+  }
+
+  private void removeUdpClientSession(final String key, final UdpClientSession 
session) {
+    if (!udpClientSessions.remove(key, session)) {
+      return;
+    }
+
+    final boolean registeredSession = 
SessionManager.getInstance().registerSession(session.session);
+    try {
+      PipeDataNodeAgent.receiver().thrift().handleClientExit(key);

Review Comment:
   The UDP path still uses the existing Thrift receiver agent for request 
dispatch and client/session lifecycle. UDP changes only the transport: each 
datagram is decoded into the existing transfer-request shape and passed through 
the same receiver handlers, avoiding a second implementation of pipe request 
handling.



##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/receiver/protocol/thrift/IoTDBDataNodeReceiverAgent.java:
##########
@@ -38,16 +42,24 @@ protected void initConstructors() {
 
   @Override
   protected IoTDBReceiver getReceiverWithSpecifiedClient(final String ignore) {
-    return receiverThreadLocal.get();
+    return ignore == null ? receiverThreadLocal.get() : 
receiverMap.get(ignore);

Review Comment:
   Renamed the parameter from `ignore` to `key`; it identifies the client whose 
receiver instance is selected, so `key` reflects its role.



-- 
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