vongosling closed pull request #227: [ROCKETMQ-332] fix concurrent bug in 
MappedFileQueue#findMappedFileByOffset, which m…
URL: https://github.com/apache/rocketmq/pull/227
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/store/src/main/java/org/apache/rocketmq/store/MappedFileQueue.java 
b/store/src/main/java/org/apache/rocketmq/store/MappedFileQueue.java
index 9eb3b3ab0..74ca4bcc2 100644
--- a/store/src/main/java/org/apache/rocketmq/store/MappedFileQueue.java
+++ b/store/src/main/java/org/apache/rocketmq/store/MappedFileQueue.java
@@ -460,36 +460,84 @@ public boolean commit(final int commitLeastPages) {
      * @return Mapped file or null (when not found and returnFirstOnNotFound 
is <code>false</code>).
      */
     public MappedFile findMappedFileByOffset(final long offset, final boolean 
returnFirstOnNotFound) {
-        try {
-            MappedFile mappedFile = this.getFirstMappedFile();
-            if (mappedFile != null) {
-                int index = (int) ((offset / this.mappedFileSize) - 
(mappedFile.getFileFromOffset() / this.mappedFileSize));
-                if (index < 0 || index >= this.mappedFiles.size()) {
-                    LOG_ERROR.warn("Offset for {} not matched. Request offset: 
{}, index: {}, " +
-                            "mappedFileSize: {}, mappedFiles count: {}",
-                        mappedFile,
-                        offset,
-                        index,
-                        this.mappedFileSize,
-                        this.mappedFiles.size());
-                }
+        for (int i = 0; i < 3; i++) {
+            try {
+                MappedFile firstMappedFile = this.getFirstMappedFile();
+                MappedFile targetMappedFile = null;
+                if (firstMappedFile != null) {
+                    if (firstMappedFile.getFileFromOffset() > offset) {
+                        return returnFirstOnNotFound ? firstMappedFile : null;
+                    }
+                    int index = (int) ((offset / this.mappedFileSize) - 
(firstMappedFile.getFileFromOffset() / this.mappedFileSize));
+                    long mfSize = this.mappedFiles.size();
+                    if (index < 0 || index >= mfSize) {
+                        LOG_ERROR.warn("Offset for {} not matched. Request 
offset: {}, index: {}, " +
+                                        "mappedFileSize: {}, mappedFiles 
count: {}",
+                                firstMappedFile,
+                                offset,
+                                index,
+                                this.mappedFileSize,
+                                mfSize);
+                    } else {
+                        try {
+                            targetMappedFile = this.mappedFiles.get(index);
+                        } catch (IndexOutOfBoundsException e) {
+                            //IGNORE.
+                        } catch (Exception e) {
+                            LOG_ERROR.warn("findMappedFileByOffset failure. 
offset={}" + e.getMessage(), offset);
+                        }
+                        //find the right MappedFile
+                        if (targetMappedFile != null && offset >= 
targetMappedFile.getFileFromOffset()
+                                && offset < 
targetMappedFile.getFileFromOffset() + this.mappedFileSize) {
+                            return targetMappedFile;
+                        }
+                    }
 
-                try {
-                    return this.mappedFiles.get(index);
-                } catch (Exception e) {
-                    if (returnFirstOnNotFound) {
-                        return mappedFile;
+                    MappedFile newFirstMappedFile = getFirstMappedFile();
+                    if (firstMappedFile != newFirstMappedFile) {
+                        //retry
+                        log.info("firstMappedFiles changed during 
findMappedFileByOffset. DO RETRY. OLD:{},NEW:{}," +
+                                        
"offset={}.index={},mfSize={},curMFSize={}",
+                                firstMappedFile, newFirstMappedFile, offset, 
index, mfSize, this.mappedFiles.size());
+                    } else { //no mapped files deleted.
+                        if (targetMappedFile != null) {
+                            // should not happen, do a retry.
+                            LOG_ERROR.error("[NOTIFY ME] 
findMappedFileByOffset failed, offset not match. target:{}, offset:{}",
+                                    targetMappedFile, offset);
+                        } else {
+                            return returnFirstOnNotFound ? firstMappedFile : 
null;
+                        }
                     }
-                    LOG_ERROR.warn("findMappedFileByOffset failure. ", e);
+                } else {
+                    //mappedFiles is empty
+                    return null;
                 }
+            } catch (Exception e) {
+                LOG_ERROR.error("findMappedFileByOffset Exception, offset={}", 
offset, e);
+                return null;
             }
-        } catch (Exception e) {
-            log.error("findMappedFileByOffset Exception", e);
         }
+        return findMappedFileByOffsetWithIteration(offset, 
returnFirstOnNotFound);
+    }
 
-        return null;
+    public MappedFile findMappedFileByOffsetWithIteration(final long offset, 
final boolean returnFirstOnNotFound) {
+        MappedFile targetMappedFile = null;
+        for (MappedFile tmpMappedFile : mappedFiles) {
+            if (offset >= tmpMappedFile.getFileFromOffset()
+                    && offset < tmpMappedFile.getFileFromOffset() + 
this.mappedFileSize) {
+                targetMappedFile = tmpMappedFile;
+                break;
+            }
+        }
+        log.warn("[NOTIFY ME] findMappedFileByOffsetWithIteration. result={}, 
offset={}", targetMappedFile, offset);
+        if (targetMappedFile != null) {
+            return targetMappedFile;
+        } else {
+            return returnFirstOnNotFound ? this.getFirstMappedFile() : null;
+        }
     }
 
+
     public MappedFile getFirstMappedFile() {
         MappedFile mappedFileFirst = null;
 
diff --git 
a/store/src/test/java/org/apache/rocketmq/store/MappedFileQueueTest.java 
b/store/src/test/java/org/apache/rocketmq/store/MappedFileQueueTest.java
index 92f1876b2..74d9a7450 100644
--- a/store/src/test/java/org/apache/rocketmq/store/MappedFileQueueTest.java
+++ b/store/src/test/java/org/apache/rocketmq/store/MappedFileQueueTest.java
@@ -20,7 +20,11 @@
 import java.io.File;
 import java.nio.ByteBuffer;
 import java.util.Arrays;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicLong;
+
 import org.apache.rocketmq.common.UtilAll;
+import org.assertj.core.util.Lists;
 import org.junit.After;
 import org.junit.Test;
 
@@ -229,6 +233,92 @@ public void testDeleteExpiredFileByTime() throws Exception 
{
         assertThat(mappedFileQueue.getMappedFiles().size()).isEqualTo(45);
     }
 
+    @Test
+    public void testFindMappedFileByOffsetConcurrently() throws Exception {
+        final byte[] data = new byte[256];
+        final long testDataSize = data.length * 400000;
+        final MappedFileQueue mappedFileQueue =
+                new MappedFileQueue("target/unit_test_store/g", 1024, null);
+        final AtomicLong offset = new AtomicLong(0);
+        final AtomicBoolean running = new AtomicBoolean(true);
+        Thread ct = new Thread(new Runnable() {
+            @Override
+            public void run() {
+                while (offset.get() < testDataSize) {
+                    if (mappedFileQueue.getMappedFiles().size() < 50) {
+                        MappedFile mappedFile = 
mappedFileQueue.getLastMappedFile(offset.get());
+                        assertThat(mappedFile).isNotNull();
+                        assertThat(mappedFile.appendMessage(data)).isTrue();
+                        mappedFile.commit(4);
+                        offset.addAndGet(data.length);
+                    }
+                }
+                try {
+                    Thread.sleep(5000);
+                } catch (InterruptedException e) {
+                    e.printStackTrace();
+                }
+                running.set(false);
+            }
+        });
+        ct.start();
+
+        final AtomicLong readOffsetAL = new AtomicLong(0);
+        Thread dt = new Thread(new Runnable() {
+            @Override
+            public void run() {
+                while (running.get()) {
+                    if (mappedFileQueue.getMappedFiles().size() > 30) {
+                        MappedFile mf = mappedFileQueue.getFirstMappedFile();
+                        assertThat(mf).isNotNull();
+                        if (mf.getFileFromOffset() + 
mappedFileQueue.getMappedFileSize() <= readOffsetAL.get()) {
+                            assertThat(mf.destroy(120 * 1000)).isTrue();
+                            
mappedFileQueue.deleteExpiredFile(Lists.newArrayList(mf));
+                        }
+                    }
+                }
+            }
+        });
+        dt.start();
+
+        while (running.get()) {
+            long readOffset = readOffsetAL.get();
+            if (readOffset < offset.get()) {
+                MappedFile mf = 
mappedFileQueue.findMappedFileByOffset(readOffset);
+                assertThat(mf).isNotNull();
+                assertThat(mf.getFileFromOffset() <= readOffset
+                        && mf.getFileFromOffset() + 
mappedFileQueue.getMappedFileSize() > readOffset).isTrue();
+                readOffsetAL.getAndAdd(data.length);
+            }
+        }
+
+        ct.join();
+        dt.join();
+        mappedFileQueue.shutdown(1000);
+        mappedFileQueue.destroy();
+    }
+
+    @Test
+    public void testFindMappedFileByOffsetWithIteration() {
+        MappedFileQueue mappedFileQueue =
+                new MappedFileQueue("target/unit_test_store/h/", 1024, null);
+
+        //Start from a non-zero offset
+        MappedFile mappedFile = mappedFileQueue.getLastMappedFile(1024);
+        assertThat(mappedFile).isNotNull();
+
+        
assertThat(mappedFileQueue.findMappedFileByOffsetWithIteration(1025,false)).isEqualTo(mappedFile);
+
+        assertThat(mappedFileQueue.findMappedFileByOffsetWithIteration(123, 
false)).isNull();
+        assertThat(mappedFileQueue.findMappedFileByOffsetWithIteration(123, 
true)).isEqualTo(mappedFile);
+
+        assertThat(mappedFileQueue.findMappedFileByOffsetWithIteration(0, 
false)).isNull();
+        assertThat(mappedFileQueue.findMappedFileByOffsetWithIteration(0, 
true)).isEqualTo(mappedFile);
+
+        mappedFileQueue.shutdown(1000);
+        mappedFileQueue.destroy();
+    }
+
     @After
     public void destory() {
         File file = new File("target/unit_test_store");


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to