]> Sergey Matveev's repositories - public-inbox.git/log
public-inbox.git
3 years agodoc: update TODO and WIP 1.6.0 release notes
Eric Wong [Wed, 10 Jun 2020 18:39:09 +0000 (18:39 +0000)]
doc: update TODO and WIP 1.6.0 release notes

Lots of big changes coming   Thanks to The Linux Foundation for
sponsoring me to hack on this in 2020 :)

3 years agonntpd+imapd: detect replaced over.sqlite3
Eric Wong [Thu, 11 Jun 2020 00:57:53 +0000 (00:57 +0000)]
nntpd+imapd: detect replaced over.sqlite3

For v1 inboxes (and possibly v2 in the future, for VACUUM),
public-inbox-compact replaces over.sqlite3 with a new file.

This currently doesn't need an extra inotify watch descriptor
(or FD for kevent) at the moment, so it can coexist nicely for
systems w/o IO::KQueue or Linux::Inotify2.

3 years agoimap: introduce memory-efficient uo2m mapping
Eric Wong [Fri, 12 Jun 2020 23:49:24 +0000 (23:49 +0000)]
imap: introduce memory-efficient uo2m mapping

Since we limit our mailboxes slices to 50K and can guarantee a
contiguous UID space for those mailboxes, we can store a mapping
of "UID offsets" (not full UIDs) to Message Sequence Numbers as
an array of 16-bit unsigned integers in a 100K scalar.

For UID-only FETCH responses, we can momentarily unpack the
compact 100K representation to a ~1.6M Perl array of IV/UV
elements for a slight speedup.

Furthermore, we can (ab)use hash key deduplication in Perl5 to
deduplicate this 100K scalar across all clients with the same
mailbox slice open.

Technically we can increase our slice size to 64K w/o increasing
our storage overhead, but I suspect humans are more accustomed
to slices easily divisible by 10.

3 years agoimap: FETCH: proper MSN => UID mapping for requests
Eric Wong [Wed, 10 Jun 2020 07:05:19 +0000 (07:05 +0000)]
imap: FETCH: proper MSN => UID mapping for requests

This finally seems to make mutt header caching behave properly.

We expect to be able to safely load 50K IV/UVs in memory without
OOM, since that's "only" 1.6 MB that won't live beyond a single
event loop iteration.  So create a simple array which can
quickly map MSNs in requests to UIDs and not leave out messages.

MSNs in the FETCH response will NOT be correct, since it's
inefficient to implement properly and mutt doesn't seem to
care.

Since the conversion code is easily shared, "UID SEARCH" can
allow the same MSN => UID mapping non-UID "FETCH" does.

3 years agoover: uid_range: remove LIMIT
Eric Wong [Wed, 10 Jun 2020 07:05:18 +0000 (07:05 +0000)]
over: uid_range: remove LIMIT

The IMAP code already limits the range to UID_SLICE (50K),
so that's about 1.6MB of of IVs for an ephemeral allocation
that won't live beyond one iteration of the event loop.

3 years agoimap: remove non-UID SEARCH for now
Eric Wong [Wed, 10 Jun 2020 07:05:17 +0000 (07:05 +0000)]
imap: remove non-UID SEARCH for now

Supporting MSNs in long-lived connections beyond the lifetime of
a single request/response cycle is not scalable to a C10K
scenario.  It's probably not needed, since most clients seem to
use UIDs.

A somewhat efficient implementation I can come up uses
pack("S*" ...) (AKA "uint16_t mapping[50000]") has an overhead
of 100K per-client socket on a mailbox with 50K messages.  The
100K is a contiguous scalar, so it could be swapped out for
idle clients on most architectures if THP is disabled.

An alternative could be to use a tempfile as an allocator
partitioned into 100K chunks (or SQLite); but I'll only do that
if somebody presents a compelling case to support MSN SEARCH.

3 years agoimapd: don't bother sorting LIST output
Eric Wong [Wed, 10 Jun 2020 07:05:16 +0000 (07:05 +0000)]
imapd: don't bother sorting LIST output

The sort was unstable on my test instance anyways, and
clients don't seem to mind.  So stop wasting CPU cycles.

3 years agoimap: misc cleanups and notes
Eric Wong [Wed, 10 Jun 2020 07:05:15 +0000 (07:05 +0000)]
imap: misc cleanups and notes

Note some of our limitations for potential hackers.

We'll be renaming "UID_BLOCK" to "UID_SLICE", since "block" is
overused term and "slice" isn't used in our codebase.  Also,
document how "slice" and "epochs" are similar concepts for
different clients.

3 years agoimap: wire up Xapian, MSN SEARCH and multi sequence-sets
Eric Wong [Wed, 10 Jun 2020 07:05:14 +0000 (07:05 +0000)]
imap: wire up Xapian, MSN SEARCH and multi sequence-sets

Simple queries work, more complex queries involving parentheses,
"OR", "NOT" don't work, yet.

Tested with "=b", "=B", and "=H" search and limits in mutt
on both v1 and v2 with multiple Xapian shards.

3 years agoimap: UID SEARCH: support multiple ranges
Eric Wong [Wed, 10 Jun 2020 07:05:13 +0000 (07:05 +0000)]
imap: UID SEARCH: support multiple ranges

We can share a bit of code with FETCH to refill UID
ranges which hit the SQLite overview.

3 years agoimap: STATUS/EXAMINE: rely on SQLite overview
Eric Wong [Wed, 10 Jun 2020 07:05:12 +0000 (07:05 +0000)]
imap: STATUS/EXAMINE: rely on SQLite overview

