[ python-Bugs-1607951 ] mailbox.Maildir re-reads directory too often

SourceForge.net noreply at sourceforge.net
Fri May 4 20:30:06 CEST 2007


Bugs item #1607951, was opened at 2006-12-03 09:28
Message generated for change (Comment added) made by joshtriplett
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1607951&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Python Library
Group: Python 2.5
Status: Open
Resolution: None
Priority: 5
Private: No
Submitted By: Matthias Klose (doko)
Assigned to: A.M. Kuchling (akuchling)
Summary: mailbox.Maildir re-reads directory too often

Initial Comment:
[forwarded from http://bugs.debian.org/401395]

Various functions in mailbox.Maildir call self._refresh, which always re-reads the cur and new directories with os.listdir.  _refresh should stat each of the two directories first to see if they changed.  This cuts processing time of a series of lookups down by a factor of the number of messages in the folder, a potentially large number.


----------------------------------------------------------------------

Comment By: Josh Triplett (joshtriplett)
Date: 2007-05-04 11:30

Message:
Logged In: YES 
user_id=1785566
Originator: NO

akuchling wrote:
> By stat()'ing the directories, do you mean checking the mtime?  I think
> this isn't safe because of the limited resolution of mtime on
filesystems;
> ext3 seems to have a 1-second resolution for mtime, for example.

Regardless of the resolution on any particular filesystem, stat and
stat64
return file timestamps as time_t, which has one-second resolution.

> This means that _refresh() might read a directory, and if some other
process
> adds or deletes a message in the same second, _refresh() couldn't detect
the
> change.

True.  mailbox.Maildir's behavior of always representing the current
contents
of the maildir without requiring the caller to explicitly refresh will
not
work with an implementation that checks for an mtime increase.

The two solutions below (inotify and your suggested mtime-1 approach)
would
allow the automatic updates to work efficiently.

> Is there some other property of directories that could be used for
> a more reliable check?

On Linux, you could use inotify to get a notice when anything changes in
the
directory.  You then wouldn't even incur the overhead of a repeated
directory
stat, and wouldn't need to re-read the entire directory to find the new
mail.

> The attached patch implements checking of mtime, but I don't recommend
> applying it; it causes the test suite in test_mailbox.py to break all
over
> the place, because the process modifies mailboxes so quickly that the
mtime
> check doesn't notice the process's own changes.

Fair enough.

> I'll wait a bit for any alternative suggestion, and then close this bug
as
> "won't fix".

Please don't.  The performance hit of repeatedly re-reading the directory
makes mailbox.Maildir unusably slow on even moderately large maildirs (a
few
thousand messages).  For the application we originally wanted
mailbox.Maildir
for, we had to rewrite the code to manually operate on the maildir
instead,
and we achieved an improvement of several orders of magnitude.

akuchling:
> Stray thought: would it help if the patch stored the (mtime - 1sec)
instead
> of the mtime?  Successive calls in the same second would then always
re-read
> the directories, but once the clock ticks to the next second, re-reads
would
> only occur if the directories have actually changed.  The check would be
'if
> new_mtime > self._new_mtime' instead of '=='.

Good idea.  That would work fine as well, and I believe it would have
addressed our performance problem.  This would work as a good solution on
platforms that lack inotify, or if you don't want to use inotify.

Compared to inotify, this would have somewhat more overhead (but still
far
less than the current approach), and would still perform poorly if you
insert
messages as you go (sometimes avoidable).  However, compared to the
current
approach, this has a massive improvement, so please consider implementing
it.

> Is this sort of mtime-based checking reliable on remote filesystems such
as
> NFS?

This particular sort of checking, yes, I think so.  The times do not
necessarily match the system clock (because they come from a remote
system
that does not necessarily have a synchronized clock), but the times
should
remain consistent with *each other*.

Thank you for looking at this problem,
Josh Triplett


----------------------------------------------------------------------

Comment By: A.M. Kuchling (akuchling)
Date: 2006-12-14 11:09

Message:
Logged In: YES 
user_id=11375
Originator: NO

Stray thought: would it help if the patch stored the (mtime - 1sec)
instead of the mtime?  Successive calls in the same second would then
always re-read the directories, but once the clock ticks to the next
second, re-reads would only occur if the directories have actually changed.
 The check would be 'if new_mtime > self._new_mtime' instead of '=='.

Is this sort of mtime-based checking reliable on remote filesystems such
as NFS?

----------------------------------------------------------------------

Comment By: A.M. Kuchling (akuchling)
Date: 2006-12-13 05:38

Message:
Logged In: YES 
user_id=11375
Originator: NO

By stat()'ing the directories, do you mean checking the mtime?  I think
this isn't safe because of the limited resolution of mtime on filesystems;
ext3 seems to have a 1-second resolution for mtime, for example.  This
means that _refresh() might read a directory, and if some other process
adds or deletes a message in the same second, _refresh() couldn't detect
the change.  Is there some other property of directories that could be used
for a more reliable check?

The attached patch implements checking of mtime, but I don't recommend
applying it; it causes the test suite in test_mailbox.py to break all over
the place, because the process modifies mailboxes so quickly that the mtime
check doesn't notice the process's own changes.

I'll wait a bit for any alternative suggestion, and then close this bug as
"won't fix".


File Added: mailbox-mtime.patch

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1607951&group_id=5470


More information about the Python-bugs-list mailing list