Package org.apache.hadoop.fs.impl
Class TrackingByteBufferPool
java.lang.Object
org.apache.hadoop.fs.impl.TrackingByteBufferPool
- All Implemented Interfaces:
AutoCloseable,ByteBufferPool
@VisibleForTesting
public final class TrackingByteBufferPool
extends Object
implements ByteBufferPool, AutoCloseable
A wrapper
ByteBufferPool implementation that tracks whether all allocated buffers
are released.
It throws the related exception at close() if any buffer remains un-released.
It also clears the buffers at release so if they continued being used it'll generate errors.
To be used for testing..
The stacktraces of the allocation are not stored by default because it can significantly decrease the unit test performance. Configuring this class to log at DEBUG will trigger their collection.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classStrack trace of allocation as saved in the tracking map.static classstatic final classException raised inclose()if there was an unreleased buffer.static final classException raised inputBuffer(ByteBuffer)if the buffer to release was not in the hash map. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Expect all buffers to be released -if not, log unreleased ones and then raise an exception with the stack trace of the first unreleased buffer.booleancontainsBuffer(ByteBuffer buffer) Check if the buffer is in the pool.getBuffer(boolean direct, int size) Get a buffer from the pool.intintvoidputBuffer(ByteBuffer buffer) Release a buffer back to the pool.intsize()Get the number of allocated buffers.static TrackingByteBufferPoolwrap(ByteBufferPool allocator) Wrap an existing allocator with this tracking allocator.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.apache.hadoop.io.ByteBufferPool
release
-
Method Details
-
wrap
Wrap an existing allocator with this tracking allocator.- Parameters:
allocator- allocator to wrap.- Returns:
- a new allocator.
-
getBufferAllocations
public int getBufferAllocations() -
getBufferReleases
public int getBufferReleases() -
getBuffer
Get a buffer from the pool.This increments the
bufferAllocationscounter and stores the singleron or local allocation stack trace in theallocatedmap.- Specified by:
getBufferin interfaceByteBufferPool- Parameters:
direct- whether to allocate a direct buffer or notsize- size of the buffer to allocate- Returns:
- a ByteBuffer instance
-
putBuffer
public void putBuffer(ByteBuffer buffer) throws TrackingByteBufferPool.ReleasingUnallocatedByteBufferException Release a buffer back to the pool.This increments the
bufferReleasescounter and removes the buffer from theallocatedmap.If the buffer was not allocated by this pool, it throws
TrackingByteBufferPool.ReleasingUnallocatedByteBufferException.- Specified by:
putBufferin interfaceByteBufferPool- Parameters:
buffer- buffer to release- Throws:
TrackingByteBufferPool.ReleasingUnallocatedByteBufferException- if the buffer was not allocated by this pool
-
containsBuffer
Check if the buffer is in the pool.- Parameters:
buffer- buffer- Returns:
- true if the buffer is in the pool
-
size
public int size()Get the number of allocated buffers.- Returns:
- number of allocated buffers
-
close
Expect all buffers to be released -if not, log unreleased ones and then raise an exception with the stack trace of the first unreleased buffer.- Specified by:
closein interfaceAutoCloseable- Throws:
TrackingByteBufferPool.LeakedByteBufferException- if at least one buffer was not released
-