AsyncFile.flush bug?
It looks like AsyncFile.flush does not ensure that the entire data is written out before calling FileChannel.force(). It should have waited until the outstanding write has been flushed before calling FileChannel.force()
@vietj do you think this bug is valid?
do you have a reproducer ?
Difficult to nail the reproducer. It's mainly a contract issue where the flush does not ensure that it establishes a checkpoint where it needs to ensure that the data that have been received by AsyncFile so far must be written to the disk (as it stays in the buffer).
What if the WriteStream contract was extended to support a writeQueueEmpty handler? Once the writeQueueEmpty handler was called AsyncFile.flush could be called. I suspect the writeQueueEmpty handler would be useful in most non trivial cases of ReadStream to WriteStream delegate chains. Here's one example I can think of: I'm pumping an HttpServerRequest to a CipherWriteStream which is eventually delegating to an AsyncFile that I keep open since I'm writing to specific positions within the AsyncFile. When the HttpServerRequest stream endHandler is called the WriteStream.end method at the top of the WriteStream chain is called in turn causing the CipherWriteStream to finalize the stream and write to the next delegate which would eventually call AsyncFile.write. If there was writeQueueEmpty handler support on the WriteStream contract the wrapper code around AsyncFile would be able call AsyncFile.flush once once the write queue was empty. Once the AsyncFile.flush method completed the writeQueueEmpty handler registered in the AsyncFile wrapper code would allow the application to continue on and write 201 OK response to the HttpServerResponse and then call HttpServerResponse.end.
@vietj I think this should be addressed as well in new ReadStream/WriteStream implementation where it is not possible to check whether there is any outstanding write or not.
@luengnat what do you mean by outstanding write ?
there is nothing new in the WriteStream part, only ReadStream is affected
but I see now wht you mean.
we could try that when a write operation is in progress, a boolean flush flag should be set if outstandingWrites > 0 and when oustandingWrites == 0 then flush is done.
a reproducer would be great
What if AsyncFile was changed to support some kind "drained handler" that fired once writesOutstanding == 0. It would allow for some flexibility so the caller could choose to flush then close or just flush.
flush should actually wait for outstandingWrites == 0 before calling the flush handler.
Agreed that it probably makes sense to wait though there's a case for forcing at a fixed interval via a periodic while writing in which case waiting for outstandingWrites == 0 wouldn't make sense. Another point in support of "drained handler"... What if the caller wants to wait for the write q to be empty and not call force? Something along the lines of not calling HttpServerResponse.end() until the AsyncFile write q has been drained to the os disk cache.
wait for outstandingWrites == 0 makes sense for the case where you want to make sure that all the data you write back to disk and you have no further writes
maybe the best then would be to have a write queue task for each write task and the flush would just be a task in this queue executed among others, this way we can have operations properly ordered from the perspective of the caller.
That sounds reasonable. I think with that structure it would also be trivial to fire a write queue drained handler if it has been set.
Perhaps to expand on the idea a little which may or may not be beyond the scope of this issue.... I implemented a concurrent write queue helper and an async file writer so that concurrent writes could be executed against an AsynchronousFileChannel. It would be nifty if one could call vertx.fileSystem().getOrOpenWriter(String path, OpenOptions options, Handler<AsyncResult