iemejia commented on code in PR #8511:
URL: https://github.com/apache/hadoop/pull/8511#discussion_r4589783008


##########
hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/ChecksumFileSystem.java:
##########
@@ -493,21 +498,108 @@ public void readVectored(final List<? extends FileRange> 
ranges,
       }
       List<CombinedFileRange> checksumRanges = findChecksumRanges(dataRanges,
           bytesPerSum, minSeek, maxSize);
-      sums.readVectored(checksumRanges, allocate, release);
-      datas.readVectored(dataRanges, allocate, release);
+      Map<FileRange, ByteBuffer> callerBuffers = new IdentityHashMap<>();
+      try {
+        // Allocate the caller's buffers synchronously, before submitting any 
reads.
+        // Internal reads may be rounded to checksum boundaries; never return 
slices
+        // of those buffers, as identity-tracking allocators cannot recycle 
them.
+        for (FileRange range : sorted) {
+          ByteBuffer buffer = allocate.apply(range.getLength());
+          callerBuffers.put(range, buffer);
+          buffer.limit(range.getLength());
+        }
+        sums.readVectored(checksumRanges, allocate, release);
+        datas.readVectored(dataRanges, allocate, release);
+      } catch (IOException | RuntimeException | Error e) {
+        // No buffers have been exposed to the caller yet. Reads already 
started
+        // may still be in flight, so release their buffers only after success.
+        releaseReadBuffers(checksumRanges, release);
+        releaseReadBuffers(dataRanges, release);
+        for (ByteBuffer buffer : callerBuffers.values()) {
+          releaseBuffer(buffer, release);
+        }
+        for (FileRange range : sorted) {
+          CompletableFuture<ByteBuffer> failed = new CompletableFuture<>();
+          failed.completeExceptionally(e);
+          range.setData(failed);
+        }
+        throw e;
+      }
       for(CombinedFileRange checksumRange: checksumRanges) {
+        List<CompletableFuture<ByteBuffer>> verifications = new ArrayList<>();
         for(FileRange dataRange: checksumRange.getUnderlying()) {
           // when we have both the ranges, validate the checksum
           CompletableFuture<ByteBuffer> result =
               checksumRange.getData().thenCombineAsync(dataRange.getData(),
                   (sumBuffer, dataBuffer) ->
                       checkBytes(sumBuffer, checksumRange.getOffset(),
                           dataBuffer, dataRange.getOffset(), bytesPerSum, 
file));
-          // Now, slice the read data range to the user's ranges
-          for(FileRange original: ((CombinedFileRange) 
dataRange).getUnderlying()) {
-            original.setData(result.thenApply(
-                (b) -> VectoredReadUtils.sliceTo(b, dataRange.getOffset(), 
original)));
+          verifications.add(result);
+          List<CompletableFuture<ByteBuffer>> copies = new ArrayList<>();
+          for (FileRange original : ((CombinedFileRange) 
dataRange).getUnderlying()) {
+            ByteBuffer target = callerBuffers.get(original);
+            CompletableFuture<ByteBuffer> copy = result.thenApply(buffer -> {
+              target.put(VectoredReadUtils.sliceTo(buffer, 
dataRange.getOffset(), original));
+              target.flip();
+              return target;
+            }).whenComplete((buffer, ex) -> {
+              if (ex != null) {
+                release.accept(target);

Review Comment:
   Thanks for catching the distinction between `get()` and `join()`. Addressed 
in b18d92f92c0: caller-buffer cleanup now uses `releaseBuffer(target, 
release)`, so release-callback failures are logged without replacing the 
original checksum or copy failure.
   
   Added heap/direct regression tests for both checksum and copy failures with 
a throwing release callback. They observe the original failure through `get()` 
and separately assert that the cleanup failure is logged.



##########
hadoop-common-project/hadoop-common/src/site/markdown/filesystem/fsdatainputstream.md:
##########
@@ -669,13 +664,20 @@ Stream.hasCapability("in:readvectored")
 
 [HADOOP-18296](https://issues.apache.org/jira/browse/HADOOP-18296),
 _Memory fragmentation in ChecksumFileSystem Vectored IO implementation_
-highlights that `ChecksumFileSystem` (which the default implementation of 
`file://`
-subclasses), may return buffers which are sliced subsets of buffers allocated
-through the `allocate()` function passed in.
-
-This will happen during reads with and without range coalescing.
-
-Checksum verification may be disabled by setting the option
+highlights that older versions of `ChecksumFileSystem` (which the default 
implementation
+of `file://` subclasses) return sliced subsets of buffers allocated through the
+`allocate()` function passed in. These slices cannot be recycled by allocators 
which
+track buffer identity and can retain oversized backing allocations.
+
+With [HADOOP-19901](https://issues.apache.org/jira/browse/HADOOP-19901),
+`ChecksumFileSystem` verifies data in internal buffers and copies
+each requested range into its own buffer, allocated synchronously through 
`allocate()`.
+It returns exactly that buffer, not a slice, and reports `false` for
+`fs.capability.vectoredio.sliced` on both the filesystem and the stream.
+This applies with and without checksum verification, so direct buffers and
+identity-tracking allocators can be used without a slicing workaround.
+
+For older releases, checksum verification may be disabled by setting the option

Review Comment:
   Removed the obsolete sentence from the `fs.file.checksum.verify` description 
in `core-default.xml` in b18d92f92c0. Returning the exact allocated range 
buffers no longer depends on disabling checksum verification.



##########
hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/ChecksumFileSystem.java:
##########
@@ -493,21 +498,108 @@ public void readVectored(final List<? extends FileRange> 
ranges,
       }
       List<CombinedFileRange> checksumRanges = findChecksumRanges(dataRanges,
           bytesPerSum, minSeek, maxSize);
-      sums.readVectored(checksumRanges, allocate, release);
-      datas.readVectored(dataRanges, allocate, release);
+      Map<FileRange, ByteBuffer> callerBuffers = new IdentityHashMap<>();
+      try {
+        // Allocate the caller's buffers synchronously, before submitting any 
reads.
+        // Internal reads may be rounded to checksum boundaries; never return 
slices
+        // of those buffers, as identity-tracking allocators cannot recycle 
them.
+        for (FileRange range : sorted) {
+          ByteBuffer buffer = allocate.apply(range.getLength());
+          callerBuffers.put(range, buffer);
+          buffer.limit(range.getLength());
+        }
+        sums.readVectored(checksumRanges, allocate, release);
+        datas.readVectored(dataRanges, allocate, release);
+      } catch (IOException | RuntimeException | Error e) {
+        // No buffers have been exposed to the caller yet. Reads already 
started
+        // may still be in flight, so release their buffers only after success.
+        releaseReadBuffers(checksumRanges, release);
+        releaseReadBuffers(dataRanges, release);
+        for (ByteBuffer buffer : callerBuffers.values()) {
+          releaseBuffer(buffer, release);
+        }
+        for (FileRange range : sorted) {
+          CompletableFuture<ByteBuffer> failed = new CompletableFuture<>();
+          failed.completeExceptionally(e);
+          range.setData(failed);
+        }
+        throw e;
+      }
       for(CombinedFileRange checksumRange: checksumRanges) {
+        List<CompletableFuture<ByteBuffer>> verifications = new ArrayList<>();
         for(FileRange dataRange: checksumRange.getUnderlying()) {
           // when we have both the ranges, validate the checksum
           CompletableFuture<ByteBuffer> result =
               checksumRange.getData().thenCombineAsync(dataRange.getData(),
                   (sumBuffer, dataBuffer) ->
                       checkBytes(sumBuffer, checksumRange.getOffset(),
                           dataBuffer, dataRange.getOffset(), bytesPerSum, 
file));
-          // Now, slice the read data range to the user's ranges
-          for(FileRange original: ((CombinedFileRange) 
dataRange).getUnderlying()) {
-            original.setData(result.thenApply(
-                (b) -> VectoredReadUtils.sliceTo(b, dataRange.getOffset(), 
original)));
+          verifications.add(result);
+          List<CompletableFuture<ByteBuffer>> copies = new ArrayList<>();
+          for (FileRange original : ((CombinedFileRange) 
dataRange).getUnderlying()) {
+            ByteBuffer target = callerBuffers.get(original);
+            CompletableFuture<ByteBuffer> copy = result.thenApply(buffer -> {
+              target.put(VectoredReadUtils.sliceTo(buffer, 
dataRange.getOffset(), original));
+              target.flip();
+              return target;
+            }).whenComplete((buffer, ex) -> {
+              if (ex != null) {
+                release.accept(target);
+              }
+            });
+            original.setData(copy);
+            copies.add(copy);
           }
+          // Wait for every copy before recycling the shared internal data 
buffer.
+          CompletableFuture.allOf(copies.toArray(new CompletableFuture[0]))

Review Comment:
   Addressed in b18d92f92c0. Verification, copying, and cleanup now run on 
private future stages, with a separate result future exposed for each caller 
range. Cancelling that result future no longer triggers internal cleanup early 
or blocks on the pending read. If delivery fails because the caller has already 
cancelled or completed the result future, the target buffer is released.
   
   Added deterministic heap/direct tests covering cancellation before and after 
data-read completion, including checksum failure. They assert that no buffers 
are released while verification is pending, poison recycled storage to detect 
premature reuse, check exact release counts, and verify that successful sibling 
buffers remain caller-owned. The cancellation call also has a bounded wait to 
catch the previous blocking behavior.



##########
hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/ChecksumFileSystem.java:
##########
@@ -493,21 +498,108 @@ public void readVectored(final List<? extends FileRange> 
ranges,
       }
       List<CombinedFileRange> checksumRanges = findChecksumRanges(dataRanges,
           bytesPerSum, minSeek, maxSize);
-      sums.readVectored(checksumRanges, allocate, release);
-      datas.readVectored(dataRanges, allocate, release);
+      Map<FileRange, ByteBuffer> callerBuffers = new IdentityHashMap<>();
+      try {
+        // Allocate the caller's buffers synchronously, before submitting any 
reads.
+        // Internal reads may be rounded to checksum boundaries; never return 
slices
+        // of those buffers, as identity-tracking allocators cannot recycle 
them.
+        for (FileRange range : sorted) {
+          ByteBuffer buffer = allocate.apply(range.getLength());
+          callerBuffers.put(range, buffer);
+          buffer.limit(range.getLength());
+        }
+        sums.readVectored(checksumRanges, allocate, release);

Review Comment:
   Implemented the suggested split in b18d92f92c0. The two-argument overload 
now uses unpooled `ByteBuffer::allocate` buffers for internal data and checksum 
reads; the caller's allocator is used only for the range-result buffers. The 
three-argument overload continues to use the supplied allocator and release 
callback for internal buffers too.
   
   Updated `PositionedReadable` and `fsdatainputstream.md` to explain that the 
allocator need not see internal allocations, including the trade-off that 
direct-buffer callers' internal buffers are now heap-allocated and outside 
their allocator's accounting and limits.
   
   The heap/direct identity tests now require exactly one caller allocation per 
requested range on the two-argument path and recycle only the returned results, 
without a capturing-allocator workaround.



-- 
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