yashmayya commented on code in PR #19021:
URL: https://github.com/apache/pinot/pull/19021#discussion_r4672851796


##########
pinot-foreign-buffers/src/main/java/org/apache/pinot/segment/memory/foreign/ForeignPinotBuffer.java:
##########
@@ -0,0 +1,261 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.segment.memory.foreign;
+
+import java.lang.foreign.Arena;
+import java.lang.foreign.MemorySegment;
+import java.lang.foreign.ValueLayout;
+import java.lang.ref.Cleaner;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import javax.annotation.Nullable;
+import org.apache.pinot.segment.spi.memory.NonNativePinotDataBuffer;
+import org.apache.pinot.segment.spi.memory.PinotDataBuffer;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/// A [PinotDataBuffer] backed by the Foreign Function & Memory API 
([MemorySegment]).
+///
+/// This is the Java 21+ replacement for the `sun.misc.Unsafe` based buffer. 
Like the Unsafe implementation it only
+/// reads and writes in [native byte order][ByteOrder#nativeOrder()]; 
non-native order is provided by wrapping it in a
+/// [NonNativePinotDataBuffer]. Because [MemorySegment] natively supports 
`long` indexing, a single implementation
+/// covers buffers larger than [Integer#MAX_VALUE] without any of the manual 
pointer arithmetic, page alignment or
+/// reflective `DirectByteBuffer` construction the Unsafe path required.
+///
+/// All accessors use the `*_UNALIGNED` [ValueLayout] constants because 
callers read and write at arbitrary byte
+/// offsets. Bounds checking is performed by [MemorySegment] itself (throwing 
[IndexOutOfBoundsException]), so this
+/// class does not add its own checks. Access is memory-safe: reads and writes 
after the backing [Arena] is closed fail
+/// deterministically instead of corrupting memory.
+///
+/// Thread-safety: instances created through [ForeignPinotBufferFactory] use a 
[shared arena][Arena#ofShared()], so the
+/// buffer may be read, written and closed from any thread. As with every 
[PinotDataBuffer], closing a buffer while
+/// another thread accesses it is a caller error. Note that closing a shared 
arena does not fail fast: [#release()]
+/// blocks until all in-flight accesses on this buffer and its views have 
completed (and the inherited synchronized
+/// [#close()] serialises concurrent close calls on the same instance).

Review Comment:
   Nit, non-blocking: the end of this paragraph does not match JDK 25. 
`release()` never blocks. From `SharedSession.justClose()`:
   
   - If an I/O operation still holds a `ByteBuffer` from `toDirectByteBuffer`, 
`close()` throws `IllegalStateException: Session is acquired by N clients`. The 
memory stays allocated.
   - In all other cases, the close succeeds at once. A thread that reads or 
writes at the same time gets `IllegalStateException: Already closed`.
   
   Suggested wording:
   
   ```suggestion
   /// another thread accesses it is a caller error. Closing does not wait for 
other threads, and it stays memory-safe:
   /// a concurrent read or write on this buffer or its views fails with 
[IllegalStateException]. If an I/O operation
   /// still uses a [ByteBuffer] from [#toDirectByteBuffer(long, int, 
ByteOrder)], [#release()] throws
   /// [IllegalStateException] and the memory stays allocated. The inherited 
synchronized [#close()] serialises
   /// concurrent close calls on the same instance.
   ```
   



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