How file manipulations perform during power outage

30
June 12, 2019, at 07:50 AM

Linux machine, Java standalone application

I am having the following situation:

I have: consecutive file write(which creates the destination file and writes some content to it) and file move.

I also have a power outage problem, which instantly cuts off the power of computer during these operations.

As a result, I am getting that the file was created, and it was moved as well, but the file content is empty.

The question is what under the hood can be causing this exact outcome? Considering the time sensitivity, may be hard drive is disabled before the processor and RAM during the cut out, but in that case, how is it possible that the file is created and moved after, but the write before moving is not successful?

I tried catching and logging the exception and debug information but the problem is power outage disables the logging abilities(I/O) as well.

try {
    FileUtils.writeStringToFile(file, JsonUtils.toJson(object));
} finally {
    if (file.exists()) {
        FileUtils.moveFileToDirectory(file, new File(path), true);
    }
}
Answer 1

Linux file systems don't necessarily write things to disk immediately, or in exactly the order that you wrote them. That includes both file content and file / directory metadata.

So if you get a power failure at the wrong time, you may find that the file data and metadata is inconsistent.

Normally this doesn't matter. (If the power fails and you don't have a UPS, the applications go away without getting a chance to finish what they were doing.)

However, if it does matter, you can do the following: to force the file to "sync" before you move it:

   FileOutputStream fos = ...
   // write to file
   fs.getFD().sync();
   fs.close();
   // now move it

You need to read the javadoc for sync() carefully to understand what the method actually does.

You also need to read the javadoc for the method you are using to move the file regarding atomicity.

READ ALSO
How can I make one flowfile the child of a separate pre-existing flowfile?

How can I make one flowfile the child of a separate pre-existing flowfile?

When a flowfile comes into existence from an external source, but knows the uuid of an prior flowfile that it came from, how can I mark the existing flowfile as a parent of this one

58
Open PDF in new tab, saving file gives wrong file name

Open PDF in new tab, saving file gives wrong file name

There is an Angular 6 app having a feature to download pdf filesIt uses REST API from another Spring Boot application to download files

62
Jpa return more than one result although only one row is in database

Jpa return more than one result although only one row is in database

I am running different JPA queries in the form of

45
Java - What do the Bytes Recieved by Reading from an AudioInputStream Correspond to?

Java - What do the Bytes Recieved by Reading from an AudioInputStream Correspond to?

Can someone explain what the bytes you get by reading an AudioInputStream correspond to? Things like volume or frequency/pitchI have this code that reads the bytes from the entire file

57