Class LibaioContext<Callback extends SubmitInfo>
- java.lang.Object
-
- org.apache.activemq.artemis.nativo.jlibaio.LibaioContext<Callback>
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable
public class LibaioContext<Callback extends SubmitInfo> extends java.lang.Object implements java.io.CloseableThis class is used as an aggregator for theLibaioFile.
It holds native data, and it will share a libaio queue that can be used by multiple files.
You need to use the poll methods to read the result of write and read submissions.
You also need to use the special buffer created byLibaioFileas you need special alignments when dealing with O_DIRECT files.
A Single controller can server multiple files. There's no need to create one controller per file.
Interesting reading for this.
-
-
Constructor Summary
Constructors Constructor Description LibaioContext(int queueSize, boolean useSemaphore, boolean useFdatasync)The queue size here will use resources defined on the kernel parameter fs.aio-max-nr .
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()This is used to close the libaio queues and cleanup the native data used.static voidclose(int fd)static voidfreeBuffer(java.nio.ByteBuffer buffer)This will call posix free to release the inner buffer allocated atnewAlignedBuffer(int, int).static intgetBlockSize(java.io.File path)static intgetBlockSize(java.lang.String path)static longgetTotalMaxIO()This is used to validate leaks on tests.static booleanisForceSyscall()The system may choose to set this if a failing condition happened inside the code.static booleanisLoaded()static booleanlock(int fd)voidmemsetBuffer(java.nio.ByteBuffer buffer)It will reset all the positions on the buffer to 0, using memset.static voidmemsetBuffer(java.nio.ByteBuffer buffer, int size)static java.nio.ByteBuffernewAlignedBuffer(int size, int alignment)Buffers for O_DIRECT need to use posix_memalign.static intopen(java.lang.String path, boolean direct)it will return a file descriptor.static LibaioFileopenControlFile(java.lang.String file, boolean direct)It will open a file disassociated with any sort of factory.LibaioFile<Callback>openFile(java.io.File file, boolean direct)It will open a file.LibaioFile<Callback>openFile(java.lang.String file, boolean direct)It will open a file.voidpoll()It will start polling and will keep doing until the context is closed.intpoll(Callback[] callbacks, int min, int max)It will poll the libaio queue for results.static voidresetMaxAIO()This is used on tests validating for leaks.static voidsetForceSyscall(boolean value)voidsubmitRead(int fd, long position, int size, java.nio.ByteBuffer bufferWrite, Callback callback)voidsubmitWrite(int fd, long position, int size, java.nio.ByteBuffer bufferWrite, Callback callback)
-
-
-
Constructor Detail
-
LibaioContext
public LibaioContext(int queueSize, boolean useSemaphore, boolean useFdatasync)The queue size here will use resources defined on the kernel parameter fs.aio-max-nr .- Parameters:
queueSize- the size to be initialize on libaio io_queue_init which can't be higher than /proc/sys/fs/aio-max-nr.useSemaphore- should block on a semaphore avoiding using more submits than what's available.useFdatasync- should use fdatasync before calling callbacks.
-
-
Method Detail
-
isLoaded
public static boolean isLoaded()
-
setForceSyscall
public static void setForceSyscall(boolean value)
-
isForceSyscall
public static boolean isForceSyscall()
The system may choose to set this if a failing condition happened inside the code.
-
getTotalMaxIO
public static long getTotalMaxIO()
This is used to validate leaks on tests.- Returns:
- the number of allocated aio, to be used on test checks.
-
memsetBuffer
public void memsetBuffer(java.nio.ByteBuffer buffer)
It will reset all the positions on the buffer to 0, using memset.- Parameters:
buffer- a native buffer. s
-
resetMaxAIO
public static void resetMaxAIO()
This is used on tests validating for leaks.
-
submitWrite
public void submitWrite(int fd, long position, int size, java.nio.ByteBuffer bufferWrite, Callback callback) throws java.io.IOException- Parameters:
fd- the file descriptorposition- the write positionsize- number of bytes to usebufferWrite- the native buffercallback- a callback- Throws:
java.io.IOException- in case of error
-
submitRead
public void submitRead(int fd, long position, int size, java.nio.ByteBuffer bufferWrite, Callback callback) throws java.io.IOException- Throws:
java.io.IOException
-
close
public void close()
This is used to close the libaio queues and cleanup the native data used.
It is unsafe to close the controller while you have pending writes or files open as this could cause core dumps or VM crashes.- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable
-
openFile
public LibaioFile<Callback> openFile(java.io.File file, boolean direct) throws java.io.IOException
It will open a file. If you set the direct flag = false then you won't need to use the special buffer. Notice: This will create an empty file if the file doesn't already exist.- Parameters:
file- the file to be open.direct- will set ODIRECT.- Returns:
- It will return a LibaioFile instance.
- Throws:
java.io.IOException- in case of error.
-
openFile
public LibaioFile<Callback> openFile(java.lang.String file, boolean direct) throws java.io.IOException
It will open a file. If you set the direct flag = false then you won't need to use the special buffer. Notice: This will create an empty file if the file doesn't already exist.- Parameters:
file- the file to be open.direct- should use O_DIRECT when opening the file.- Returns:
- a new open file.
- Throws:
java.io.IOException- in case of error.
-
openControlFile
public static LibaioFile openControlFile(java.lang.String file, boolean direct) throws java.io.IOException
It will open a file disassociated with any sort of factory. This is useful when you won't use reading / writing through libaio like locking files.- Parameters:
file- a file namedirect- will use O_DIRECT- Returns:
- a new file
- Throws:
java.io.IOException- in case of error.
-
poll
public int poll(Callback[] callbacks, int min, int max)
It will poll the libaio queue for results. It should block until min is reached Results are placed on the callback.
This shouldn't be called concurrently. You should provide your own synchronization if you need more than one Thread polling for any reason.
Notice that the native layer will invokeSubmitInfo.onError(int, String)in case of failures, but it won't call done method for you.- Parameters:
callbacks- area to receive the callbacks passed on submission.The size of this callback has to be greater than the parameter max.min- the minimum number of elements to receive. It will block until this is achieved.max- The maximum number of elements to receive.- Returns:
- Number of callbacks returned.
- See Also:
LibaioFile.write(long, int, java.nio.ByteBuffer, SubmitInfo),LibaioFile.read(long, int, java.nio.ByteBuffer, SubmitInfo)
-
poll
public void poll()
It will start polling and will keep doing until the context is closed. This will call callbacks onSubmitInfo.onError(int, String)andSubmitInfo.done(). In case of error, bothSubmitInfo.onError(int, String)andSubmitInfo.done()are called.
-
open
public static int open(java.lang.String path, boolean direct)it will return a file descriptor.- Parameters:
path- the file name.direct- translates as O_DIRECT On open- Returns:
- a fd from open C call.
-
close
public static void close(int fd)
-
newAlignedBuffer
public static java.nio.ByteBuffer newAlignedBuffer(int size, int alignment)Buffers for O_DIRECT need to use posix_memalign.
Documented atLibaioFile.newBuffer(int).- Parameters:
size- needs to be % alignmentalignment- the alignment used at the dispositive- Returns:
- a new native buffer used with posix_memalign
-
freeBuffer
public static void freeBuffer(java.nio.ByteBuffer buffer)
This will call posix free to release the inner buffer allocated atnewAlignedBuffer(int, int).- Parameters:
buffer- a native buffer allocated withnewAlignedBuffer(int, int).
-
lock
public static boolean lock(int fd)
-
memsetBuffer
public static void memsetBuffer(java.nio.ByteBuffer buffer, int size)
-
getBlockSize
public static int getBlockSize(java.io.File path)
-
getBlockSize
public static int getBlockSize(java.lang.String path)
-
-