Apache Nutch is an extensible and scalable web crawler
NUTCH-696 Timeout for Parser
Julien Nioche6a0c40419bc4
diff --git a/CHANGES.txt b/CHANGES.txtindex 7f63061ec..933ed4ceb 100644--- a/CHANGES.txt+++ b/CHANGES.txt@@ -2,6 +2,8 @@ Nutch Change LogRelease 2.0 - Current Development+* NUTCH-696 Timeout for Parser (ab, jnioche)+* NUTCH-863 Benchmark and a testbed proxy server (ab)* NUTCH-677 Segment merge filtering based on segment content (Marcin Okraszewski via mattmann)diff --git a/conf/nutch-default.xml b/conf/nutch-default.xmlindex 4a7146dcf..e4519a4c0 100644--- a/conf/nutch-default.xml+++ b/conf/nutch-default.xml@@ -926,6 +926,16 @@</description></property>+<property>+ <name>parser.timeout</name>+ <value>30</value>+ <description>Timeout in seconds for the parsing of a document, otherwise treats it as an exception and+ moves on the the following documents. This parameter is applied to any Parser implementation.+ Set to -1 to deactivate, bearing in mind that this could cause+ the parsing to crash because of a very long or corrupted document.+ </description>+</property>+<!-- urlfilter plugin properties --><property>diff --git a/src/java/org/apache/nutch/parse/ParseCallable.java b/src/java/org/apache/nutch/parse/ParseCallable.javanew file mode 100644index 000000000..c296a0e33--- /dev/null+++ b/src/java/org/apache/nutch/parse/ParseCallable.java@@ -0,0 +1,20 @@+package org.apache.nutch.parse;++import java.util.concurrent.Callable;++import org.apache.nutch.protocol.Content;++class ParseCallable implements Callable<ParseResult> {+ private Parser p;+ private Content content;++ public ParseCallable(Parser p, Content content) {+ this.p = p;+ this.content = content;+ }++ @Override+ public ParseResult call() throws Exception {+ return p.getParse(content);+ }+}\ No newline at end of filediff --git a/src/java/org/apache/nutch/parse/ParseUtil.java b/src/java/org/apache/nutch/parse/ParseUtil.javaindex beb8a25d4..d621b03ba 100644--- a/src/java/org/apache/nutch/parse/ParseUtil.java+++ b/src/java/org/apache/nutch/parse/ParseUtil.java@@ -17,14 +17,15 @@package org.apache.nutch.parse;// Commons Logging imports-import org.apache.commons.logging.Log;-import org.apache.commons.logging.LogFactory;-// Nutch Imports-import org.apache.nutch.protocol.Content;+import java.util.concurrent.FutureTask;+import java.util.concurrent.TimeUnit;+import java.util.concurrent.TimeoutException;-// Hadoop imports+import org.apache.commons.logging.Log;+import org.apache.commons.logging.LogFactory;import org.apache.hadoop.conf.Configuration;+import org.apache.nutch.protocol.Content;/**@@ -41,6 +42,8 @@ public class ParseUtil {/* our log stream */public static final Log LOG = LogFactory.getLog(ParseUtil.class);private ParserFactory parserFactory;+ /** Parser timeout set to 30 sec by default. Set -1 to deactivate **/+ private int MAX_PARSE_TIME = 30;/***@@ -48,6 +51,7 @@ public class ParseUtil {*/public ParseUtil(Configuration conf) {this.parserFactory = new ParserFactory(conf);+ MAX_PARSE_TIME=conf.getInt("parser.timeout", 30);}/**@@ -79,7 +83,11 @@ public class ParseUtil {if (LOG.isDebugEnabled()) {LOG.debug("Parsing [" + content.getUrl() + "] with [" + parsers[i] + "]");}- parseResult = parsers[i].getParse(content);+ if (MAX_PARSE_TIME!=-1)+ parseResult = runParser(parsers[i], content);+ else+ parseResult = parsers[i].getParse(content);+if (parseResult != null && !parseResult.isEmpty())return parseResult;}@@ -124,7 +132,11 @@ public class ParseUtil {throw new ParseException(e.getMessage());}- ParseResult parseResult = p.getParse(content);+ ParseResult parseResult = null;+ if (MAX_PARSE_TIME!=-1)+ parseResult = runParser(p, content);+ else+ parseResult = p.getParse(content);if (parseResult != null && !parseResult.isEmpty()) {return parseResult;} else {@@ -134,6 +146,27 @@ public class ParseUtil {}return new ParseStatus(new ParseException("Unable to successfully parse content")).getEmptyParseResult(content.getUrl(), null);}- }+ }++ private ParseResult runParser(Parser p, Content content) {+ ParseCallable pc = new ParseCallable(p, content);+ FutureTask<ParseResult> task = new FutureTask<ParseResult>(pc);+ ParseResult res = null;+ Thread t = new Thread(task);+ t.start();+ try {+ res = task.get(MAX_PARSE_TIME, TimeUnit.SECONDS);+ } catch (TimeoutException e) {+ LOG.warn("TIMEOUT parsing " + content.getUrl() + " with " + p);+ } catch (Exception e) {+ task.cancel(true);+ res = null;+ t.interrupt();+ } finally {+ t = null;+ pc = null;+ }+ return res;+ }}