Atomic modification of files over multiple networks

I have an application that modifies 5 identical xml files, each located on a different network share. I know this is uselessly redundant, but "it should be."

Each time this app is launched, only one item (no more, no less) will be added / removed / changed.

Initially, the application opens each XML file, adds / removes / changes an element to the corresponding node, and saves the file or throws an error if it cannot (unable to access the network share, timeout, etc ..)

How to make this atomic?

My initial guess was this:

foreach (var path in NetworkPaths)
    if (!File.Exists(path)
        isAtomic = false;

if (isAtomic)
{
    //Do things
}

      

But I can see that this is only happening now. Is there any other way to do this, or a direction to point to?

0


a source to share


3 answers


Unfortunately, it is actually impossible for it to be truly "atomic". My best advice would be to wrap your transaction form for you so that you can at least undo the changes.

I would do something like checking for each file - if it doesn't exist, throw it.

Backing up every file, saving the state you need to undo, or keeping a copy in memory if they're not huge. If you can't, quit.



Make your changes, then save the files. If you get a failure here, try restoring from each backup. Here you will need to handle some errors so you don't quit until all backups have been restored. Post your exception after recovery.

At least this way, you most likely won't change any files. Hopefully if you can change one file you can restore it from backup / undo your modification.

+2


a source


I suggest the following solution.

  • Try to open all write-locked files.
    • If one or more fail, cancel.
    • Change and clear all files.
      • If one or more fail, discard the ones already changed and flush them again.
  • Close all files.


If the rollback fails ... well ... try again, try again and try again ... and give up in a mismatched state.

If you have control over all the processes writing these files, you can implement a simple locking mechanism using a lock file. You can even write ahead and write scheduled changes to the lock file. If your process crashes, the next one trying to modify the files detects an incomplete operation and can continue before doing it with one modification.

+1


a source


I would provide a version of the files. You can do this easily by adding a suffix to the file name. for example a counting variable. The process for the reader is as follows:

  • prepare the next version of the file
  • write it to a temporary file with a different name.
  • Get the highest version number
  • add this version one by one
  • rename temp file to new file
  • delete old files (you can keep, for example, 2 of them)

as a reader you do - find the file with the highest revision - read it

0


a source







All Articles