Skip to content

armgit / nutch

Public Imported from GitHub
0

Apache Nutch is an extensible and scalable web crawler

NUTCH-2619 protocol-okhttp: allow to keep partially fetched docs as truncated - return content as successful response (marked as truncated) if http.partial.truncated is true and there is already content fetched

Sebastian Nagela4569f13efdf
diff --git a/conf/nutch-default.xml b/conf/nutch-default.xml
index cb3a2a804..2c07c8a43 100644
--- a/conf/nutch-default.xml
+++ b/conf/nutch-default.xml
@@ -237,6 +237,18 @@
</description>
</property>
+<property>
+ <name>http.partial.truncated</name>
+ <value>false</value>
+ <description>
+ If true the HTTP protocol implementation may store the content of
+ partial fetches and mark the response as truncated instead of
+ throwing an exception which will cause the fetch to fail. This
+ allows to use the data which has already been fetched, instead of
+ retrying the fetch later. Note: supported only by protocol-okhttp.
+ </description>
+</property>
+
<property>
<name>http.proxy.host</name>
<value></value>
diff --git a/src/java/org/apache/nutch/net/protocols/Response.java b/src/java/org/apache/nutch/net/protocols/Response.java
index 92d41e328..a609b8f0f 100644
--- a/src/java/org/apache/nutch/net/protocols/Response.java
+++ b/src/java/org/apache/nutch/net/protocols/Response.java
@@ -64,7 +64,7 @@ public interface Response extends HttpHeaders {
LENGTH,
/** fetch exceeded configured http.fetch.duration */
TIME,
- /** network disconnect during fetch */
+ /** network disconnect or timeout during fetch */
DISCONNECT,
/** implementation internal reason */
INTERNAL,
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.java
index 83fc8da82..42f479312 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
@@ -91,6 +91,9 @@ public abstract class HttpBase implements Protocol {
/** The time limit to download the entire content, in seconds. */
protected int maxDuration = 300;
+ /** Whether to save partial fetches as truncated content. */
+ protected boolean partialAsTruncated = false;
+
/** The Nutch 'User-Agent' request header */
protected String userAgent = getAgentString("NutchCVS", null, "Nutch",
"http://nutch.apache.org/bot.html", "[email protected]");
@@ -190,6 +193,8 @@ public abstract class HttpBase implements Protocol {
this.timeout = conf.getInt("http.timeout", 10000);
this.maxContent = conf.getInt("http.content.limit", 64 * 1024);
this.maxDuration = conf.getInt("http.time.limit", -1);
+ this.partialAsTruncated = conf
+ .getBoolean("http.partial.truncated", false);
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"));
@@ -199,7 +204,7 @@ public abstract class HttpBase implements Protocol {
this.accept = conf.get("http.accept", accept).trim();
this.mimeTypes = new MimeUtil(conf);
// backward-compatible default setting
- this.useHttp11 = conf.getBoolean("http.useHttp11", false);
+ this.useHttp11 = conf.getBoolean("http.useHttp11", true);
this.useHttp2 = conf.getBoolean("http.useHttp2", false);
this.responseTime = conf.getBoolean("http.store.responsetime", true);
this.storeIPAddress = conf.getBoolean("store.ip.address", false);
@@ -454,6 +459,14 @@ public abstract class HttpBase implements Protocol {
return maxDuration;
}
+ /**
+ * Whether to save partial fetches as truncated content, cf. the property
+ * <code>http.partial.truncated</code>.
+ */
+ public boolean isStorePartialAsTruncated() {
+ return partialAsTruncated;
+ }
+
public String getUserAgent() {
if (userAgentNames != null) {
return userAgentNames
diff --git a/src/plugin/protocol-okhttp/src/java/org/apache/nutch/protocol/okhttp/OkHttpResponse.java b/src/plugin/protocol-okhttp/src/java/org/apache/nutch/protocol/okhttp/OkHttpResponse.java
index 34bff3ae0..b720c287f 100644
--- a/src/plugin/protocol-okhttp/src/java/org/apache/nutch/protocol/okhttp/OkHttpResponse.java
+++ b/src/plugin/protocol-okhttp/src/java/org/apache/nutch/protocol/okhttp/OkHttpResponse.java
@@ -114,7 +114,7 @@ public class OkHttpResponse implements Response {
TruncatedContent truncated = new TruncatedContent();
content = toByteArray(response.body(), truncated, okhttp.getMaxContent(),
- okhttp.getMaxDuration());
+ okhttp.getMaxDuration(), okhttp.isStorePartialAsTruncated());
responsemetadata.add(FETCH_TIME, Long.toString(System.currentTimeMillis()));
if (truncated.booleanValue()) {
if (!call.isCanceled()) {
@@ -133,7 +133,8 @@ public class OkHttpResponse implements Response {
}
private final byte[] toByteArray(final ResponseBody responseBody,
- TruncatedContent truncated, int maxContent, int maxDuration) throws IOException {
+ TruncatedContent truncated, int maxContent, int maxDuration,
+ boolean partialAsTruncated) throws IOException {
if (responseBody == null) {
return new byte[] {};
@@ -156,7 +157,17 @@ public class OkHttpResponse implements Response {
while (contentBytesBuffered < maxContentBytes) {
contentBytesRequested += Math.min(bufferGrowStepBytes,
(maxContentBytes - contentBytesBuffered));
- boolean success = source.request(contentBytesRequested);
+ boolean success = false;
+ try {
+ success = source.request(contentBytesRequested);
+ } catch (IOException e) {
+ if (partialAsTruncated && contentBytesBuffered > 0) {
+ // treat already fetched content as truncated
+ truncated.setReason(TruncatedContentReason.DISCONNECT);
+ } else {
+ throw e;
+ }
+ }
contentBytesBuffered = (int) source.buffer().size();
if (LOG.isDebugEnabled()) {
LOG.debug("total bytes requested = {}, buffered = {}",