You are viewing a plain text version of this content. The canonical link for it is here.
Posted to gitbox@activemq.apache.org by "clebertsuconic (via GitHub)" <gi...@apache.org> on 2024/03/04 21:35:44 UTC

[PR] ARTEMIS-4668 Moving AMQP Large Message file handling away from Netty thread [activemq-artemis]

clebertsuconic opened a new pull request, #4840:
URL: https://github.com/apache/activemq-artemis/pull/4840

   (no comment)


-- 
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: gitbox-unsubscribe@activemq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] ARTEMIS-4668 Moving AMQP Large Message file handling away from Netty thread [activemq-artemis]

Posted by "clebertsuconic (via GitHub)" <gi...@apache.org>.
clebertsuconic commented on code in PR #4840:
URL: https://github.com/apache/activemq-artemis/pull/4840#discussion_r1514683343


##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/AMQPLargeMessageWriter.java:
##########
@@ -81,33 +84,55 @@ public boolean isWriting() {
    public void close() {
       if (!closed) {
          try {
+            try {
+               if (largeBodyReader != null) {
+                  largeBodyReader.close();
+               }
+            } catch (Exception e) {
+               // if we get an error only at this point, there's nothing else we could do other than log.warn
+               logger.warn("{}", e.getMessage(), e);
+            }
             if (message != null) {
                message.usageDown();
             }
          } finally {
-            reset(true);
+            resetClosed();
          }
       }
    }
 
    @Override
-   public AMQPLargeMessageWriter open() {
-      if (!closed) {

Review Comment:
   was an accident I placed it back already



-- 
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: gitbox-unsubscribe@activemq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] ARTEMIS-4668 Moving AMQP Large Message file handling away from Netty thread [activemq-artemis]

Posted by "clebertsuconic (via GitHub)" <gi...@apache.org>.
clebertsuconic commented on code in PR #4840:
URL: https://github.com/apache/activemq-artemis/pull/4840#discussion_r1512013812


##########
artemis-protocols/artemis-amqp-protocol/src/test/java/org/apache/activemq/artemis/protocol/amqp/proton/AMQPTunneledCoreLargeMessageReaderTest.java:
##########
@@ -159,19 +169,18 @@ public void testReadMessageByteByByteFromDeliveryBuffer() throws Exception {
          when(receiver.recv()).thenReturn(deliveryAnnotations.duplicate().position(i - 1).limit(i).slice());
 
          try {
-            assertNull(reader.readBytes(delivery));
+            readMessage = null;
+            reader.readBytes(delivery);
+            Assert.assertNull(readMessage);
          } catch (IllegalStateException e) {
             fail("Should not throw as the reader should be able to read just delivery annotations.");
          }
       }
 
-      assertNotNull(reader.getDeliveryAnnotations());
-
-      final DeliveryAnnotations annotations = reader.getDeliveryAnnotations();
-
-      assertTrue(annotations.getValue().get(Symbol.valueOf("a")).equals("a"));
-      assertTrue(annotations.getValue().get(Symbol.valueOf("b")).equals("b"));
-      assertTrue(annotations.getValue().get(Symbol.valueOf("c")).equals("c"));
+      DeliveryAnnotations currentAnnotations = reader.getDeliveryAnnotations();

Review Comment:
   @tabish121 I remembered what happened.. I tried removing the open method by just the accept.. and I had made a mess back and forth until I stabilized on what you saw... 
   
   
   thanks for the review.. I made the change.



-- 
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: gitbox-unsubscribe@activemq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] ARTEMIS-4668 Moving AMQP Large Message file handling away from Netty thread [activemq-artemis]

Posted by "clebertsuconic (via GitHub)" <gi...@apache.org>.
clebertsuconic commented on code in PR #4840:
URL: https://github.com/apache/activemq-artemis/pull/4840#discussion_r1513285024


##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/AMQPConnectionContext.java:
##########
@@ -378,6 +389,16 @@ public AMQPConnectionCallback getConnectionCallback() {
       return connectionCallback;
    }
 
+   public void exception(Throwable e) {
+      logger.warn(e.getMessage(), e);
+      ErrorCondition error = new ErrorCondition();
+      error.setCondition(AmqpError.INTERNAL_ERROR);
+      error.setDescription("Unrecoverable error: " + (e.getMessage() == null ? e.getClass().getSimpleName() : e.getMessage()));
+      getHandler().getConnection().setCondition(error);
+      getHandler().getConnection().close();
+      flush();
+   }

Review Comment:
   I added a commit to be squashed addressing this



-- 
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: gitbox-unsubscribe@activemq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] ARTEMIS-4668 Moving AMQP Large Message file handling away from Netty thread [activemq-artemis]

Posted by "tabish121 (via GitHub)" <gi...@apache.org>.
tabish121 commented on code in PR #4840:
URL: https://github.com/apache/activemq-artemis/pull/4840#discussion_r1514638970


##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/ProtonAbstractReceiver.java:
##########
@@ -320,28 +325,45 @@ public final void onMessage(Delivery delivery) throws ActiveMQAMQPException {
             return;
          }
 
-         final Message message = messageReader.readBytes(delivery);
+         {
+            Message completeMessage;
+            if ((completeMessage = messageReader.readBytes(delivery)) != null) {
+               // notice the AMQP Large Message Reader will always return false
+               // and call the onMessageComplete directly
+               // since that happens asynchronously
+               onMessageComplete(delivery, completeMessage, messageReader.getDeliveryAnnotations());
+            }
+         }
+      } catch (Exception e) {
+         logger.warn(e.getMessage(), e);
+         throw new ActiveMQAMQPInternalErrorException(e.getMessage(), e);
+      }
+   }
 
