incubator-stormcrawler icon indicating copy to clipboard operation
incubator-stormcrawler copied to clipboard

FetchItemQueues removes empty queues, which skips the politeness delay

Open dpol1 opened this issue 4 weeks ago • 2 comments

FetchItemQueues.finishFetchItem removes a queue as soon as its last URL is done. The next URL for that host gets a new queue, which is ready straight away. If that URL arrives after the queue has emptied, for instance an outlink of the host's last page, it can be fetched at once, ignoring fetcher.server.delay and the Crawl-delay from the robots.txt.

This test fails in FetchItemQueuesTest, as a.net/2 is handed out at once:

FetchItemQueues q = queues("fetcher.server.delay", 5.0f);
add(q, "http://a.net/1");
q.finishFetchItem(q.getFetchItem(), false); // the queue of a.net is removed
add(q, "http://a.net/2");
Assertions.assertNull(q.getFetchItem());

In a local crawl with fetcher.server.delay: 3, I got two requests to the same host 11 ms apart. In 12-minute crawls of GQ32K hosts with the default delay of 1 s, a fetch started less than 1 s after the previous one to the same host had ended in 107 of 18,315 cases with OpenSearch, and in 5,422 of 14,151 with URLFrontier, across 799 hosts. The usual gap is just over 1 s. The higher figure is probably because URLFrontier hands out a host's next URLs sooner than the OpenSearch spout, so they more often arrive while the delay is still running. These figures come from the worker logs. I haven't traced them to this code path, and a shorter Crawl-delay could explain some of them.

3.7.0 has the same gap, though not in every case. Its empty queues stay until a getFetchItem scan reaches them, and the threads scan after each fetch and every 100 ms when idle, so a URL arriving before the scan still waits. Since #2132 finishFetchItem removes the queue itself, so the new queue is immediately eligible. I ran the 3.7.0 case only on its queue code.

NUTCH-2947 fixed the same problem in Nutch 1.19 by keeping the state of empty queues. I suggest keeping the time of a host's last fetch until its delay has passed. I can open a PR with the test and a fix, for 4.0 or later.

cc @GGraziadei, since #2132 expected the per-host rate to stay bounded by the crawl delay.

dpol1 avatar Nov 06 '26 17:11 dpol1

I'd fix before cutting rel, objections?

dpol1 avatar Nov 06 '26 18:11 dpol1

Thanks @dpol1. I ran your test against current main, and it fails as you described. The underlying issue predates this, but #2132 made it worse by reaping the queue as soon as it drains.

One potential fix that reuses the existing semantic is to issue a ticket for nextFetchTime when a drained queue still has a future nextFetchTime, rather than reaping it immediately. That way, getFetchItem can reap the queue once the delay has elapsed, while any URL arriving in the meantime still respects the delay.

I haven't tried this approach yet, happy to help with your PR review.

GGraziadei avatar Nov 06 '26 18:11 GGraziadei