Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is an AsynchronousFileChannel write "complete"?

In Java, when one calls AsynchronousFileChannel.write(...) one receives a Future, which one can then wait on for completion via Future.get().

When that get() call returns, has the write been written to the disk, or just the page cache?

If it matters, the specific platform I'm curious about is Linux & ext4.

like image 649
Bryce Avatar asked Oct 19 '15 02:10

Bryce


1 Answers

Check out this documentation:

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

It looks like even after the get method returns on the Future, there some some caches at the operating system level that may prevent the contents from being written to disk. If you want to be really sure, I think you must do a force after the Future returns.

A relevant excerpt:

Forces any updates to this channel's file to be written to the storage device that contains it. If this channel's file resides on a local storage device then when this method returns it is guaranteed that all changes made to the file since this channel was created, or since this method was last invoked, will have been written to that device. This is useful for ensuring that critical information is not lost in the event of a system crash.

If the file does not reside on a local device then no such guarantee is made.

The metaData parameter can be used to limit the number of I/O operations that this method is required to perform. Passing false for this parameter indicates that only updates to the file's content need be written to storage; passing true indicates that updates to both the file's content and metadata must be written, which generally requires at least one more I/O operation. Whether this parameter actually has any effect is dependent upon the underlying operating system and is therefore unspecified.

Invoking this method may cause an I/O operation to occur even if the channel was only opened for reading. Some operating systems, for example, maintain a last-access time as part of a file's metadata, and this time is updated whenever the file is read. Whether or not this is actually done is system-dependent and is therefore unspecified.

This method is only guaranteed to force changes that were made to this channel's file via the methods defined in this class.

like image 169
Venkat Avatar answered Oct 06 '22 16:10

Venkat