Print or die: how to check if a GPFS network mount

2019-07-07 02:25发布

问题:

I have a Perl script that writes to a file located on a GPFS network mounted storage point. There is a standard check open FILE or die $!.

The file system went down, but the script did not quit; it kept trying to write. The Perl script finished once the file system came back up. How can I check whether the file system can be written to, and die if it cannot?

The reason I ask this is because the last line of my Perl script removes the file's .tmp extension. This statement should never have been reached because at one point the script's output wasn't going anywhere. GPFS was down on the write point, but the read point was still up.

回答1:

print has a return code. Most people don't bother to check it. But it does have one.

http://perldoc.perl.org/functions/print.html

print Prints a string or a list of strings. Returns true if successful.

So yes, you can do 'print or die'. But you do need to make sure you bracket it correctly, so the 'or' associates with the right expression.

print 0 || warn "1 failed";
print 0 or warn "2 failed";

This will give two different results, because the former 'tests' the zero. where the latter tests the print. If you bracket the print, then this doesn't matter, which might be desirable for the sake of clarity.

So if testing - you can do:

open ( my $fh, "<", "bad_open" ); #opens read only, so prints will fail. 

print {$fh} 0,1,2 || warn "1 failed";
print {$fh} 1,2,3  or warn "2 failed";

(I was going to suggest autodie but sadly it doesn't get applied to print. )

Alternatively, looking at the manpage for GPFS:

http://www-01.ibm.com/support/knowledgecenter/SSFKCN_3.5.0/com.ibm.cluster.gpfs.v3r5.gpfs100.doc/bl1adm_gsmo.htm

Can I suggest checking your mount options? Is your GPFS NFS mounted on your client?

Looking at it, GPFS doesn't support soft mounting like NFS. By design, a soft mount will fail a write on a transient error, where hard mounts will block whilst the mount is offline.

hard is the default, because then you avoid data corruption - anything writing will block until the mount recovers. It looks like it's hanging which can seen undesirably, but bear in mind that it greatly improves data integrity.

I don't know if there's an easy test for a 'stalled' hard mount from within a script. If you're network mounting your GPFS over NFS, you may find you can simply mount it as soft instead (perhaps using a separate mountpoint and mounting twice if that'll affect others).