flock() and NFS

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rex Karz

    flock() and NFS

    Newbie [PHP] here.

    I interpret the fine print at
    http://us2.php.net/manual/en/function.flock. php
    to mean that flock() does not work where the file being locked is
    on an NFS filesystem.

    The commentary following the "official" description of flock()
    offers numerous circumventions broken flock() where the file
    needing atomic access is on an NFS filesystem.

    One comment offers a PHP function that creates a temporary file in
    the same directory (and on the NFS filesystem) as the target of
    the write with some code for ifexists to assure concurrency. Then
    another poster says that fopen() is not atomic, that mkdir() is
    atomic.

    There is much discussion of process interruption while holding the
    lockfile in these functions where to concequence of the
    interruption is a blocked site. The discussion continues with how
    to detect an interrupted process and recovery via detecting
    filetimes on the lockfile.

    Do any of these actually work? What's the truth?


    --
    The name, "Rex Karz" is a pseudonym. The From: email address
    is a spam trap. Messages sent to that address may cause the
    sender's IP-address to be listed in one or more DNSBls.

  • Peter Albertsson

    #2
    Re: flock() and NFS

    "Rex Karz" <RexKarz@gmail. com> wrote in message
    news:114dnrdki2 0ir66@news.supe rnews.com...[color=blue]
    > Newbie [PHP] here.
    >
    > I interpret the fine print at
    > http://us2.php.net/manual/en/function.flock. php
    > to mean that flock() does not work where the file being locked is on an
    > NFS filesystem.
    >
    > The commentary following the "official" description of flock() offers
    > numerous circumventions broken flock() where the file needing atomic
    > access is on an NFS filesystem.
    >
    > One comment offers a PHP function that creates a temporary file in the
    > same directory (and on the NFS filesystem) as the target of the write with
    > some code for ifexists to assure concurrency. Then another poster says
    > that fopen() is not atomic, that mkdir() is atomic.
    >
    > There is much discussion of process interruption while holding the
    > lockfile in these functions where to concequence of the interruption is a
    > blocked site. The discussion continues with how to detect an interrupted
    > process and recovery via detecting filetimes on the lockfile.
    >
    > Do any of these actually work? What's the truth?
    >
    >
    > --
    > The name, "Rex Karz" is a pseudonym. The From: email address
    > is a spam trap. Messages sent to that address may cause the
    > sender's IP-address to be listed in one or more DNSBls.
    >[/color]

    NFS or SMB shares do not support file locking, and I would guess, since not
    even the Berkeley DB can do file locking on network shares, that it is not
    possible.

    Use a lock file on the local filesystem instead, or any other locking
    mechanisms, such as semaphores or locks in a database if your files that
    needs to be locked may be accessed from multiple hosts.

    Concurrency equals head ache...

    Best Regards.

    Peter Albertsson


    Comment

    • Colin McKinnon

      #3
      Re: flock() and NFS

      Peter Albertsson wrote:
      [color=blue]
      > "Rex Karz" <RexKarz@gmail. com> wrote in message
      > news:114dnrdki2 0ir66@news.supe rnews.com...[color=green]
      >> Newbie [PHP] here.
      >>
      >> I interpret the fine print at
      >> http://us2.php.net/manual/en/function.flock. php
      >> to mean that flock() does not work where the file being locked is on an
      >> NFS filesystem.
      >>[/color]
      >
      > NFS or SMB shares do not support file locking, and I would guess, since
      > not even the Berkeley DB can do file locking on network shares, that it is
      > not possible.
      >[/color]

      Oh yes they do. Using 'flock()', only the machine on which the function is
      called knows about the lock. Both protocols support locking though. IIRC
      PHP's flock is a straight wrapper around the POSIX flock() function. PHP
      provides access to the fcntl() function (see Direct I/O) which will lock
      over NFS (depending on implementation of NFS).....but for practicality it's
      simpler to consider that conventional locking is not available.
      [color=blue][color=green]
      >> There is much discussion of process interruption while holding the
      >> lockfile in these functions where to concequence of the interruption is a
      >> blocked site. The discussion continues with how to detect an interrupted
      >> process and recovery via detecting filetimes on the lockfile.[/color][/color]

      Do the sums. How probable is contention? If it's even remotely possible that
      you may have contention in the time it takes to setup a lockfile, then you
      should be able to afford to look at a different solution.

      C.

      Comment

      • Raj Shekhar

        #4
        Re: flock() and NFS

        Rex Karz <RexKarz@gmail. com> writes:
        [color=blue]
        >
        > One comment offers a PHP function that creates a temporary file in the
        > same directory (and on the NFS filesystem) as the target of the write
        > with some code for ifexists to assure concurrency. Then another poster
        > says that fopen() is not atomic, that mkdir() is atomic.[/color]

        Is there an official documentation which says that mkdir is guaranteed
        to be atomic ? You best bet seems to be to

        - create a lock_status table
        - add a column in it (running_myscri pt)
        - check the value of running_myscrip t column. If it is true, then exit
        - if not true then update that column to true
        - Do your work
        - set running_myscrip t to false
        - exit
        [color=blue]
        >
        > Do any of these actually work? What's the truth?
        >[/color]

        The answer is 42 :-)

        --
        Raj Shekhar Y! : Operations Engineer
        MySQL DBA, programmer and slacker Y!IM : lunatech3007
        home : http://rajshekhar.net blog : http://rajshekhar.net/blog/

        Comment

        Working...