We can get exact values for EXISTS, UIDNEXT using SQLite
rather than calculating off $ibx->mm->max ourselves.

Furthermore, $ibx->mm is less useful than $ibx->over for IMAP
(and for our read-only daemons in general) so do not depend on
$ibx->mm outside of startup/reload to save FDs and reduce kernel
page cache footprint.

3 years agoimap: FETCH: try to make fake MSNs sequentially
Eric Wong [Wed, 10 Jun 2020 07:05:11 +0000 (07:05 +0000)]
imap: FETCH: try to make fake MSNs sequentially

This appears to significantly improve header caching behavior
with mutt.  With the current public-inbox.org/git mirror(*),
mutt will only re-FETCH the last ~300 or so messages in the
final "inbox.comp.version-control.git.7" mailbox, instead of
~49,000 messages every time.

It's not perfect, but a 500ms query is better than a >10s query
and mutt itself spends as much time loading its header cache.

(*) there are many gaps in NNTP article numbers (UIDs) due to
    spam removal from public-inbox-learn.

3 years agoimap: further speed up HEADER.FIELDS FETCH requests
Eric Wong [Wed, 10 Jun 2020 07:05:10 +0000 (07:05 +0000)]
imap: further speed up HEADER.FIELDS FETCH requests

Since headers are big and include a lot of lines MUAs don't
care about, we can skip the CRLF_HDR ops and just do the
CRLF conversion in partial_hdr_get and partial_hdr_not.
This is another 10-15% speedup for mutt w/o header caching.

3 years agoimap: FETCH: more granular CRLF conversion
Eric Wong [Wed, 10 Jun 2020 07:05:09 +0000 (07:05 +0000)]
imap: FETCH: more granular CRLF conversion

This speeds up requests from mutt for HEADER.FIELDS by around 10%
since we don't waste time doing CRLF conversion on large message
bodies that get discarded, anyways.

3 years agoimap: cleanup ->{uid_base} usage
Eric Wong [Wed, 10 Jun 2020 07:05:08 +0000 (07:05 +0000)]
imap: cleanup ->{uid_base} usage

Ensure {uid_base} is always set, so we don't need to add `//'
checks everywhere.  Furthermore, this fixes a hard-to-test bug
where the STATUS command would inadvertantly clobber {uid_base}.

3 years agoimap: reinstate some message sequence number support
Eric Wong [Wed, 10 Jun 2020 07:05:07 +0000 (07:05 +0000)]
imap: reinstate some message sequence number support

The performance problem with mutt not using header caches isn't
fixed, yet, but mutt header caching seems to depend on MSNs
(message sequence numbers).  We'll switch to storing the 0-based
{uid_base} instead of the 1-based {uid_min} since it simplifies
most of our code.

3 years agoimap: support 8000 octet lines
Eric Wong [Wed, 10 Jun 2020 07:05:06 +0000 (07:05 +0000)]
imap: support 8000 octet lines

RFC 2683 section 3.2.1.5 recommends it:

> For its part, a server should allow for a command line of at least
> 8000 octets.  This provides plenty of leeway for accepting reasonable
> length commands from clients.  The server should send a BAD response
> to a command that does not end within the server's maximum accepted
> command length.

To conserve memory, we won't bother reading the entire line
before sending the BAD response and disconnecting them.

3 years agoimap: LIST shows "INBOX" in all caps
Eric Wong [Wed, 10 Jun 2020 07:05:05 +0000 (07:05 +0000)]
imap: LIST shows "INBOX" in all caps

While selecting a mailbox is done case-insensitively, "INBOX" is
special for the LIST command, according to RFC 3501 6.3.8:

> The special name INBOX is included in the output from LIST, if
> INBOX is supported by this server for this user and if the
> uppercase string "INBOX" matches the interpreted reference and
> mailbox name arguments with wildcards as described above.  The
> criteria for omitting INBOX is whether SELECT INBOX will
> return failure; it is not relevant whether the user's real
> INBOX resides on this or some other server.

Thus, the existing news.public-inbox.org convention of naming
newsgroups starting with "inbox." needs to be special-cased to
not confuse clients.

While we're at it, do not create ".0" for dummy newsgroups if
they're selected, either.

3 years agoimap: UID FETCH requires at least one data item
Eric Wong [Wed, 10 Jun 2020 07:05:04 +0000 (07:05 +0000)]
imap: UID FETCH requires at least one data item

It seems required based on my reading of RFC 3501 for
the non-UID "FETCH" command.

3 years agoimap: rely on smsg->{bytes} for RFC822.SIZE
Eric Wong [Wed, 10 Jun 2020 07:05:03 +0000 (07:05 +0000)]
imap: rely on smsg->{bytes} for RFC822.SIZE

Since we started indexing the CRLF-adjusted size of messages,
we can take an order-of-magnitude speedup for certain MUAs
which fetch this attribute without needing much else.

Admins are encouraged to --reindex existing inboxes for IMAP
support, anyways.  It won't be fatal if it's not reindexed, but
some client bugs and warnings can be fixed and they'll be able
to support more of IMAP.

3 years agoindex: account for CRLF conversion when storing bytes
Eric Wong [Wed, 10 Jun 2020 07:05:02 +0000 (07:05 +0000)]
index: account for CRLF conversion when storing bytes

NNTP and IMAP both require CRLF conversions on the wire.
They're also the only components which care about
$smsg->{bytes}, so store the CRLF-adjusted value in over.sqlite3
and Xapian DBs..

