[LeetCode] Web Crawler Multithreaded

1242. Web Crawler Multithreaded

Given a url startUrl and an interface HtmlParser, implement a Multi-threaded web crawler to crawl all links that are under the same hostname as startUrl.

Return all urls obtained by your web crawler in any order.

Your crawler should:

  • Start from the page: startUrl
  • Call HtmlParser.getUrls(url) to get all urls from a webpage of given url.
  • Do not crawl the same link twice.
  • Explore only the links that are under the same hostname as startUrl.

As shown in the example url above, the hostname is example.org. For simplicity sake, you may assume all urls use http protocol without any port specified. For example, the urls http://leetcode.com/problems and http://leetcode.com/contest are under the same hostname, while urls http://example.org/test and http://example.com/abc are not under the same hostname.

The HtmlParser interface is defined as such:

1
2
3
4
5
interface HtmlParser {
// Return a list of all urls from a webpage of given url.
// This is a blocking call, that means it will do HTTP request and return when this request is finished.
public List<String> getUrls(String url);
}

Note that getUrls(String url) simulates performing a HTTP request. You can treat it as a blocking function call which waits for a HTTP request to finish. It is guaranteed that getUrls(String url) will return the urls within 15ms. Single-threaded solutions will exceed the time limit so, can your multi-threaded web crawler do better?

Below are two examples explaining the functionality of the problem, for custom testing purposes you’ll have three variables urls, edges and startUrl. Notice that you will only have access to startUrl in your code, while urls and edges are not directly accessible to you in code.

Follow up:

  1. Assume we have 10,000 nodes and 1 billion URLs to crawl. We will deploy the same software onto each node. The software can know about all the nodes. We have to minimize communication between machines and make sure each node does equal amount of work. How would your web crawler design change?
  2. What if one node fails or does not work?
  3. How do you know when the crawler is done?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/**
* // This is the HtmlParser's API interface.
* // You should not implement it, or speculate about its implementation
* interface HtmlParser {
* public List<String> getUrls(String url) {}
* }
*/
class Solution {
private String host;
private Set<String> urlSet;
public List<String> crawl(String startUrl, HtmlParser htmlParser) {
host = getHost(startUrl);
urlSet = new ConcurrentSkipListSet<String>(){{add(startUrl);}};

return _crawl(startUrl, htmlParser).collect(Collectors.toList());
}

private Stream<String> _crawl(String target, HtmlParser htmlParser) {
Stream<String> stream = htmlParser.getUrls(target)
.parallelStream()
.filter(url -> getHost(url).equals(host))
.filter(urlSet::add)
.flatMap(url -> _crawl(url, htmlParser));

return Stream.concat(Stream.of(target), stream);
}

private String getHost(String url) {
int pos = url.indexOf('/', 7);
return pos != -1 ? url.substring(0, pos) : url;
}
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2021/06/28/PS/LeetCode/web-crawler-multithreaded/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.