I use NFS V4 to test the java  FileLock.

The 192.168.1.233 machine is NFS Server,  the nfs configuration  are
/home/hdfs.ha/share  192.168.1.221(rw,sync,no_root_squash)
/home/hdfs.ha/share  192.168.1.222(rw,sync,no_root_squash)
in /etc/exports file.

I run below commands to start nfs server:
service nfs start
service nfslock start

The 192.168.1.221 and 192.168.1.222 machines are NFS Client, the nfs
configuration is
192.168.1.223:/home/hdfs.ha/share /home/hdfs.ha/share  nfs
rsize=8192,wsize=8192,timeo=14,intr in /etc/fstab file.

I run below commands to start nfs client:
service nfs start
service nfslock start

I write one programm to receive file lock:
public class FileLockTest {
 FileLock lock;

   public void lock(String path,boolean isShare) throws IOException {
       this.lock = tryLock(path,isShare);
       if (lock == null) {
         String msg = "Cannot lock storage " + path
             + ". The directory is already locked.";
        System.out.println(msg);
         throw new IOException(msg);
       }
     }

    private FileLock tryLock(String path,boolean isShare) throws
IOException {
        boolean deletionHookAdded = false;
        File lockF = new File(path);
        if (!lockF.exists()) {
          lockF.deleteOnExit();
          deletionHookAdded = true;
        }
        RandomAccessFile file = new RandomAccessFile(lockF, "rws");
        FileLock res = null;
        try {
          res = file.getChannel().tryLock(0,Long.MAX_VALUE,isShare);
        } catch (OverlappingFileLockException oe) {
          file.close();
          return null;
        } catch (IOException e) {
          e.printStackTrace();
          file.close();
          throw e;
        }
        if (res != null && !deletionHookAdded) {
          // If the file existed prior to our startup, we didn't
          // call deleteOnExit above. But since we successfully locked
          // the dir, we can take care of cleaning it up.
          lockF.deleteOnExit();
        }
        return res;
      }

    public static void main(String[] s) {
     FileLockTest fileLockTest =new FileLockTest();
     try {
      fileLockTest.lock(s[0], Boolean.valueOf(s[1]));

      Thread.sleep(1000*60*60*1);
     } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
    }
}

I do two test cases.

1. The network is OK
I run " java -cp ./filelock.jar lock.FileLockTest
/home/hdfs.ha/share/test.lock false" command in 192.168.1.221 to hold file
lock, and then I run same command to hold same file lock in 192.168.1.222,
throw below exception:
Cannot lock storage /home/hdfs.ha/share/test.lock. The directory is already
locked.
java.io.IOException: Cannot lock storage /home/hdfs.ha/share/test.lock. The
directory is already locked.
        at lock.FileLockTest.lock(FileLockTest.java:18)
        at lock.FileLockTest.main(FileLockTest.java:53)

2. machine which hold file lock is diconnected
I run " java -cp ./filelock.jar lock.FileLockTest
/home/hdfs.ha/share/test.lock false" command on 192.168.1.221,  then
192.168.1.221  machine is disconnected from network . After three minutes ,
I run the "  java -cp ./filelock.jar lock.FileLockTest
/home/hdfs.ha/share/test.lock false" command on 192.168.1.222, that can
hold the file lock.
 I use "mount | grep nfs" command to examine the mount nfs directory on
192.168.1.221, the share directory /home/hdfs.ha/share/ is disappear on
192.168.1.221 machine.  So I think when the machine is disconnected for
a long time, other machine can receive the same file lock.

Reply via email to