This will allow us to optimize RFC822.SIZE fetch item in IMAP
without triggering size mismatch errors in some clients' default
configurations (e.g. Mail::IMAPClient), but not most others.

It could also fix hypothetical problems with NNTP clients that
report discrepancies between overview and article data.

3 years agosearchidx: v1 (re)-index uses git asynchronously
Eric Wong [Wed, 10 Jun 2020 07:05:01 +0000 (07:05 +0000)]
searchidx: v1 (re)-index uses git asynchronously

We can cleanup some of our v1 code slightly and let git do
I/O+decoding in parallel.  This gives a slight 2-4%
re-indexing performance boost even on an SSD.

3 years agoimap: split ->logged_in attribute into a separate class
Eric Wong [Wed, 10 Jun 2020 07:05:00 +0000 (07:05 +0000)]
imap: split ->logged_in attribute into a separate class

This is one boolean attribute not worth wasting space for.
With 20000 sockets, this reduces RSS by around 5% at a glance,
and locked hashes doesn't do us much good when clients
use compression, anyways.

3 years agoimap: 30 minute auto-logout timer
Eric Wong [Wed, 10 Jun 2020 07:04:59 +0000 (07:04 +0000)]
imap: 30 minute auto-logout timer

RFC 3501 section 5.4 requires this to be >= 30 minutes,
10x higher than what is recommended for NNTP.  Fortunately
our design is reasonably memory-efficient despite being Perl.

3 years agoimap: IDLE: avoid extraneous wakeups, keep-alive
Eric Wong [Wed, 10 Jun 2020 07:04:58 +0000 (07:04 +0000)]
imap: IDLE: avoid extraneous wakeups, keep-alive

We should not waste memory for IDLE unless it's used on the most
recent inbox slice.  We also need to keep the IDLE connection
alive regardless of $PublicInbox::DS::EXPTIME.

3 years agoimap: UID FETCH: optimize (UID FLAGS) harder
Eric Wong [Wed, 10 Jun 2020 07:04:57 +0000 (07:04 +0000)]
imap: UID FETCH: optimize (UID FLAGS) harder

We can speed up this common mutt request by another 2-3x by not
loading the entire smsg from SQLite, just the UID.

3 years agoimap: UID FETCH: optimize for smsg-only case
Eric Wong [Wed, 10 Jun 2020 07:04:56 +0000 (07:04 +0000)]
imap: UID FETCH: optimize for smsg-only case

We can avoid loading the entire message from git when mutt makes
a "UID FETCH" request for "(UID FLAGS)".  This speeds mutt up by
more than an order-of-magnitude in informal measurements.

3 years agoimap: compile UID FETCH to opcodes
Eric Wong [Wed, 10 Jun 2020 07:04:55 +0000 (07:04 +0000)]
imap: compile UID FETCH to opcodes

This is just a hair faster and cacheable in the future, if we
need it.  Most notably, this avoids doing PublicInbox::Eml->new
for simple "RFC822", "BODY[]", and "RFC822.SIZE" requests.

3 years agoimap: remove dummies from sequence number FETCH
Eric Wong [Wed, 10 Jun 2020 07:04:54 +0000 (07:04 +0000)]
imap: remove dummies from sequence number FETCH

Dummy messages make for bad user experience with MUAs which
still use sequence numbers.  Not being able to fetch a message
doesn't seem fatal in mutt, so just ignore (sometimes large)
gaps.

3 years agosearch: index UID for IMAP search, too
Eric Wong [Wed, 10 Jun 2020 07:04:53 +0000 (07:04 +0000)]
search: index UID for IMAP search, too

We'll need to support searching UID ranges for IMAP,
so make sure it's indexed, too.

3 years agosearch: index byte size of a message for IMAP search
Eric Wong [Wed, 10 Jun 2020 07:04:52 +0000 (07:04 +0000)]
search: index byte size of a message for IMAP search

Searching for messages smaller than a certain size is allowed by
offlineimap(1), mbsync(1), and possibly other tools.  Maybe
public-inbox-watch will support it, too.

I don't see a reason to expose searching by size via WWW search
right now (but maybe in the future, I could be convinced to).

Note: we only store the byte-size of the message in git,
this is typically LF-only and we won't have the correct
size after CRLF conversion for NNTP or IMAP.

3 years agoover: get_art: use dbh->prepare_cached
Eric Wong [Wed, 10 Jun 2020 07:04:51 +0000 (07:04 +0000)]
over: get_art: use dbh->prepare_cached

This speeds up xt/imapd-validate.t by around 10% when used with
an abandoned patch to remove ->query_xover.  We may also depend
on this further if we abandon storing doc_data in Xapian to save
disk space.

3 years agoimap: allow UID range search on timestamps
Eric Wong [Wed, 10 Jun 2020 07:04:50 +0000 (07:04 +0000)]
imap: allow UID range search on timestamps

Since it seems somewhat common for IMAP clients to limit
searches by sent Date: or INTERNALDATE, we can rely on
the NNTP/WWW-optimized overview DB.

For other queries, we'll have to depend on the Xapian DB.

3 years agoimap: SEARCH: clamp results to the 50K UID range
Eric Wong [Wed, 10 Jun 2020 07:04:49 +0000 (07:04 +0000)]
imap: SEARCH: clamp results to the 50K UID range

We won't support searching across mailboxes, just yet;
but maybe in the future.

