gaohoward commented on a change in pull request #2859: ARTEMIS-2513 Large 
message's copy may be interfered by other threads
URL: https://github.com/apache/activemq-artemis/pull/2859#discussion_r333093509
 
 

 ##########
 File path: 
artemis-server/src/main/java/org/apache/activemq/artemis/core/persistence/impl/journal/LargeServerMessageImpl.java
 ##########
 @@ -368,52 +368,48 @@ public Message copy(final long newID) {
       try {
          LargeServerMessage newMessage = 
storageManager.createLargeMessage(newID, this);
 
-         boolean originallyOpen = file != null && file.isOpen();
+         //clone a SequentialFile to avoid concurrent access
+         ensureFileExists(false);
+         SequentialFile cloneFile = file.cloneFile();
 
-         validateFile();
-
-         byte[] bufferBytes = new byte[100 * 1024];
-
-         ByteBuffer buffer = ByteBuffer.wrap(bufferBytes);
+         try {
+            byte[] bufferBytes = new byte[100 * 1024];
 
-         long oldPosition = file.position();
+            ByteBuffer buffer = ByteBuffer.wrap(bufferBytes);
 
-         if (!file.isOpen()) {
-            file.open();
-         }
-         file.position(0);
-
-         for (;;) {
-            // The buffer is reused...
-            // We need to make sure we clear the limits and the buffer before 
reusing it
-            buffer.clear();
-            int bytesRead = file.read(buffer);
-
-            byte[] bufferToWrite;
-            if (bytesRead <= 0) {
-               break;
-            } else if (bytesRead == bufferBytes.length && 
!this.storageManager.isReplicated()) {
-               // ARTEMIS-1220: We cannot reuse the same buffer if it's 
replicated
-               // otherwise there could be another thread still using the 
buffer on a
-               // replication.
-               bufferToWrite = bufferBytes;
-            } else {
-               bufferToWrite = new byte[bytesRead];
-               System.arraycopy(bufferBytes, 0, bufferToWrite, 0, bytesRead);
+            if (!cloneFile.isOpen()) {
+               cloneFile.open();
             }
 
-            newMessage.addBytes(bufferToWrite);
-
-            if (bytesRead < bufferBytes.length) {
-               break;
+            cloneFile.position(0);
+
+            for (;;) {
+               // The buffer is reused...
+               // We need to make sure we clear the limits and the buffer 
before reusing it
+               buffer.clear();
+               int bytesRead = cloneFile.read(buffer);
+
+               byte[] bufferToWrite;
+               if (bytesRead <= 0) {
+                  break;
+               } else if (bytesRead == bufferBytes.length && 
!this.storageManager.isReplicated()) {
+                  // ARTEMIS-1220: We cannot reuse the same buffer if it's 
replicated
+                  // otherwise there could be another thread still using the 
buffer on a
+                  // replication.
+                  bufferToWrite = bufferBytes;
+               } else {
+                  bufferToWrite = new byte[bytesRead];
+                  System.arraycopy(bufferBytes, 0, bufferToWrite, 0, 
bytesRead);
+               }
+
+               newMessage.addBytes(bufferToWrite);
+
+               if (bytesRead < bufferBytes.length) {
+                  break;
+               }
             }
-         }
-
-         file.position(oldPosition);
-
-         if (!originallyOpen) {
-            file.close(false);
-            newMessage.getFile().close();
+         } finally {
+            cloneFile.close();
          }
 
 Review comment:
   yes it should be closed as original code. That'll cause file leak.

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to