Apache Nutch is an extensible and scalable web crawler
NUTCH-876 Remove remaining robots/IP blocking code in lib-http.
Andrzej Bialecki80f14cf7271b
diff --git a/CHANGES.txt b/CHANGES.txtindex fe340f8eb..ece4617dd 100644--- a/CHANGES.txt+++ b/CHANGES.txt@@ -2,6 +2,8 @@ Nutch Change LogRelease 2.0 - Current Development+* NUTCH-876 Remove remaining robots/IP blocking code in lib-http (ab)+* NUTCH-851 Port logging to slf4j (jnioche)* NUTCH-564 External parser supports encoding attribute (Antony Bowesman, mattmann)diff --git a/src/plugin/lib-http/src/java/org/apache/nutch/protocol/http/api/HttpBase.java b/src/plugin/lib-http/src/java/org/apache/nutch/protocol/http/api/HttpBase.javaindex f9dc6cf92..d061ab8e0 100644--- a/src/plugin/lib-http/src/java/org/apache/nutch/protocol/http/api/HttpBase.java+++ b/src/plugin/lib-http/src/java/org/apache/nutch/protocol/http/api/HttpBase.java@@ -18,9 +18,7 @@ package org.apache.nutch.protocol.http.api;// JDK importsimport java.io.IOException;-import java.net.InetAddress;import java.net.URL;-import java.net.UnknownHostException;import java.util.HashMap;import java.util.LinkedList;@@ -68,21 +66,6 @@ public abstract class HttpBase implements Protocol {/** The length limit for downloaded content, in bytes. */protected int maxContent = 64 * 1024;- /** The number of times a thread will delay when trying to fetch a page. */- protected int maxDelays = 3;-- /**- * The maximum number of threads that should be allowed- * to access a host at one time.- */- protected int maxThreadsPerHost = 1;-- /**- * The number of seconds the fetcher will delay between- * successive requests to the same server.- */- protected long serverDelay = 1000;-/** The Nutch 'User-Agent' request header */protected String userAgent = getAgentString("NutchCVS", null, "Nutch",@@ -92,27 +75,6 @@ public abstract class HttpBase implements Protocol {/** The "Accept-Language" request header value. */protected String acceptLanguage = "en-us,en-gb,en;q=0.7,*;q=0.3";-- /**- * Maps from host to a Long naming the time it should be unblocked.- * The Long is zero while the host is in use, then set to now+wait when- * a request finishes. This way only one thread at a time accesses a- * host.- */- private static HashMap<String, Long> BLOCKED_ADDR_TO_TIME =- new HashMap<String, Long>();-- /**- * Maps a host to the number of threads accessing that host.- */- private static HashMap<String, Integer> THREADS_PER_HOST_COUNT =- new HashMap<String, Integer>();-- /**- * Queue of blocked hosts. This contains all of the non-zero entries- * from BLOCKED_ADDR_TO_TIME, ordered by increasing time.- */- private static LinkedList<String> BLOCKED_ADDR_QUEUE = new LinkedList<String>();/** The default logger */private final static Logger LOGGER = LoggerFactory.getLogger(HttpBase.class);@@ -123,23 +85,11 @@ public abstract class HttpBase implements Protocol {/** The nutch configuration */private Configuration conf = null;- /** Do we block by IP addresses or by hostnames? */- private boolean byIP = true;-private MimeUtil mimeTypes;/** Do we use HTTP/1.1? */protected boolean useHttp11 = false;- /** Skip page if Crawl-Delay longer than this value. */- protected long maxCrawlDelay = -1L;-- /** Plugin should handle host blocking internally. */- protected boolean checkBlocking = true;-- /** Plugin should handle robot rules checking internally. */- protected boolean checkRobots = true;-/** Creates a new instance of HttpBase */public HttpBase() {this(null);@@ -161,20 +111,12 @@ public abstract class HttpBase implements Protocol {this.useProxy = (proxyHost != null && proxyHost.length() > 0);this.timeout = conf.getInt("http.timeout", 10000);this.maxContent = conf.getInt("http.content.limit", 64 * 1024);- this.maxDelays = conf.getInt("http.max.delays", 3);- this.maxThreadsPerHost = conf.getInt("fetcher.threads.per.host", 1);this.userAgent = getAgentString(conf.get("http.agent.name"), conf.get("http.agent.version"), conf.get("http.agent.description"), conf.get("http.agent.url"), conf.get("http.agent.email"));this.acceptLanguage = conf.get("http.accept.language", acceptLanguage);- this.serverDelay = (long) (conf.getFloat("fetcher.server.delay", 1.0f) * 1000);- this.maxCrawlDelay = (conf.getInt("fetcher.max.crawl.delay", -1) * 1000);- // backward-compatible default setting- this.byIP = conf.getBoolean("fetcher.threads.per.host.by.ip", true);this.mimeTypes = new MimeUtil(conf);this.useHttp11 = conf.getBoolean("http.useHttp11", false);this.robots.setConf(conf);- this.checkBlocking = conf.getBoolean(Protocol.CHECK_BLOCKING, true);- this.checkRobots = conf.getBoolean(Protocol.CHECK_ROBOTS, true);logConf();}@@ -189,44 +131,8 @@ public abstract class HttpBase implements Protocol {try {URL u = new URL(url);- long delay = serverDelay;- if (checkRobots) {- try {- if (!robots.isAllowed(this, u)) {- return new ProtocolOutput(null,- ProtocolStatusUtils.makeStatus(ProtocolStatusCodes.ROBOTS_DENIED, url));- }- } catch (Throwable e) {- // XXX Maybe bogus: assume this is allowed.- if (logger.isTraceEnabled()) {- logger.trace("Exception checking robot rules for " + url + ": " + e);- }- }-- long crawlDelay = robots.getCrawlDelay(this, u);- delay = crawlDelay > 0 ? crawlDelay : serverDelay;- }- if (checkBlocking && maxCrawlDelay >= 0 && delay > maxCrawlDelay) {- // skip this page, otherwise the thread would block for too long.- LOGGER.info("Skipping: " + u + " exceeds fetcher.max.crawl.delay, max="- + (maxCrawlDelay / 1000) + ", Crawl-Delay=" + (delay / 1000));- return new ProtocolOutput(null, ProtocolStatusUtils.STATUS_WOULDBLOCK);- }String host = null;- if (checkBlocking) {- try {- host = blockAddr(u, delay);- } catch (BlockedException be) {- return new ProtocolOutput(null, ProtocolStatusUtils.STATUS_BLOCKED);- }- }- Response response;- try {- response = getResponse(u, page, false); // make a request- } finally {- if (checkBlocking) unblockAddr(host, delay);- }-+ Response response = getResponse(u, page, false); // make a requestint code = response.getCode();byte[] content = response.getContent();Content c = new Content(u.toString(), u.toString(),@@ -319,18 +225,6 @@ public abstract class HttpBase implements Protocol {return maxContent;}- public int getMaxDelays() {- return maxDelays;- }-- public int getMaxThreadsPerHost() {- return maxThreadsPerHost;- }-- public long getServerDelay() {- return serverDelay;- }-public String getUserAgent() {return userAgent;}@@ -346,94 +240,6 @@ public abstract class HttpBase implements Protocol {return useHttp11;}- private String blockAddr(URL url, long crawlDelay) throws ProtocolException {-- String host;- if (byIP) {- try {- InetAddress addr = InetAddress.getByName(url.getHost());- host = addr.getHostAddress();- } catch (UnknownHostException e) {- // unable to resolve it, so don't fall back to host name- throw new HttpException(e);- }- } else {- host = url.getHost();- if (host == null)- throw new HttpException("Unknown host for url: " + url);- host = host.toLowerCase();- }-- int delays = 0;- while (true) {- cleanExpiredServerBlocks(); // free held addresses-- Long time;- synchronized (BLOCKED_ADDR_TO_TIME) {- time = BLOCKED_ADDR_TO_TIME.get(host);- if (time == null) { // address is free-- // get # of threads already accessing this addr- Integer counter = THREADS_PER_HOST_COUNT.get(host);- int count = (counter == null) ? 0 : counter.intValue();-- count++; // increment & store- THREADS_PER_HOST_COUNT.put(host, new Integer(count));-- if (count >= maxThreadsPerHost) {- BLOCKED_ADDR_TO_TIME.put(host, new Long(0)); // block it- }- return host;- }- }-- if (delays == maxDelays)- throw new BlockedException("Exceeded http.max.delays: retry later.");-- long done = time.longValue();- long now = System.currentTimeMillis();- long sleep = 0;- if (done == 0) { // address is still in use- sleep = crawlDelay; // wait at least delay-- } else if (now < done) { // address is on hold- sleep = done - now; // wait until its free- }-- try {- Thread.sleep(sleep);- } catch (InterruptedException e) {}- delays++;- }- }-- private void unblockAddr(String host, long crawlDelay) {- synchronized (BLOCKED_ADDR_TO_TIME) {- int addrCount = THREADS_PER_HOST_COUNT.get(host).intValue();- if (addrCount == 1) {- THREADS_PER_HOST_COUNT.remove(host);- BLOCKED_ADDR_QUEUE.addFirst(host);- BLOCKED_ADDR_TO_TIME.put- (host, new Long(System.currentTimeMillis() + crawlDelay));- } else {- THREADS_PER_HOST_COUNT.put(host, new Integer(addrCount - 1));- }- }- }-- private static void cleanExpiredServerBlocks() {- synchronized (BLOCKED_ADDR_TO_TIME) {- for (int i = BLOCKED_ADDR_QUEUE.size() - 1; i >= 0; i--) {- String host = BLOCKED_ADDR_QUEUE.get(i);- long time = BLOCKED_ADDR_TO_TIME.get(host).longValue();- if (time <= System.currentTimeMillis()) {- BLOCKED_ADDR_TO_TIME.remove(host);- BLOCKED_ADDR_QUEUE.remove(i);- }- }- }- }-private static String getAgentString(String agentName,String agentVersion,String agentDesc,@@ -487,12 +293,6 @@ public abstract class HttpBase implements Protocol {logger.info("http.content.limit = " + maxContent);logger.info("http.agent = " + userAgent);logger.info("http.accept.language = " + acceptLanguage);- logger.info(Protocol.CHECK_BLOCKING + " = " + checkBlocking);- logger.info(Protocol.CHECK_ROBOTS + " = " + checkRobots);- if (checkBlocking) {- logger.info("fetcher.server.delay = " + serverDelay);- logger.info("http.max.delays = " + maxDelays);- }}}diff --git a/src/plugin/protocol-httpclient/src/java/org/apache/nutch/protocol/httpclient/Http.java b/src/plugin/protocol-httpclient/src/java/org/apache/nutch/protocol/httpclient/Http.javaindex 508b96b0b..200f59951 100644--- a/src/plugin/protocol-httpclient/src/java/org/apache/nutch/protocol/httpclient/Http.java+++ b/src/plugin/protocol-httpclient/src/java/org/apache/nutch/protocol/httpclient/Http.java@@ -190,11 +190,6 @@ public class Http extends HttpBase {params.setSendBufferSize(BUFFER_SIZE);params.setReceiveBufferSize(BUFFER_SIZE);params.setMaxTotalConnections(maxThreadsTotal);- if (maxThreadsTotal > maxThreadsPerHost) {- params.setDefaultMaxConnectionsPerHost(maxThreadsPerHost);- } else {- params.setDefaultMaxConnectionsPerHost(maxThreadsTotal);- }// executeMethod(HttpMethod) seems to ignore the connection timeout on// the connection manager.