database - Locking file in distributed system -


i have distributed application; is, have homogeneous process running on multiple computers talking central database , accessing network file share.

this process picks collection files network file share (via cifs), runs transformation algorithm on files , copies output onto network file share.

i need lock input files other servers -- running same process -- not work on same files. sake of argument, assume description oversimplified , locks absolute must.

here proposed solutions, , thoughts.

1) use opportunistic locks (oplocks). solution uses file system lock files. problem here have try lock find out if lock exists. seems can expensive network redirectors negotiate locks. nice thing oplocks can created in such way self delete when there error.

2) use database app locks (via sp_getapplock). seems faster, using database lock file system. also, database app locks can scoped via transaction or session means must hold onto connection if want hold onto -- , later release -- app lock. currently, using connection pooling, have change , may bigger conversation unto itself. nice thing approach locks cleaned if lose our connection server. of course, means if lose connection database, not network file share, lock goes away while still processing input files.

3) create database table , stored procedures represent items lock. process straight forward. down side of of course potential network errors. if reason, database becomes unreachable, lock remain in effect. need derive algorithm clean @ later date.

what best solution , why? answers not limited mentioned above.

for situation should use share-mode locks. made for.

oplocks won't want - oplock not lock, , doesn't prevent doing anything. it's notification mechanism let client machine know if accesses file. communicated machine "breaking" oplock, not makes way application layer (i.e. code) - generates message client operating system tell invalidate it's cached copy , fetch again server.

see msdn here:

the explanation of happens when process opens file on hold oplock here:

however important point oplocks not prevent other processes opening file, allow coordination between client computers. therefore, oplocks not lock file @ application level - feature of network protocol used network file system stack implement caching. not applications use.


since programming on windows appropriate solution seems share-mode locks, i.e. opening file share_deny_read|share_deny_write|share_deny_delete.

if share-mode locks not supported on cifs server, might consider flock() type locks. (named after traditional unix technique).

if processing xyz.xml create file called xyz.xml.lock (with create_new mode don't clobber existing one). once done, delete it. if fail create file because exists, means process working on it. might useful write information lockfile useful in debugging, such servername , pid. have have way of cleaning abandoned lock files since won't occur automatically.

database locks might appropriate if cifs example replicated system flock() lock not occur atomically across system. otherwise stick filesystem there 1 thing go wrong.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -