Gargi-jais11 commented on code in PR #9649:
URL: https://github.com/apache/ozone/pull/9649#discussion_r2715401405


##########
hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/keyvalue/impl/ChunkManagerDummyImpl.java:
##########
@@ -40,6 +46,49 @@
  */
 public class ChunkManagerDummyImpl implements ChunkManager {
 
+  private static final int DEFAULT_MAP_SIZE = 1 * 1024 * 1024; // 1MB
+
+  private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
+  private volatile MappedByteBuffer mapped;
+  private volatile int mappedSize;
+  private volatile Path backingFile;
+
+  private void ensureMapped(int minSize)
+      throws StorageContainerException {
+    if (mapped != null && mappedSize >= minSize) {
+      return;
+    }
+
+    lock.writeLock().lock();
+    try {
+      if (mapped != null && mappedSize >= minSize) {
+        return;
+      }
+
+      int newSize = Math.max(DEFAULT_MAP_SIZE, minSize);
+      if (backingFile == null) {
+        backingFile = Files.createTempFile("ozone-dummy-chunk-", ".bin");
+        backingFile.toFile().deleteOnExit();
+      }
+
+      try (FileChannel ch = FileChannel.open(backingFile,
+          StandardOpenOption.READ, StandardOpenOption.WRITE)) {
+        if (ch.size() < newSize) {
+          ch.truncate(newSize);
+        }

Review Comment:
   
   Here, the code uses `ch.truncate(newSize)` to expand the file when 
`ch.size() < newSize`, but `truncate()` **only reduces file size** - it cannot 
expand files. 
   
   According to Java API:> "If the given size is greater than or equal to the 
file's current size then the file is not modified."
   When the file is smaller than `newSize`, `truncate()` does nothing, leaving 
the file unchanged. This causes silent failures when read requests exceed the 
current mapped buffer size.
   
   1. **Scenario 1: File is Smaller, Need Smaller Size (< 1MB)**
   
   **Example:**
   - Current file: 500 KB
   - Request: Read 256 KB
   - `newSize = max(1MB, 256KB) = 1MB` (because of DEFAULT_MAP_SIZE)
   
   ```
   Current: ch.size() = 500 KB
   Need:    newSize = 1 MB
   Check:   Is 500 KB < 1 MB? → YES 
   Action:  ch.truncate(1 MB)
   Result:  ❌ truncate() does NOTHING (can't expand)
            File stays 500 KB, but we need 1 MB!
   ```
   
   **Problem:** File doesn't expand to 1 MB, mapping fails or maps wrong size.
   
   2.  **Scenario 2: File is Larger, Need Smaller Size (< 1MB)**
   
   **Example:**
   - Current file: 2 MB
   - Request: Read 256 KB  
   - `newSize = max(1MB, 256KB) = 1MB`
   
   ```
   Current: ch.size() = 2 MB
   Need:    newSize = 1 MB
   Check:   Is 2 MB < 1 MB? → NO ❌
   Action:  Skip truncate (condition false)
   Result:  File is already big enough (2 MB > 1 MB)
            But we're mapping only 1 MB, so it works!
   ```
   
   **Status:** Works (but wastes space - file is 2 MB but we only use 1 MB)
   
   We need to take care of these scenarios.
   
   ```
   if (ch.size() < newSize) {
     // LOGIC:  Need to EXPAND file 
   } else if (ch.size() > newSize) {
     // Need to SHRINK file
     ch.truncate(newSize);  // This works for shrinking!
   }
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to