Stas Bekman [mailto:[EMAIL PROTECTED]] wrote:
> On Thu, 22 Mar 2001, David Harris wrote:
> > If you want to downgrade a lock from exclusive to shared, sync the
database
> > and change the lock status. This will allow other readers access to a
> > fully-written database. No one else will be allowed to write the
database
> > (requiring your process to have invalidated any cached data) until you
have
> > released the shared lock. No problem there.
>
> Are you sure? Doesn't it contradict with the fact that other readers have
> already cached the first 4k of data? And you have modified the database
> and possibly the first 4k during the write, so if this is the case, now
> readers have the wrong 4k in their cache.
>
> Or do you mean that when a process that switches from EX to SH, doesn't
> need to re-tie(), since it has *its* cache valid. Other process do need to
> re-tie when acquiring any kind of lock, if they don't have none yet.

Two points about switching from exclusive mode to shared mode:

(1) When downgrading from EX to SH, no other processes need to have cached
data invalidated because no one else can have the database open. There is no
cache in other processes, therefore none to be invalidated.

Explanation: Lets say the method for downgrading a lock from EX to SH is
like this: write data, sync(), flock(FLOCK_SH), read data. Until the
flock(FLOCK_SH) nobody else can have the database open because of the
exclusive lock. Therefore, there will not be any other processes with the
database open and the first 4k cached in memory when the sync() happens.

(2) When downgrading from EX to SH, our processes does not need to
invalidate cached data because its cached data is correct at the sync() and
the data on disk will not be changed until the database is closed.

Explanation: Again we downgrade form EX to SH by doing this: write data,
sync(), flock(FLOCK_SH), read data. Our cache remains valid the entire time
here. With the sync(), data in our cache is written to disk, so at that
point we are good. Then after the flock(FLOCK_SH) we are still good because
the shared lock prevents anyone else from writing to the database and
changing the data on disk. There is no need to do a re-tie().

David Harris
President, DRH Internet Inc.
[EMAIL PROTECTED]
http://www.drh.net/



Reply via email to