Skip to content

armgit / nutch

Public Imported from GitHub
0

Apache Nutch is an extensible and scalable web crawler

Assorted fixes related to crawler's "yield":

Andrzej Bialecki260615cc687f
diff --git a/src/java/org/apache/nutch/fetcher/Fetcher.java b/src/java/org/apache/nutch/fetcher/Fetcher.java
index e954181c1..e555c0557 100644
--- a/src/java/org/apache/nutch/fetcher/Fetcher.java
+++ b/src/java/org/apache/nutch/fetcher/Fetcher.java
@@ -115,7 +115,7 @@ public class Fetcher {
if (!fle.getFetch()) { // should we fetch this page?
if (LOG.isLoggable(Level.FINE))
LOG.fine("not fetching " + url);
- handleNoFetch(fle, ProtocolStatus.STATUS_NOTFETCHING);
+ handleFetch(fle, new ProtocolOutput(null, ProtocolStatus.STATUS_NOTFETCHING));
continue;
}
@@ -124,7 +124,7 @@ public class Fetcher {
// in parsing mode). Protocol-level redirects take precedence over
// content-level redirects. Some plugins can handle redirects
// automatically, so that only the final success or failure will be
- // shown? here.
+ // reported here.
boolean refetch = false;
int redirCnt = 0;
do {
@@ -145,7 +145,7 @@ public class Fetcher {
status();
}
}
- ParseStatus ps = handleFetch(url, fle, output);
+ ParseStatus ps = handleFetch(fle, output);
if (ps != null && ps.getMinorCode() == ParseStatus.SUCCESS_REDIRECT) {
url = ps.getMessage();
url = URLFilters.filter(url);
@@ -161,7 +161,7 @@ public class Fetcher {
case ProtocolStatus.MOVED: // try to redirect immediately
case ProtocolStatus.TEMP_MOVED: // try to redirect immediately
// record the redirect. perhaps the DB will want to know this.
- handleNoFetch(fle, pstat);
+ handleFetch(fle, output);
url = pstat.getMessage();
if (url != null) {
refetch = true;
@@ -177,22 +177,22 @@ public class Fetcher {
case ProtocolStatus.ROBOTS_DENIED:
case ProtocolStatus.RETRY:
case ProtocolStatus.NOTMODIFIED:
- handleNoFetch(fle, pstat);
+ handleFetch(fle, output);
break;
case ProtocolStatus.EXCEPTION:
logError(url, fle, new Exception(pstat.getMessage())); // retry?
- handleNoFetch(fle, pstat);
+ handleFetch(fle, output);
break;
default:
LOG.warning("Unknown ProtocolStatus: " + pstat.getCode());
- handleNoFetch(fle, pstat);
+ handleFetch(fle, output);
}
} while (refetch && (redirCnt < MAX_REDIRECT));
} catch (Throwable t) { // an unchecked exception
if (fle != null) {
logError(url, fle, t); // retry?
- handleNoFetch(fle, new ProtocolStatus(t));
+ handleFetch(fle, new ProtocolOutput(null, new ProtocolStatus(t)));
}
}
}
@@ -220,59 +220,47 @@ public class Fetcher {
}
}
- private ParseStatus handleFetch(String url, FetchListEntry fle, ProtocolOutput output) {
+ private ParseStatus handleFetch(FetchListEntry fle, ProtocolOutput output) {
Content content = output.getContent();
+ MD5Hash hash = null;
+ String url = fle.getPage().getURL().toString();
+ if (content == null) {
+ content = new Content(url, url, new byte[0], "", new Properties());
+ hash = MD5Hash.digest(url);
+ } else {
+ hash = MD5Hash.digest(content.getContent());
+ }
ProtocolStatus protocolStatus = output.getStatus();
if (!Fetcher.this.parsing) {
- outputPage(new FetcherOutput(fle, MD5Hash.digest(content.getContent()),
- protocolStatus),
+ outputPage(new FetcherOutput(fle, hash, protocolStatus),
content, null, null);
return null;
}
-
- String contentType = content.getContentType();
- Parser parser = null;
- Parse parse = null;
- ParseStatus status = null;
- try {
- parser = ParserFactory.getParser(contentType, url);
- parse = parser.getParse(content);
- status = parse.getData().getStatus();
- } catch (Exception e) {
- e.printStackTrace();
- status = new ParseStatus(e);
- }
- if (status.isSuccess()) {
- outputPage(new FetcherOutput(fle, MD5Hash.digest(content.getContent()),
- protocolStatus),
- content, new ParseText(parse.getText()), parse.getData());
- } else {
- LOG.info("fetch okay, but can't parse " + url + ", reason: "
- + status.toString());
- outputPage(new FetcherOutput(fle, MD5Hash.digest(content.getContent()),
- protocolStatus),
- content, new ParseText(""),
- new ParseData(status, "", new Outlink[0], new Properties()));
- }
- return status;
- }
-
- private void handleNoFetch(FetchListEntry fle, ProtocolStatus status) {
- String url = fle.getPage().getURL().toString();
- MD5Hash hash = MD5Hash.digest(url);
-
- if (Fetcher.this.parsing) {
- outputPage(new FetcherOutput(fle, hash, status),
- new Content(url, url, new byte[0], "", new Properties()),
- new ParseText(""),
- new ParseData(ParseStatus.STATUS_NOTPARSED, "", new Outlink[0], new Properties()));
+ String contentType = content.getContentType();
+ Parser parser = null;
+ Parse parse = null;
+ ParseStatus status = null;
+ try {
+ parser = ParserFactory.getParser(contentType, url);
+ parse = parser.getParse(content);
+ status = parse.getData().getStatus();
+ } catch (Exception e) {
+ e.printStackTrace();
+ status = new ParseStatus(e);
+ }
+ if (status.isSuccess()) {
+ outputPage(new FetcherOutput(fle, hash, protocolStatus),
+ content, new ParseText(parse.getText()), parse.getData());
} else {
- outputPage(new FetcherOutput(fle, hash, status),
- new Content(url, url, new byte[0], "", new Properties()),
- null, null);
+ LOG.info("fetch okay, but can't parse " + url + ", reason: "
+ + status.toString());
+ outputPage(new FetcherOutput(fle, hash, protocolStatus),
+ content, new ParseText(""),
+ new ParseData(status, "", new Outlink[0], new Properties()));
}
+ return status;
}
-
+
private void outputPage(FetcherOutput fo, Content content,
ParseText text, ParseData parseData) {
try {
@@ -363,8 +351,8 @@ public class Fetcher {
for (int i = 0; i < n; i++) {
// this thread may have gone away in the meantime
if (list[i] == null) continue;
- String name = list[i].getName();
- if (name.startsWith(THREAD_GROUP_NAME)) // prove it
+ String tname = list[i].getName();
+ if (tname.startsWith(THREAD_GROUP_NAME)) // prove it
noMoreFetcherThread = false;
if (LOG.isLoggable(Level.FINE))
LOG.fine(list[i].toString());
@@ -446,7 +434,6 @@ public class Fetcher {
/** Run the fetcher. */
public static void main(String[] args) throws Exception {
int threadCount = -1;
- long delay = -1;
String logLevel = "info";
boolean parsing = true;
boolean showThreadID = false;
diff --git a/src/java/org/apache/nutch/parse/ParseStatus.java b/src/java/org/apache/nutch/parse/ParseStatus.java
index 7b385b803..c5e63f6e1 100644
--- a/src/java/org/apache/nutch/parse/ParseStatus.java
+++ b/src/java/org/apache/nutch/parse/ParseStatus.java
@@ -168,7 +168,10 @@ public class ParseStatus extends VersionedWritable {
public String toString() {
StringBuffer res = new StringBuffer();
- res.append(majorCodes[majorCode] + "(" + majorCode + "," + minorCode + ")");
+ String name = null;
+ if (majorCode >= 0 && majorCode < majorCodes.length) name = majorCodes[majorCode];
+ else name = "UNKNOWN!";
+ res.append(name + "(" + majorCode + "," + minorCode + ")");
if (args != null) {
if (args.length == 1) {
res.append(": " + String.valueOf(args[0]));
@@ -221,22 +224,22 @@ public class ParseStatus extends VersionedWritable {
}
return true;
}
-}
-
-class EmptyParseImpl implements Parse {
-
- private ParseData data = null;
- public EmptyParseImpl(ParseStatus status) {
- data = new ParseData(status, "", new Outlink[0], new Properties());
- }
-
- public ParseData getData() {
- return data;
- }
+ private static class EmptyParseImpl implements Parse {
+
+ private ParseData data = null;
+
+ public EmptyParseImpl(ParseStatus status) {
+ data = new ParseData(status, "", new Outlink[0], new Properties());
+ }
+
+ public ParseData getData() {
+ return data;
+ }
- public String getText() {
- return "";
+ public String getText() {
+ return "";
+ }
}
}
diff --git a/src/java/org/apache/nutch/protocol/ProtocolStatus.java b/src/java/org/apache/nutch/protocol/ProtocolStatus.java
index 4a8089239..7f4b745d5 100644
--- a/src/java/org/apache/nutch/protocol/ProtocolStatus.java
+++ b/src/java/org/apache/nutch/protocol/ProtocolStatus.java
@@ -19,6 +19,7 @@ package org.apache.nutch.protocol;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
+import java.util.HashMap;
import org.apache.nutch.io.VersionedWritable;
import org.apache.nutch.io.WritableUtils;
@@ -77,6 +78,24 @@ public class ProtocolStatus extends VersionedWritable {
private long lastModified;
private String[] args;
+ private static HashMap codeToName = new HashMap();
+ static {
+ codeToName.put(new Integer(SUCCESS), "success");
+ codeToName.put(new Integer(FAILED), "failed");
+ codeToName.put(new Integer(PROTO_NOT_FOUND), "proto_not_found");
+ codeToName.put(new Integer(GONE), "gone");
+ codeToName.put(new Integer(MOVED), "moved");
+ codeToName.put(new Integer(TEMP_MOVED), "temp_moved");
+ codeToName.put(new Integer(NOTFOUND), "notfound");
+ codeToName.put(new Integer(RETRY), "retry");
+ codeToName.put(new Integer(EXCEPTION), "exception");
+ codeToName.put(new Integer(ACCESS_DENIED), "access_denied");
+ codeToName.put(new Integer(ROBOTS_DENIED), "robots_denied");
+ codeToName.put(new Integer(REDIR_EXCEEDED), "redir_exceeded");
+ codeToName.put(new Integer(NOTFETCHING), "notfetching");
+ codeToName.put(new Integer(NOTMODIFIED), "notmodified");
+ }
+
public ProtocolStatus() {
}
@@ -215,7 +234,7 @@ public class ProtocolStatus extends VersionedWritable {
public String toString() {
StringBuffer res = new StringBuffer();
- res.append("(" + code + "), lastModified=" + lastModified);
+ res.append(codeToName.get(new Integer(code)) + "(" + code + "), lastModified=" + lastModified);
if (args != null) {
if (args.length == 1) {
res.append(": " + String.valueOf(args[0]));
diff --git a/src/java/org/apache/nutch/segment/SegmentReader.java b/src/java/org/apache/nutch/segment/SegmentReader.java
index 9c4c80a39..f13091fe5 100644
--- a/src/java/org/apache/nutch/segment/SegmentReader.java
+++ b/src/java/org/apache/nutch/segment/SegmentReader.java
@@ -528,6 +528,7 @@ public class SegmentReader {
cnt++;
if (dump) reader.dump(sorted, System.out);
} catch (Throwable t) {
+ t.printStackTrace();
LOG.warning(t.getMessage());
}
}
diff --git a/src/java/org/apache/nutch/tools/ParseSegment.java b/src/java/org/apache/nutch/tools/ParseSegment.java
index 09dd6c0cb..e5e00a47a 100644
--- a/src/java/org/apache/nutch/tools/ParseSegment.java
+++ b/src/java/org/apache/nutch/tools/ParseSegment.java
@@ -183,31 +183,22 @@ public class ParseSegment {
continue;
}
- // if fetch was successful or
- // previously unable to parse (so try again)
- ProtocolStatus ps = fetcherOutput.getProtocolStatus();
- if (ps.isSuccess()) {
- handleContent(url, content);
- synchronized (ParseSegment.this) {
- pages++; // record successful parse
- bytes += content.getContent().length;
- if ((pages % 100) == 0)
- status();
- }
- } else {
- // errored at fetch step
- logError(url, new ProtocolException("Error at fetch stage: " + ps));
- handleNoContent(new ParseStatus(ParseStatus.FAILED_MISSING_CONTENT));
+ handleContent(fetcherOutput, content);
+ synchronized (ParseSegment.this) {
+ pages++; // record successful parse
+ bytes += content.getContent().length;
+ if ((pages % 100) == 0)
+ status();
}
} catch (ParseException e) {
logError(url, e);
- handleNoContent(new ParseStatus(e));
+ handleError(new ParseStatus(e));
} catch (Throwable t) { // an unchecked exception
if (fle != null) {
logError(url, t);
- handleNoContent(new ParseStatus(t));
+ handleError(new ParseStatus(t));
} else {
LOG.severe("Unexpected exception");
}
@@ -224,27 +215,35 @@ public class ParseSegment {
}
}
- private void handleContent(String url, Content content)
+ private void handleContent(FetcherOutput fo, Content content)
throws ParseException {
- //String contentType = content.getContentType();
- String contentType = content.getMetadata().getProperty("Content-Type");
+ String url = fo.getUrl().toString();
+ if (content != null) {
+ String contentType = content.getMetadata().getProperty("Content-Type");
+ if (ParseSegment.this.dryRun) {
+ LOG.info("To be handled as Content-Type: "+contentType);
+ return;
+ }
- if (ParseSegment.this.dryRun) {
- LOG.info("To be handled as Content-Type: "+contentType);
- return;
+ Parser parser = ParserFactory.getParser(contentType, url);
+ Parse parse = parser.getParse(content);
+ outputPage(new ParseText(parse.getText()), parse.getData());
+
+ } else {
+ if (ParseSegment.this.dryRun) {
+ LOG.info("To be handled as no content");
+ return;
+ }
+ outputPage(new ParseText(""),
+ new ParseData(new ParseStatus(ParseStatus.FAILED, ParseStatus.FAILED_MISSING_CONTENT),
+ "", new Outlink[0], new Properties()));
}
-
- Parser parser = ParserFactory.getParser(contentType, url);
- Parse parse = parser.getParse(content);
-
- outputPage
- (new ParseText(parse.getText()), parse.getData());
}
- private void handleNoContent(ParseStatus status) {
+ private void handleError(ParseStatus status) {
if (ParseSegment.this.dryRun) {
- LOG.info("To be handled as no content");
+ LOG.info("To be handled as error");
return;
}
outputPage(new ParseText(""),
@@ -267,6 +266,7 @@ public class ParseSegment {
+" wait="+(t4-t3) +" write="+(t5-t4) +"ms");
}
} catch (Throwable t) {
+ t.printStackTrace();
LOG.severe("error writing output:" + t.toString());
}
}
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.java
index 3c4a2db0e..d210edb16 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
@@ -216,15 +216,20 @@ public class Http implements org.apache.nutch.protocol.Protocol {
}
int code = response.getCode();
+ Content c = response.toContent();
if (code == 200) { // got a good response
- return new ProtocolOutput(response.toContent()); // return it
+ return new ProtocolOutput(c); // return it
} else if (code == 410) { // page is gone
- return new ProtocolOutput(null, new ProtocolStatus(ProtocolStatus.GONE, "Http: " + code + " url=" + url));
+ return new ProtocolOutput(c, new ProtocolStatus(ProtocolStatus.GONE, "Http: " + code + " url=" + url));
} else if (code >= 300 && code < 400) { // handle redirect
- url = new URL(url, response.getHeader("Location"));
+ String location = response.getHeader("Location");
+ // some broken servers, such as MS IIS, use lowercase header name...
+ if (location == null) location = response.getHeader("location");
+ if (location == null) location = "";
+ url = new URL(url, location);
int protocolStatusCode;
switch (code) {
case 300: // multiple choices, preferred value in Location
@@ -246,29 +251,29 @@ public class Http implements org.apache.nutch.protocol.Protocol {
protocolStatusCode = ProtocolStatus.MOVED;
}
// handle this in the higher layer.
- return new ProtocolOutput(null, new ProtocolStatus(protocolStatusCode, url));
+ return new ProtocolOutput(c, new ProtocolStatus(protocolStatusCode, url));
} else if (code == 400) { // bad request, mark as GONE
LOG.fine("400 Bad request: " + url);
- return new ProtocolOutput(null, new ProtocolStatus(ProtocolStatus.GONE, url));
+ return new ProtocolOutput(c, new ProtocolStatus(ProtocolStatus.GONE, url));
} else if (code == 401) { // requires authorization
LOG.fine("401 Authentication Required");
if (redirects == MAX_REDIRECTS)
- return new ProtocolOutput(null, new ProtocolStatus(ProtocolStatus.REDIR_EXCEEDED,
+ return new ProtocolOutput(c, new ProtocolStatus(ProtocolStatus.REDIR_EXCEEDED,
"Too many redirects: " + urlString));
- Properties p = response.toContent().getMetadata();
+ Properties p = c.getMetadata();
if (p instanceof MultiProperties) {
auth = HttpAuthenticationFactory.findAuthentication((MultiProperties) p);
} else {
- return new ProtocolOutput(null, new ProtocolStatus(ProtocolStatus.ACCESS_DENIED, "Authorization required: "
+ return new ProtocolOutput(c, new ProtocolStatus(ProtocolStatus.ACCESS_DENIED, "Authorization required: "
+ urlString));
}
redirects++;
} else if (code == 404) {
- return new ProtocolOutput(null, new ProtocolStatus(ProtocolStatus.NOTFOUND, url));
+ return new ProtocolOutput(c, new ProtocolStatus(ProtocolStatus.NOTFOUND, url));
} else if (code == 410) { // permanently GONE
- return new ProtocolOutput(null, new ProtocolStatus(ProtocolStatus.GONE, url));
+ return new ProtocolOutput(c, new ProtocolStatus(ProtocolStatus.GONE, url));
} else {
- return new ProtocolOutput(null, new ProtocolStatus(ProtocolStatus.EXCEPTION, "Http code=" + code + ", url="
+ return new ProtocolOutput(c, new ProtocolStatus(ProtocolStatus.EXCEPTION, "Http code=" + code + ", url="
+ url));
}
}
diff --git a/src/plugin/protocol-httpclient/src/java/org/apache/nutch/protocol/httpclient/HttpResponse.java b/src/plugin/protocol-httpclient/src/java/org/apache/nutch/protocol/httpclient/HttpResponse.java
index 8561f62cb..12b9514a7 100644
--- a/src/plugin/protocol-httpclient/src/java/org/apache/nutch/protocol/httpclient/HttpResponse.java
+++ b/src/plugin/protocol-httpclient/src/java/org/apache/nutch/protocol/httpclient/HttpResponse.java
@@ -28,6 +28,8 @@ public class HttpResponse {
private byte[] content;
+ private static final byte[] EMPTY_CONTENT = new byte[0];
+
private int code;
private MultiProperties headers = new MultiProperties();
@@ -53,6 +55,7 @@ public class HttpResponse {
public Content toContent() {
String contentType = getHeader("Content-Type");
if (contentType == null) contentType = "";
+ if (content == null) content = EMPTY_CONTENT;
return new Content(orig, base, content, contentType, headers);
}
@@ -77,8 +80,9 @@ public class HttpResponse {
for (int i = 0; i < heads.length; i++) {
headers.put(heads[i].getName(), heads[i].getValue());
}
- if (code == 200) {
-
+ // always read content. Sometimes content is useful to find a cause
+ // for error.
+ try {
InputStream in = get.getResponseBodyAsStream();
byte[] buffer = new byte[Http.BUFFER_SIZE];
int bufferFilled = 0;
@@ -93,7 +97,9 @@ public class HttpResponse {
content = out.toByteArray();
in.close();
-
+ } catch (Exception e) {
+ if (code == 200) throw new IOException(e.toString());
+ // for codes other than 200 OK, we are fine with empty content
}
} catch (org.apache.commons.httpclient.ProtocolException pe) {
pe.printStackTrace();