Check File Existence on NFS Disk from Different Host

风行水上 @ 2015-07-30 03:15:18
标签:

NFS(网络文件系统)本质上是一台服务器,多个客户端(host)可能并行地访问NFS上的文件。也因此会产生一些“竞争”或缓存方面的问题。

其中一个例子是“在HostA上创建文件,在HostB上检查该文件是确看不到这个文件”。

原因是:当前系统缓存了目录信息。

解决办法无非两个方向:

  • 循环等待进行查询,最长等待时间也许要不少于60秒
  • 查询文件存在之前,触发目录信息缓存的失效

用来测试的方法和Tcl脚本如下:

HostA % nfs-check.tcl touch /path/to/dir/tmp.1

HostB % nfs-check.tcl glob  /path/to/dir/tmp.1
HostB % nfs-check.tcl check /path/to/dir/tmp.1

File: "nfs-check.tcl"

#!/usr/bin/env tclsh

proc touch_file {file} {
  ::exec touch $file

  puts "[clock seconds] touch $file"
}

proc check_file {file} {
  if {[file exist $file]} {
    puts "[clock seconds] exist $file"
    exit
  }

  after 100 [list check_file $file]
}


proc glob_file {file} {
  glob -nocomplain [file dir $file] *

  if {[file exist $file]} {
    puts "[clock seconds] exist $file"
    exit
  }

  after 100 [list glob_file $file]
}

lassign $::argv act file

switch $act {
  touch {
    touch_file $file
  }
  check {
    check_file $file
    vwait forever
  }
  glob {
    glob_file $file
    vwait forever
  }
}
标签:

分享到:
comments powered by Disqus

21/23ms