Search code examples
javafilesharenfs

Polling NFS share from java breaks filesystem


We've got two servers having a directory mapped via NFS to each of them.

Process on server A (shell script) places file into mapped drive.

Java process on server B regularly polls mapped drive and processes the file as soon as it is found. After file is processed - it is renamed (and deleted by cron'ed process on server A afterwards)

The file is a small .properties file. Everything works well for couple of cycles. After that server A and server B start to see contents of mapped drive differently. We disabled NFS caching and attribute lookup. Problem is still there.

If I go to a server which has broken view and do:

ls

I'll see stalled files. BUT, if I do it again - correct file listing is printed.

We'd appreciate any help on the issue.


Solution

  • My problem was that when looping with timer, command below stops seeng actual directory content. It gets out of sync with NFS server.

    File[] files = dir.listFiles(new MyFileFilter() );
    

    The solution I've got is to request knowingly unexisting file. I assume that this makes NFS client to fix or refresh its state.

    // Key point is here: we ask NFS client to obtain unexisting file. Since the client
    // cannot find it locally - it will make a call to the NFS server which will 
    // fix client cache or whatever it uses locally when cache is disabled.
    File temp = new File(dir, "unexisting.file");
    temp.exists();
    

    If you know the mechanism behind this workaround - please share!