howto force refresh NFS cache when checking newly created file?

Gang picture Gang · Jan 27, 2016 · Viewed 16.9k times · Source

When a file is created on a Linux NFS shared mount, the client is either Linux o r mac machines. The existence or absence of a file is the key for what to do next, but the checking is not always return the right result:

e.g I am doing this in perl, this still not working well, esp with mac machines

write_key_file();  # write a file that must be checked before proceeding

The following checks cannot always return true when the file does exist

Issue - this command in perl did not return correct status in NFS system:

if( -e $file){}

Overheared solution that did not work:

 sleep(5);   # wait 5 seconds
 system("ls -ltr"); # force to cache?
 if(-e $file){}

I do not intend to check every file like this, but there are a few key places where it is important to get the proper file status.

Do we have better ways to force refresh nfs cache on a specific file on a specific directory? Thanks.

I am not sure that this is a XY problem, but there are a few weakest points that can all be solutions.

A -- NFS clients setting

If there is a solution at this stage, it would be great!

B -- exit_code or return code of writing function

$exit_code = write_key_file();

The problem with it, not all writing are within the scope of the code block. this only solve part of the problem.

C -- Disable NFS cache for the specific file or directory for file checking

I need to make sure is this possible and how? if no and why?

and I am open to all possible solutions, no solution or other possibilities.

Answer

TheCodeArtist picture TheCodeArtist · Feb 2, 2016

This solution belongs to the Category B : exit_code or return code of writing function

...only open() and fopen() need to guarantee that they get a consistent handle to a particular file for reading and writing. stat and friends are not required to retrieve fresh attributes. Thus, for the sake of close-to-open cache coherence, only open() and fopen() are considered an "open event" where fresh attributes need to be fetched immediately from the server[1].


The following solutions belong to the Category A : NFS clients setting
i.e. if you do NOT expect cached file/dir entries to be served to the client, disable caching.

Setup a shared cache

If the file in the NFS mount (whose existence is being checked) is created by another application on the same client (possibly using another mount point to the same NFS export) the consider using a single shared NFS cache on the client.

Use the sharecache option to setup the NFS mounts on the client.

This option determines how the client's data-cache and attribute-cache are shared when mounting the same export more than once concurrently. Using the same cache reduces memory requirements on the client and presents identical file contents to applications when the same remote file is accessed via different mount points.


Setting-up a NFS-mount without caching

Disable attribute caching.

Mount the NFS share on the client with the noac option.

Alternately, disable cached directory attributes from being served.

Use acdirmin=0,acdirmax=0 to set the cache timeouts to 0 (effectively disabling caching).


Setting-up a NFS-mount to ignore lookup caches

Use lookupcache=positive OR lookupcache=none

(available options : all, positive and none)

When attempting to access a directory entry over a NFS mount,
if the requested directory entry exists on the server, the result is referred to as positive.
if the requested directory entry does not exist on the server, the result is referred to as negative.

If the lookupcache option is not specified, or if all is specified, the client assumes both types of directory cache entries are valid until their parent directory's cached attributes expire.

If pos or positive is specified, the client assumes positive entries are valid until their parent directory's cached attributes expire, but always revalidates negative entires before an application can use them.

If none is specified, the client revalidates both types of directory cache entries before an application can use them. This permits quick detection of files that were created or removed by other clients, but can impact application and server performance.


References:
1. Close-To-Open Cache Consistency in the Linux NFS Client
2. NFS - Detecting remotely created files programmatically?
3. NFS cache : file content not updated on client when modified on server
4. NFS man page. Especially the "Data And Metadata Coherence" section.