3 years agoimap: start parsing out queries for SQLite and Xapian
Eric Wong [Wed, 10 Jun 2020 07:04:48 +0000 (07:04 +0000)]
imap: start parsing out queries for SQLite and Xapian

None of the new cases are wired up, yet, but existing cases
still work.

3 years agoimap: avoid uninitialized warnings on incomplete commands
Eric Wong [Wed, 10 Jun 2020 07:04:47 +0000 (07:04 +0000)]
imap: avoid uninitialized warnings on incomplete commands

No point in spewing "uninitialized" warnings into logs when
the cat jumps on the Enter key.

3 years agoimap: EXAMINE/STATUS: return correct counts
Eric Wong [Wed, 10 Jun 2020 07:04:46 +0000 (07:04 +0000)]
imap: EXAMINE/STATUS: return correct counts

We can share code between them and account for each 50K
mailbox slice.  However, we must overreport these for
non-zero slices and just return lots of empty data for
high-numbered slices because some MUAs still insist
on non-UID fetches.

3 years agoimap: STATUS and LIST are case-insensitive, too
Eric Wong [Wed, 10 Jun 2020 07:04:45 +0000 (07:04 +0000)]
imap: STATUS and LIST are case-insensitive, too

Some clients insist on sending "INBOX" in all caps,
since it's special in RFC 3501.

