When does an asynchronous FileChannel write "full"?

In Java, when you call AsynchronousFileChannel.write(...) , you get a Future , which you can wait until completion through Future.get() .

When this call to get() returns whether the write is written to disk or only the page cache?

If that matters, for a particular platform I'm interested in Linux and ext4.

+5
source share
1 answer

Check out this documentation:

https://docs.oracle.com/javase/7/docs/api/java/nio/channels/AsynchronousFileChannel.html#force(boolean)

It seems that even after the get method returns to Future , there are some caches at the operating system level that can prevent writing content to disk. If you want to be sure, I think you should make a force after Future returns.

Relevant passage:

Causes all updates to this channel file to be written to the repository that contains it. If this channel file is local then when this method returns, it is guaranteed that all changes made to the file since the creation of this channel, or the method was called last, will be written to this device. This is useful to ensure that critical information is not lost in a system failure event.

If the file is not located on the local device, then no warranty.

The metaData parameter can be used to limit the number of I / O operations that this method needs to perform. Passing false for this parameter indicates that only updating the contents of the file is required to be recorded; A true transmission indicates that updates for both file contents and metadata should be written, which usually requires at least one more I / O operation. Whether this parameter really has any effect depends on the underlying operating system and is therefore not indicated.

Calling this method can result in an I / O operation, even if the channel has been opened read-only. Some operating systems, for example, support last access time as part of file metadata and this time is updated whenever the file is read. Regardless of whether it is actually executed, it is system-dependent and therefore not specified.

This method is guaranteed only for forced changes that have been made to this channel file using the methods defined in this class.

+1
source

Source: https://habr.com/ru/post/1233990/


All Articles