-         if (message != null) {
-            // Fetch this before the close of the reader as that will clear any read message
-            // delivery annotations.
-            final DeliveryAnnotations deliveryAnnotations = messageReader.getDeliveryAnnotations();
+   public void onMessageComplete(Delivery delivery,
+                          Message message, DeliveryAnnotations deliveryAnnotations) {
+      connection.requireInHandler();
 
-            this.messageReader.close();
-            this.messageReader = null;
+      try {
+         final Receiver receiver = ((Receiver) delivery.getLink());

Review Comment:
   It isn't really necessary to get and cast here as the ProtonAbstractReceiver already owns a reference to the proton receiver in play. 



##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/AMQPLargeMessageWriter.java:
##########
@@ -81,33 +84,55 @@ public boolean isWriting() {
    public void close() {
       if (!closed) {
          try {
+            try {
+               if (largeBodyReader != null) {
+                  largeBodyReader.close();
+               }
+            } catch (Exception e) {
+               // if we get an error only at this point, there's nothing else we could do other than log.warn
+               logger.warn("{}", e.getMessage(), e);
+            }
             if (message != null) {
                message.usageDown();
             }
          } finally {
-            reset(true);
+            resetClosed();
          }
       }
    }
 
    @Override
-   public AMQPLargeMessageWriter open() {
-      if (!closed) {

Review Comment:
   Closed check should still be required here



##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/ProtonAbstractReceiver.java:
##########
@@ -320,28 +325,45 @@ public final void onMessage(Delivery delivery) throws ActiveMQAMQPException {
             return;
          }
 
-         final Message message = messageReader.readBytes(delivery);
+         {

Review Comment:
   Is there a reason for the added surrounding scope here?  Seems unneeded in this context



-- 
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: gitbox-unsubscribe@activemq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] ARTEMIS-4668 Moving AMQP Large Message file handling away from Netty thread [activemq-artemis]

Posted by "clebertsuconic (via GitHub)" <gi...@apache.org>.
clebertsuconic merged PR #4840:
URL: https://github.com/apache/activemq-artemis/pull/4840


-- 
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: gitbox-unsubscribe@activemq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] ARTEMIS-4668 Moving AMQP Large Message file handling away from Netty thread [activemq-artemis]

Posted by "clebertsuconic (via GitHub)" <gi...@apache.org>.
clebertsuconic commented on code in PR #4840:
URL: https://github.com/apache/activemq-artemis/pull/4840#discussion_r1513289227


##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/AMQPLargeMessageReader.java:
##########
@@ -77,39 +73,73 @@ public AMQPLargeMessageReader open() {
    }
 
    @Override
-   public Message readBytes(Delivery delivery) throws Exception {
+   public boolean readBytes(Delivery delivery) throws Exception {
       if (closed) {
          throw new IllegalStateException("AMQP Large Message Reader is closed and read cannot proceed");
       }
 
+      serverReceiver.connection.requireInHandler();
+
       final Receiver receiver = ((Receiver) delivery.getLink());
       final ReadableBuffer dataBuffer = receiver.recv();
 
+      final AMQPSessionCallback sessionSPI = serverReceiver.getSessionContext().getSessionSPI();
+
       if (currentMessage == null) {
-         final AMQPSessionCallback sessionSPI = serverReceiver.getSessionContext().getSessionSPI();
          final long id = sessionSPI.getStorageManager().generateID();
          currentMessage = new AMQPLargeMessage(id, delivery.getMessageFormat(), null,
                                                sessionSPI.getCoreMessageObjectPools(),
                                                sessionSPI.getStorageManager());
          currentMessage.parseHeader(dataBuffer);
-
+         logger.trace("Initializing current message {} on {}", currentMessage, this);
          sessionSPI.getStorageManager().largeMessageCreated(id, currentMessage);
+         sessionSPI.execute(() -> validateFile(currentMessage));
       }
 
-      currentMessage.addBytes(dataBuffer);
+      serverReceiver.getConnection().disableAutoRead();

Review Comment:
   between the disableAutoRead and the call to the executor nothing else would throw an exception.
   
   I added handling on the exception catch to enable it back, also I'm enabling it back in the onException method I added.



-- 
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: gitbox-unsubscribe@activemq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] ARTEMIS-4668 Moving AMQP Large Message file handling away from Netty thread [activemq-artemis]

Posted by "clebertsuconic (via GitHub)" <gi...@apache.org>.
clebertsuconic commented on code in PR #4840:
URL: https://github.com/apache/activemq-artemis/pull/4840#discussion_r1513284534


##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/AMQPTunneledCoreMessageReader.java:
##########
@@ -155,6 +149,10 @@ public Message readBytes(Delivery delivery) {
       coreMessage.reloadPersistence(buffer, sessionSPI.getCoreMessageObjectPools());
       coreMessage.setMessageID(sessionSPI.getStorageManager().generateID());
 
-      return coreMessage;
+      serverReceiver.onMessageComplete(delivery, coreMessage, deliveryAnnotations);
+
+      close();

Review Comment:
   I added a commit to be squashed addressing this



##########
artemis-core-client/src/main/java/org/apache/activemq/artemis/core/remoting/impl/netty/NettyConnection.java:
##########
@@ -126,6 +126,9 @@ public final Channel getNettyChannel() {
    @Override
    public final void setAutoRead(boolean autoRead) {
       channel.config().setAutoRead(autoRead);
+      if (autoRead) {
+         channel.read();
+      }

Review Comment:
   I added a commit to be squashed addressing this



-- 
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: gitbox-unsubscribe@activemq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] ARTEMIS-4668 Moving AMQP Large Message file handling away from Netty thread [activemq-artemis]

Posted by "clebertsuconic (via GitHub)" <gi...@apache.org>.
clebertsuconic commented on code in PR #4840:
URL: https://github.com/apache/activemq-artemis/pull/4840#discussion_r1514787699


##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/AMQPLargeMessageReader.java:
##########
@@ -50,14 +50,15 @@ public DeliveryAnnotations getDeliveryAnnotations() {
    @Override
    public void close() {
       if (!closed) {
-         if (currentMessage != null) {
-            try {
-               currentMessage.deleteFile();
-            } catch (Throwable error) {
-               ActiveMQServerLogger.LOGGER.errorDeletingLargeMessageFile(error);
-            } finally {
-               currentMessage = null;
+         try {
+            AMQPLargeMessage localCurrentMessage = currentMessage;

Review Comment:
   @gemmellr actually... I am doing another change instead.
   
   
   The AMQPLargeMessage should be created within the session executor.
   
   Right now that's only working because I'm not actually creating a file there yet.. but this is a bit fragile.. as anyone could break it..
   
   
   I will move the initialization of the new Object within the addBytes..



-- 
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: gitbox-unsubscribe@activemq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] ARTEMIS-4668 Moving AMQP Large Message file handling away from Netty thread [activemq-artemis]

Posted by "tabish121 (via GitHub)" <gi...@apache.org>.
tabish121 commented on code in PR #4840:
URL: https://github.com/apache/activemq-artemis/pull/4840#discussion_r1514696844


##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/ProtonAbstractReceiver.java:
##########
@@ -320,28 +325,45 @@ public final void onMessage(Delivery delivery) throws ActiveMQAMQPException {
             return;
          }
 
-         final Message message = messageReader.readBytes(delivery);
+         {
+            Message completeMessage;
+            if ((completeMessage = messageReader.readBytes(delivery)) != null) {
+               // notice the AMQP Large Message Reader will always return false
+               // and call the onMessageComplete directly
+               // since that happens asynchronously
+               onMessageComplete(delivery, completeMessage, messageReader.getDeliveryAnnotations());
+            }
+         }
+      } catch (Exception e) {
+         logger.warn(e.getMessage(), e);
+         throw new ActiveMQAMQPInternalErrorException(e.getMessage(), e);
+      }
+   }
 
-         if (message != null) {
-            // Fetch this before the close of the reader as that will clear any read message
-            // delivery annotations.
-            final DeliveryAnnotations deliveryAnnotations = messageReader.getDeliveryAnnotations();
+   public void onMessageComplete(Delivery delivery,
+                          Message message, DeliveryAnnotations deliveryAnnotations) {
+      connection.requireInHandler();
 
-            this.messageReader.close();
-            this.messageReader = null;
+      try {
+         final Receiver receiver = ((Receiver) delivery.getLink());

Review Comment:
   I realized it afterwards, it isn't really needed as it shouldn't ever be a receiver that isn't the one we have tracked but guess its fine either way, removing unneeded code is usually a nice thing. 



-- 
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: gitbox-unsubscribe@activemq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] ARTEMIS-4668 Moving AMQP Large Message file handling away from Netty thread [activemq-artemis]

Posted by "tabish121 (via GitHub)" <gi...@apache.org>.
tabish121 commented on code in PR #4840:
URL: https://github.com/apache/activemq-artemis/pull/4840#discussion_r1514742904


##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/AMQPLargeMessageReader.java:
##########
@@ -34,7 +34,7 @@ public class AMQPLargeMessageReader implements MessageReader {
 
    private final ProtonAbstractReceiver serverReceiver;
 
-   private AMQPLargeMessage currentMessage;
+   private volatile AMQPLargeMessage currentMessage;

Review Comment:
   I'd go with abundance of caution since we are crossing threads where we weren't before.  



-- 
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: gitbox-unsubscribe@activemq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] ARTEMIS-4668 Moving AMQP Large Message file handling away from Netty thread [activemq-artemis]

Posted by "gemmellr (via GitHub)" <gi...@apache.org>.
gemmellr commented on code in PR #4840:
URL: https://github.com/apache/activemq-artemis/pull/4840#discussion_r1513187210


##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/AMQPTunneledCoreLargeMessageReader.java:
##########
@@ -209,13 +208,15 @@ public Message readBytes(Delivery delivery) throws Exception {
 
             state = State.DONE;
 
-            return result;
-         }
+            serverReceiver.onMessageComplete(delivery, result, deliveryAnnotations);
 
-         return null;
+            close();

Review Comment:
   Doesnt feel like a reader should be closing itself. Was meant to be managed by the interested party using it.



-- 
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: gitbox-unsubscribe@activemq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] ARTEMIS-4668 Moving AMQP Large Message file handling away from Netty thread [activemq-artemis]

Posted by "tabish121 (via GitHub)" <gi...@apache.org>.
tabish121 commented on code in PR #4840:
URL: https://github.com/apache/activemq-artemis/pull/4840#discussion_r1511925400


##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/AMQPBrokerConnection.java:
##########
@@ -847,6 +847,7 @@ public MessageWriter selectOutgoingMessageWriter(ProtonServerSenderContext sende
             if (message.isLargeMessage()) {
                selected = largeMessageWriter != null ? largeMessageWriter :
                   (largeMessageWriter = new AMQPLargeMessageWriter(sender));
+               largeMessageWriter.openContext(reference);

Review Comment:
   So are you saying a handler can now accept an additional message while it's trying to process the previously passed large message ?  That seem rife with errors. Otherwise I don't follow what you are saying.  



-- 
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: gitbox-unsubscribe@activemq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] ARTEMIS-4668 Moving AMQP Large Message file handling away from Netty thread [activemq-artemis]

Posted by "clebertsuconic (via GitHub)" <gi...@apache.org>.
clebertsuconic commented on code in PR #4840:
URL: https://github.com/apache/activemq-artemis/pull/4840#discussion_r1511954720


##########
artemis-protocols/artemis-amqp-protocol/src/test/java/org/apache/activemq/artemis/protocol/amqp/proton/AMQPTunneledCoreLargeMessageReaderTest.java:
##########
@@ -116,21 +128,19 @@ public void testReadDeliveryAnnotationsFromDeliveryBuffer() throws Exception {
 
       reader.open();
 
-      assertNull(reader.getDeliveryAnnotations());
-
       try {
+         readMessage = null;
          reader.readBytes(delivery);
+         Assert.assertNull(readMessage); // should not been called yet
       } catch (IllegalStateException e) {
          fail("Should not throw as the reader should be able to read just delivery annotations.");
       }
 
-      assertNotNull(reader.getDeliveryAnnotations());
-
-      final DeliveryAnnotations annotations = reader.getDeliveryAnnotations();
-
-      assertTrue(annotations.getValue().get(Symbol.valueOf("a")).equals("a"));
-      assertTrue(annotations.getValue().get(Symbol.valueOf("b")).equals("b"));
-      assertTrue(annotations.getValue().get(Symbol.valueOf("c")).equals("c"));
+      // peek current delivery annotations
+      DeliveryAnnotations currentAnnotations = reader.getDeliveryAnnotations();

Review Comment:
   The readMethod now calls close when there's no more pending. that is the onMessageComplete is called and the reader is closed. Meaning the test here would call getDeliveryAnnotations and it wouldn't be here.
   
   the only case where getDeliveryAnnotations will return anything is if the read it still partial now.



-- 
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: gitbox-unsubscribe@activemq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] ARTEMIS-4668 Moving AMQP Large Message file handling away from Netty thread [activemq-artemis]

Posted by "clebertsuconic (via GitHub)" <gi...@apache.org>.
clebertsuconic commented on PR #4840:
URL: https://github.com/apache/activemq-artemis/pull/4840#issuecomment-1979429610

   @gemmellr , @tabish121 I added 3 temporary commits to this PR. Let me know if you have any questions and I will squash them before setting it ready to merge.


-- 
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: gitbox-unsubscribe@activemq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] ARTEMIS-4668 Moving AMQP Large Message file handling away from Netty thread [activemq-artemis]

Posted by "clebertsuconic (via GitHub)" <gi...@apache.org>.
clebertsuconic commented on code in PR #4840:
URL: https://github.com/apache/activemq-artemis/pull/4840#discussion_r1514780944


##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/AMQPLargeMessageReader.java:
##########
@@ -50,14 +50,15 @@ public DeliveryAnnotations getDeliveryAnnotations() {
    @Override
    public void close() {
       if (!closed) {
-         if (currentMessage != null) {
-            try {
-               currentMessage.deleteFile();
-            } catch (Throwable error) {
-               ActiveMQServerLogger.LOGGER.errorDeletingLargeMessageFile(error);
-            } finally {
-               currentMessage = null;
+         try {
+            AMQPLargeMessage localCurrentMessage = currentMessage;

Review Comment:
   There was actually only addBytes... as the initialization should stay as is.
   
   I amended the PR with the addBytes change.



-- 
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: gitbox-unsubscribe@activemq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] ARTEMIS-4668 Moving AMQP Large Message file handling away from Netty thread [activemq-artemis]

Posted by "gemmellr (via GitHub)" <gi...@apache.org>.
gemmellr commented on code in PR #4840:
URL: https://github.com/apache/activemq-artemis/pull/4840#discussion_r1514766964


##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/AMQPLargeMessageReader.java:
##########
@@ -50,14 +50,15 @@ public DeliveryAnnotations getDeliveryAnnotations() {
    @Override
    public void close() {
       if (!closed) {
-         if (currentMessage != null) {
-            try {
-               currentMessage.deleteFile();
-            } catch (Throwable error) {
-               ActiveMQServerLogger.LOGGER.errorDeletingLargeMessageFile(error);
-            } finally {
-               currentMessage = null;
+         try {
+            AMQPLargeMessage localCurrentMessage = currentMessage;

Review Comment:
   There are various other usages of currentMessage that could probably benefit from similar update.



-- 
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: gitbox-unsubscribe@activemq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] ARTEMIS-4668 Moving AMQP Large Message file handling away from Netty thread [activemq-artemis]

Posted by "tabish121 (via GitHub)" <gi...@apache.org>.
tabish121 commented on code in PR #4840:
URL: https://github.com/apache/activemq-artemis/pull/4840#discussion_r1511873410


##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/ProtonDeliveryHandler.java:
##########
@@ -16,6 +16,7 @@
  */
 package org.apache.activemq.artemis.protocol.amqp.proton;
 
+import org.apache.activemq.artemis.api.core.Message;

Review Comment:
   Checkstyle violations causes build errors



-- 
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: gitbox-unsubscribe@activemq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] ARTEMIS-4668 Moving AMQP Large Message file handling away from Netty thread [activemq-artemis]

Posted by "clebertsuconic (via GitHub)" <gi...@apache.org>.
clebertsuconic commented on code in PR #4840:
URL: https://github.com/apache/activemq-artemis/pull/4840#discussion_r1512084416


##########
tests/artemis-test-support/src/main/java/org/apache/activemq/transport/amqp/client/AmqpMessage.java:
##########
@@ -640,6 +640,11 @@ public String getText() throws NoSuchElementException {
       throw new NoSuchElementException("Message does not contain a String body");
    }
 
+   public byte[] getBytes() {
+      Data body = (Data)getWrappedMessage().getBody();
+      return body.getValue().getArray();

Review Comment:
   I just wanted to assert a few bytes[] that were set with setBytes([]).
   
   I have no idea on what I'm supposed to do here. I will just revert the test addition.



-- 
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: gitbox-unsubscribe@activemq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] ARTEMIS-4668 Moving AMQP Large Message file handling away from Netty thread [activemq-artemis]

Posted by "clebertsuconic (via GitHub)" <gi...@apache.org>.
clebertsuconic commented on code in PR #4840:
URL: https://github.com/apache/activemq-artemis/pull/4840#discussion_r1514698925


##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/ProtonAbstractReceiver.java:
##########
@@ -320,28 +325,45 @@ public final void onMessage(Delivery delivery) throws ActiveMQAMQPException {
             return;
          }
 
-         final Message message = messageReader.readBytes(delivery);
+         {
+            Message completeMessage;
+            if ((completeMessage = messageReader.readBytes(delivery)) != null) {
+               // notice the AMQP Large Message Reader will always return false
+               // and call the onMessageComplete directly
+               // since that happens asynchronously
+               onMessageComplete(delivery, completeMessage, messageReader.getDeliveryAnnotations());
+            }
+         }
+      } catch (Exception e) {
+         logger.warn(e.getMessage(), e);
+         throw new ActiveMQAMQPInternalErrorException(e.getMessage(), e);
+      }
+   }
 
-         if (message != null) {
-            // Fetch this before the close of the reader as that will clear any read message
-            // delivery annotations.
-            final DeliveryAnnotations deliveryAnnotations = messageReader.getDeliveryAnnotations();
+   public void onMessageComplete(Delivery delivery,
+                          Message message, DeliveryAnnotations deliveryAnnotations) {
+      connection.requireInHandler();
 
-            this.messageReader.close();
-            this.messageReader = null;
+      try {
+         final Receiver receiver = ((Receiver) delivery.getLink());

Review Comment:
   @tabish121 sure.. will remove it



-- 
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: gitbox-unsubscribe@activemq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] ARTEMIS-4668 Moving AMQP Large Message file handling away from Netty thread [activemq-artemis]

Posted by "clebertsuconic (via GitHub)" <gi...@apache.org>.
clebertsuconic commented on code in PR #4840:
URL: https://github.com/apache/activemq-artemis/pull/4840#discussion_r1511939022


##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/AMQPLargeMessageReader.java:
##########
@@ -77,39 +74,68 @@ public AMQPLargeMessageReader open() {
    }
 
    @Override
-   public Message readBytes(Delivery delivery) throws Exception {
+   public boolean readBytes(Delivery delivery) throws Exception {
       if (closed) {
          throw new IllegalStateException("AMQP Large Message Reader is closed and read cannot proceed");
       }
 
+      serverReceiver.connection.requireInHandler();
+
+      logger.trace("Reading {}", delivery);
+
       final Receiver receiver = ((Receiver) delivery.getLink());
       final ReadableBuffer dataBuffer = receiver.recv();
 
+      final AMQPSessionCallback sessionSPI = serverReceiver.getSessionContext().getSessionSPI();
+
       if (currentMessage == null) {
-         final AMQPSessionCallback sessionSPI = serverReceiver.getSessionContext().getSessionSPI();
          final long id = sessionSPI.getStorageManager().generateID();
-         currentMessage = new AMQPLargeMessage(id, delivery.getMessageFormat(), null,
-                                               sessionSPI.getCoreMessageObjectPools(),
-                                               sessionSPI.getStorageManager());
+         currentMessage = new AMQPLargeMessage(id, delivery.getMessageFormat(), null, sessionSPI.getCoreMessageObjectPools(), sessionSPI.getStorageManager());
          currentMessage.parseHeader(dataBuffer);
-
+         logger.trace("Initializing current message {} on {}", currentMessage, this);
          sessionSPI.getStorageManager().largeMessageCreated(id, currentMessage);
+         sessionSPI.execute(currentMessage::validateFile);
       }
 
-      currentMessage.addBytes(dataBuffer);
+      logger.trace("Disable autoread on {}", this);
+      serverReceiver.getConnection().disableAutoRead();
 
-      final AMQPLargeMessage result;
+      byte[] bytes = new byte[dataBuffer.remaining()];
+      dataBuffer.get(bytes);
+      logger.trace("Getting {}", bytes.length);

Review Comment:
   I had one specific case I needed... it doesn't make sense to keep it now.. will remove it.



-- 
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: gitbox-unsubscribe@activemq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] ARTEMIS-4668 Moving AMQP Large Message file handling away from Netty thread [activemq-artemis]

Posted by "clebertsuconic (via GitHub)" <gi...@apache.org>.
clebertsuconic commented on code in PR #4840:
URL: https://github.com/apache/activemq-artemis/pull/4840#discussion_r1514721775


##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/AMQPLargeMessageReader.java:
##########
@@ -34,7 +34,7 @@ public class AMQPLargeMessageReader implements MessageReader {
 
    private final ProtonAbstractReceiver serverReceiver;
 
-   private AMQPLargeMessage currentMessage;
+   private volatile AMQPLargeMessage currentMessage;

Review Comment:
   @tabish121 , @gemmellr currentMessage is set to null as part of the Session Executor. 
   We shouldn't have multiple threads messing with the variable at the same time as we set readable to false and transfer the execution, but I think it would be safer to set it as volatile.
   
   
   Let me know if you have a concern here.



-- 
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: gitbox-unsubscribe@activemq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] ARTEMIS-4668 Moving AMQP Large Message file handling away from Netty thread [activemq-artemis]

Posted by "tabish121 (via GitHub)" <gi...@apache.org>.
tabish121 commented on code in PR #4840:
URL: https://github.com/apache/activemq-artemis/pull/4840#discussion_r1513124572


##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/AMQPLargeMessageReader.java:
##########
@@ -77,39 +73,73 @@ public AMQPLargeMessageReader open() {
    }
 
    @Override
-   public Message readBytes(Delivery delivery) throws Exception {
+   public boolean readBytes(Delivery delivery) throws Exception {
       if (closed) {
          throw new IllegalStateException("AMQP Large Message Reader is closed and read cannot proceed");
       }
 
+      serverReceiver.connection.requireInHandler();
+
       final Receiver receiver = ((Receiver) delivery.getLink());
       final ReadableBuffer dataBuffer = receiver.recv();
 
+      final AMQPSessionCallback sessionSPI = serverReceiver.getSessionContext().getSessionSPI();
+
       if (currentMessage == null) {
-         final AMQPSessionCallback sessionSPI = serverReceiver.getSessionContext().getSessionSPI();
          final long id = sessionSPI.getStorageManager().generateID();
          currentMessage = new AMQPLargeMessage(id, delivery.getMessageFormat(), null,
                                                sessionSPI.getCoreMessageObjectPools(),
                                                sessionSPI.getStorageManager());
          currentMessage.parseHeader(dataBuffer);
-
+         logger.trace("Initializing current message {} on {}", currentMessage, this);
          sessionSPI.getStorageManager().largeMessageCreated(id, currentMessage);
+         sessionSPI.execute(() -> validateFile(currentMessage));
       }
 
-      currentMessage.addBytes(dataBuffer);
+      serverReceiver.getConnection().disableAutoRead();
 
-      final AMQPLargeMessage result;
+      byte[] bytes = new byte[dataBuffer.remaining()];
+      dataBuffer.get(bytes);
 
-      if (!delivery.isPartial()) {
-         currentMessage.releaseResources(serverReceiver.getConnection().isLargeMessageSync(), true);
-         result = currentMessage;
-         // We don't want a close to delete the file now, we've released the resources.
-         currentMessage = null;
-         deliveryAnnotations = result.getDeliveryAnnotations();
-      } else {
-         result = null;
+      boolean partial = delivery.isPartial();
+
+      sessionSPI.execute(() -> addBytes(delivery, bytes, partial));
+
+      return partial;
+   }
+
+   private void validateFile(AMQPLargeMessage message) {
+      try {
+         message.validateFile();
+      } catch (Exception e) {
+         logger.warn(e.getMessage(), e);
+         close();
+         serverReceiver.connection.exception(e);
       }
+   }
 
-      return result;
+   private void addBytes(Delivery delivery, byte[] bytes, boolean isPartial) {
+      ReadableBuffer dataBuffer = ReadableBuffer.ByteBufferReader.wrap(bytes);
+      try {
+         logger.trace("Adding {} bytes on currentMessage={}, this={}", dataBuffer.remaining(), currentMessage, this);
+         currentMessage.addBytes(dataBuffer);
+
+         if (!isPartial) {
+            final AMQPLargeMessage message = currentMessage;
+            message.releaseResources(serverReceiver.getConnection().isLargeMessageSync(), true);
+            logger.trace("finishing {} on {}", currentMessage, this);
+            // We don't want a close to delete the file now, we've released the resources.
+            currentMessage = null;
+            close();
+            serverReceiver.connection.runNow(() -> serverReceiver.onMessageComplete(delivery, message, message.getDeliveryAnnotations()));
+         }
+      } catch (Throwable e) {
+         logger.warn(e.getMessage(), e);
+         close();
+         serverReceiver.connection.exception(e);

Review Comment:
   The addBytes is not running on the connection thread so this call will end up operating on the proton bits outside the connection thread leading to possible inconsistent state when handling this.  



-- 
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: gitbox-unsubscribe@activemq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] ARTEMIS-4668 Moving AMQP Large Message file handling away from Netty thread [activemq-artemis]

Posted by "clebertsuconic (via GitHub)" <gi...@apache.org>.
clebertsuconic commented on code in PR #4840:
URL: https://github.com/apache/activemq-artemis/pull/4840#discussion_r1513290548


##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/AMQPLargeMessageWriter.java:
##########
@@ -144,21 +159,28 @@ private void resume() {
 
    private void tryDelivering() {
       // This is discounting some bytes due to Transfer payload
-      final int frameSize = protonSender.getSession().getConnection().getTransport().getOutboundFrameSizeLimit() - 50 - (delivery.getTag() != null ? delivery.getTag().length : 0);
 
       try {
+         if (this.delivery == null) {
+            this.delivery = serverSender.createDelivery(reference, (int) this.message.getMessageFormat());
+            if (sessionSPI.invokeOutgoing(message, (ActiveMQProtonRemotingConnection) sessionSPI.getTransportConnection().getProtocolConnection()) != null) {
+               return;
+            }

Review Comment:
   will work on it..



-- 
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: gitbox-unsubscribe@activemq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] ARTEMIS-4668 Moving AMQP Large Message file handling away from Netty thread [activemq-artemis]

Posted by "gemmellr (via GitHub)" <gi...@apache.org>.
gemmellr commented on code in PR #4840:
URL: https://github.com/apache/activemq-artemis/pull/4840#discussion_r1513095440


##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/AMQPConnectionContext.java:
##########
@@ -378,6 +389,16 @@ public AMQPConnectionCallback getConnectionCallback() {
       return connectionCallback;
    }
 
+   public void exception(Throwable e) {
+      logger.warn(e.getMessage(), e);
+      ErrorCondition error = new ErrorCondition();
+      error.setCondition(AmqpError.INTERNAL_ERROR);
+      error.setDescription("Unrecoverable error: " + (e.getMessage() == null ? e.getClass().getSimpleName() : e.getMessage()));
+      getHandler().getConnection().setCondition(error);
+      getHandler().getConnection().close();
+      flush();
+   }

Review Comment:
   This is being called from the 'session executor' so it isnt thread safe in its use of the connection, which should only be used on the connection thread.



##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/AMQPLargeMessageWriter.java:
##########
@@ -144,21 +159,28 @@ private void resume() {
 
    private void tryDelivering() {
       // This is discounting some bytes due to Transfer payload
-      final int frameSize = protonSender.getSession().getConnection().getTransport().getOutboundFrameSizeLimit() - 50 - (delivery.getTag() != null ? delivery.getTag().length : 0);
 
       try {
+         if (this.delivery == null) {
+            this.delivery = serverSender.createDelivery(reference, (int) this.message.getMessageFormat());
+            if (sessionSPI.invokeOutgoing(message, (ActiveMQProtonRemotingConnection) sessionSPI.getTransportConnection().getProtocolConnection()) != null) {
+               return;
+            }

Review Comment:
   This looks like it is creating a delivery, then potentially immediately deciding not to send it. Should that not be the other way around? EDIT: Like it was originally when the code lived in writeBytes.



##########
artemis-core-client/src/main/java/org/apache/activemq/artemis/core/remoting/impl/netty/NettyConnection.java:
##########
@@ -126,6 +126,9 @@ public final Channel getNettyChannel() {
    @Override
    public final void setAutoRead(boolean autoRead) {
       channel.config().setAutoRead(autoRead);
+      if (autoRead) {
+         channel.read();
+      }

Review Comment:
   I believe Netty does this itself when setting autoRead true, if it wasnt before. This here does it _every_ time, rather than just if it changed to true. Since I think its also possible it actually reads there and then (if on the correct thread), probably dont want to do it twice or more.  



##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/AMQPLargeMessageReader.java:
##########
@@ -77,39 +73,73 @@ public AMQPLargeMessageReader open() {
    }
 
    @Override
-   public Message readBytes(Delivery delivery) throws Exception {
+   public boolean readBytes(Delivery delivery) throws Exception {
       if (closed) {
          throw new IllegalStateException("AMQP Large Message Reader is closed and read cannot proceed");
       }
 
+      serverReceiver.connection.requireInHandler();
+
       final Receiver receiver = ((Receiver) delivery.getLink());
       final ReadableBuffer dataBuffer = receiver.recv();
 
+      final AMQPSessionCallback sessionSPI = serverReceiver.getSessionContext().getSessionSPI();
+
       if (currentMessage == null) {
-         final AMQPSessionCallback sessionSPI = serverReceiver.getSessionContext().getSessionSPI();
          final long id = sessionSPI.getStorageManager().generateID();
          currentMessage = new AMQPLargeMessage(id, delivery.getMessageFormat(), null,
                                                sessionSPI.getCoreMessageObjectPools(),
                                                sessionSPI.getStorageManager());
          currentMessage.parseHeader(dataBuffer);
-
+         logger.trace("Initializing current message {} on {}", currentMessage, this);
          sessionSPI.getStorageManager().largeMessageCreated(id, currentMessage);
+         sessionSPI.execute(() -> validateFile(currentMessage));
       }
 
-      currentMessage.addBytes(dataBuffer);
+      serverReceiver.getConnection().disableAutoRead();

Review Comment:
   If it fails after this point, but before its enabled in the other thread (so earlier comment about safety seem to apply) it could stall entirely. Dont see anything handling that.



##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/AMQPTunneledCoreMessageReader.java:
##########
@@ -155,6 +149,10 @@ public Message readBytes(Delivery delivery) {
       coreMessage.reloadPersistence(buffer, sessionSPI.getCoreMessageObjectPools());
       coreMessage.setMessageID(sessionSPI.getStorageManager().generateID());
 
-      return coreMessage;
+      serverReceiver.onMessageComplete(delivery, coreMessage, deliveryAnnotations);
+
+      close();

Review Comment:
   ..commented the same on another file before I got to this one :)



##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/AMQPLargeMessageReader.java:
##########
@@ -77,39 +73,73 @@ public AMQPLargeMessageReader open() {
    }
 
    @Override
-   public Message readBytes(Delivery delivery) throws Exception {
+   public boolean readBytes(Delivery delivery) throws Exception {
       if (closed) {
          throw new IllegalStateException("AMQP Large Message Reader is closed and read cannot proceed");
       }
 
+      serverReceiver.connection.requireInHandler();
+
       final Receiver receiver = ((Receiver) delivery.getLink());
       final ReadableBuffer dataBuffer = receiver.recv();
 
+      final AMQPSessionCallback sessionSPI = serverReceiver.getSessionContext().getSessionSPI();
+
       if (currentMessage == null) {
-         final AMQPSessionCallback sessionSPI = serverReceiver.getSessionContext().getSessionSPI();
          final long id = sessionSPI.getStorageManager().generateID();
          currentMessage = new AMQPLargeMessage(id, delivery.getMessageFormat(), null,
                                                sessionSPI.getCoreMessageObjectPools(),
                                                sessionSPI.getStorageManager());
          currentMessage.parseHeader(dataBuffer);
-
+         logger.trace("Initializing current message {} on {}", currentMessage, this);
          sessionSPI.getStorageManager().largeMessageCreated(id, currentMessage);
+         sessionSPI.execute(() -> validateFile(currentMessage));

Review Comment:
   I was about to comment on much the same thing (saw it looking at the code rather than the PR). Its also seems unsafe in its use of the reader if the validation should somehow fail.



##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/MessageReader.java:
##########
@@ -51,16 +49,7 @@ public interface MessageReader {
     *
     * @param delivery
     *    The delivery that has pending incoming bytes.
+    * @return true if the delivery was partial and more reads are required to complete the operation

Review Comment:
   Rest of the method javadoc needs updating too, its now totally wrong



##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/AMQPLargeMessageReader.java:
##########
@@ -77,39 +73,73 @@ public AMQPLargeMessageReader open() {
    }
 
    @Override
-   public Message readBytes(Delivery delivery) throws Exception {
+   public boolean readBytes(Delivery delivery) throws Exception {
       if (closed) {
          throw new IllegalStateException("AMQP Large Message Reader is closed and read cannot proceed");
       }
 
+      serverReceiver.connection.requireInHandler();
+
       final Receiver receiver = ((Receiver) delivery.getLink());
       final ReadableBuffer dataBuffer = receiver.recv();
 
+      final AMQPSessionCallback sessionSPI = serverReceiver.getSessionContext().getSessionSPI();
+
       if (currentMessage == null) {
-         final AMQPSessionCallback sessionSPI = serverReceiver.getSessionContext().getSessionSPI();
          final long id = sessionSPI.getStorageManager().generateID();
          currentMessage = new AMQPLargeMessage(id, delivery.getMessageFormat(), null,
                                                sessionSPI.getCoreMessageObjectPools(),
                                                sessionSPI.getStorageManager());
          currentMessage.parseHeader(dataBuffer);
-
+         logger.trace("Initializing current message {} on {}", currentMessage, this);
          sessionSPI.getStorageManager().largeMessageCreated(id, currentMessage);
+         sessionSPI.execute(() -> validateFile(currentMessage));
       }
 
-      currentMessage.addBytes(dataBuffer);
+      serverReceiver.getConnection().disableAutoRead();
 
-      final AMQPLargeMessage result;
+      byte[] bytes = new byte[dataBuffer.remaining()];
+      dataBuffer.get(bytes);
 
-      if (!delivery.isPartial()) {
-         currentMessage.releaseResources(serverReceiver.getConnection().isLargeMessageSync(), true);
-         result = currentMessage;
-         // We don't want a close to delete the file now, we've released the resources.
-         currentMessage = null;
-         deliveryAnnotations = result.getDeliveryAnnotations();
-      } else {
-         result = null;
+      boolean partial = delivery.isPartial();
+
+      sessionSPI.execute(() -> addBytes(delivery, bytes, partial));
+
+      return partial;

Review Comment:
   This seems weird. The bytes arent added yet, but the result is returned, which could be that its no longer partial, which is then acted upon and will treat the message as processed when it likely hasnt yet been.
   
   Also, just because auto-read is disabled, doesnt actually mean there arent more bytes that have already been read already and can still arrive from Netty (TLS can sometimes provoke that I believe). 
   
   There may also be more payload for a _different message_ in proton already, which could already have been given over and will be processed when this returns.
   
   EDIT: seems like the 'handler readable' toggle is trying to tackle that. Albeit its not thread safe per earlier comments.



##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/AMQPLargeMessageReader.java:
##########
@@ -77,39 +73,73 @@ public AMQPLargeMessageReader open() {
    }
 
    @Override
-   public Message readBytes(Delivery delivery) throws Exception {
+   public boolean readBytes(Delivery delivery) throws Exception {
       if (closed) {
          throw new IllegalStateException("AMQP Large Message Reader is closed and read cannot proceed");
       }
 
+      serverReceiver.connection.requireInHandler();
+
       final Receiver receiver = ((Receiver) delivery.getLink());
       final ReadableBuffer dataBuffer = receiver.recv();
 
+      final AMQPSessionCallback sessionSPI = serverReceiver.getSessionContext().getSessionSPI();
+
       if (currentMessage == null) {
-         final AMQPSessionCallback sessionSPI = serverReceiver.getSessionContext().getSessionSPI();
          final long id = sessionSPI.getStorageManager().generateID();
          currentMessage = new AMQPLargeMessage(id, delivery.getMessageFormat(), null,
                                                sessionSPI.getCoreMessageObjectPools(),
                                                sessionSPI.getStorageManager());
          currentMessage.parseHeader(dataBuffer);
-
+         logger.trace("Initializing current message {} on {}", currentMessage, this);
          sessionSPI.getStorageManager().largeMessageCreated(id, currentMessage);
+         sessionSPI.execute(() -> validateFile(currentMessage));
       }
 
-      currentMessage.addBytes(dataBuffer);
+      serverReceiver.getConnection().disableAutoRead();
 
-      final AMQPLargeMessage result;
+      byte[] bytes = new byte[dataBuffer.remaining()];
+      dataBuffer.get(bytes);
 
-      if (!delivery.isPartial()) {
-         currentMessage.releaseResources(serverReceiver.getConnection().isLargeMessageSync(), true);
-         result = currentMessage;
-         // We don't want a close to delete the file now, we've released the resources.
-         currentMessage = null;
-         deliveryAnnotations = result.getDeliveryAnnotations();
-      } else {
-         result = null;
+      boolean partial = delivery.isPartial();
+
+      sessionSPI.execute(() -> addBytes(delivery, bytes, partial));
+
+      return partial;
+   }
+
+   private void validateFile(AMQPLargeMessage message) {
+      try {
+         message.validateFile();
+      } catch (Exception e) {
+         logger.warn(e.getMessage(), e);
+         close();
+         serverReceiver.connection.exception(e);
       }
+   }
 
-      return result;
+   private void addBytes(Delivery delivery, byte[] bytes, boolean isPartial) {
+      ReadableBuffer dataBuffer = ReadableBuffer.ByteBufferReader.wrap(bytes);
+      try {
+         logger.trace("Adding {} bytes on currentMessage={}, this={}", dataBuffer.remaining(), currentMessage, this);
+         currentMessage.addBytes(dataBuffer);
+
+         if (!isPartial) {
+            final AMQPLargeMessage message = currentMessage;
+            message.releaseResources(serverReceiver.getConnection().isLargeMessageSync(), true);
+            logger.trace("finishing {} on {}", currentMessage, this);
+            // We don't want a close to delete the file now, we've released the resources.
+            currentMessage = null;
+            close();

Review Comment:
   Yep, I commented on the same before getting to this comment.



##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/AMQPTunneledCoreLargeMessageReader.java:
##########
@@ -209,13 +208,15 @@ public Message readBytes(Delivery delivery) throws Exception {
 
             state = State.DONE;
 
-            return result;
-         }
+            serverReceiver.onMessageComplete(delivery, result, deliveryAnnotations);
 
-         return null;
+            close();

Review Comment:
   Doesnt feel like a read should be closing itself. Was meant to be managed by the interested party using it.



##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/AMQPConnectionContext.java:
##########
@@ -94,6 +94,17 @@ public class AMQPConnectionContext extends ProtonInitializable implements EventH
 
    private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
 
+   public void disableAutoRead() {
+      connectionCallback.getTransportConnection().setAutoRead(false);
+      handler.setReadable(false);
+   }
+
+   public void enableAutoRead() {
+      connectionCallback.getTransportConnection().setAutoRead(true);
+      getHandler().setReadable(true);

Review Comment:
   seems likely to be non-thread-safe depending on how its used...i.e if its ever governed from 2 threads its clearly not safe as the 2 seperate toggles could overlap in a non-safe manner.



##########
artemis-server/src/main/java/org/apache/activemq/artemis/core/persistence/impl/journal/LargeServerMessageImpl.java:
##########
@@ -363,13 +363,17 @@ public String toString() {
    }
 
    @Override
-   public void validateFile() throws ActiveMQException {
+   public void validateFile() {
       this.ensureFileExists(true);
    }
 
-   public void ensureFileExists(boolean toOpen) throws ActiveMQException {
+   public void ensureFileExists(boolean toOpen) {
       synchronized (largeBody) {
-         largeBody.ensureFileExists(toOpen);
+         try {
+            largeBody.ensureFileExists(toOpen);
+         } catch (Exception e) {
+            logger.debug(e.getMessage(), e);
+         }

Review Comment:
   Seems odd to swallow this...so ensureFileExists() no longer ensures the file exists? Meaning validateFile() isnt validating much?



-- 
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: gitbox-unsubscribe@activemq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] ARTEMIS-4668 Moving AMQP Large Message file handling away from Netty thread [activemq-artemis]

Posted by "clebertsuconic (via GitHub)" <gi...@apache.org>.
clebertsuconic commented on code in PR #4840:
URL: https://github.com/apache/activemq-artemis/pull/4840#discussion_r1511921835


##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/AMQPBrokerConnection.java:
##########
@@ -847,6 +847,7 @@ public MessageWriter selectOutgoingMessageWriter(ProtonServerSenderContext sende
             if (message.isLargeMessage()) {
                selected = largeMessageWriter != null ? largeMessageWriter :
                   (largeMessageWriter = new AMQPLargeMessageWriter(sender));
+               largeMessageWriter.openContext(reference);

Review Comment:
   thing is.. it's always asynchronous... except to large messages... (since the previous interaction already you had the current large message returning something to make the handler return BUSY).
   
   So.. in all the other cases it should be treating through the acceptor.. except to this large message case here.



-- 
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: gitbox-unsubscribe@activemq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] ARTEMIS-4668 Moving AMQP Large Message file handling away from Netty thread [activemq-artemis]

Posted by "clebertsuconic (via GitHub)" <gi...@apache.org>.
clebertsuconic commented on code in PR #4840:
URL: https://github.com/apache/activemq-artemis/pull/4840#discussion_r1513065063


##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/AMQPLargeMessageReader.java:
##########
@@ -77,39 +73,73 @@ public AMQPLargeMessageReader open() {
    }
 
    @Override
-   public Message readBytes(Delivery delivery) throws Exception {
+   public boolean readBytes(Delivery delivery) throws Exception {
       if (closed) {
          throw new IllegalStateException("AMQP Large Message Reader is closed and read cannot proceed");
       }
 
+      serverReceiver.connection.requireInHandler();
+
       final Receiver receiver = ((Receiver) delivery.getLink());
       final ReadableBuffer dataBuffer = receiver.recv();
 
+      final AMQPSessionCallback sessionSPI = serverReceiver.getSessionContext().getSessionSPI();
+
       if (currentMessage == null) {
-         final AMQPSessionCallback sessionSPI = serverReceiver.getSessionContext().getSessionSPI();
          final long id = sessionSPI.getStorageManager().generateID();
          currentMessage = new AMQPLargeMessage(id, delivery.getMessageFormat(), null,
                                                sessionSPI.getCoreMessageObjectPools(),
                                                sessionSPI.getStorageManager());
          currentMessage.parseHeader(dataBuffer);
-
+         logger.trace("Initializing current message {} on {}", currentMessage, this);
          sessionSPI.getStorageManager().largeMessageCreated(id, currentMessage);
+         sessionSPI.execute(() -> validateFile(currentMessage));

Review Comment:
   It still needs to create the file. which should happen away from the Netty Thread. 
   
   in case of an exception it will initialize the exception on the session... The current writes are still going through though but the exception would be initialized. I could interrupt the addBytes if there was an exception, but the possibility of having an exception there is actually so low that if any issues are happening with the storage, other things will happen to put the server down anyways through IOExceptions.



-- 
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: gitbox-unsubscribe@activemq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] ARTEMIS-4668 Moving AMQP Large Message file handling away from Netty thread [activemq-artemis]

Posted by "clebertsuconic (via GitHub)" <gi...@apache.org>.
clebertsuconic commented on code in PR #4840:
URL: https://github.com/apache/activemq-artemis/pull/4840#discussion_r1514682247


##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/ProtonAbstractReceiver.java:
##########
@@ -320,28 +325,45 @@ public final void onMessage(Delivery delivery) throws ActiveMQAMQPException {
             return;
          }
 
-         final Message message = messageReader.readBytes(delivery);
+         {
+            Message completeMessage;
+            if ((completeMessage = messageReader.readBytes(delivery)) != null) {
+               // notice the AMQP Large Message Reader will always return false
+               // and call the onMessageComplete directly
+               // since that happens asynchronously
+               onMessageComplete(delivery, completeMessage, messageReader.getDeliveryAnnotations());
+            }
+         }
+      } catch (Exception e) {
+         logger.warn(e.getMessage(), e);
+         throw new ActiveMQAMQPInternalErrorException(e.getMessage(), e);
+      }
+   }
 
-         if (message != null) {
-            // Fetch this before the close of the reader as that will clear any read message
-            // delivery annotations.
-            final DeliveryAnnotations deliveryAnnotations = messageReader.getDeliveryAnnotations();
+   public void onMessageComplete(Delivery delivery,
+                          Message message, DeliveryAnnotations deliveryAnnotations) {
+      connection.requireInHandler();
 
-            this.messageReader.close();
-            this.messageReader = null;
+      try {
+         final Receiver receiver = ((Receiver) delivery.getLink());

Review Comment:
   @tabish121 I am just moving code from here:
   
   https://github.com/apache/activemq-artemis/blob/93a74dc00c5885c238e59e8b612aec47c657f541/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/ProtonAbstractReceiver.java#L305
   
   
   I assumed it was needed.. and I changed as minimal as possible.
   
   
   Should I use the receiver already stored instead?



-- 
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: gitbox-unsubscribe@activemq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] ARTEMIS-4668 Moving AMQP Large Message file handling away from Netty thread [activemq-artemis]

Posted by "clebertsuconic (via GitHub)" <gi...@apache.org>.
clebertsuconic commented on code in PR #4840:
URL: https://github.com/apache/activemq-artemis/pull/4840#discussion_r1514674021


##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/ProtonAbstractReceiver.java:
##########
@@ -320,28 +325,45 @@ public final void onMessage(Delivery delivery) throws ActiveMQAMQPException {
             return;
          }
 
-         final Message message = messageReader.readBytes(delivery);
+         {

Review Comment:
   I added it to make explicit only.. removing it.



-- 
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: gitbox-unsubscribe@activemq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] ARTEMIS-4668 Moving AMQP Large Message file handling away from Netty thread [activemq-artemis]

Posted by "clebertsuconic (via GitHub)" <gi...@apache.org>.
clebertsuconic commented on code in PR #4840:
URL: https://github.com/apache/activemq-artemis/pull/4840#discussion_r1511936965


##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/AMQPLargeMessageReader.java:
##########
@@ -77,39 +74,68 @@ public AMQPLargeMessageReader open() {
    }
 
    @Override
-   public Message readBytes(Delivery delivery) throws Exception {
+   public boolean readBytes(Delivery delivery) throws Exception {
       if (closed) {
          throw new IllegalStateException("AMQP Large Message Reader is closed and read cannot proceed");
       }
 
+      serverReceiver.connection.requireInHandler();
+
+      logger.trace("Reading {}", delivery);
+
       final Receiver receiver = ((Receiver) delivery.getLink());
       final ReadableBuffer dataBuffer = receiver.recv();
 
+      final AMQPSessionCallback sessionSPI = serverReceiver.getSessionContext().getSessionSPI();
+
       if (currentMessage == null) {
-         final AMQPSessionCallback sessionSPI = serverReceiver.getSessionContext().getSessionSPI();
          final long id = sessionSPI.getStorageManager().generateID();
-         currentMessage = new AMQPLargeMessage(id, delivery.getMessageFormat(), null,
-                                               sessionSPI.getCoreMessageObjectPools(),
-                                               sessionSPI.getStorageManager());
+         currentMessage = new AMQPLargeMessage(id, delivery.getMessageFormat(), null, sessionSPI.getCoreMessageObjectPools(), sessionSPI.getStorageManager());
          currentMessage.parseHeader(dataBuffer);
-
+         logger.trace("Initializing current message {} on {}", currentMessage, this);
          sessionSPI.getStorageManager().largeMessageCreated(id, currentMessage);
+         sessionSPI.execute(currentMessage::validateFile);

Review Comment:
   I was swallowing on purpose, as any subsequent reads would endue on an exception through addBytes calls (which is calling AMQPConnectionContext::exception
   
   
   I'm adding the validation now just in case (amended the commit)
   
   https://github.com/clebertsuconic/activemq-artemis/blob/1bae8665458e53a8c1793330147ee6b389e3b586/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/AMQPConnectionContext.java#L392-L400



-- 
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: gitbox-unsubscribe@activemq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] ARTEMIS-4668 Moving AMQP Large Message file handling away from Netty thread [activemq-artemis]

Posted by "clebertsuconic (via GitHub)" <gi...@apache.org>.
clebertsuconic commented on code in PR #4840:
URL: https://github.com/apache/activemq-artemis/pull/4840#discussion_r1511951178


##########
artemis-protocols/artemis-amqp-protocol/src/test/java/org/apache/activemq/artemis/protocol/amqp/proton/AMQPTunneledCoreLargeMessageReaderTest.java:
##########
@@ -159,19 +169,18 @@ public void testReadMessageByteByByteFromDeliveryBuffer() throws Exception {
          when(receiver.recv()).thenReturn(deliveryAnnotations.duplicate().position(i - 1).limit(i).slice());
 
          try {
-            assertNull(reader.readBytes(delivery));
+            readMessage = null;
+            reader.readBytes(delivery);
+            Assert.assertNull(readMessage);
          } catch (IllegalStateException e) {
             fail("Should not throw as the reader should be able to read just delivery annotations.");
          }
       }
 
-      assertNotNull(reader.getDeliveryAnnotations());
-
-      final DeliveryAnnotations annotations = reader.getDeliveryAnnotations();
-
-      assertTrue(annotations.getValue().get(Symbol.valueOf("a")).equals("a"));
-      assertTrue(annotations.getValue().get(Symbol.valueOf("b")).equals("b"));
-      assertTrue(annotations.getValue().get(Symbol.valueOf("c")).equals("c"));
+      DeliveryAnnotations currentAnnotations = reader.getDeliveryAnnotations();

Review Comment:
   The readMethod now calls close when there's no more pending. that is the onMessageComplete is called and the reader is closed. Meaning the test here would call getDeliveryAnnotations and it wouldn't be here.
   
   
   the only case where getDeliveryAnnotations will return anything is if the read it still partial now.



-- 
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: gitbox-unsubscribe@activemq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] ARTEMIS-4668 Moving AMQP Large Message file handling away from Netty thread [activemq-artemis]

Posted by "tabish121 (via GitHub)" <gi...@apache.org>.
tabish121 commented on code in PR #4840:
URL: https://github.com/apache/activemq-artemis/pull/4840#discussion_r1511948478


##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/AMQPBrokerConnection.java:
##########
@@ -847,6 +847,7 @@ public MessageWriter selectOutgoingMessageWriter(ProtonServerSenderContext sende
             if (message.isLargeMessage()) {
                selected = largeMessageWriter != null ? largeMessageWriter :
                   (largeMessageWriter = new AMQPLargeMessageWriter(sender));
+               largeMessageWriter.openContext(reference);

Review Comment:
   I've made the change here locally and I don't see any reason it doesn't work to just make the open method accept the message reference and do the initialization there as opposed to adding a second "special" open method the select of the writer is done in the same thread as the sender will be calling open prior to handling the write into the connection thread.  



-- 
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: gitbox-unsubscribe@activemq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] ARTEMIS-4668 Moving AMQP Large Message file handling away from Netty thread [activemq-artemis]

Posted by "clebertsuconic (via GitHub)" <gi...@apache.org>.
clebertsuconic commented on code in PR #4840:
URL: https://github.com/apache/activemq-artemis/pull/4840#discussion_r1513335419


##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/AMQPLargeMessageWriter.java:
##########
@@ -144,21 +159,28 @@ private void resume() {
 
    private void tryDelivering() {
       // This is discounting some bytes due to Transfer payload
-      final int frameSize = protonSender.getSession().getConnection().getTransport().getOutboundFrameSizeLimit() - 50 - (delivery.getTag() != null ? delivery.getTag().length : 0);
 
       try {
+         if (this.delivery == null) {
+            this.delivery = serverSender.createDelivery(reference, (int) this.message.getMessageFormat());
+            if (sessionSPI.invokeOutgoing(message, (ActiveMQProtonRemotingConnection) sessionSPI.getTransportConnection().getProtocolConnection()) != null) {
+               return;
+            }

Review Comment:
   commit to be squashed 55ce243a690a1dcae26587ea6e962effb896f039 is addressing this



-- 
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: gitbox-unsubscribe@activemq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] ARTEMIS-4668 Moving AMQP Large Message file handling away from Netty thread [activemq-artemis]

Posted by "clebertsuconic (via GitHub)" <gi...@apache.org>.
clebertsuconic commented on code in PR #4840:
URL: https://github.com/apache/activemq-artemis/pull/4840#discussion_r1514672036


##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/ProtonAbstractReceiver.java:
##########
@@ -320,28 +325,45 @@ public final void onMessage(Delivery delivery) throws ActiveMQAMQPException {
             return;
          }
 
-         final Message message = messageReader.readBytes(delivery);
+         {
+            Message completeMessage;
+            if ((completeMessage = messageReader.readBytes(delivery)) != null) {
+               // notice the AMQP Large Message Reader will always return false
+               // and call the onMessageComplete directly
+               // since that happens asynchronously

Review Comment:
   thanks



-- 
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: gitbox-unsubscribe@activemq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] ARTEMIS-4668 Moving AMQP Large Message file handling away from Netty thread [activemq-artemis]

Posted by "gemmellr (via GitHub)" <gi...@apache.org>.
gemmellr commented on code in PR #4840:
URL: https://github.com/apache/activemq-artemis/pull/4840#discussion_r1514790848


##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/AMQPLargeMessageReader.java:
##########
@@ -50,14 +50,15 @@ public DeliveryAnnotations getDeliveryAnnotations() {
    @Override
    public void close() {
       if (!closed) {
-         if (currentMessage != null) {
-            try {
-               currentMessage.deleteFile();
-            } catch (Throwable error) {
-               ActiveMQServerLogger.LOGGER.errorDeletingLargeMessageFile(error);
-            } finally {
-               currentMessage = null;
+         try {
+            AMQPLargeMessage localCurrentMessage = currentMessage;

Review Comment:
   > There was actually only addBytes... as the initialization should stay as is.
   > 
   > I amended the PR with the addBytes change.
   
   readBytes also reads (and writes) the variable multiple times.



-- 
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: gitbox-unsubscribe@activemq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] ARTEMIS-4668 Moving AMQP Large Message file handling away from Netty thread [activemq-artemis]

Posted by "clebertsuconic (via GitHub)" <gi...@apache.org>.
clebertsuconic commented on code in PR #4840:
URL: https://github.com/apache/activemq-artemis/pull/4840#discussion_r1513291190


##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/AMQPTunneledCoreLargeMessageReader.java:
##########
@@ -209,13 +208,15 @@ public Message readBytes(Delivery delivery) throws Exception {
 
             state = State.DONE;
 
-            return result;
-         }
+            serverReceiver.onMessageComplete(delivery, result, deliveryAnnotations);
 
-         return null;
+            close();

Review Comment:
   to be squashed commit handling it.



-- 
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: gitbox-unsubscribe@activemq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] ARTEMIS-4668 Moving AMQP Large Message file handling away from Netty thread [activemq-artemis]

Posted by "tabish121 (via GitHub)" <gi...@apache.org>.
tabish121 commented on code in PR #4840:
URL: https://github.com/apache/activemq-artemis/pull/4840#discussion_r1513206030


##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/AMQPLargeMessageWriter.java:
##########
@@ -81,33 +84,54 @@ public boolean isWriting() {
    public void close() {
       if (!closed) {
          try {
+            try {
+               if (largeBodyReader != null) {
+                  largeBodyReader.close();
+               }
+            } catch (Exception e) {
+               // if we get an error only at this point, there's nothing else we could do other than log.warn
+               logger.warn("{}", e.getMessage(), e);
+            }
             if (message != null) {
                message.usageDown();
             }
          } finally {
-            reset(true);
+            resetClosed();
          }
       }
    }
 
    @Override
-   public AMQPLargeMessageWriter open() {
-      if (!closed) {
-         throw new IllegalStateException("Trying to open an AMQP Large Message writer that was not closed");
+   public AMQPLargeMessageWriter open(MessageReference reference) {
+      this.reference = reference;
+      this.message = (AMQPLargeMessage) reference.getMessage();

Review Comment:
   This is storing the message which means a call to close() will call usageDown on the reference but the way the code is now structured there isn't a 100% certainly that usageUp will always be called on the reference.  It needs to be called here likely so that a close will indeed not accidentally lower usage when it hadn't been increased. 



-- 
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: gitbox-unsubscribe@activemq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] ARTEMIS-4668 Moving AMQP Large Message file handling away from Netty thread [activemq-artemis]

Posted by "clebertsuconic (via GitHub)" <gi...@apache.org>.
clebertsuconic commented on code in PR #4840:
URL: https://github.com/apache/activemq-artemis/pull/4840#discussion_r1513284233


##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/AMQPLargeMessageReader.java:
##########
@@ -77,39 +73,73 @@ public AMQPLargeMessageReader open() {
    }
 
    @Override
-   public Message readBytes(Delivery delivery) throws Exception {
+   public boolean readBytes(Delivery delivery) throws Exception {
       if (closed) {
          throw new IllegalStateException("AMQP Large Message Reader is closed and read cannot proceed");
       }
 
+      serverReceiver.connection.requireInHandler();
+
       final Receiver receiver = ((Receiver) delivery.getLink());
       final ReadableBuffer dataBuffer = receiver.recv();
 
+      final AMQPSessionCallback sessionSPI = serverReceiver.getSessionContext().getSessionSPI();
+
       if (currentMessage == null) {
-         final AMQPSessionCallback sessionSPI = serverReceiver.getSessionContext().getSessionSPI();
          final long id = sessionSPI.getStorageManager().generateID();
          currentMessage = new AMQPLargeMessage(id, delivery.getMessageFormat(), null,
                                                sessionSPI.getCoreMessageObjectPools(),
                                                sessionSPI.getStorageManager());
          currentMessage.parseHeader(dataBuffer);
-
+         logger.trace("Initializing current message {} on {}", currentMessage, this);
          sessionSPI.getStorageManager().largeMessageCreated(id, currentMessage);
+         sessionSPI.execute(() -> validateFile(currentMessage));
       }
 
-      currentMessage.addBytes(dataBuffer);
+      serverReceiver.getConnection().disableAutoRead();
 
-      final AMQPLargeMessage result;
+      byte[] bytes = new byte[dataBuffer.remaining()];
+      dataBuffer.get(bytes);
 
-      if (!delivery.isPartial()) {
-         currentMessage.releaseResources(serverReceiver.getConnection().isLargeMessageSync(), true);
-         result = currentMessage;
-         // We don't want a close to delete the file now, we've released the resources.
-         currentMessage = null;
-         deliveryAnnotations = result.getDeliveryAnnotations();
-      } else {
-         result = null;
+      boolean partial = delivery.isPartial();
+
+      sessionSPI.execute(() -> addBytes(delivery, bytes, partial));
+
+      return partial;
+   }
+
+   private void validateFile(AMQPLargeMessage message) {
+      try {
+         message.validateFile();
+      } catch (Exception e) {
+         logger.warn(e.getMessage(), e);
+         close();
+         serverReceiver.connection.exception(e);
       }
+   }
 
-      return result;
+   private void addBytes(Delivery delivery, byte[] bytes, boolean isPartial) {
+      ReadableBuffer dataBuffer = ReadableBuffer.ByteBufferReader.wrap(bytes);
+      try {
+         logger.trace("Adding {} bytes on currentMessage={}, this={}", dataBuffer.remaining(), currentMessage, this);
+         currentMessage.addBytes(dataBuffer);
+
+         if (!isPartial) {
+            final AMQPLargeMessage message = currentMessage;
+            message.releaseResources(serverReceiver.getConnection().isLargeMessageSync(), true);
+            logger.trace("finishing {} on {}", currentMessage, this);
+            // We don't want a close to delete the file now, we've released the resources.
+            currentMessage = null;
+            close();

Review Comment:
   I added a commit to be squashed addressing this



##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/AMQPLargeMessageReader.java:
##########
@@ -77,39 +73,73 @@ public AMQPLargeMessageReader open() {
    }
 
    @Override
-   public Message readBytes(Delivery delivery) throws Exception {
+   public boolean readBytes(Delivery delivery) throws Exception {
       if (closed) {
          throw new IllegalStateException("AMQP Large Message Reader is closed and read cannot proceed");
       }
 
+      serverReceiver.connection.requireInHandler();
+
       final Receiver receiver = ((Receiver) delivery.getLink());
       final ReadableBuffer dataBuffer = receiver.recv();
 
+      final AMQPSessionCallback sessionSPI = serverReceiver.getSessionContext().getSessionSPI();
+
       if (currentMessage == null) {
-         final AMQPSessionCallback sessionSPI = serverReceiver.getSessionContext().getSessionSPI();
          final long id = sessionSPI.getStorageManager().generateID();
          currentMessage = new AMQPLargeMessage(id, delivery.getMessageFormat(), null,
                                                sessionSPI.getCoreMessageObjectPools(),
                                                sessionSPI.getStorageManager());
          currentMessage.parseHeader(dataBuffer);
-
+         logger.trace("Initializing current message {} on {}", currentMessage, this);
          sessionSPI.getStorageManager().largeMessageCreated(id, currentMessage);
+         sessionSPI.execute(() -> validateFile(currentMessage));

Review Comment:
   I added a commit to be squashed addressing this



-- 
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: gitbox-unsubscribe@activemq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] ARTEMIS-4668 Moving AMQP Large Message file handling away from Netty thread [activemq-artemis]

Posted by "clebertsuconic (via GitHub)" <gi...@apache.org>.
clebertsuconic commented on code in PR #4840:
URL: https://github.com/apache/activemq-artemis/pull/4840#discussion_r1511938204


##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/AMQPLargeMessageReader.java:
##########
@@ -77,39 +74,68 @@ public AMQPLargeMessageReader open() {
    }
 
    @Override
-   public Message readBytes(Delivery delivery) throws Exception {
+   public boolean readBytes(Delivery delivery) throws Exception {
       if (closed) {
          throw new IllegalStateException("AMQP Large Message Reader is closed and read cannot proceed");
       }
 
+      serverReceiver.connection.requireInHandler();
+
+      logger.trace("Reading {}", delivery);
+
       final Receiver receiver = ((Receiver) delivery.getLink());
       final ReadableBuffer dataBuffer = receiver.recv();
 
+      final AMQPSessionCallback sessionSPI = serverReceiver.getSessionContext().getSessionSPI();
+
       if (currentMessage == null) {
-         final AMQPSessionCallback sessionSPI = serverReceiver.getSessionContext().getSessionSPI();
          final long id = sessionSPI.getStorageManager().generateID();
-         currentMessage = new AMQPLargeMessage(id, delivery.getMessageFormat(), null,
-                                               sessionSPI.getCoreMessageObjectPools(),
-                                               sessionSPI.getStorageManager());
+         currentMessage = new AMQPLargeMessage(id, delivery.getMessageFormat(), null, sessionSPI.getCoreMessageObjectPools(), sessionSPI.getStorageManager());
          currentMessage.parseHeader(dataBuffer);
-
+         logger.trace("Initializing current message {} on {}", currentMessage, this);
          sessionSPI.getStorageManager().largeMessageCreated(id, currentMessage);
+         sessionSPI.execute(currentMessage::validateFile);
       }
 
-      currentMessage.addBytes(dataBuffer);
+      logger.trace("Disable autoread on {}", this);

Review Comment:
   @tabish121 I had a toString() method added when I was debugging this... let me think about it tomorrow if I should add the toString or remove this trace.



-- 
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: gitbox-unsubscribe@activemq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] ARTEMIS-4668 Moving AMQP Large Message file handling away from Netty thread [activemq-artemis]

Posted by "clebertsuconic (via GitHub)" <gi...@apache.org>.
clebertsuconic commented on code in PR #4840:
URL: https://github.com/apache/activemq-artemis/pull/4840#discussion_r1512017452


##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/AMQPBrokerConnection.java:
##########
@@ -847,6 +847,7 @@ public MessageWriter selectOutgoingMessageWriter(ProtonServerSenderContext sende
             if (message.isLargeMessage()) {
                selected = largeMessageWriter != null ? largeMessageWriter :
                   (largeMessageWriter = new AMQPLargeMessageWriter(sender));
+               largeMessageWriter.openContext(reference);

Review Comment:
   I made the change.
   
   I had done some back and forth changes and I couldn't see it now.. thanks for the review.



-- 
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: gitbox-unsubscribe@activemq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] ARTEMIS-4668 Moving AMQP Large Message file handling away from Netty thread [activemq-artemis]

Posted by "clebertsuconic (via GitHub)" <gi...@apache.org>.
clebertsuconic commented on code in PR #4840:
URL: https://github.com/apache/activemq-artemis/pull/4840#discussion_r1514687780


##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/handler/ProtonHandler.java:
##########
@@ -213,9 +224,7 @@ public boolean isHandler() {
    }
 
    public void requireHandler() {
-      if (!workerExecutor.inEventLoop()) {
-         throw new IllegalStateException("this method requires to be called within the handler, use the executor");
-      }
+      assert workerExecutor.inEventLoop();

Review Comment:
   I placed it back...



-- 
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: gitbox-unsubscribe@activemq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] ARTEMIS-4668 Moving AMQP Large Message file handling away from Netty thread [activemq-artemis]

Posted by "tabish121 (via GitHub)" <gi...@apache.org>.
tabish121 commented on code in PR #4840:
URL: https://github.com/apache/activemq-artemis/pull/4840#discussion_r1513034841


##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/AMQPLargeMessageReader.java:
##########
@@ -77,39 +73,73 @@ public AMQPLargeMessageReader open() {
    }
 
    @Override
-   public Message readBytes(Delivery delivery) throws Exception {
+   public boolean readBytes(Delivery delivery) throws Exception {
       if (closed) {
          throw new IllegalStateException("AMQP Large Message Reader is closed and read cannot proceed");
       }
 
+      serverReceiver.connection.requireInHandler();
+
       final Receiver receiver = ((Receiver) delivery.getLink());
       final ReadableBuffer dataBuffer = receiver.recv();
 
+      final AMQPSessionCallback sessionSPI = serverReceiver.getSessionContext().getSessionSPI();
+
       if (currentMessage == null) {
-         final AMQPSessionCallback sessionSPI = serverReceiver.getSessionContext().getSessionSPI();
          final long id = sessionSPI.getStorageManager().generateID();
          currentMessage = new AMQPLargeMessage(id, delivery.getMessageFormat(), null,
                                                sessionSPI.getCoreMessageObjectPools(),
                                                sessionSPI.getStorageManager());
          currentMessage.parseHeader(dataBuffer);
-
+         logger.trace("Initializing current message {} on {}", currentMessage, this);
          sessionSPI.getStorageManager().largeMessageCreated(id, currentMessage);
+         sessionSPI.execute(() -> validateFile(currentMessage));
       }
 
-      currentMessage.addBytes(dataBuffer);
+      serverReceiver.getConnection().disableAutoRead();
 
-      final AMQPLargeMessage result;
+      byte[] bytes = new byte[dataBuffer.remaining()];
+      dataBuffer.get(bytes);
 
-      if (!delivery.isPartial()) {
-         currentMessage.releaseResources(serverReceiver.getConnection().isLargeMessageSync(), true);
-         result = currentMessage;
-         // We don't want a close to delete the file now, we've released the resources.
-         currentMessage = null;
-         deliveryAnnotations = result.getDeliveryAnnotations();
-      } else {
-         result = null;
+      boolean partial = delivery.isPartial();
+
+      sessionSPI.execute(() -> addBytes(delivery, bytes, partial));
+
+      return partial;
+   }
+
+   private void validateFile(AMQPLargeMessage message) {
+      try {
+         message.validateFile();
+      } catch (Exception e) {
+         logger.warn(e.getMessage(), e);
+         close();
+         serverReceiver.connection.exception(e);
       }
+   }
 
-      return result;
+   private void addBytes(Delivery delivery, byte[] bytes, boolean isPartial) {
+      ReadableBuffer dataBuffer = ReadableBuffer.ByteBufferReader.wrap(bytes);
+      try {
+         logger.trace("Adding {} bytes on currentMessage={}, this={}", dataBuffer.remaining(), currentMessage, this);
+         currentMessage.addBytes(dataBuffer);
+
+         if (!isPartial) {
+            final AMQPLargeMessage message = currentMessage;
+            message.releaseResources(serverReceiver.getConnection().isLargeMessageSync(), true);
+            logger.trace("finishing {} on {}", currentMessage, this);
+            // We don't want a close to delete the file now, we've released the resources.
+            currentMessage = null;
+            close();

Review Comment:
   This seems to be incorrect as closing here is wrong as a general rule as the resource that opened this should be closing it but also this is occurring in a different thread than the connection thread where the reader was opened which could lead to error on inconsistent state.  The receiver should be closing this in the connection thread.  



-- 
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: gitbox-unsubscribe@activemq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] ARTEMIS-4668 Moving AMQP Large Message file handling away from Netty thread [activemq-artemis]

Posted by "tabish121 (via GitHub)" <gi...@apache.org>.
tabish121 commented on code in PR #4840:
URL: https://github.com/apache/activemq-artemis/pull/4840#discussion_r1513036542


##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/AMQPLargeMessageReader.java:
##########
@@ -77,39 +73,73 @@ public AMQPLargeMessageReader open() {
    }
 
    @Override
-   public Message readBytes(Delivery delivery) throws Exception {
+   public boolean readBytes(Delivery delivery) throws Exception {
       if (closed) {
          throw new IllegalStateException("AMQP Large Message Reader is closed and read cannot proceed");
       }
 
+      serverReceiver.connection.requireInHandler();
+
       final Receiver receiver = ((Receiver) delivery.getLink());
       final ReadableBuffer dataBuffer = receiver.recv();
 
+      final AMQPSessionCallback sessionSPI = serverReceiver.getSessionContext().getSessionSPI();
+
       if (currentMessage == null) {
-         final AMQPSessionCallback sessionSPI = serverReceiver.getSessionContext().getSessionSPI();
          final long id = sessionSPI.getStorageManager().generateID();
          currentMessage = new AMQPLargeMessage(id, delivery.getMessageFormat(), null,
                                                sessionSPI.getCoreMessageObjectPools(),
                                                sessionSPI.getStorageManager());
          currentMessage.parseHeader(dataBuffer);
-
+         logger.trace("Initializing current message {} on {}", currentMessage, this);
          sessionSPI.getStorageManager().largeMessageCreated(id, currentMessage);
+         sessionSPI.execute(() -> validateFile(currentMessage));

Review Comment:
   This check seems to be mostly pointless now as the code doesn't use a continuation to resume delivery after validation it just throws it into another thread and then continues on as if it was successful  



##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/AMQPTunneledCoreMessageReader.java:
##########
@@ -155,6 +149,10 @@ public Message readBytes(Delivery delivery) {
       coreMessage.reloadPersistence(buffer, sessionSPI.getCoreMessageObjectPools());
       coreMessage.setMessageID(sessionSPI.getStorageManager().generateID());
 
-      return coreMessage;
+      serverReceiver.onMessageComplete(delivery, coreMessage, deliveryAnnotations);
+
+      close();

Review Comment:
   The receiver should be closing this in onMessageComplete, a reader shouldn't close itself. 



-- 
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: gitbox-unsubscribe@activemq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] ARTEMIS-4668 Moving AMQP Large Message file handling away from Netty thread [activemq-artemis]

Posted by "tabish121 (via GitHub)" <gi...@apache.org>.
tabish121 commented on code in PR #4840:
URL: https://github.com/apache/activemq-artemis/pull/4840#discussion_r1513082939


##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/AMQPTunneledCoreMessageReader.java:
##########
@@ -155,6 +149,10 @@ public Message readBytes(Delivery delivery) {
       coreMessage.reloadPersistence(buffer, sessionSPI.getCoreMessageObjectPools());
       coreMessage.setMessageID(sessionSPI.getStorageManager().generateID());
 
-      return coreMessage;
+      serverReceiver.onMessageComplete(delivery, coreMessage, deliveryAnnotations);
+
+      close();

Review Comment:
   I'm saying its wrong and I'm saying its thread unsafe.  And if I need to I'm saying -1 until I'm sure it is



-- 
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: gitbox-unsubscribe@activemq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] ARTEMIS-4668 Moving AMQP Large Message file handling away from Netty thread [activemq-artemis]

Posted by "clebertsuconic (via GitHub)" <gi...@apache.org>.
clebertsuconic commented on PR #4840:
URL: https://github.com/apache/activemq-artemis/pull/4840#issuecomment-1979453938

   I'm thinking on reverting some of the readBytes(Delivery) methods and only call the onMessageComplete for the Large Message case.
   
   will be working on it now.


-- 
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: gitbox-unsubscribe@activemq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] ARTEMIS-4668 Moving AMQP Large Message file handling away from Netty thread [activemq-artemis]

Posted by "clebertsuconic (via GitHub)" <gi...@apache.org>.
clebertsuconic commented on code in PR #4840:
URL: https://github.com/apache/activemq-artemis/pull/4840#discussion_r1514863816


##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/AMQPLargeMessageReader.java:
##########
@@ -50,14 +50,15 @@ public DeliveryAnnotations getDeliveryAnnotations() {
    @Override
    public void close() {
       if (!closed) {
-         if (currentMessage != null) {
-            try {
-               currentMessage.deleteFile();
-            } catch (Throwable error) {
-               ActiveMQServerLogger.LOGGER.errorDeletingLargeMessageFile(error);
-            } finally {
-               currentMessage = null;
+         try {
+            AMQPLargeMessage localCurrentMessage = currentMessage;

Review Comment:
   not any more after my push -f



-- 
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: gitbox-unsubscribe@activemq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] ARTEMIS-4668 Moving AMQP Large Message file handling away from Netty thread [activemq-artemis]

Posted by "clebertsuconic (via GitHub)" <gi...@apache.org>.
clebertsuconic commented on code in PR #4840:
URL: https://github.com/apache/activemq-artemis/pull/4840#discussion_r1513265169


##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/AMQPLargeMessageReader.java:
##########
@@ -77,39 +73,73 @@ public AMQPLargeMessageReader open() {
    }
 
    @Override
-   public Message readBytes(Delivery delivery) throws Exception {
+   public boolean readBytes(Delivery delivery) throws Exception {
       if (closed) {
          throw new IllegalStateException("AMQP Large Message Reader is closed and read cannot proceed");
       }
 
+      serverReceiver.connection.requireInHandler();
+
       final Receiver receiver = ((Receiver) delivery.getLink());
       final ReadableBuffer dataBuffer = receiver.recv();
 
+      final AMQPSessionCallback sessionSPI = serverReceiver.getSessionContext().getSessionSPI();
+
       if (currentMessage == null) {
-         final AMQPSessionCallback sessionSPI = serverReceiver.getSessionContext().getSessionSPI();
          final long id = sessionSPI.getStorageManager().generateID();
          currentMessage = new AMQPLargeMessage(id, delivery.getMessageFormat(), null,
                                                sessionSPI.getCoreMessageObjectPools(),
                                                sessionSPI.getStorageManager());
          currentMessage.parseHeader(dataBuffer);
-
+         logger.trace("Initializing current message {} on {}", currentMessage, this);
          sessionSPI.getStorageManager().largeMessageCreated(id, currentMessage);
+         sessionSPI.execute(() -> validateFile(currentMessage));
       }
 
-      currentMessage.addBytes(dataBuffer);
+      serverReceiver.getConnection().disableAutoRead();
 
-      final AMQPLargeMessage result;
+      byte[] bytes = new byte[dataBuffer.remaining()];
+      dataBuffer.get(bytes);
 
-      if (!delivery.isPartial()) {
-         currentMessage.releaseResources(serverReceiver.getConnection().isLargeMessageSync(), true);
-         result = currentMessage;
-         // We don't want a close to delete the file now, we've released the resources.
-         currentMessage = null;
-         deliveryAnnotations = result.getDeliveryAnnotations();
-      } else {
-         result = null;
+      boolean partial = delivery.isPartial();
+
+      sessionSPI.execute(() -> addBytes(delivery, bytes, partial));
+
+      return partial;

Review Comment:
   the handler.readable is trying to handle that.. yes..
   
   
   I'm calling the enable from the Netty thread on my next push.
   
   I'm working on it now.



-- 
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: gitbox-unsubscribe@activemq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] ARTEMIS-4668 Moving AMQP Large Message file handling away from Netty thread [activemq-artemis]

Posted by "tabish121 (via GitHub)" <gi...@apache.org>.
tabish121 commented on code in PR #4840:
URL: https://github.com/apache/activemq-artemis/pull/4840#discussion_r1511892522


##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/federation/AMQPFederationBaseSenderController.java:
##########
@@ -89,6 +89,7 @@ public MessageWriter selectOutgoingMessageWriter(ProtonServerSenderContext sende
          if (message.isLargeMessage()) {
             selected = largeMessageWriter != null ? largeMessageWriter :
                (largeMessageWriter = new AMQPLargeMessageWriter(sender));
+            largeMessageWriter.openContext(reference);

Review Comment:
   Ditto, extending the open likely makes more sense if eventually all these large message handlers are going to need this on having different opens called in different places is confusing.



##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/AMQPLargeMessageReader.java:
##########
@@ -77,39 +74,68 @@ public AMQPLargeMessageReader open() {
    }
 
    @Override
-   public Message readBytes(Delivery delivery) throws Exception {
+   public boolean readBytes(Delivery delivery) throws Exception {
       if (closed) {
          throw new IllegalStateException("AMQP Large Message Reader is closed and read cannot proceed");
       }
 
+      serverReceiver.connection.requireInHandler();
+
+      logger.trace("Reading {}", delivery);
+
       final Receiver receiver = ((Receiver) delivery.getLink());
       final ReadableBuffer dataBuffer = receiver.recv();
 
+      final AMQPSessionCallback sessionSPI = serverReceiver.getSessionContext().getSessionSPI();
+
       if (currentMessage == null) {
-         final AMQPSessionCallback sessionSPI = serverReceiver.getSessionContext().getSessionSPI();
          final long id = sessionSPI.getStorageManager().generateID();
-         currentMessage = new AMQPLargeMessage(id, delivery.getMessageFormat(), null,
-                                               sessionSPI.getCoreMessageObjectPools(),
-                                               sessionSPI.getStorageManager());
+         currentMessage = new AMQPLargeMessage(id, delivery.getMessageFormat(), null, sessionSPI.getCoreMessageObjectPools(), sessionSPI.getStorageManager());
          currentMessage.parseHeader(dataBuffer);
-
+         logger.trace("Initializing current message {} on {}", currentMessage, this);
          sessionSPI.getStorageManager().largeMessageCreated(id, currentMessage);
+         sessionSPI.execute(currentMessage::validateFile);

Review Comment:
   This is now happening in a different thread, how is the error propagated to the remote peer now that it is gone, and the method is swallowing all errors?



##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/AMQPLargeMessageReader.java:
##########
@@ -77,39 +74,68 @@ public AMQPLargeMessageReader open() {
    }
 
    @Override
-   public Message readBytes(Delivery delivery) throws Exception {
+   public boolean readBytes(Delivery delivery) throws Exception {
       if (closed) {
          throw new IllegalStateException("AMQP Large Message Reader is closed and read cannot proceed");
       }
 
+      serverReceiver.connection.requireInHandler();
+
+      logger.trace("Reading {}", delivery);
+
       final Receiver receiver = ((Receiver) delivery.getLink());
       final ReadableBuffer dataBuffer = receiver.recv();
 
+      final AMQPSessionCallback sessionSPI = serverReceiver.getSessionContext().getSessionSPI();
+
       if (currentMessage == null) {
-         final AMQPSessionCallback sessionSPI = serverReceiver.getSessionContext().getSessionSPI();
          final long id = sessionSPI.getStorageManager().generateID();
-         currentMessage = new AMQPLargeMessage(id, delivery.getMessageFormat(), null,
-                                               sessionSPI.getCoreMessageObjectPools(),
-                                               sessionSPI.getStorageManager());
+         currentMessage = new AMQPLargeMessage(id, delivery.getMessageFormat(), null, sessionSPI.getCoreMessageObjectPools(), sessionSPI.getStorageManager());
          currentMessage.parseHeader(dataBuffer);
-
+         logger.trace("Initializing current message {} on {}", currentMessage, this);
          sessionSPI.getStorageManager().largeMessageCreated(id, currentMessage);
+         sessionSPI.execute(currentMessage::validateFile);
       }
 
-      currentMessage.addBytes(dataBuffer);
+      logger.trace("Disable autoread on {}", this);

Review Comment:
   There is no toString in this type, logging this just logs the object reference value, not super helpful for logging.  Also if you need to log this doesn't it make more sense to have the disable and enable methods log it, along with connection id data.  



##########
artemis-protocols/artemis-amqp-protocol/src/test/java/org/apache/activemq/artemis/protocol/amqp/proton/AMQPTunneledCoreLargeMessageReaderTest.java:
##########
@@ -238,18 +247,18 @@ public void testReadMessageInByteChunksFromDeliveryBuffer() throws Exception {
       when(receiver.recv()).thenReturn(deliveryAnnotations.duplicate());
 
       try {
-         assertNull(reader.readBytes(delivery));
+         readMessage = null;
+         reader.readBytes(delivery);
+         Assert.assertNull(readMessage);
       } catch (IllegalStateException e) {
          fail("Should not throw as the reader should be able to read just delivery annotations.");
       }
 
-      assertNotNull(reader.getDeliveryAnnotations());
+      DeliveryAnnotations currentDeliveryAnnotations = reader.getDeliveryAnnotations();

Review Comment:
   Change appears unnecessary



##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/AMQPLargeMessageReader.java:
##########
@@ -77,39 +74,68 @@ public AMQPLargeMessageReader open() {
    }
 
    @Override
-   public Message readBytes(Delivery delivery) throws Exception {
+   public boolean readBytes(Delivery delivery) throws Exception {
       if (closed) {
          throw new IllegalStateException("AMQP Large Message Reader is closed and read cannot proceed");
       }
 
+      serverReceiver.connection.requireInHandler();
+
+      logger.trace("Reading {}", delivery);
+
       final Receiver receiver = ((Receiver) delivery.getLink());
       final ReadableBuffer dataBuffer = receiver.recv();
 
+      final AMQPSessionCallback sessionSPI = serverReceiver.getSessionContext().getSessionSPI();
+
       if (currentMessage == null) {
-         final AMQPSessionCallback sessionSPI = serverReceiver.getSessionContext().getSessionSPI();
          final long id = sessionSPI.getStorageManager().generateID();
-         currentMessage = new AMQPLargeMessage(id, delivery.getMessageFormat(), null,
-                                               sessionSPI.getCoreMessageObjectPools(),
-                                               sessionSPI.getStorageManager());
+         currentMessage = new AMQPLargeMessage(id, delivery.getMessageFormat(), null, sessionSPI.getCoreMessageObjectPools(), sessionSPI.getStorageManager());
          currentMessage.parseHeader(dataBuffer);
-
+         logger.trace("Initializing current message {} on {}", currentMessage, this);
          sessionSPI.getStorageManager().largeMessageCreated(id, currentMessage);
+         sessionSPI.execute(currentMessage::validateFile);
       }
 
-      currentMessage.addBytes(dataBuffer);
+      logger.trace("Disable autoread on {}", this);
+      serverReceiver.getConnection().disableAutoRead();
 
-      final AMQPLargeMessage result;
+      byte[] bytes = new byte[dataBuffer.remaining()];
+      dataBuffer.get(bytes);
+      logger.trace("Getting {}", bytes.length);
 
-      if (!delivery.isPartial()) {
-         currentMessage.releaseResources(serverReceiver.getConnection().isLargeMessageSync(), true);
-         result = currentMessage;
-         // We don't want a close to delete the file now, we've released the resources.
-         currentMessage = null;
-         deliveryAnnotations = result.getDeliveryAnnotations();
-      } else {
-         result = null;
-      }
+      boolean partial = delivery.isPartial();
+
+      sessionSPI.execute(() -> addBytes(delivery, bytes, partial));
+
+      return partial;
+   }
 
-      return result;
+   private void addBytes(Delivery delivery, byte[] bytes, boolean isPartial) {
+      ReadableBuffer dataBuffer = ReadableBuffer.ByteBufferReader.wrap(bytes);
+      try {
+         logger.trace("Adding {} bytes on currentMessage={}, this={}", dataBuffer.remaining(), currentMessage, this);
+         currentMessage.addBytes(dataBuffer);
+
+         if (!isPartial) {
+            final AMQPLargeMessage message = currentMessage;
+            message.releaseResources(serverReceiver.getConnection().isLargeMessageSync(), true);
+            logger.trace("finishing {} on {}", currentMessage, this);
+            // We don't want a close to delete the file now, we've released the resources.
+            currentMessage = null;
+            close();
+            serverReceiver.connection.runNow(() -> {
+               DeliveryAnnotations deliveryAnnotations = message.getDeliveryAnnotations();
+               serverReceiver.onMessageComplete(delivery, message, deliveryAnnotations);
+            });
+         }
+      } catch (Throwable e) {
+         logger.warn(e.getMessage(), e);
+         close();
+         serverReceiver.connection.exception(e);
+      } finally {
+         serverReceiver.getConnection().enableAutoRead();
+      }
    }
+

Review Comment:
   Unneeded added newline



##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/AMQPTunneledCoreMessageReader.java:
##########
@@ -155,6 +150,10 @@ public Message readBytes(Delivery delivery) {
       coreMessage.reloadPersistence(buffer, sessionSPI.getCoreMessageObjectPools());
       coreMessage.setMessageID(sessionSPI.getStorageManager().generateID());
 
-      return coreMessage;
+      serverReceiver.onMessageComplete(delivery, coreMessage, deliveryAnnotations);
+
+      close();
+
+      return delivery.isPartial();

Review Comment:
   You already check isPartial above, can just return false. 



##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/AMQPLargeMessageReader.java:
##########
@@ -77,39 +74,68 @@ public AMQPLargeMessageReader open() {
    }
 
    @Override
-   public Message readBytes(Delivery delivery) throws Exception {
+   public boolean readBytes(Delivery delivery) throws Exception {
       if (closed) {
          throw new IllegalStateException("AMQP Large Message Reader is closed and read cannot proceed");
       }
 
+      serverReceiver.connection.requireInHandler();
+
+      logger.trace("Reading {}", delivery);
+
       final Receiver receiver = ((Receiver) delivery.getLink());
       final ReadableBuffer dataBuffer = receiver.recv();
 
+      final AMQPSessionCallback sessionSPI = serverReceiver.getSessionContext().getSessionSPI();
+
       if (currentMessage == null) {
-         final AMQPSessionCallback sessionSPI = serverReceiver.getSessionContext().getSessionSPI();
          final long id = sessionSPI.getStorageManager().generateID();
-         currentMessage = new AMQPLargeMessage(id, delivery.getMessageFormat(), null,
-                                               sessionSPI.getCoreMessageObjectPools(),
-                                               sessionSPI.getStorageManager());
+         currentMessage = new AMQPLargeMessage(id, delivery.getMessageFormat(), null, sessionSPI.getCoreMessageObjectPools(), sessionSPI.getStorageManager());
          currentMessage.parseHeader(dataBuffer);
-
+         logger.trace("Initializing current message {} on {}", currentMessage, this);
          sessionSPI.getStorageManager().largeMessageCreated(id, currentMessage);
+         sessionSPI.execute(currentMessage::validateFile);
       }
 
-      currentMessage.addBytes(dataBuffer);
+      logger.trace("Disable autoread on {}", this);
+      serverReceiver.getConnection().disableAutoRead();
 
-      final AMQPLargeMessage result;
+      byte[] bytes = new byte[dataBuffer.remaining()];
+      dataBuffer.get(bytes);
+      logger.trace("Getting {}", bytes.length);

Review Comment:
   In debugging such value log messages don't help to orient you on what is going on



##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/AMQPLargeMessageReader.java:
##########
@@ -77,39 +74,68 @@ public AMQPLargeMessageReader open() {
    }
 
    @Override
-   public Message readBytes(Delivery delivery) throws Exception {
+   public boolean readBytes(Delivery delivery) throws Exception {
       if (closed) {
          throw new IllegalStateException("AMQP Large Message Reader is closed and read cannot proceed");
       }
 
+      serverReceiver.connection.requireInHandler();
+
+      logger.trace("Reading {}", delivery);
+
       final Receiver receiver = ((Receiver) delivery.getLink());
       final ReadableBuffer dataBuffer = receiver.recv();
 
+      final AMQPSessionCallback sessionSPI = serverReceiver.getSessionContext().getSessionSPI();
+
       if (currentMessage == null) {
-         final AMQPSessionCallback sessionSPI = serverReceiver.getSessionContext().getSessionSPI();
          final long id = sessionSPI.getStorageManager().generateID();
-         currentMessage = new AMQPLargeMessage(id, delivery.getMessageFormat(), null,
-                                               sessionSPI.getCoreMessageObjectPools(),
-                                               sessionSPI.getStorageManager());
+         currentMessage = new AMQPLargeMessage(id, delivery.getMessageFormat(), null, sessionSPI.getCoreMessageObjectPools(), sessionSPI.getStorageManager());

Review Comment:
   Unneeded reformatting.



##########
artemis-protocols/artemis-amqp-protocol/src/test/java/org/apache/activemq/artemis/protocol/amqp/proton/AMQPTunneledCoreLargeMessageReaderTest.java:
##########
@@ -159,19 +169,18 @@ public void testReadMessageByteByByteFromDeliveryBuffer() throws Exception {
          when(receiver.recv()).thenReturn(deliveryAnnotations.duplicate().position(i - 1).limit(i).slice());
 
          try {
-            assertNull(reader.readBytes(delivery));
+            readMessage = null;
+            reader.readBytes(delivery);
+            Assert.assertNull(readMessage);
          } catch (IllegalStateException e) {
             fail("Should not throw as the reader should be able to read just delivery annotations.");
          }
       }
 
-      assertNotNull(reader.getDeliveryAnnotations());
-
-      final DeliveryAnnotations annotations = reader.getDeliveryAnnotations();
-
-      assertTrue(annotations.getValue().get(Symbol.valueOf("a")).equals("a"));
-      assertTrue(annotations.getValue().get(Symbol.valueOf("b")).equals("b"));
-      assertTrue(annotations.getValue().get(Symbol.valueOf("c")).equals("c"));
+      DeliveryAnnotations currentAnnotations = reader.getDeliveryAnnotations();

Review Comment:
   Change appears unnecessary 



##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/AMQPBrokerConnection.java:
##########
@@ -847,6 +847,7 @@ public MessageWriter selectOutgoingMessageWriter(ProtonServerSenderContext sende
             if (message.isLargeMessage()) {
                selected = largeMessageWriter != null ? largeMessageWriter :
                   (largeMessageWriter = new AMQPLargeMessageWriter(sender));
+               largeMessageWriter.openContext(reference);

Review Comment:
   It likely makes more sense to extend the open API in the MessageWriter to take the message reference instead of adding yet another open method that is confusing, and not documented as other methods in the API where.  



##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/DefaultSenderController.java:
##########
@@ -453,6 +453,7 @@ public MessageWriter selectOutgoingMessageWriter(ProtonServerSenderContext sende
 
       if (reference.getMessage() instanceof AMQPLargeMessage) {
          selected = largeMessageWriter;
+         largeMessageWriter.openContext(reference);

Review Comment:
   Replace with updated open API



##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/AMQPConnectionContext.java:
##########
@@ -94,6 +94,18 @@ public class AMQPConnectionContext extends ProtonInitializable implements EventH
 
    private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
 
+   public void disableAutoRead() {
+      connectionCallback.getTransportConnection().setAutoRead(false);
+      handler.setReadable(false);
+   }
+
+   public void enableAutoRead() {
+      connectionCallback.getTransportConnection().setAutoRead(true);
+      getHandler().setReadable(true);
+      flush();
+   }
+
+

Review Comment:
   Unneeded extra newline.



##########
artemis-protocols/artemis-amqp-protocol/src/test/java/org/apache/activemq/artemis/protocol/amqp/proton/AMQPTunneledCoreLargeMessageReaderTest.java:
##########
@@ -116,21 +128,19 @@ public void testReadDeliveryAnnotationsFromDeliveryBuffer() throws Exception {
 
       reader.open();
 
-      assertNull(reader.getDeliveryAnnotations());
-
       try {
+         readMessage = null;
          reader.readBytes(delivery);
+         Assert.assertNull(readMessage); // should not been called yet
       } catch (IllegalStateException e) {
          fail("Should not throw as the reader should be able to read just delivery annotations.");
       }
 
-      assertNotNull(reader.getDeliveryAnnotations());
-
-      final DeliveryAnnotations annotations = reader.getDeliveryAnnotations();
-
-      assertTrue(annotations.getValue().get(Symbol.valueOf("a")).equals("a"));
-      assertTrue(annotations.getValue().get(Symbol.valueOf("b")).equals("b"));
-      assertTrue(annotations.getValue().get(Symbol.valueOf("c")).equals("c"));
+      // peek current delivery annotations
+      DeliveryAnnotations currentAnnotations = reader.getDeliveryAnnotations();

Review Comment:
   Change appears unnecessary 



##########
tests/artemis-test-support/src/main/java/org/apache/activemq/transport/amqp/client/AmqpMessage.java:
##########
@@ -640,6 +640,11 @@ public String getText() throws NoSuchElementException {
       throw new NoSuchElementException("Message does not contain a String body");
    }
 
+   public byte[] getBytes() {
+      Data body = (Data)getWrappedMessage().getBody();
+      return body.getValue().getArray();

Review Comment:
   The byte array contained within is not guaranteed to be zero indexed or span the full array, you need to check and account for that if you are going to expose it.



##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/AMQPLargeMessageReader.java:
##########
@@ -77,39 +74,68 @@ public AMQPLargeMessageReader open() {
    }
 
    @Override
-   public Message readBytes(Delivery delivery) throws Exception {
+   public boolean readBytes(Delivery delivery) throws Exception {
       if (closed) {
          throw new IllegalStateException("AMQP Large Message Reader is closed and read cannot proceed");
       }
 
+      serverReceiver.connection.requireInHandler();
+
+      logger.trace("Reading {}", delivery);
+
       final Receiver receiver = ((Receiver) delivery.getLink());
       final ReadableBuffer dataBuffer = receiver.recv();
 
+      final AMQPSessionCallback sessionSPI = serverReceiver.getSessionContext().getSessionSPI();
+
       if (currentMessage == null) {
-         final AMQPSessionCallback sessionSPI = serverReceiver.getSessionContext().getSessionSPI();
          final long id = sessionSPI.getStorageManager().generateID();
-         currentMessage = new AMQPLargeMessage(id, delivery.getMessageFormat(), null,
-                                               sessionSPI.getCoreMessageObjectPools(),
-                                               sessionSPI.getStorageManager());
+         currentMessage = new AMQPLargeMessage(id, delivery.getMessageFormat(), null, sessionSPI.getCoreMessageObjectPools(), sessionSPI.getStorageManager());
          currentMessage.parseHeader(dataBuffer);
-
+         logger.trace("Initializing current message {} on {}", currentMessage, this);
          sessionSPI.getStorageManager().largeMessageCreated(id, currentMessage);
+         sessionSPI.execute(currentMessage::validateFile);
       }
 
-      currentMessage.addBytes(dataBuffer);
+      logger.trace("Disable autoread on {}", this);
+      serverReceiver.getConnection().disableAutoRead();
 
-      final AMQPLargeMessage result;
+      byte[] bytes = new byte[dataBuffer.remaining()];
+      dataBuffer.get(bytes);
+      logger.trace("Getting {}", bytes.length);
 
-      if (!delivery.isPartial()) {
-         currentMessage.releaseResources(serverReceiver.getConnection().isLargeMessageSync(), true);
-         result = currentMessage;
-         // We don't want a close to delete the file now, we've released the resources.
-         currentMessage = null;
-         deliveryAnnotations = result.getDeliveryAnnotations();
-      } else {
-         result = null;
-      }
+      boolean partial = delivery.isPartial();
+
+      sessionSPI.execute(() -> addBytes(delivery, bytes, partial));
+
+      return partial;
+   }
 
-      return result;
+   private void addBytes(Delivery delivery, byte[] bytes, boolean isPartial) {
+      ReadableBuffer dataBuffer = ReadableBuffer.ByteBufferReader.wrap(bytes);
+      try {
+         logger.trace("Adding {} bytes on currentMessage={}, this={}", dataBuffer.remaining(), currentMessage, this);
+         currentMessage.addBytes(dataBuffer);
+
+         if (!isPartial) {
+            final AMQPLargeMessage message = currentMessage;
+            message.releaseResources(serverReceiver.getConnection().isLargeMessageSync(), true);
+            logger.trace("finishing {} on {}", currentMessage, this);
+            // We don't want a close to delete the file now, we've released the resources.
+            currentMessage = null;
+            close();
+            serverReceiver.connection.runNow(() -> {
+               DeliveryAnnotations deliveryAnnotations = message.getDeliveryAnnotations();

Review Comment:
   Can skip temp var creation and just call message.getDeliveryAnnotations directly.



##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/AMQPLargeMessageWriter.java:
##########
@@ -81,11 +84,20 @@ public boolean isWriting() {
    public void close() {
       if (!closed) {
          try {
+            try {
+               if (largeBodyReader != null) {
+                  largeBodyReader.close();
+               }
+               largeBodyReader = null;

Review Comment:
   Should be nulled in a finally block, which would happen in the resetClosed method called in the main finally block if you added a null there for the large body reader as you should.



##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/AMQPLargeMessageWriter.java:
##########
@@ -59,6 +59,9 @@ public class AMQPLargeMessageWriter implements MessageWriter {
 
    private MessageReference reference;
    private AMQPLargeMessage message;
+
+   LargeBodyReader largeBodyReader;

Review Comment:
   Can be made private from looking though the code, if not an accessor that checks opened state is likely a better idea



##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/AMQPLargeMessageWriter.java:
##########
@@ -96,18 +108,36 @@ public AMQPLargeMessageWriter open() {
          throw new IllegalStateException("Trying to open an AMQP Large Message writer that was not closed");
       }
 
-      reset(false);
+      resetOpen();
 
       return this;
    }
 
-   private void reset(boolean closedState) {
+   public void openContext(MessageReference reference) {
+      this.reference = reference;
+      this.message = (AMQPLargeMessage) reference.getMessage();
+
+      try {
+         largeBodyReader = message.getLargeBodyReader();
+         largeBodyReader.open();
+      } catch (Exception e) {
+         serverSender.reportDeliveryError(this, reference, e);
+      }
+   }
+
+   private void resetClosed() {

Review Comment:
   added member large body reader is no null as it should be here



##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/AMQPLargeMessageReader.java:
##########
@@ -77,39 +74,68 @@ public AMQPLargeMessageReader open() {
    }
 
    @Override
-   public Message readBytes(Delivery delivery) throws Exception {
+   public boolean readBytes(Delivery delivery) throws Exception {
       if (closed) {
          throw new IllegalStateException("AMQP Large Message Reader is closed and read cannot proceed");
       }
 
+      serverReceiver.connection.requireInHandler();
+
+      logger.trace("Reading {}", delivery);

Review Comment:
   This trace isn't likely to add much useful context just more log noise



-- 
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: gitbox-unsubscribe@activemq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] ARTEMIS-4668 Moving AMQP Large Message file handling away from Netty thread [activemq-artemis]

Posted by "clebertsuconic (via GitHub)" <gi...@apache.org>.
clebertsuconic commented on code in PR #4840:
URL: https://github.com/apache/activemq-artemis/pull/4840#discussion_r1511926376


##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/AMQPBrokerConnection.java:
##########
@@ -847,6 +847,7 @@ public MessageWriter selectOutgoingMessageWriter(ProtonServerSenderContext sende
             if (message.isLargeMessage()) {
                selected = largeMessageWriter != null ? largeMessageWriter :
                   (largeMessageWriter = new AMQPLargeMessageWriter(sender));
+               largeMessageWriter.openContext(reference);

Review Comment:
   this method only makes sense in the context of the AMQP Large Message. It doesn't make sense in any other writers (not yet at least).  
   
   I've tried to make them all use this method, but the other writers were writing it asynchronously as I mentioned earlier.



-- 
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: gitbox-unsubscribe@activemq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] ARTEMIS-4668 Moving AMQP Large Message file handling away from Netty thread [activemq-artemis]

Posted by "clebertsuconic (via GitHub)" <gi...@apache.org>.
clebertsuconic commented on code in PR #4840:
URL: https://github.com/apache/activemq-artemis/pull/4840#discussion_r1514670185


##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/AMQPLargeMessageWriter.java:
##########
@@ -81,33 +84,55 @@ public boolean isWriting() {
    public void close() {
       if (!closed) {
          try {
+            try {
+               if (largeBodyReader != null) {
+                  largeBodyReader.close();
+               }
+            } catch (Exception e) {
+               // if we get an error only at this point, there's nothing else we could do other than log.warn
+               logger.warn("{}", e.getMessage(), e);
+            }
             if (message != null) {
                message.usageDown();
             }
          } finally {
-            reset(true);
+            resetClosed();
          }
       }
    }
 
    @Override
-   public AMQPLargeMessageWriter open() {
-      if (!closed) {
-         throw new IllegalStateException("Trying to open an AMQP Large Message writer that was not closed");

Review Comment:
   it was an honest mistake.. I didn't mean to.. will place it back.



-- 
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: gitbox-unsubscribe@activemq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] ARTEMIS-4668 Moving AMQP Large Message file handling away from Netty thread [activemq-artemis]

Posted by "clebertsuconic (via GitHub)" <gi...@apache.org>.
clebertsuconic commented on PR #4840:
URL: https://github.com/apache/activemq-artemis/pull/4840#issuecomment-1979744561

   @tabish121 , @gemmellr  I tried simplifying the changes as much as possible here...


-- 
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: gitbox-unsubscribe@activemq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] ARTEMIS-4668 Moving AMQP Large Message file handling away from Netty thread [activemq-artemis]

Posted by "gemmellr (via GitHub)" <gi...@apache.org>.
gemmellr commented on code in PR #4840:
URL: https://github.com/apache/activemq-artemis/pull/4840#discussion_r1514629712


##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/ProtonAbstractReceiver.java:
##########
@@ -320,28 +325,45 @@ public final void onMessage(Delivery delivery) throws ActiveMQAMQPException {
             return;
          }
 
-         final Message message = messageReader.readBytes(delivery);
+         {

Review Comment:
   Needed? Its already inside a small try block where this is the only actual work.



##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/AMQPLargeMessageWriter.java:
##########
@@ -81,33 +84,55 @@ public boolean isWriting() {
    public void close() {
       if (!closed) {
          try {
+            try {
+               if (largeBodyReader != null) {
+                  largeBodyReader.close();
+               }
+            } catch (Exception e) {
+               // if we get an error only at this point, there's nothing else we could do other than log.warn
+               logger.warn("{}", e.getMessage(), e);
+            }
             if (message != null) {
                message.usageDown();
             }
          } finally {
-            reset(true);
+            resetClosed();
          }
       }
    }
 
    @Override
-   public AMQPLargeMessageWriter open() {
-      if (!closed) {
-         throw new IllegalStateException("Trying to open an AMQP Large Message writer that was not closed");

Review Comment:
   Is it now allowed to duplicate-open a writer? Doesnt seem like it should be given what it does.



##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/ProtonAbstractReceiver.java:
##########
@@ -320,28 +325,45 @@ public final void onMessage(Delivery delivery) throws ActiveMQAMQPException {
             return;
          }
 
-         final Message message = messageReader.readBytes(delivery);
+         {
+            Message completeMessage;
+            if ((completeMessage = messageReader.readBytes(delivery)) != null) {
+               // notice the AMQP Large Message Reader will always return false
+               // and call the onMessageComplete directly
+               // since that happens asynchronously

Review Comment:
   return null?



##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/handler/ProtonHandler.java:
##########
@@ -213,9 +224,7 @@ public boolean isHandler() {
    }
 
    public void requireHandler() {
-      if (!workerExecutor.inEventLoop()) {
-         throw new IllegalStateException("this method requires to be called within the handler, use the executor");
-      }
+      assert workerExecutor.inEventLoop();

Review Comment:
   Is the trade-off worth it? If it goes wrong in live usage then previously we would always find out. Now we probably wont, except any weird consequences of illegal use.
   
   Also feels like a separate change either way.



-- 
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: gitbox-unsubscribe@activemq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] ARTEMIS-4668 Moving AMQP Large Message file handling away from Netty thread [activemq-artemis]

Posted by "clebertsuconic (via GitHub)" <gi...@apache.org>.
clebertsuconic commented on code in PR #4840:
URL: https://github.com/apache/activemq-artemis/pull/4840#discussion_r1513076021


##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/AMQPTunneledCoreMessageReader.java:
##########
@@ -155,6 +149,10 @@ public Message readBytes(Delivery delivery) {
       coreMessage.reloadPersistence(buffer, sessionSPI.getCoreMessageObjectPools());
       coreMessage.setMessageID(sessionSPI.getStorageManager().generateID());
 
-      return coreMessage;
+      serverReceiver.onMessageComplete(delivery, coreMessage, deliveryAnnotations);
+
+      close();

Review Comment:
   I changed the contract there. so the thing is closing itself before calling the onComplete.
   
   I was going to use that to release the resources.. but it's not needed any more.



-- 
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: gitbox-unsubscribe@activemq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] ARTEMIS-4668 Moving AMQP Large Message file handling away from Netty thread [activemq-artemis]

Posted by "clebertsuconic (via GitHub)" <gi...@apache.org>.
clebertsuconic commented on code in PR #4840:
URL: https://github.com/apache/activemq-artemis/pull/4840#discussion_r1513286921


##########
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/AMQPConnectionContext.java:
##########
@@ -94,6 +94,17 @@ public class AMQPConnectionContext extends ProtonInitializable implements EventH
 
    private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
 
+   public void disableAutoRead() {
+      connectionCallback.getTransportConnection().setAutoRead(false);
+      handler.setReadable(false);
+   }
+
+   public void enableAutoRead() {
+      connectionCallback.getTransportConnection().setAutoRead(true);
+      getHandler().setReadable(true);

Review Comment:
   enableAutoRead is now called from the Netty Thread with a runNow call 
   
   
   this was going to call flush which would transfer the execution anyways.
   
   
   my commit to be squashed is addressing this



-- 
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: gitbox-unsubscribe@activemq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org