3 years agoxt/*: show some tunable parameters
Eric Wong [Wed, 10 Jun 2020 07:04:44 +0000 (07:04 +0000)]
xt/*: show some tunable parameters

This will make it easier to show parameters used for testing
and potential tweaks to be made.

3 years agot/config.t: always compare against git bool behavior
Eric Wong [Wed, 10 Jun 2020 07:04:43 +0000 (07:04 +0000)]
t/config.t: always compare against git bool behavior

We'll use the xqx() to avoid losing too much performance
compared to normal `backtick` (qx) when testing using
"make check-run" + Inline::C.

3 years agoimap: omit $UID_END from mailbox name, use index
Eric Wong [Wed, 10 Jun 2020 07:04:42 +0000 (07:04 +0000)]
imap: omit $UID_END from mailbox name, use index

Having two large numbers separated by a dash can make visual
comparisons difficult when numbers are in the 3,000,000 range
for LKML.  So avoid the $UID_END value, since it can be
calculated from $UID_MIN.  And we can avoid large values of
$UID_MIN, too, by instead storing the block index and just
multiplying it by 50000 (and adding 1) on the server side.

Of course, LKML still goes up to 72, at the moment.

3 years agoimapd: ensure LIST is sorted alphabetically, for now
Eric Wong [Wed, 10 Jun 2020 07:04:41 +0000 (07:04 +0000)]
imapd: ensure LIST is sorted alphabetically, for now

I'm not sure this matters, and it could be a waste of
CPU cycles if no real clients care.  However, it does
make debugging over telnet or s_client a bit easier.

3 years agoimap: require ".$UID_MIN-$UID_END" suffix
Eric Wong [Wed, 10 Jun 2020 07:04:40 +0000 (07:04 +0000)]
imap: require ".$UID_MIN-$UID_END" suffix

Finish up the IMAP-only portion of iterative config reloading,
which allows us to create all sub-ranges of an inbox up front.
The InboxIdler still uses ->each_inbox which will struggle with
100K inboxes.

Having messages in the top-level newsgroup name of an inbox will
still waste bandwidth for clients which want to do full syncs
once there's a rollover to a new 50K range.  So instead, make
every inbox accessible exclusively via 50K slices in the form of
"$NEWSGROUP.$UID_MIN-$UID_END".

This introduces the DummyInbox, which makes $NEWSGROUP
and every parent component a selectable, empty inbox.
This aids navigation with mutt and possibly other MUAs.

Finally, the xt/perf-imap-list maintainer test is broken, now,
so remove it.  The grep perlfunc is already proven effective,
and we'll have separate tests for mocking out ~100k inboxes.

3 years agoimap: start doing iterative config reloading
Eric Wong [Wed, 10 Jun 2020 07:04:39 +0000 (07:04 +0000)]
imap: start doing iterative config reloading

This will be used to prevent reloading a giant config with
tens/hundreds of thousands of inboxes from blocking the event
loop.

3 years agoimap: break giant inboxes into sub-inboxes of 50K messages
Eric Wong [Wed, 10 Jun 2020 07:04:38 +0000 (07:04 +0000)]
imap: break giant inboxes into sub-inboxes of 50K messages

This limit on mailbox size should keep users of tools like
mbsync (isync) and offlineimap happy, since typical filesystems
struggle with giant Maildirs.

I chose 50K since it's a bit more than what LKML typically sees
in a month and still manages to give acceptable performance on
my ancient Centrino laptop.

There were also no responses to my original proposal at:
<https://public-inbox.org/meta/20200519090000.GA24273@dcvr/>
so no objections, either :>

3 years agoimap: case-insensitive mailbox name comparisons
Eric Wong [Wed, 10 Jun 2020 07:04:37 +0000 (07:04 +0000)]
imap: case-insensitive mailbox name comparisons

IMAP RFC 3501 stipulates case-insensitive comparisons, and so
does RFC 977 (NNTP).  However, INN (nnrpd) uses case-sensitive
comparisons, so we've always used case-sensitive comparisons for
NNTP to match nnrpd behavior.

Unfortunately, some IMAP clients insist on sending "INBOX" with
caps, which causes problems for us.  Since NNTP group names are
typically all lowercase anyways, just force all comparisons to
lowercase for IMAP and warn admins if uppercase-containing
newsgroups won't be accessible over IMAP.

This ensures our existing -nntpd behavior remains unchanged
while being compatible with the expectations of real-world IMAP
clients.

3 years agoxt/perf-imap-list: time refresh_inboxlist
Eric Wong [Wed, 10 Jun 2020 07:04:36 +0000 (07:04 +0000)]
xt/perf-imap-list: time refresh_inboxlist

It's useful to know how fast SIGHUP can be handled, too.

3 years agoimap: support out-of-bounds ranges
Eric Wong [Wed, 10 Jun 2020 07:04:35 +0000 (07:04 +0000)]
imap: support out-of-bounds ranges

"$UID_START:*" needs to return at least one message according
to RFC 3501 section 6.4.8.

While we're in the area, coerce ranges to (unsigned) integers by
adding zero ("+ 0") to reduce memory overhead.

3 years agoxt: add imapd-validate and imapd-mbsync-oimap
Eric Wong [Wed, 10 Jun 2020 07:04:34 +0000 (07:04 +0000)]
xt: add imapd-validate and imapd-mbsync-oimap

imapd-validate is a beefed up version of our nntpd-validate test
which hammers the server with parallel connections over regular
IMAP, IMAPS, IMAP+STARTTLS; and COMPRESS=DEFLATE variants of
each of those.  It uses $START_UID:$END_UID fetch ranges to
reduce requests and slurp many responses at once to saturate
"git cat-file --batch" processes.

mbsync(1) also uses pipelining extensively (but IMHO
unnecessarily), so it was able to shake out some bugs in
the async git code.

Finally, we remove xt/cmp-imapd-compress.t since it's
redundant now that we have PublicInbox::IMAPClient to work
around bugs in Mail::IMAPClient.

3 years agoimapclient: wrapper for Mail::IMAPClient
Eric Wong [Wed, 10 Jun 2020 07:04:33 +0000 (07:04 +0000)]
imapclient: wrapper for Mail::IMAPClient

We'll be using this wrapper class to workaround some upstream
bugs in Mail::IMAPClient.  There may also be experiments with
new APIs for more performance.

3 years agogit: async: automatic retry on alternates change
Eric Wong [Wed, 10 Jun 2020 07:04:32 +0000 (07:04 +0000)]
git: async: automatic retry on alternates change

This matches the behavior of the existing synchronous ->cat_file
method.  In fact, ->cat_file now becomes a small wrapper around
the ->cat_async method.

3 years agogit: move async_cat reference to PublicInbox::Git
Eric Wong [Wed, 10 Jun 2020 07:04:31 +0000 (07:04 +0000)]
git: move async_cat reference to PublicInbox::Git

Trying to avoid a circular reference by relying on $ibx object
here makes no sense, since skipping GitCatAsync::close will
result in an FD leak, anyways.  So keep GitAsyncCat contained to
git-only operations, since we'll be using it for Solver in the
distant feature.

3 years agogit: cat_async: provide requested OID + "missing" on missing blobs
Eric Wong [Wed, 10 Jun 2020 07:04:30 +0000 (07:04 +0000)]
git: cat_async: provide requested OID + "missing" on missing blobs

This will make it easier to implement the retries on
alternates_changed() of the synchronous ->cat_file API.

3 years agoimap: fix pipelining with async git
Eric Wong [Wed, 10 Jun 2020 07:04:29 +0000 (07:04 +0000)]
imap: fix pipelining with async git

Since IMAP yields control to GitAsyncCat, IMAP->event_step may
be invoked with {long_cb} still active.  We must be sure to
bail out of IMAP->event_step if that happens and continue to let
GitAsyncCat drive IMAP.

This also improves fairness by never processing more than one
request per ->event_step.

3 years ago*deflate: drop invalid comment about rbuf
Eric Wong [Wed, 10 Jun 2020 07:04:28 +0000 (07:04 +0000)]
*deflate: drop invalid comment about rbuf

It must be a scalar reference, unlike ->write

3 years agotestcommon: tcp_(server|connect): BAIL_OUT on failure
Eric Wong [Wed, 10 Jun 2020 07:04:27 +0000 (07:04 +0000)]
testcommon: tcp_(server|connect): BAIL_OUT on failure

None of our tests rely on this failing, so just bail out
if the system is out of resources.

3 years agoadd imapd compression test
Eric Wong [Wed, 10 Jun 2020 07:04:26 +0000 (07:04 +0000)]
add imapd compression test

Include a test for Mail::IMAPTalk, here, since Mail::IMAPClient
stalls with compression enabled:
https://rt.cpan.org/Ticket/Display.html?id=132720

3 years agoimap: FETCH: support comma-delimited ranges
Eric Wong [Wed, 10 Jun 2020 07:04:25 +0000 (07:04 +0000)]
imap: FETCH: support comma-delimited ranges

The RFC 3501 `sequence-set' definition allows comma-delimited
ranges, so we'll support it in case clients send them.

Coalescing overlapping ranges isn't required, so we won't
support it as such an attempt to save bandwidth would waste
memory on the server, instead.

3 years agoimap: support LSUB command
Eric Wong [Wed, 10 Jun 2020 07:04:24 +0000 (07:04 +0000)]
imap: support LSUB command

Since we only support read-only operation, we can't save
subscriptions requested by clients.  So just list no inboxes as
subscribed, some MUAs may blindly try to fetch everything its
subscribed to.

3 years agogit: idle rbuf for async
Eric Wong [Wed, 10 Jun 2020 07:04:23 +0000 (07:04 +0000)]
git: idle rbuf for async

We do this for the C10K-oriented HTTP/NNTP/IMAP processes, and
we may support thousands of git-cat-file processes in the
future.

3 years agoimap: use git-cat-file asynchronously
Eric Wong [Wed, 10 Jun 2020 07:04:22 +0000 (07:04 +0000)]
imap: use git-cat-file asynchronously

This ought to improve overall performance with multiple clients.
Single client performance suffers a tiny bit due to extra
syscall overhead from epoll.

This also makes the existing async interface easier-to-use,
since calling cat_async_begin is no longer required.

3 years agogit: do our own read buffering for cat-file
Eric Wong [Wed, 10 Jun 2020 07:04:21 +0000 (07:04 +0000)]
git: do our own read buffering for cat-file

To work with our event loop, we must perform read buffering
ourselves or risk starvation, as there doesn't appear to be
a way to check the amount of data buffered in userspace by
by the PerlIO layers without resorting to C or XS.

This lets us perform fewer syscalls at the expense of more Perl
ops.  As it stands, there seems to be a tiny performance
improvement, but more will be possible in the future.

3 years agogit: async: flatten the inflight array
Eric Wong [Wed, 10 Jun 2020 07:04:20 +0000 (07:04 +0000)]
git: async: flatten the inflight array

Small array refs have considerable overhead in Perl, so reduce
AV/SV overhead and instead allow the inflight array to grow
twice as large.

3 years agoimap: speed up HEADER.FIELDS[.NOT] range fetches
Eric Wong [Wed, 10 Jun 2020 07:04:19 +0000 (07:04 +0000)]
imap: speed up HEADER.FIELDS[.NOT] range fetches

While we can't memoize the regexp forever like we do with other
Eml users, we can still benefit from caching regexp compilation
on a per-request basis.

A FETCH request from mutt on a 4K message inbox is around 8%
faster after this.  Since regexp compilation via qr// isn't
unbearably slow, a shared cache probably isn't worth the
trouble of implementing.  A per-request cache seems enough.

3 years agoimap: support the CLOSE command
Eric Wong [Wed, 10 Jun 2020 07:04:18 +0000 (07:04 +0000)]
imap: support the CLOSE command

It seems worthless to support CLOSE for read-only inboxes, but
mutt sends it, so don't return a BAD error with proper use.

3 years agoimap: do not include ".PEEK" in responses
Eric Wong [Wed, 10 Jun 2020 07:04:17 +0000 (07:04 +0000)]
imap: do not include ".PEEK" in responses

They're not specified in RFC 3501 for responses, and at least
mutt fails to handle it.

3 years agoimap: support sequence number FETCH
Eric Wong [Wed, 10 Jun 2020 07:04:16 +0000 (07:04 +0000)]
imap: support sequence number FETCH

We'll return dummy messages for now when sequence numbers go
missing, in case clients can't handle missing messages.

3 years agoimap: simplify partial fetch structure
Eric Wong [Wed, 10 Jun 2020 07:04:15 +0000 (07:04 +0000)]
imap: simplify partial fetch structure

While the contents of normal %want hash keys are bounded in
size, %partial can cause more overhead and lead to repeated sort
calls on multi-message fetches.  So sort it once and use
arrayrefs to make the data structure more compact.

3 years agoimap: fix multi-message partial header fetches
Eric Wong [Wed, 10 Jun 2020 07:04:14 +0000 (07:04 +0000)]
imap: fix multi-message partial header fetches

We must keep the contents of {-partial} around when handling
a request to fetch multiple messages.

3 years agoimap: split out unit tests and benchmarks
Eric Wong [Wed, 10 Jun 2020 07:04:13 +0000 (07:04 +0000)]
imap: split out unit tests and benchmarks

This makes the test code easier-to-manage and allows us to run
faster unit tests which don't involve loading Mail::IMAPClient.

3 years agoimap: always include `resp-text' in responses
Eric Wong [Wed, 10 Jun 2020 07:04:12 +0000 (07:04 +0000)]
imap: always include `resp-text' in responses

Mail::IMAPClient doesn't seem to mind the lack of `resp-text';
but it's required by RFC 3501.  Preliminary tests with
offlineimap(1) indicates the presence of `resp-text' is
necessary, even if it's just the freeform `text'.

And make the `text' more consistent, favoring "done" over
"complete" or "completed"; while we're at it.

3 years agoimap: allow fetch of partial of BODY[...] and headers
Eric Wong [Wed, 10 Jun 2020 07:04:11 +0000 (07:04 +0000)]
imap: allow fetch of partial of BODY[...] and headers

IMAP supports a high level of granularity when it comes to
fetching, but fortunately Perl makes it fairly easy to support.

3 years agoeml: each_part: single part $idx is 1
Eric Wong [Wed, 10 Jun 2020 07:04:10 +0000 (07:04 +0000)]
eml: each_part: single part $idx is 1

Instead of counts starting at 0, we start the single-part
message at 1 like we do with subparts of a multipart message.
This will make it easier to map offsets for "BODY[$SECTION]"
when using IMAP FETCH, since $SECTION must contain non-zero
numbers according to RFC 3501.

This doesn't make any difference for WWW URLs, since single part
messages cannot have downloadable attachments.

3 years agoimap: support fetch for BODYSTRUCTURE and BODY
Eric Wong [Wed, 10 Jun 2020 07:04:09 +0000 (07:04 +0000)]
imap: support fetch for BODYSTRUCTURE and BODY

I'm not sure which clients use these, but it could be useful
down the line.

3 years agot/imapd: support FakeInotify and KQNotify
Eric Wong [Wed, 10 Jun 2020 07:04:08 +0000 (07:04 +0000)]
t/imapd: support FakeInotify and KQNotify

We can fill in some missing pieces from the emulation APIs
to enable IMAP IDLE tests on non-Linux platforms.

3 years agoimap: support LIST command
Eric Wong [Wed, 10 Jun 2020 07:04:07 +0000 (07:04 +0000)]
imap: support LIST command

We'll optimize for the common case of: $TAG LIST "" *
and rely on the grep perlfunc to handle trickier cases.

3 years agoimap: use Text::ParseWords::parse_line to handle quoted words
Eric Wong [Wed, 10 Jun 2020 07:04:06 +0000 (07:04 +0000)]
imap: use Text::ParseWords::parse_line to handle quoted words

IMAP clients may quote args and escape similar to POSIX shell,
so attempt to handle them properly using this standard library
module.

3 years agoimap: implement STATUS command
Eric Wong [Wed, 10 Jun 2020 07:04:05 +0000 (07:04 +0000)]
imap: implement STATUS command

I'm not sure if there's much use for this command, but it's
part of RFC3501 and works read-only.

3 years agoimap: delay InboxIdle start, support refresh
Eric Wong [Wed, 10 Jun 2020 07:04:04 +0000 (07:04 +0000)]
imap: delay InboxIdle start, support refresh

InboxIdle should not be holding onto Inbox objects after the
Config object they came from expires, and Config objects may
expire on SIGHUP.

Old Inbox objects still persist due to IMAP clients holding onto
them, but that's a concern we'll deal with at another time, or
not at all, since all clients expire, eventually.

Regardless, stale inotify watch descriptors should not be left
hanging after SIGHUP refreshes.

3 years agomsgmap: split ->max into its own method
Eric Wong [Wed, 10 Jun 2020 07:04:03 +0000 (07:04 +0000)]
msgmap: split ->max into its own method

There's enough places where we only care about the max NNTP
article number to warrant avoiding a call into SQLite.

Using ->num_highwater in read-only packages such as
PublicInbox::IMAP is also incorrect, since that memoizes
and won't pick up changes made by other processes.

3 years agoimap: support IDLE
Eric Wong [Wed, 10 Jun 2020 07:04:02 +0000 (07:04 +0000)]
imap: support IDLE

It seems to be working as far as Mail::IMAPClient is concerned.

3 years agoinboxidle: new class to detect inbox changes
Eric Wong [Wed, 10 Jun 2020 07:04:01 +0000 (07:04 +0000)]
inboxidle: new class to detect inbox changes

This will be used to implement IMAP IDLE, first.

Eventually, it may be used to trigger other things:

* incremental internal updates for manifest.js.gz
* restart `git cat-file' processes on pack index unlink
* IMAP IDLE-like long-polling HTTP endpoint

And maybe more things we haven't thought of, yet.

It uses Linux::Inotify2 or IO::KQueue depending on what packages
are installed and what the kernel supports.  It falls back to
nanosecond-aware Time::HiRes::stat() (available with Perl 5.10.0+)
on systems lacking Linux::Inotify2 and IO::KQueue.

In the future, a pure Perl alternative to Linux::Inotify2 may be
supplied for users of architectures we already support signalfd
and epoll on.

v2 changes:
- avoid O_TRUNC on lock file
- change ctime on Linux systems w/o inotify
- fix naming of comments and fields

3 years agopreliminary imap server implementation
Eric Wong [Wed, 10 Jun 2020 07:04:00 +0000 (07:04 +0000)]
preliminary imap server implementation

It shares a bit of code with NNTP.  It's copy+pasted for now
since this provides new ground to experiment with APIs for
dealing with slow storage and many inboxes.

3 years agonntpd: restrict allowed newsgroup names
Eric Wong [Wed, 10 Jun 2020 07:03:59 +0000 (07:03 +0000)]
nntpd: restrict allowed newsgroup names

We'll be using newsgroup names as mailbox names for IMAP,
too, so ensure we don't send wonky characters in responses.

I doubt this affects any real-world instances, but a BOFH could
choose strange names to cause grief for clients.

3 years agodoc: add some IMAP standards
Eric Wong [Wed, 10 Jun 2020 07:03:58 +0000 (07:03 +0000)]
doc: add some IMAP standards

There's more, but IMAP is big and complex already.

3 years agoindex: v2: parallel by default
Eric Wong [Sun, 7 Jun 2020 20:02:15 +0000 (20:02 +0000)]
index: v2: parallel by default

InboxWritable should only set $v2w->{parallel} if the $parallel
flag is defined to 0 or 1.  We want indexing a new inbox to
utilize SMP, just like --reindex.

-index once again allows -j0/--jobs=0 to force single-process
use, and we'll be ensuring that works in tests to maintain
performance on small systems.

Fixes: 61a2fff5b34a3e32 ("admin: move index_inbox over")
3 years agosearchidx: v1: fix retries when Xapian and Msgmap are out-of-sync
Eric Wong [Fri, 5 Jun 2020 02:01:10 +0000 (02:01 +0000)]
searchidx: v1: fix retries when Xapian and Msgmap are out-of-sync

We forcibly stop git-log here, so erroring out on git-log close
failures is wrong since it sees SIGPIPE.  Noticed while
reindexing a large v1 inbox for IMAP changes.

Fixes: b32b47fb12a3043d ("index: "git log" failures are fatal")
3 years agowwwatomstream: drop smsg->{mid} fallback for non-SQLite
Eric Wong [Mon, 1 Jun 2020 10:06:57 +0000 (10:06 +0000)]
wwwatomstream: drop smsg->{mid} fallback for non-SQLite

It's no longer necessary to populate the smsg->{mid} field now
that ->smsg_eml calls smsg->populate in rare cases where the
smsg did not originate from SQLite.

3 years agosmsg: remove remaining accessor methods
Eric Wong [Mon, 1 Jun 2020 10:06:56 +0000 (10:06 +0000)]
smsg: remove remaining accessor methods

We'll continue to favor simpler data models that can be
used directly rather than wasting time and memory with
accessor APIs.

The ->from, ->to, -cc, ->mid, ->subject, >references methods can
all be trivially replaced by hash lookups since all their values
are stored in doc_data.  Most remaining callers of those methods
were test cases, anyways.

->from_name is only used in the PSGI code, so we can just
use ->psgi_cull to take care of populating the {from_name}
field.

3 years agosmsg: remove ->bytes and ->lines methods
Eric Wong [Mon, 1 Jun 2020 10:06:55 +0000 (10:06 +0000)]
smsg: remove ->bytes and ->lines methods

They're stored directly in Xapian and SQLite document data.
NNTP accesses those fields directly to avoid method invocation
overhead so there's no reason to waste several kilobytes for
each sub.

3 years agosmsg: get rid of remaining {mime} users
Eric Wong [Mon, 1 Jun 2020 10:06:54 +0000 (10:06 +0000)]
smsg: get rid of remaining {mime} users

We'll let $smsg->populate take care of everything all at once
without hanging onto the header object for too long.

3 years agonntp: smsg_range_i: favor ->{$field} lookups when possible
Eric Wong [Mon, 1 Jun 2020 10:06:53 +0000 (10:06 +0000)]
nntp: smsg_range_i: favor ->{$field} lookups when possible

PublicInbox::Smsg::date remains the only exception which
requires any subroutine calls, here, so we'll just have
a branch just for that.

3 years agowww: remove smsg_mime API and adjust callers
Eric Wong [Mon, 1 Jun 2020 10:06:52 +0000 (10:06 +0000)]
www: remove smsg_mime API and adjust callers

To further simplify callers and avoid embarrasing memory
explosions[1], we can finally eliminate this method in
favor of smsg_eml.

[1] commit 7d02b9e64455831d3bda20cd2e64e0c15dc07df5
    ("view: stop storing all MIME objects on large threads")
    fixed a huge memory blowup.

3 years agoinbox: msg_by_*: remove $(size)ref args
Eric Wong [Mon, 1 Jun 2020 10:06:51 +0000 (10:06 +0000)]
inbox: msg_by_*: remove $(size)ref args

None of our current callers care about the size of the blob
we're retrieving, so stop wasting stack space and code for
it.

3 years agosmsg: get rid of ->wrap initializer, too
Eric Wong [Mon, 1 Jun 2020 10:06:50 +0000 (10:06 +0000)]
smsg: get rid of ->wrap initializer, too

We'll just use `bless' like most current PublicInbox::Smsg callers.

3 years agosmsg: introduce ->populate method
Eric Wong [Mon, 1 Jun 2020 10:06:49 +0000 (10:06 +0000)]
smsg: introduce ->populate method

This will eventually replace the __hdr() calling methods and
eradicate {mime} usage from Smsg.  For now, we can eliminate
PublicInbox::Smsg->new since most callers already rely on an
open `bless' to avoid the old {mime} arg.

3 years agoimport: modernize to use Perl 5.10 features
Eric Wong [Mon, 1 Jun 2020 10:06:48 +0000 (10:06 +0000)]
import: modernize to use Perl 5.10 features

First, prefer the leaner "parent" module over the heavy "base"
module to establish ISA relationships, since "base" is only
needed for "fields".

The "//" and "//=" operators allow us simplify our code and fix
minor bugs where a value of "0" was disallowed.  Yes, we'll
allow "0" as an email address, too, since some twisted BOFH
could theoretically use it as a local user name.

Going forward, we'll also be avoiding "use warnings" and
instead rely on `-w' in the shebang.

3 years agov2writable: fix non-sensical interpolation in BUG message
Eric Wong [Mon, 1 Jun 2020 10:06:47 +0000 (10:06 +0000)]
v2writable: fix non-sensical interpolation in BUG message

No point in attempting to print the value of an undefined
variable if there's a bug.  Fortunately, (AFAIK) we've never hit
that bug check :>

3 years agowwwatomstream: convert callers to use smsg_eml
Eric Wong [Mon, 1 Jun 2020 10:06:46 +0000 (10:06 +0000)]
wwwatomstream: convert callers to use smsg_eml

We can simplify WwwAtomStream callbacks by performing ->smsg_eml
calls in the `feed_entry' sub itself.  This simplifies callers,
by reducing the number of places which can load an Eml object
into memory.

3 years agoinbox: introduce smsg_eml method
Eric Wong [Mon, 1 Jun 2020 10:06:45 +0000 (10:06 +0000)]
inbox: introduce smsg_eml method

The goal of this is to eventually remove the $smsg->{mime} field
which is easy-to-misuse and cause memory explosions which
necessitated fixes like commit 7d02b9e64455831d
("view: stop storing all MIME objects on large threads").