This is an automated email from the ASF dual-hosted git repository.

yuzhou pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/rocketmq.git


The following commit(s) were added to refs/heads/develop by this push:
     new 84f03c34b [ISSUE #4143] Remove file lock related code snippets in 
index file (#4135)
84f03c34b is described below

commit 84f03c34b5a2b4c1ce567a856c3c699631a55815
Author: 灼华 <[email protected]>
AuthorDate: Sun Apr 17 23:54:30 2022 +0800

    [ISSUE #4143] Remove file lock related code snippets in index file (#4135)
    
    * Remove fileLock related code snippets in IndexFile
    
    * Remove unused fileChannel
    
    Co-authored-by: 灼华 <[email protected]>
---
 .../org/apache/rocketmq/store/index/IndexFile.java | 39 ++--------------------
 .../apache/rocketmq/store/index/IndexService.java  |  2 +-
 .../apache/rocketmq/store/index/IndexFileTest.java |  2 +-
 3 files changed, 4 insertions(+), 39 deletions(-)

diff --git a/store/src/main/java/org/apache/rocketmq/store/index/IndexFile.java 
b/store/src/main/java/org/apache/rocketmq/store/index/IndexFile.java
index e513edf11..bdea3307b 100644
--- a/store/src/main/java/org/apache/rocketmq/store/index/IndexFile.java
+++ b/store/src/main/java/org/apache/rocketmq/store/index/IndexFile.java
@@ -19,8 +19,6 @@ package org.apache.rocketmq.store.index;
 import java.io.IOException;
 import java.nio.ByteBuffer;
 import java.nio.MappedByteBuffer;
-import java.nio.channels.FileChannel;
-import java.nio.channels.FileLock;
 import java.util.List;
 import org.apache.rocketmq.common.constant.LoggerName;
 import org.apache.rocketmq.logging.InternalLogger;
@@ -35,7 +33,6 @@ public class IndexFile {
     private final int hashSlotNum;
     private final int indexNum;
     private final MappedFile mappedFile;
-    private final FileChannel fileChannel;
     private final MappedByteBuffer mappedByteBuffer;
     private final IndexHeader indexHeader;
 
@@ -44,7 +41,6 @@ public class IndexFile {
         int fileTotalSize =
             IndexHeader.INDEX_HEADER_SIZE + (hashSlotNum * hashSlotSize) + 
(indexNum * indexSize);
         this.mappedFile = new MappedFile(fileName, fileTotalSize);
-        this.fileChannel = this.mappedFile.getFileChannel();
         this.mappedByteBuffer = this.mappedFile.getMappedByteBuffer();
         this.hashSlotNum = hashSlotNum;
         this.indexNum = indexNum;
@@ -95,12 +91,8 @@ public class IndexFile {
             int slotPos = keyHash % this.hashSlotNum;
             int absSlotPos = IndexHeader.INDEX_HEADER_SIZE + slotPos * 
hashSlotSize;
 
-            FileLock fileLock = null;
-
             try {
 
-                // fileLock = this.fileChannel.lock(absSlotPos, hashSlotSize,
-                // false);
                 int slotValue = this.mappedByteBuffer.getInt(absSlotPos);
                 if (slotValue <= invalidIndex || slotValue > 
this.indexHeader.getIndexCount()) {
                     slotValue = invalidIndex;
@@ -144,14 +136,6 @@ public class IndexFile {
                 return true;
             } catch (Exception e) {
                 log.error("putKey exception, Key: " + key + " KeyHashCode: " + 
key.hashCode(), e);
-            } finally {
-                if (fileLock != null) {
-                    try {
-                        fileLock.release();
-                    } catch (IOException e) {
-                        log.error("Failed to release the lock", e);
-                    }
-                }
             }
         } else {
             log.warn("Over index file capacity: index count = " + 
this.indexHeader.getIndexCount()
@@ -189,25 +173,14 @@ public class IndexFile {
     }
 
     public void selectPhyOffset(final List<Long> phyOffsets, final String key, 
final int maxNum,
-        final long begin, final long end, boolean lock) {
+                                final long begin, final long end) {
         if (this.mappedFile.hold()) {
             int keyHash = indexKeyHashMethod(key);
             int slotPos = keyHash % this.hashSlotNum;
             int absSlotPos = IndexHeader.INDEX_HEADER_SIZE + slotPos * 
hashSlotSize;
 
-            FileLock fileLock = null;
             try {
-                if (lock) {
-                    // fileLock = this.fileChannel.lock(absSlotPos,
-                    // hashSlotSize, true);
-                }
-
                 int slotValue = this.mappedByteBuffer.getInt(absSlotPos);
-                // if (fileLock != null) {
-                // fileLock.release();
-                // fileLock = null;
-                // }
-
                 if (slotValue <= invalidIndex || slotValue > 
this.indexHeader.getIndexCount()
                     || this.indexHeader.getIndexCount() <= 1) {
                 } else {
@@ -223,7 +196,7 @@ public class IndexFile {
                         int keyHashRead = 
this.mappedByteBuffer.getInt(absIndexPos);
                         long phyOffsetRead = 
this.mappedByteBuffer.getLong(absIndexPos + 4);
 
-                        long timeDiff = (long) 
this.mappedByteBuffer.getInt(absIndexPos + 4 + 8);
+                        long timeDiff = 
this.mappedByteBuffer.getInt(absIndexPos + 4 + 8);
                         int prevIndexRead = 
this.mappedByteBuffer.getInt(absIndexPos + 4 + 8 + 4);
 
                         if (timeDiff < 0) {
@@ -251,14 +224,6 @@ public class IndexFile {
             } catch (Exception e) {
                 log.error("selectPhyOffset exception ", e);
             } finally {
-                if (fileLock != null) {
-                    try {
-                        fileLock.release();
-                    } catch (IOException e) {
-                        log.error("Failed to release the lock", e);
-                    }
-                }
-
                 this.mappedFile.release();
             }
         }
diff --git 
a/store/src/main/java/org/apache/rocketmq/store/index/IndexService.java 
b/store/src/main/java/org/apache/rocketmq/store/index/IndexService.java
index bf17ecffe..8e41477e3 100644
--- a/store/src/main/java/org/apache/rocketmq/store/index/IndexService.java
+++ b/store/src/main/java/org/apache/rocketmq/store/index/IndexService.java
@@ -173,7 +173,7 @@ public class IndexService {
 
                     if (f.isTimeMatched(begin, end)) {
 
-                        f.selectPhyOffset(phyOffsets, buildKey(topic, key), 
maxNum, begin, end, lastFile);
+                        f.selectPhyOffset(phyOffsets, buildKey(topic, key), 
maxNum, begin, end);
                     }
 
                     if (f.getBeginTimestamp() < begin) {
diff --git 
a/store/src/test/java/org/apache/rocketmq/store/index/IndexFileTest.java 
b/store/src/test/java/org/apache/rocketmq/store/index/IndexFileTest.java
index 7ad5b38db..5ef8dce11 100644
--- a/store/src/test/java/org/apache/rocketmq/store/index/IndexFileTest.java
+++ b/store/src/test/java/org/apache/rocketmq/store/index/IndexFileTest.java
@@ -63,7 +63,7 @@ public class IndexFileTest {
         assertThat(putResult).isFalse();
 
         final List<Long> phyOffsets = new ArrayList<Long>();
-        indexFile.selectPhyOffset(phyOffsets, "60", 10, 0, Long.MAX_VALUE, 
true);
+        indexFile.selectPhyOffset(phyOffsets, "60", 10, 0, Long.MAX_VALUE);
         assertThat(phyOffsets).isNotEmpty();
         assertThat(phyOffsets.size()).isEqualTo(1);
         indexFile.destroy(0);

Reply via email to