Skip to content

armgit / nutch

Public Imported from GitHub
0

Apache Nutch is an extensible and scalable web crawler

NUTCH-669 - Consolidate code for Fetcher and Fetcher2

Sami Sirenfc37cf3733d2
diff --git a/CHANGES.txt b/CHANGES.txt
index 4a0dbce36..df84eb45b 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -372,6 +372,8 @@ Unreleased changes (1.0-dev)
139. NUTCH-700 - Neko1.9.11 goes into a loop (Julien Nioche, siren)
+140. NUTCH-669 - Consolidate code for Fetcher and Fetcher2 (siren)
+
Release 0.9 - 2007-04-02
diff --git a/bin/nutch b/bin/nutch
index 36a8ae098..b1bf2c6f6 100755
--- a/bin/nutch
+++ b/bin/nutch
@@ -41,7 +41,6 @@ if [ $# = 0 ]; then
echo " generate generate new segments to fetch from crawl db"
echo " freegen generate new segments to fetch from text files"
echo " fetch fetch a segment's pages"
- echo " fetch2 fetch a segment's pages using Fetcher2 implementation"
echo " parse parse a segment's pages"
echo " readseg read / dump segment data"
echo " mergesegs merge several segments, with optional filtering and slicing"
diff --git a/src/java/org/apache/nutch/crawl/Crawl.java b/src/java/org/apache/nutch/crawl/Crawl.java
index 9ae8a9684..75939ef85 100644
--- a/src/java/org/apache/nutch/crawl/Crawl.java
+++ b/src/java/org/apache/nutch/crawl/Crawl.java
@@ -24,7 +24,6 @@ import java.text.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
-import org.apache.nutch.fetcher.Fetcher;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.mapred.*;
@@ -36,6 +35,8 @@ import org.apache.nutch.util.HadoopFSUtil;
import org.apache.nutch.util.NutchConfiguration;
import org.apache.nutch.util.NutchJob;
+import org.apache.nutch.fetcher.Fetcher;
+
public class Crawl {
public static final Log LOG = LogFactory.getLog(Crawl.class);
@@ -118,7 +119,7 @@ public class Crawl {
LOG.info("Stopping at depth=" + i + " - no more URLs to fetch.");
break;
}
- fetcher.fetch(segment, threads); // fetch it
+ fetcher.fetch(segment, threads, org.apache.nutch.fetcher.Fetcher.isParsing(conf)); // fetch it
if (!Fetcher.isParsing(job)) {
parseSegment.parse(segment); // parse it, if needed
}
diff --git a/src/java/org/apache/nutch/fetcher/Fetcher.java b/src/java/org/apache/nutch/fetcher/Fetcher.java
index fc9bb1988..4b6c2b6a4 100644
--- a/src/java/org/apache/nutch/fetcher/Fetcher.java
+++ b/src/java/org/apache/nutch/fetcher/Fetcher.java
@@ -1,4 +1,4 @@
-/**
+/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
@@ -14,12 +14,17 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-
package org.apache.nutch.fetcher;
import java.io.IOException;
+import java.net.InetAddress;
import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.UnknownHostException;
+import java.util.*;
import java.util.Map.Entry;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.atomic.AtomicLong;
// Commons Logging imports
import org.apache.commons.logging.Log;
@@ -29,7 +34,7 @@ import org.apache.hadoop.io.*;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.mapred.*;
-import org.apache.hadoop.util.*;
+import org.apache.hadoop.util.StringUtils;
import org.apache.nutch.crawl.CrawlDatum;
import org.apache.nutch.crawl.NutchWritable;
@@ -43,24 +48,58 @@ import org.apache.nutch.scoring.ScoringFilters;
import org.apache.nutch.util.*;
-/** The fetcher. Most of the work is done by plugins. */
-public class Fetcher extends Configured implements Tool, MapRunnable<WritableComparable, Writable, Text, NutchWritable> {
+/**
+ * A queue-based fetcher.
+ *
+ * <p>This fetcher uses a well-known model of one producer (a QueueFeeder)
+ * and many consumers (FetcherThread-s).
+ *
+ * <p>QueueFeeder reads input fetchlists and
+ * populates a set of FetchItemQueue-s, which hold FetchItem-s that
+ * describe the items to be fetched. There are as many queues as there are unique
+ * hosts, but at any given time the total number of fetch items in all queues
+ * is less than a fixed number (currently set to a multiple of the number of
+ * threads).
+ *
+ * <p>As items are consumed from the queues, the QueueFeeder continues to add new
+ * input items, so that their total count stays fixed (FetcherThread-s may also
+ * add new items to the queues e.g. as a results of redirection) - until all
+ * input items are exhausted, at which point the number of items in the queues
+ * begins to decrease. When this number reaches 0 fetcher will finish.
+ *
+ * <p>This fetcher implementation handles per-host blocking itself, instead
+ * of delegating this work to protocol-specific plugins.
+ * Each per-host queue handles its own "politeness" settings, such as the
+ * maximum number of concurrent requests and crawl delay between consecutive
+ * requests - and also a list of requests in progress, and the time the last
+ * request was finished. As FetcherThread-s ask for new items to be fetched,
+ * queues may return eligible items or null if for "politeness" reasons this
+ * host's queue is not yet ready.
+ *
+ * <p>If there are still unfetched items in the queues, but none of the items
+ * are ready, FetcherThread-s will spin-wait until either some items become
+ * available, or a timeout is reached (at which point the Fetcher will abort,
+ * assuming the task is hung).
+ *
+ * @author Andrzej Bialecki
+ */
+public class Fetcher extends Configured implements
+ MapRunnable<Text, CrawlDatum, Text, NutchWritable> {
- public static final Log LOG = LogFactory.getLog(Fetcher.class);
-
public static final int PERM_REFRESH_TIME = 5;
public static final String CONTENT_REDIR = "content";
public static final String PROTOCOL_REDIR = "protocol";
- public static class InputFormat extends SequenceFileInputFormat<WritableComparable, Writable> {
+ public static final Log LOG = LogFactory.getLog(Fetcher.class);
+
+ public static class InputFormat extends SequenceFileInputFormat<Text, CrawlDatum> {
/** Don't split inputs, to keep things polite. */
public InputSplit[] getSplits(JobConf job, int nSplits)
throws IOException {
FileStatus[] files = listStatus(job);
- FileSystem fs = FileSystem.get(job);
- InputSplit[] splits = new InputSplit[files.length];
+ FileSplit[] splits = new FileSplit[files.length];
for (int i = 0; i < files.length; i++) {
FileStatus cur = files[i];
splits[i] = new FileSplit(cur.getPath(), 0,
@@ -70,24 +109,332 @@ public class Fetcher extends Configured implements Tool, MapRunnable<WritableCom
}
}
- private RecordReader<WritableComparable, Writable> input;
private OutputCollector<Text, NutchWritable> output;
private Reporter reporter;
-
+
private String segmentName;
- private int activeThreads;
- private int maxRedirect;
+ private AtomicInteger activeThreads = new AtomicInteger(0);
+ private AtomicInteger spinWaiting = new AtomicInteger(0);
private long start = System.currentTimeMillis(); // start time of fetcher run
- private long lastRequestStart = start;
+ private AtomicLong lastRequestStart = new AtomicLong(start);
- private long bytes; // total bytes fetched
- private int pages; // total pages fetched
- private int errors; // total pages errored
+ private AtomicLong bytes = new AtomicLong(0); // total bytes fetched
+ private AtomicInteger pages = new AtomicInteger(0); // total pages fetched
+ private AtomicInteger errors = new AtomicInteger(0); // total pages errored
private boolean storingContent;
private boolean parsing;
+ FetchItemQueues fetchQueues;
+ QueueFeeder feeder;
+
+ /**
+ * This class described the item to be fetched.
+ */
+ private static class FetchItem {
+ String queueID;
+ Text url;
+ URL u;
+ CrawlDatum datum;
+
+ public FetchItem(Text url, URL u, CrawlDatum datum, String queueID) {
+ this.url = url;
+ this.u = u;
+ this.datum = datum;
+ this.queueID = queueID;
+ }
+
+ /** Create an item. Queue id will be created based on <code>byIP</code>
+ * argument, either as a protocol + hostname pair, or protocol + IP
+ * address pair.
+ */
+ public static FetchItem create(Text url, CrawlDatum datum, boolean byIP) {
+ String queueID;
+ URL u = null;
+ try {
+ u = new URL(url.toString());
+ } catch (Exception e) {
+ LOG.warn("Cannot parse url: " + url, e);
+ return null;
+ }
+ String proto = u.getProtocol().toLowerCase();
+ String host;
+ if (byIP) {
+ try {
+ InetAddress addr = InetAddress.getByName(u.getHost());
+ host = addr.getHostAddress();
+ } catch (UnknownHostException e) {
+ // unable to resolve it, so don't fall back to host name
+ LOG.warn("Unable to resolve: " + u.getHost() + ", skipping.");
+ return null;
+ }
+ } else {
+ host = u.getHost();
+ if (host == null) {
+ LOG.warn("Unknown host for url: " + url + ", skipping.");
+ return null;
+ }
+ host = host.toLowerCase();
+ }
+ queueID = proto + "://" + host;
+ return new FetchItem(url, u, datum, queueID);
+ }
+
+ public CrawlDatum getDatum() {
+ return datum;
+ }
+
+ public String getQueueID() {
+ return queueID;
+ }
+ public Text getUrl() {
+ return url;
+ }
+
+ public URL getURL2() {
+ return u;
+ }
+ }
+
+ /**
+ * This class handles FetchItems which come from the same host ID (be it
+ * a proto/hostname or proto/IP pair). It also keeps track of requests in
+ * progress and elapsed time between requests.
+ */
+ private static class FetchItemQueue {
+ List<FetchItem> queue = Collections.synchronizedList(new LinkedList<FetchItem>());
+ Set<FetchItem> inProgress = Collections.synchronizedSet(new HashSet<FetchItem>());
+ AtomicLong nextFetchTime = new AtomicLong();
+ long crawlDelay;
+ long minCrawlDelay;
+ int maxThreads;
+ Configuration conf;
+
+ public FetchItemQueue(Configuration conf, int maxThreads, long crawlDelay, long minCrawlDelay) {
+ this.conf = conf;
+ this.maxThreads = maxThreads;
+ this.crawlDelay = crawlDelay;
+ this.minCrawlDelay = minCrawlDelay;
+ // ready to start
+ setEndTime(System.currentTimeMillis() - crawlDelay);
+ }
+
+ public int getQueueSize() {
+ return queue.size();
+ }
+
+ public int getInProgressSize() {
+ return inProgress.size();
+ }
+
+ public void finishFetchItem(FetchItem it, boolean asap) {
+ if (it != null) {
+ inProgress.remove(it);
+ setEndTime(System.currentTimeMillis(), asap);
+ }
+ }
+
+ public void addFetchItem(FetchItem it) {
+ if (it == null) return;
+ queue.add(it);
+ }
+
+ public void addInProgressFetchItem(FetchItem it) {
+ if (it == null) return;
+ inProgress.add(it);
+ }
+
+ public FetchItem getFetchItem() {
+ if (inProgress.size() >= maxThreads) return null;
+ long now = System.currentTimeMillis();
+ if (nextFetchTime.get() > now) return null;
+ FetchItem it = null;
+ if (queue.size() == 0) return null;
+ try {
+ it = queue.remove(0);
+ inProgress.add(it);
+ } catch (Exception e) {
+ LOG.error("Cannot remove FetchItem from queue or cannot add it to inProgress queue", e);
+ }
+ return it;
+ }
+
+ public synchronized void dump() {
+ LOG.info(" maxThreads = " + maxThreads);
+ LOG.info(" inProgress = " + inProgress.size());
+ LOG.info(" crawlDelay = " + crawlDelay);
+ LOG.info(" minCrawlDelay = " + minCrawlDelay);
+ LOG.info(" nextFetchTime = " + nextFetchTime.get());
+ LOG.info(" now = " + System.currentTimeMillis());
+ for (int i = 0; i < queue.size(); i++) {
+ FetchItem it = queue.get(i);
+ LOG.info(" " + i + ". " + it.url);
+ }
+ }
+
+ private void setEndTime(long endTime) {
+ setEndTime(endTime, false);
+ }
+
+ private void setEndTime(long endTime, boolean asap) {
+ if (!asap)
+ nextFetchTime.set(endTime + (maxThreads > 1 ? minCrawlDelay : crawlDelay));
+ else
+ nextFetchTime.set(endTime);
+ }
+ }
+
+ /**
+ * Convenience class - a collection of queues that keeps track of the total
+ * number of items, and provides items eligible for fetching from any queue.
+ */
+ private static class FetchItemQueues {
+ public static final String DEFAULT_ID = "default";
+ Map<String, FetchItemQueue> queues = new HashMap<String, FetchItemQueue>();
+ AtomicInteger totalSize = new AtomicInteger(0);
+ int maxThreads;
+ boolean byIP;
+ long crawlDelay;
+ long minCrawlDelay;
+ Configuration conf;
+
+ public FetchItemQueues(Configuration conf) {
+ this.conf = conf;
+ this.maxThreads = conf.getInt("fetcher.threads.per.host", 1);
+ // backward-compatible default setting
+ this.byIP = conf.getBoolean("fetcher.threads.per.host.by.ip", false);
+ this.crawlDelay = (long) (conf.getFloat("fetcher.server.delay", 1.0f) * 1000);
+ this.minCrawlDelay = (long) (conf.getFloat("fetcher.server.min.delay", 0.0f) * 1000);
+ }
+
+ public int getTotalSize() {
+ return totalSize.get();
+ }
+
+ public int getQueueCount() {
+ return queues.size();
+ }
+
+ public void addFetchItem(Text url, CrawlDatum datum) {
+ FetchItem it = FetchItem.create(url, datum, byIP);
+ if (it != null) addFetchItem(it);
+ }
+
+ public void addFetchItem(FetchItem it) {
+ FetchItemQueue fiq = getFetchItemQueue(it.queueID);
+ fiq.addFetchItem(it);
+ totalSize.incrementAndGet();
+ }
+
+ public void finishFetchItem(FetchItem it) {
+ finishFetchItem(it, false);
+ }
+
+ public void finishFetchItem(FetchItem it, boolean asap) {
+ FetchItemQueue fiq = queues.get(it.queueID);
+ if (fiq == null) {
+ LOG.warn("Attempting to finish item from unknown queue: " + it);
+ return;
+ }
+ fiq.finishFetchItem(it, asap);
+ }
+
+ public synchronized FetchItemQueue getFetchItemQueue(String id) {
+ FetchItemQueue fiq = queues.get(id);
+ if (fiq == null) {
+ // initialize queue
+ fiq = new FetchItemQueue(conf, maxThreads, crawlDelay, minCrawlDelay);
+ queues.put(id, fiq);
+ }
+ return fiq;
+ }
+
+ public synchronized FetchItem getFetchItem() {
+ Iterator<Map.Entry<String, FetchItemQueue>> it =
+ queues.entrySet().iterator();
+ while (it.hasNext()) {
+ FetchItemQueue fiq = it.next().getValue();
+ // reap empty queues
+ if (fiq.getQueueSize() == 0 && fiq.getInProgressSize() == 0) {
+ it.remove();
+ continue;
+ }
+ FetchItem fit = fiq.getFetchItem();
+ if (fit != null) {
+ totalSize.decrementAndGet();
+ return fit;
+ }
+ }
+ return null;
+ }
+
+ public synchronized void dump() {
+ for (String id : queues.keySet()) {
+ FetchItemQueue fiq = queues.get(id);
+ if (fiq.getQueueSize() == 0) continue;
+ LOG.info("* queue: " + id);
+ fiq.dump();
+ }
+ }
+ }
+
+ /**
+ * This class feeds the queues with input items, and re-fills them as
+ * items are consumed by FetcherThread-s.
+ */
+ private static class QueueFeeder extends Thread {
+ private RecordReader<Text, CrawlDatum> reader;
+ private FetchItemQueues queues;
+ private int size;
+
+ public QueueFeeder(RecordReader<Text, CrawlDatum> reader,
+ FetchItemQueues queues, int size) {
+ this.reader = reader;
+ this.queues = queues;
+ this.size = size;
+ this.setDaemon(true);
+ this.setName("QueueFeeder");
+ }
+
+ public void run() {
+ boolean hasMore = true;
+ int cnt = 0;
+
+ while (hasMore) {
+ int feed = size - queues.getTotalSize();
+ if (feed <= 0) {
+ // queues are full - spin-wait until they have some free space
+ try {
+ Thread.sleep(1000);
+ } catch (Exception e) {};
+ continue;
+ } else {
+ LOG.debug("-feeding " + feed + " input urls ...");
+ while (feed > 0 && hasMore) {
+ try {
+ Text url = new Text();
+ CrawlDatum datum = new CrawlDatum();
+ hasMore = reader.next(url, datum);
+ if (hasMore) {
+ queues.addFetchItem(url, datum);
+ cnt++;
+ feed--;
+ }
+ } catch (IOException e) {
+ LOG.fatal("QueueFeeder error reading input, record " + cnt, e);
+ return;
+ }
+ }
+ }
+ }
+ LOG.info("QueueFeeder finished: total " + cnt + " records.");
+ }
+ }
+
+ /**
+ * This class picks items from queues and fetches the pages.
+ */
private class FetcherThread extends Thread {
private Configuration conf;
private URLFilters urlFilters;
@@ -95,9 +442,13 @@ public class Fetcher extends Configured implements Tool, MapRunnable<WritableCom
private ParseUtil parseUtil;
private URLNormalizers normalizers;
private ProtocolFactory protocolFactory;
+ private long maxCrawlDelay;
+ private boolean byIP;
+ private int maxRedirect;
+ private String reprUrl;
private boolean redirecting;
private int redirectCount;
- private String reprUrl;
+ private boolean ignoreExternalLinks;
public FetcherThread(Configuration conf) {
this.setDaemon(true); // don't hang JVM on exit
@@ -108,83 +459,124 @@ public class Fetcher extends Configured implements Tool, MapRunnable<WritableCom
this.parseUtil = new ParseUtil(conf);
this.protocolFactory = new ProtocolFactory(conf);
this.normalizers = new URLNormalizers(conf, URLNormalizers.SCOPE_FETCHER);
+ this.maxCrawlDelay = conf.getInt("fetcher.max.crawl.delay", 30) * 1000;
+ // backward-compatible default setting
+ this.byIP = conf.getBoolean("fetcher.threads.per.host.by.ip", true);
+ this.maxRedirect = conf.getInt("http.redirect.max", 3);
+ this.ignoreExternalLinks =
+ conf.getBoolean("db.ignore.external.links", false);
}
public void run() {
- synchronized (Fetcher.this) {activeThreads++;} // count threads
+ activeThreads.incrementAndGet(); // count threads
+ FetchItem fit = null;
try {
- Text key = new Text();
- CrawlDatum datum = new CrawlDatum();
while (true) {
- // TODO : NUTCH-258 ...
- // If something bad happened, then exit
- // if (conf.getBoolean("fetcher.exit", false)) {
- // break;
- // ]
-
- try { // get next entry from input
- if (!input.next(key, datum)) {
- break; // at eof, exit
- }
- } catch (IOException e) {
- if (LOG.isFatalEnabled()) {
- e.printStackTrace(LogUtil.getFatalStream(LOG));
- LOG.fatal("fetcher caught:"+e.toString());
+ fit = fetchQueues.getFetchItem();
+ if (fit == null) {
+ if (feeder.isAlive() || fetchQueues.getTotalSize() > 0) {
+ LOG.debug(getName() + " spin-waiting ...");
+ // spin-wait.
+ spinWaiting.incrementAndGet();
+ try {
+ Thread.sleep(500);
+ } catch (Exception e) {}
+ spinWaiting.decrementAndGet();
+ continue;
+ } else {
+ // all done, finish this thread
+ return;
}
- break;
}
-
- synchronized (Fetcher.this) {
- lastRequestStart = System.currentTimeMillis();
- }
-
- // url may be changed through redirects.
- Text url = new Text(key);
-
+ lastRequestStart.set(System.currentTimeMillis());
Text reprUrlWritable =
- (Text) datum.getMetaData().get(Nutch.WRITABLE_REPR_URL_KEY);
+ (Text) fit.datum.getMetaData().get(Nutch.WRITABLE_REPR_URL_KEY);
if (reprUrlWritable == null) {
- reprUrl = key.toString();
+ reprUrl = fit.url.toString();
} else {
reprUrl = reprUrlWritable.toString();
}
-
try {
- if (LOG.isInfoEnabled()) { LOG.info("fetching " + url); }
+ if (LOG.isInfoEnabled()) { LOG.info("fetching " + fit.url); }
// fetch the page
+ redirecting = false;
redirectCount = 0;
do {
if (LOG.isDebugEnabled()) {
LOG.debug("redirectCount=" + redirectCount);
}
redirecting = false;
- Protocol protocol = this.protocolFactory.getProtocol(url.toString());
- ProtocolOutput output = protocol.getProtocolOutput(url, datum);
+ Protocol protocol = this.protocolFactory.getProtocol(fit.url.toString());
+ RobotRules rules = protocol.getRobotRules(fit.url, fit.datum);
+ if (!rules.isAllowed(fit.u)) {
+ // unblock
+ fetchQueues.finishFetchItem(fit, true);
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("Denied by robots.txt: " + fit.url);
+ }
+ output(fit.url, fit.datum, null, ProtocolStatus.STATUS_ROBOTS_DENIED, CrawlDatum.STATUS_FETCH_GONE);
+ continue;
+ }
+ if (rules.getCrawlDelay() > 0) {
+ if (rules.getCrawlDelay() > maxCrawlDelay) {
+ // unblock
+ fetchQueues.finishFetchItem(fit, true);
+ LOG.debug("Crawl-Delay for " + fit.url + " too long (" + rules.getCrawlDelay() + "), skipping");
+ output(fit.url, fit.datum, null, ProtocolStatus.STATUS_ROBOTS_DENIED, CrawlDatum.STATUS_FETCH_GONE);
+ continue;
+ } else {
+ FetchItemQueue fiq = fetchQueues.getFetchItemQueue(fit.queueID);
+ fiq.crawlDelay = rules.getCrawlDelay();
+ }
+ }
+ ProtocolOutput output = protocol.getProtocolOutput(fit.url, fit.datum);
ProtocolStatus status = output.getStatus();
Content content = output.getContent();
ParseStatus pstatus = null;
+ // unblock queue
+ fetchQueues.finishFetchItem(fit);
- String urlString = url.toString();
- if (reprUrl != null && !reprUrl.equals(urlString)) {
- datum.getMetaData().put(Nutch.WRITABLE_REPR_URL_KEY,
- new Text(reprUrl));
- }
+ String urlString = fit.url.toString();
switch(status.getCode()) {
+
+ case ProtocolStatus.WOULDBLOCK:
+ // retry ?
+ fetchQueues.addFetchItem(fit);
+ break;
case ProtocolStatus.SUCCESS: // got a page
- pstatus = output(url, datum, content, status, CrawlDatum.STATUS_FETCH_SUCCESS);
+ pstatus = output(fit.url, fit.datum, content, status, CrawlDatum.STATUS_FETCH_SUCCESS);
updateStatus(content.getContent().length);
if (pstatus != null && pstatus.isSuccess() &&
pstatus.getMinorCode() == ParseStatus.SUCCESS_REDIRECT) {
String newUrl = pstatus.getMessage();
int refreshTime = Integer.valueOf(pstatus.getArgs()[1]);
- url = handleRedirect(url, datum, urlString, newUrl,
- refreshTime < PERM_REFRESH_TIME,
- CONTENT_REDIR);
+ Text redirUrl =
+ handleRedirect(fit.url, fit.datum,
+ urlString, newUrl,
+ refreshTime < Fetcher.PERM_REFRESH_TIME,
+ Fetcher.CONTENT_REDIR);
+ if (redirUrl != null) {
+ CrawlDatum newDatum = new CrawlDatum(CrawlDatum.STATUS_DB_UNFETCHED,
+ fit.datum.getFetchInterval(), fit.datum.getScore());
+ if (reprUrl != null) {
+ newDatum.getMetaData().put(Nutch.WRITABLE_REPR_URL_KEY,
+ new Text(reprUrl));
+ }
+ fit = FetchItem.create(redirUrl, newDatum, byIP);
+ if (fit != null) {
+ FetchItemQueue fiq =
+ fetchQueues.getFetchItemQueue(fit.queueID);
+ fiq.addInProgressFetchItem(fit);
+ } else {
+ // stop redirecting
+ redirecting = false;
+ }
+ }
}
break;
@@ -199,55 +591,75 @@ public class Fetcher extends Configured implements Tool, MapRunnable<WritableCom
code = CrawlDatum.STATUS_FETCH_REDIR_TEMP;
temp = true;
}
- output(url, datum, content, status, code);
+ output(fit.url, fit.datum, content, status, code);
String newUrl = status.getMessage();
- url = handleRedirect(url, datum, urlString, newUrl,
- temp, PROTOCOL_REDIR);
+ Text redirUrl =
+ handleRedirect(fit.url, fit.datum,
+ urlString, newUrl, temp,
+ Fetcher.PROTOCOL_REDIR);
+ if (redirUrl != null) {
+ CrawlDatum newDatum = new CrawlDatum(CrawlDatum.STATUS_DB_UNFETCHED,
+ fit.datum.getFetchInterval(), fit.datum.getScore());
+ if (reprUrl != null) {
+ newDatum.getMetaData().put(Nutch.WRITABLE_REPR_URL_KEY,
+ new Text(reprUrl));
+ }
+ fit = FetchItem.create(redirUrl, newDatum, byIP);
+ if (fit != null) {
+ FetchItemQueue fiq =
+ fetchQueues.getFetchItemQueue(fit.queueID);
+ fiq.addInProgressFetchItem(fit);
+ } else {
+ // stop redirecting
+ redirecting = false;
+ }
+ } else {
+ // stop redirecting
+ redirecting = false;
+ }
break;
- // failures - increase the retry counter
case ProtocolStatus.EXCEPTION:
- logError(url, status.getMessage());
- /* FALLTHROUGH */
+ logError(fit.url, status.getMessage());
+ /* FALLTHROUGH */
case ProtocolStatus.RETRY: // retry
- case ProtocolStatus.WOULDBLOCK:
case ProtocolStatus.BLOCKED:
- output(url, datum, null, status, CrawlDatum.STATUS_FETCH_RETRY);
+ output(fit.url, fit.datum, null, status, CrawlDatum.STATUS_FETCH_RETRY);
break;
- // permanent failures
case ProtocolStatus.GONE: // gone
case ProtocolStatus.NOTFOUND:
case ProtocolStatus.ACCESS_DENIED:
case ProtocolStatus.ROBOTS_DENIED:
- output(url, datum, null, status, CrawlDatum.STATUS_FETCH_GONE);
+ output(fit.url, fit.datum, null, status, CrawlDatum.STATUS_FETCH_GONE);
break;
case ProtocolStatus.NOTMODIFIED:
- output(url, datum, null, status, CrawlDatum.STATUS_FETCH_NOTMODIFIED);
+ output(fit.url, fit.datum, null, status, CrawlDatum.STATUS_FETCH_NOTMODIFIED);
break;
-
+
default:
if (LOG.isWarnEnabled()) {
LOG.warn("Unknown ProtocolStatus: " + status.getCode());
}
- output(url, datum, null, status, CrawlDatum.STATUS_FETCH_GONE);
+ output(fit.url, fit.datum, null, status, CrawlDatum.STATUS_FETCH_RETRY);
}
if (redirecting && redirectCount >= maxRedirect) {
+ fetchQueues.finishFetchItem(fit);
if (LOG.isInfoEnabled()) {
- LOG.info(" - redirect count exceeded " + url);
+ LOG.info(" - redirect count exceeded " + fit.url);
}
- output(url, datum, null, status, CrawlDatum.STATUS_FETCH_GONE);
+ output(fit.url, fit.datum, null, ProtocolStatus.STATUS_REDIR_EXCEEDED, CrawlDatum.STATUS_FETCH_GONE);
}
} while (redirecting && (redirectCount < maxRedirect));
-
} catch (Throwable t) { // unexpected exception
- logError(url, t.toString());
- output(url, datum, null, null, CrawlDatum.STATUS_FETCH_RETRY);
-
+ // unblock
+ fetchQueues.finishFetchItem(fit);
+ logError(fit.url, t.toString());
+ output(fit.url, fit.datum, null, ProtocolStatus.STATUS_FAILED, CrawlDatum.STATUS_FETCH_RETRY);
}
}
@@ -257,7 +669,9 @@ public class Fetcher extends Configured implements Tool, MapRunnable<WritableCom
LOG.fatal("fetcher caught:"+e.toString());
}
} finally {
- synchronized (Fetcher.this) {activeThreads--;} // count threads
+ if (fit != null) fetchQueues.finishFetchItem(fit);
+ activeThreads.decrementAndGet(); // count threads
+ LOG.info("-finishing thread " + getName() + ", activeThreads=" + activeThreads);
}
}
@@ -267,6 +681,22 @@ public class Fetcher extends Configured implements Tool, MapRunnable<WritableCom
throws MalformedURLException, URLFilterException {
newUrl = normalizers.normalize(newUrl, URLNormalizers.SCOPE_FETCHER);
newUrl = urlFilters.filter(newUrl);
+
+ if (ignoreExternalLinks) {
+ try {
+ String origHost = new URL(urlString).getHost().toLowerCase();
+ String newHost = new URL(newUrl).getHost().toLowerCase();
+ if (!origHost.equals(newHost)) {
+ if (LOG.isDebugEnabled()) {
+ LOG.debug(" - ignoring redirect " + redirType + " from " +
+ urlString + " to " + newUrl +
+ " because external links are ignored");
+ }
+ return null;
+ }
+ } catch (MalformedURLException e) { }
+ }
+
if (newUrl != null && !newUrl.equals(urlString)) {
reprUrl = URLUtil.chooseRepr(reprUrl, newUrl, temp);
url = new Text(newUrl);
@@ -275,11 +705,12 @@ public class Fetcher extends Configured implements Tool, MapRunnable<WritableCom
redirectCount++;
if (LOG.isDebugEnabled()) {
LOG.debug(" - " + redirType + " redirect to " +
- url + " (fetching now)");
+ url + " (fetching now)");
}
return url;
} else {
- CrawlDatum newDatum = new CrawlDatum();
+ CrawlDatum newDatum = new CrawlDatum(CrawlDatum.STATUS_LINKED,
+ datum.getFetchInterval());
if (reprUrl != null) {
newDatum.getMetaData().put(Nutch.WRITABLE_REPR_URL_KEY,
new Text(reprUrl));
@@ -287,7 +718,7 @@ public class Fetcher extends Configured implements Tool, MapRunnable<WritableCom
output(url, newDatum, null, null, CrawlDatum.STATUS_LINKED);
if (LOG.isDebugEnabled()) {
LOG.debug(" - " + redirType + " redirect to " +
- url + " (fetching later)");
+ url + " (fetching later)");
}
return null;
}
@@ -304,9 +735,7 @@ public class Fetcher extends Configured implements Tool, MapRunnable<WritableCom
if (LOG.isInfoEnabled()) {
LOG.info("fetch of " + url + " failed with: " + message);
}
- synchronized (Fetcher.this) { // record failure
- errors++;
- }
+ errors.incrementAndGet();
}
private ParseStatus output(Text key, CrawlDatum datum,
@@ -408,37 +837,32 @@ public class Fetcher extends Configured implements Tool, MapRunnable<WritableCom
if (p != null) {
return p.getData().getStatus();
}
- }
+ }
return null;
}
}
- private synchronized void updateStatus(int bytesInPage) throws IOException {
- pages++;
- bytes += bytesInPage;
+ public Fetcher() { super(null); }
+
+ public Fetcher(Configuration conf) { super(conf); }
+
+ private void updateStatus(int bytesInPage) throws IOException {
+ pages.incrementAndGet();
+ bytes.addAndGet(bytesInPage);
}
+
private void reportStatus() throws IOException {
String status;
- synchronized (this) {
- long elapsed = (System.currentTimeMillis() - start)/1000;
- status =
- pages+" pages, "+errors+" errors, "
- + Math.round(((float)pages*10)/elapsed)/10.0+" pages/s, "
- + Math.round(((((float)bytes)*8)/1024)/elapsed)+" kb/s, ";
- }
+ long elapsed = (System.currentTimeMillis() - start)/1000;
+ status = activeThreads + " threads, " +
+ pages+" pages, "+errors+" errors, "
+ + Math.round(((float)pages.get()*10)/elapsed)/10.0+" pages/s, "
+ + Math.round(((((float)bytes.get())*8)/1024)/elapsed)+" kb/s, ";
reporter.setStatus(status);
}
- public Fetcher() {
-
- }
-
- public Fetcher(Configuration conf) {
- setConf(conf);
- }
-
public void configure(JobConf job) {
setConf(job);
@@ -461,18 +885,25 @@ public class Fetcher extends Configured implements Tool, MapRunnable<WritableCom
return conf.getBoolean("fetcher.store.content", true);
}
- public void run(RecordReader<WritableComparable, Writable> input, OutputCollector<Text, NutchWritable> output,
+ public void run(RecordReader<Text, CrawlDatum> input,
+ OutputCollector<Text, NutchWritable> output,
Reporter reporter) throws IOException {
- this.input = input;
this.output = output;
this.reporter = reporter;
+ this.fetchQueues = new FetchItemQueues(getConf());
- this.maxRedirect = getConf().getInt("http.redirect.max", 3);
-
int threadCount = getConf().getInt("fetcher.threads.fetch", 10);
if (LOG.isInfoEnabled()) { LOG.info("Fetcher: threads: " + threadCount); }
+ feeder = new QueueFeeder(input, fetchQueues, threadCount * 50);
+ //feeder.setPriority((Thread.MAX_PRIORITY + Thread.NORM_PRIORITY) / 2);
+ feeder.start();
+
+ // set non-blocking & no-robots mode for HTTP protocol plugins.
+ getConf().setBoolean(Protocol.CHECK_BLOCKING, false);
+ getConf().setBoolean(Protocol.CHECK_ROBOTS, false);
+
for (int i = 0; i < threadCount; i++) { // spawn threads
new FetcherThread(getConf()).start();
}
@@ -486,24 +917,30 @@ public class Fetcher extends Configured implements Tool, MapRunnable<WritableCom
} catch (InterruptedException e) {}
reportStatus();
+ LOG.info("-activeThreads=" + activeThreads + ", spinWaiting=" + spinWaiting.get()
+ + ", fetchQueues.totalSize=" + fetchQueues.getTotalSize());
+ if (!feeder.isAlive() && fetchQueues.getTotalSize() < 5) {
+ fetchQueues.dump();
+ }
// some requests seem to hang, despite all intentions
- synchronized (this) {
- if ((System.currentTimeMillis() - lastRequestStart) > timeout) {
- if (LOG.isWarnEnabled()) {
- LOG.warn("Aborting with "+activeThreads+" hung threads.");
- }
- return;
+ if ((System.currentTimeMillis() - lastRequestStart.get()) > timeout) {
+ if (LOG.isWarnEnabled()) {
+ LOG.warn("Aborting with "+activeThreads+" hung threads.");
}
+ return;
}
- } while (activeThreads > 0);
+ } while (activeThreads.get() > 0);
+ LOG.info("-activeThreads=" + activeThreads);
}
- public void fetch(Path segment, int threads)
+ public void fetch(Path segment, int threads, boolean parsing)
throws IOException {
+ checkConfiguration();
+
if (LOG.isInfoEnabled()) {
LOG.info("Fetcher: starting");
LOG.info("Fetcher: segment: " + segment);
@@ -514,6 +951,7 @@ public class Fetcher extends Configured implements Tool, MapRunnable<WritableCom
job.setInt("fetcher.threads.fetch", threads);
job.set(Nutch.SEGMENT_NAME_KEY, segment.getName());
+ job.setBoolean("fetcher.parse", parsing);
// for politeness, don't permit parallel execution of a single task
job.setSpeculativeExecution(false);
@@ -535,21 +973,19 @@ public class Fetcher extends Configured implements Tool, MapRunnable<WritableCom
/** Run the fetcher. */
public static void main(String[] args) throws Exception {
- int res = ToolRunner.run(NutchConfiguration.create(), new Fetcher(), args);
- System.exit(res);
- }
-
- public int run(String[] args) throws Exception {
String usage = "Usage: Fetcher <segment> [-threads n] [-noParsing]";
if (args.length < 1) {
System.err.println(usage);
- return -1;
+ System.exit(-1);
}
Path segment = new Path(args[0]);
- int threads = getConf().getInt("fetcher.threads.fetch", 10);
+
+ Configuration conf = NutchConfiguration.create();
+
+ int threads = conf.getInt("fetcher.threads.fetch", 10);
boolean parsing = true;
for (int i = 1; i < args.length; i++) { // parse command line
@@ -558,17 +994,49 @@ public class Fetcher extends Configured implements Tool, MapRunnable<WritableCom
} else if (args[i].equals("-noParsing")) parsing = false;
}
- getConf().setInt("fetcher.threads.fetch", threads);
+ conf.setInt("fetcher.threads.fetch", threads);
if (!parsing) {
- getConf().setBoolean("fetcher.parse", parsing);
- }
- try {
- fetch(segment, threads); // run the Fetcher
- return 0;
- } catch (Exception e) {
- LOG.fatal("Fetcher: " + StringUtils.stringifyException(e));
- return -1;
+ conf.setBoolean("fetcher.parse", parsing);
}
+ Fetcher fetcher = new Fetcher(conf); // make a Fetcher
+
+ fetcher.fetch(segment, threads, parsing); // run the Fetcher
+
+ }
+
+ private void checkConfiguration() {
+
+ // ensure that a value has been set for the agent name and that that
+ // agent name is the first value in the agents we advertise for robot
+ // rules parsing
+ String agentName = getConf().get("http.agent.name");
+ if (agentName == null || agentName.trim().length() == 0) {
+ String message = "Fetcher: No agents listed in 'http.agent.name'"
+ + " property.";
+ if (LOG.isFatalEnabled()) {
+ LOG.fatal(message);
+ }
+ throw new IllegalArgumentException(message);
+ } else {
+
+ // get all of the agents that we advertise
+ String agentNames = getConf().get("http.robots.agents");
+ StringTokenizer tok = new StringTokenizer(agentNames, ",");
+ ArrayList<String> agents = new ArrayList<String>();
+ while (tok.hasMoreTokens()) {
+ agents.add(tok.nextToken().trim());
+ }
+ // if the first one is not equal to our agent name, log fatal and throw
+ // an exception
+ if (!(agents.get(0)).equalsIgnoreCase(agentName)) {
+ String message = "Fetcher: Your 'http.agent.name' value should be "
+ + "listed first in 'http.robots.agents' property.";
+ if (LOG.isWarnEnabled()) {
+ LOG.warn(message);
+ }
+ }
+ }
}
+
}
diff --git a/src/java/org/apache/nutch/fetcher/Fetcher2.java b/src/java/org/apache/nutch/fetcher/Fetcher2.java
deleted file mode 100644
index 7f2c377d0..000000000
--- a/src/java/org/apache/nutch/fetcher/Fetcher2.java
+++ /dev/null
@@ -1,1036 +0,0 @@
-/**
- * Copyright 2005 The Apache Software Foundation
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.nutch.fetcher;
-
-import java.io.IOException;
-import java.net.InetAddress;
-import java.net.MalformedURLException;
-import java.net.URL;
-import java.net.UnknownHostException;
-import java.util.*;
-import java.util.Map.Entry;
-import java.util.concurrent.atomic.AtomicInteger;
-import java.util.concurrent.atomic.AtomicLong;
-
-// Commons Logging imports
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-
-import org.apache.hadoop.io.*;
-import org.apache.hadoop.fs.*;
-import org.apache.hadoop.conf.*;
-import org.apache.hadoop.mapred.*;
-import org.apache.hadoop.util.StringUtils;
-
-import org.apache.nutch.crawl.CrawlDatum;
-import org.apache.nutch.crawl.NutchWritable;
-import org.apache.nutch.crawl.SignatureFactory;
-import org.apache.nutch.metadata.Metadata;
-import org.apache.nutch.metadata.Nutch;
-import org.apache.nutch.net.*;
-import org.apache.nutch.protocol.*;
-import org.apache.nutch.parse.*;
-import org.apache.nutch.scoring.ScoringFilters;
-import org.apache.nutch.util.*;
-
-
-/**
- * A queue-based fetcher.
- *
- * <p>This fetcher uses a well-known model of one producer (a QueueFeeder)
- * and many consumers (FetcherThread-s).
- *
- * <p>QueueFeeder reads input fetchlists and
- * populates a set of FetchItemQueue-s, which hold FetchItem-s that
- * describe the items to be fetched. There are as many queues as there are unique
- * hosts, but at any given time the total number of fetch items in all queues
- * is less than a fixed number (currently set to a multiple of the number of
- * threads).
- *
- * <p>As items are consumed from the queues, the QueueFeeder continues to add new
- * input items, so that their total count stays fixed (FetcherThread-s may also
- * add new items to the queues e.g. as a results of redirection) - until all
- * input items are exhausted, at which point the number of items in the queues
- * begins to decrease. When this number reaches 0 fetcher will finish.
- *
- * <p>This fetcher implementation handles per-host blocking itself, instead
- * of delegating this work to protocol-specific plugins.
- * Each per-host queue handles its own "politeness" settings, such as the
- * maximum number of concurrent requests and crawl delay between consecutive
- * requests - and also a list of requests in progress, and the time the last
- * request was finished. As FetcherThread-s ask for new items to be fetched,
- * queues may return eligible items or null if for "politeness" reasons this
- * host's queue is not yet ready.
- *
- * <p>If there are still unfetched items in the queues, but none of the items
- * are ready, FetcherThread-s will spin-wait until either some items become
- * available, or a timeout is reached (at which point the Fetcher will abort,
- * assuming the task is hung).
- *
- * @author Andrzej Bialecki
- */
-public class Fetcher2 extends Configured implements
- MapRunnable<Text, CrawlDatum, Text, NutchWritable> {
-
- public static final Log LOG = LogFactory.getLog(Fetcher2.class);
-
- public static class InputFormat extends SequenceFileInputFormat<Text, CrawlDatum> {
- /** Don't split inputs, to keep things polite. */
- public InputSplit[] getSplits(JobConf job, int nSplits)
- throws IOException {
- FileStatus[] files = listStatus(job);
- FileSplit[] splits = new FileSplit[files.length];
- for (int i = 0; i < files.length; i++) {
- FileStatus cur = files[i];
- splits[i] = new FileSplit(cur.getPath(), 0,
- cur.getLen(), (String[])null);
- }
- return splits;
- }
- }
-
- private OutputCollector<Text, NutchWritable> output;
- private Reporter reporter;
-
- private String segmentName;
- private AtomicInteger activeThreads = new AtomicInteger(0);
- private AtomicInteger spinWaiting = new AtomicInteger(0);
-
- private long start = System.currentTimeMillis(); // start time of fetcher run
- private AtomicLong lastRequestStart = new AtomicLong(start);
-
- private AtomicLong bytes = new AtomicLong(0); // total bytes fetched
- private AtomicInteger pages = new AtomicInteger(0); // total pages fetched
- private AtomicInteger errors = new AtomicInteger(0); // total pages errored
-
- private boolean storingContent;
- private boolean parsing;
- FetchItemQueues fetchQueues;
- QueueFeeder feeder;
-
- /**
- * This class described the item to be fetched.
- */
- private static class FetchItem {
- String queueID;
- Text url;
- URL u;
- CrawlDatum datum;
-
- public FetchItem(Text url, URL u, CrawlDatum datum, String queueID) {
- this.url = url;
- this.u = u;
- this.datum = datum;
- this.queueID = queueID;
- }
-
- /** Create an item. Queue id will be created based on <code>byIP</code>
- * argument, either as a protocol + hostname pair, or protocol + IP
- * address pair.
- */
- public static FetchItem create(Text url, CrawlDatum datum, boolean byIP) {
- String queueID;
- URL u = null;
- try {
- u = new URL(url.toString());
- } catch (Exception e) {
- LOG.warn("Cannot parse url: " + url, e);
- return null;
- }
- String proto = u.getProtocol().toLowerCase();
- String host;
- if (byIP) {
- try {
- InetAddress addr = InetAddress.getByName(u.getHost());
- host = addr.getHostAddress();
- } catch (UnknownHostException e) {
- // unable to resolve it, so don't fall back to host name
- LOG.warn("Unable to resolve: " + u.getHost() + ", skipping.");
- return null;
- }
- } else {
- host = u.getHost();
- if (host == null) {
- LOG.warn("Unknown host for url: " + url + ", skipping.");
- return null;
- }
- host = host.toLowerCase();
- }
- queueID = proto + "://" + host;
- return new FetchItem(url, u, datum, queueID);
- }
-
- public CrawlDatum getDatum() {
- return datum;
- }
-
- public String getQueueID() {
- return queueID;
- }
-
- public Text getUrl() {
- return url;
- }
-
- public URL getURL2() {
- return u;
- }
- }
-
- /**
- * This class handles FetchItems which come from the same host ID (be it
- * a proto/hostname or proto/IP pair). It also keeps track of requests in
- * progress and elapsed time between requests.
- */
- private static class FetchItemQueue {
- List<FetchItem> queue = Collections.synchronizedList(new LinkedList<FetchItem>());
- Set<FetchItem> inProgress = Collections.synchronizedSet(new HashSet<FetchItem>());
- AtomicLong nextFetchTime = new AtomicLong();
- long crawlDelay;
- long minCrawlDelay;
- int maxThreads;
- Configuration conf;
-
- public FetchItemQueue(Configuration conf, int maxThreads, long crawlDelay, long minCrawlDelay) {
- this.conf = conf;
- this.maxThreads = maxThreads;
- this.crawlDelay = crawlDelay;
- this.minCrawlDelay = minCrawlDelay;
- // ready to start
- setEndTime(System.currentTimeMillis() - crawlDelay);
- }
-
- public int getQueueSize() {
- return queue.size();
- }
-
- public int getInProgressSize() {
- return inProgress.size();
- }
-
- public void finishFetchItem(FetchItem it, boolean asap) {
- if (it != null) {
- inProgress.remove(it);
- setEndTime(System.currentTimeMillis(), asap);
- }
- }
-
- public void addFetchItem(FetchItem it) {
- if (it == null) return;
- queue.add(it);
- }
-
- public void addInProgressFetchItem(FetchItem it) {
- if (it == null) return;
- inProgress.add(it);
- }
-
- public FetchItem getFetchItem() {
- if (inProgress.size() >= maxThreads) return null;
- long now = System.currentTimeMillis();
- if (nextFetchTime.get() > now) return null;
- FetchItem it = null;
- if (queue.size() == 0) return null;
- try {
- it = queue.remove(0);
- inProgress.add(it);
- } catch (Exception e) {
- LOG.error("Cannot remove FetchItem from queue or cannot add it to inProgress queue", e);
- }
- return it;
- }
-
- public synchronized void dump() {
- LOG.info(" maxThreads = " + maxThreads);
- LOG.info(" inProgress = " + inProgress.size());
- LOG.info(" crawlDelay = " + crawlDelay);
- LOG.info(" minCrawlDelay = " + minCrawlDelay);
- LOG.info(" nextFetchTime = " + nextFetchTime.get());
- LOG.info(" now = " + System.currentTimeMillis());
- for (int i = 0; i < queue.size(); i++) {
- FetchItem it = queue.get(i);
- LOG.info(" " + i + ". " + it.url);
- }
- }
-
- private void setEndTime(long endTime) {
- setEndTime(endTime, false);
- }
-
- private void setEndTime(long endTime, boolean asap) {
- if (!asap)
- nextFetchTime.set(endTime + (maxThreads > 1 ? minCrawlDelay : crawlDelay));
- else
- nextFetchTime.set(endTime);
- }
- }
-
- /**
- * Convenience class - a collection of queues that keeps track of the total
- * number of items, and provides items eligible for fetching from any queue.
- */
- private static class FetchItemQueues {
- public static final String DEFAULT_ID = "default";
- Map<String, FetchItemQueue> queues = new HashMap<String, FetchItemQueue>();
- AtomicInteger totalSize = new AtomicInteger(0);
- int maxThreads;
- boolean byIP;
- long crawlDelay;
- long minCrawlDelay;
- Configuration conf;
-
- public FetchItemQueues(Configuration conf) {
- this.conf = conf;
- this.maxThreads = conf.getInt("fetcher.threads.per.host", 1);
- // backward-compatible default setting
- this.byIP = conf.getBoolean("fetcher.threads.per.host.by.ip", false);
- this.crawlDelay = (long) (conf.getFloat("fetcher.server.delay", 1.0f) * 1000);
- this.minCrawlDelay = (long) (conf.getFloat("fetcher.server.min.delay", 0.0f) * 1000);
- }
-
- public int getTotalSize() {
- return totalSize.get();
- }
-
- public int getQueueCount() {
- return queues.size();
- }
-
- public void addFetchItem(Text url, CrawlDatum datum) {
- FetchItem it = FetchItem.create(url, datum, byIP);
- if (it != null) addFetchItem(it);
- }
-
- public void addFetchItem(FetchItem it) {
- FetchItemQueue fiq = getFetchItemQueue(it.queueID);
- fiq.addFetchItem(it);
- totalSize.incrementAndGet();
- }
-
- public void finishFetchItem(FetchItem it) {
- finishFetchItem(it, false);
- }
-
- public void finishFetchItem(FetchItem it, boolean asap) {
- FetchItemQueue fiq = queues.get(it.queueID);
- if (fiq == null) {
- LOG.warn("Attempting to finish item from unknown queue: " + it);
- return;
- }
- fiq.finishFetchItem(it, asap);
- }
-
- public synchronized FetchItemQueue getFetchItemQueue(String id) {
- FetchItemQueue fiq = queues.get(id);
- if (fiq == null) {
- // initialize queue
- fiq = new FetchItemQueue(conf, maxThreads, crawlDelay, minCrawlDelay);
- queues.put(id, fiq);
- }
- return fiq;
- }
-
- public synchronized FetchItem getFetchItem() {
- Iterator<Map.Entry<String, FetchItemQueue>> it =
- queues.entrySet().iterator();
- while (it.hasNext()) {
- FetchItemQueue fiq = it.next().getValue();
- // reap empty queues
- if (fiq.getQueueSize() == 0 && fiq.getInProgressSize() == 0) {
- it.remove();
- continue;
- }
- FetchItem fit = fiq.getFetchItem();
- if (fit != null) {
- totalSize.decrementAndGet();
- return fit;
- }
- }
- return null;
- }
-
- public synchronized void dump() {
- for (String id : queues.keySet()) {
- FetchItemQueue fiq = queues.get(id);
- if (fiq.getQueueSize() == 0) continue;
- LOG.info("* queue: " + id);
- fiq.dump();
- }
- }
- }
-
- /**
- * This class feeds the queues with input items, and re-fills them as
- * items are consumed by FetcherThread-s.
- */
- private static class QueueFeeder extends Thread {
- private RecordReader<Text, CrawlDatum> reader;
- private FetchItemQueues queues;
- private int size;
-
- public QueueFeeder(RecordReader<Text, CrawlDatum> reader,
- FetchItemQueues queues, int size) {
- this.reader = reader;
- this.queues = queues;
- this.size = size;
- this.setDaemon(true);
- this.setName("QueueFeeder");
- }
-
- public void run() {
- boolean hasMore = true;
- int cnt = 0;
-
- while (hasMore) {
- int feed = size - queues.getTotalSize();
- if (feed <= 0) {
- // queues are full - spin-wait until they have some free space
- try {
- Thread.sleep(1000);
- } catch (Exception e) {};
- continue;
- } else {
- LOG.debug("-feeding " + feed + " input urls ...");
- while (feed > 0 && hasMore) {
- try {
- Text url = new Text();
- CrawlDatum datum = new CrawlDatum();
- hasMore = reader.next(url, datum);
- if (hasMore) {
- queues.addFetchItem(url, datum);
- cnt++;
- feed--;
- }
- } catch (IOException e) {
- LOG.fatal("QueueFeeder error reading input, record " + cnt, e);
- return;
- }
- }
- }
- }
- LOG.info("QueueFeeder finished: total " + cnt + " records.");
- }
- }
-
- /**
- * This class picks items from queues and fetches the pages.
- */
- private class FetcherThread extends Thread {
- private Configuration conf;
- private URLFilters urlFilters;
- private ScoringFilters scfilters;
- private ParseUtil parseUtil;
- private URLNormalizers normalizers;
- private ProtocolFactory protocolFactory;
- private long maxCrawlDelay;
- private boolean byIP;
- private int maxRedirect;
- private String reprUrl;
- private boolean redirecting;
- private int redirectCount;
- private boolean ignoreExternalLinks;
-
- public FetcherThread(Configuration conf) {
- this.setDaemon(true); // don't hang JVM on exit
- this.setName("FetcherThread"); // use an informative name
- this.conf = conf;
- this.urlFilters = new URLFilters(conf);
- this.scfilters = new ScoringFilters(conf);
- this.parseUtil = new ParseUtil(conf);
- this.protocolFactory = new ProtocolFactory(conf);
- this.normalizers = new URLNormalizers(conf, URLNormalizers.SCOPE_FETCHER);
- this.maxCrawlDelay = conf.getInt("fetcher.max.crawl.delay", 30) * 1000;
- // backward-compatible default setting
- this.byIP = conf.getBoolean("fetcher.threads.per.host.by.ip", true);
- this.maxRedirect = conf.getInt("http.redirect.max", 3);
- this.ignoreExternalLinks =
- conf.getBoolean("db.ignore.external.links", false);
- }
-
- public void run() {
- activeThreads.incrementAndGet(); // count threads
-
- FetchItem fit = null;
- try {
-
- while (true) {
- fit = fetchQueues.getFetchItem();
- if (fit == null) {
- if (feeder.isAlive() || fetchQueues.getTotalSize() > 0) {
- LOG.debug(getName() + " spin-waiting ...");
- // spin-wait.
- spinWaiting.incrementAndGet();
- try {
- Thread.sleep(500);
- } catch (Exception e) {}
- spinWaiting.decrementAndGet();
- continue;
- } else {
- // all done, finish this thread
- return;
- }
- }
- lastRequestStart.set(System.currentTimeMillis());
- Text reprUrlWritable =
- (Text) fit.datum.getMetaData().get(Nutch.WRITABLE_REPR_URL_KEY);
- if (reprUrlWritable == null) {
- reprUrl = fit.url.toString();
- } else {
- reprUrl = reprUrlWritable.toString();
- }
- try {
- if (LOG.isInfoEnabled()) { LOG.info("fetching " + fit.url); }
-
- // fetch the page
- redirecting = false;
- redirectCount = 0;
- do {
- if (LOG.isDebugEnabled()) {
- LOG.debug("redirectCount=" + redirectCount);
- }
- redirecting = false;
- Protocol protocol = this.protocolFactory.getProtocol(fit.url.toString());
- RobotRules rules = protocol.getRobotRules(fit.url, fit.datum);
- if (!rules.isAllowed(fit.u)) {
- // unblock
- fetchQueues.finishFetchItem(fit, true);
- if (LOG.isDebugEnabled()) {
- LOG.debug("Denied by robots.txt: " + fit.url);
- }
- output(fit.url, fit.datum, null, ProtocolStatus.STATUS_ROBOTS_DENIED, CrawlDatum.STATUS_FETCH_GONE);
- continue;
- }
- if (rules.getCrawlDelay() > 0) {
- if (rules.getCrawlDelay() > maxCrawlDelay) {
- // unblock
- fetchQueues.finishFetchItem(fit, true);
- LOG.debug("Crawl-Delay for " + fit.url + " too long (" + rules.getCrawlDelay() + "), skipping");
- output(fit.url, fit.datum, null, ProtocolStatus.STATUS_ROBOTS_DENIED, CrawlDatum.STATUS_FETCH_GONE);
- continue;
- } else {
- FetchItemQueue fiq = fetchQueues.getFetchItemQueue(fit.queueID);
- fiq.crawlDelay = rules.getCrawlDelay();
- }
- }
- ProtocolOutput output = protocol.getProtocolOutput(fit.url, fit.datum);
- ProtocolStatus status = output.getStatus();
- Content content = output.getContent();
- ParseStatus pstatus = null;
- // unblock queue
- fetchQueues.finishFetchItem(fit);
-
- String urlString = fit.url.toString();
-
- switch(status.getCode()) {
-
- case ProtocolStatus.WOULDBLOCK:
- // retry ?
- fetchQueues.addFetchItem(fit);
- break;
-
- case ProtocolStatus.SUCCESS: // got a page
- pstatus = output(fit.url, fit.datum, content, status, CrawlDatum.STATUS_FETCH_SUCCESS);
- updateStatus(content.getContent().length);
- if (pstatus != null && pstatus.isSuccess() &&
- pstatus.getMinorCode() == ParseStatus.SUCCESS_REDIRECT) {
- String newUrl = pstatus.getMessage();
- int refreshTime = Integer.valueOf(pstatus.getArgs()[1]);
- Text redirUrl =
- handleRedirect(fit.url, fit.datum,
- urlString, newUrl,
- refreshTime < Fetcher.PERM_REFRESH_TIME,
- Fetcher.CONTENT_REDIR);
- if (redirUrl != null) {
- CrawlDatum newDatum = new CrawlDatum(CrawlDatum.STATUS_DB_UNFETCHED,
- fit.datum.getFetchInterval(), fit.datum.getScore());
- if (reprUrl != null) {
- newDatum.getMetaData().put(Nutch.WRITABLE_REPR_URL_KEY,
- new Text(reprUrl));
- }
- fit = FetchItem.create(redirUrl, newDatum, byIP);
- if (fit != null) {
- FetchItemQueue fiq =
- fetchQueues.getFetchItemQueue(fit.queueID);
- fiq.addInProgressFetchItem(fit);
- } else {
- // stop redirecting
- redirecting = false;
- }
- }
- }
- break;
-
- case ProtocolStatus.MOVED: // redirect
- case ProtocolStatus.TEMP_MOVED:
- int code;
- boolean temp;
- if (status.getCode() == ProtocolStatus.MOVED) {
- code = CrawlDatum.STATUS_FETCH_REDIR_PERM;
- temp = false;
- } else {
- code = CrawlDatum.STATUS_FETCH_REDIR_TEMP;
- temp = true;
- }
- output(fit.url, fit.datum, content, status, code);
- String newUrl = status.getMessage();
- Text redirUrl =
- handleRedirect(fit.url, fit.datum,
- urlString, newUrl, temp,
- Fetcher.PROTOCOL_REDIR);
- if (redirUrl != null) {
- CrawlDatum newDatum = new CrawlDatum(CrawlDatum.STATUS_DB_UNFETCHED,
- fit.datum.getFetchInterval(), fit.datum.getScore());
- if (reprUrl != null) {
- newDatum.getMetaData().put(Nutch.WRITABLE_REPR_URL_KEY,
- new Text(reprUrl));
- }
- fit = FetchItem.create(redirUrl, newDatum, byIP);
- if (fit != null) {
- FetchItemQueue fiq =
- fetchQueues.getFetchItemQueue(fit.queueID);
- fiq.addInProgressFetchItem(fit);
- } else {
- // stop redirecting
- redirecting = false;
- }
- } else {
- // stop redirecting
- redirecting = false;
- }
- break;
-
- case ProtocolStatus.EXCEPTION:
- logError(fit.url, status.getMessage());
- /* FALLTHROUGH */
- case ProtocolStatus.RETRY: // retry
- case ProtocolStatus.BLOCKED:
- output(fit.url, fit.datum, null, status, CrawlDatum.STATUS_FETCH_RETRY);
- break;
-
- case ProtocolStatus.GONE: // gone
- case ProtocolStatus.NOTFOUND:
- case ProtocolStatus.ACCESS_DENIED:
- case ProtocolStatus.ROBOTS_DENIED:
- output(fit.url, fit.datum, null, status, CrawlDatum.STATUS_FETCH_GONE);
- break;
-
- case ProtocolStatus.NOTMODIFIED:
- output(fit.url, fit.datum, null, status, CrawlDatum.STATUS_FETCH_NOTMODIFIED);
- break;
-
- default:
- if (LOG.isWarnEnabled()) {
- LOG.warn("Unknown ProtocolStatus: " + status.getCode());
- }
- output(fit.url, fit.datum, null, status, CrawlDatum.STATUS_FETCH_RETRY);
- }
-
- if (redirecting && redirectCount >= maxRedirect) {
- fetchQueues.finishFetchItem(fit);
- if (LOG.isInfoEnabled()) {
- LOG.info(" - redirect count exceeded " + fit.url);
- }
- output(fit.url, fit.datum, null, ProtocolStatus.STATUS_REDIR_EXCEEDED, CrawlDatum.STATUS_FETCH_GONE);
- }
-
- } while (redirecting && (redirectCount < maxRedirect));
-
- } catch (Throwable t) { // unexpected exception
- // unblock
- fetchQueues.finishFetchItem(fit);
- logError(fit.url, t.toString());
- output(fit.url, fit.datum, null, ProtocolStatus.STATUS_FAILED, CrawlDatum.STATUS_FETCH_RETRY);
- }
- }
-
- } catch (Throwable e) {
- if (LOG.isFatalEnabled()) {
- e.printStackTrace(LogUtil.getFatalStream(LOG));
- LOG.fatal("fetcher caught:"+e.toString());
- }
- } finally {
- if (fit != null) fetchQueues.finishFetchItem(fit);
- activeThreads.decrementAndGet(); // count threads
- LOG.info("-finishing thread " + getName() + ", activeThreads=" + activeThreads);
- }
- }
-
- private Text handleRedirect(Text url, CrawlDatum datum,
- String urlString, String newUrl,
- boolean temp, String redirType)
- throws MalformedURLException, URLFilterException {
- newUrl = normalizers.normalize(newUrl, URLNormalizers.SCOPE_FETCHER);
- newUrl = urlFilters.filter(newUrl);
-
- if (ignoreExternalLinks) {
- try {
- String origHost = new URL(urlString).getHost().toLowerCase();
- String newHost = new URL(newUrl).getHost().toLowerCase();
- if (!origHost.equals(newHost)) {
- if (LOG.isDebugEnabled()) {
- LOG.debug(" - ignoring redirect " + redirType + " from " +
- urlString + " to " + newUrl +
- " because external links are ignored");
- }
- return null;
- }
- } catch (MalformedURLException e) { }
- }
-
- if (newUrl != null && !newUrl.equals(urlString)) {
- reprUrl = URLUtil.chooseRepr(reprUrl, newUrl, temp);
- url = new Text(newUrl);
- if (maxRedirect > 0) {
- redirecting = true;
- redirectCount++;
- if (LOG.isDebugEnabled()) {
- LOG.debug(" - " + redirType + " redirect to " +
- url + " (fetching now)");
- }
- return url;
- } else {
- CrawlDatum newDatum = new CrawlDatum(CrawlDatum.STATUS_LINKED,
- datum.getFetchInterval());
- if (reprUrl != null) {
- newDatum.getMetaData().put(Nutch.WRITABLE_REPR_URL_KEY,
- new Text(reprUrl));
- }
- output(url, newDatum, null, null, CrawlDatum.STATUS_LINKED);
- if (LOG.isDebugEnabled()) {
- LOG.debug(" - " + redirType + " redirect to " +
- url + " (fetching later)");
- }
- return null;
- }
- } else {
- if (LOG.isDebugEnabled()) {
- LOG.debug(" - " + redirType + " redirect skipped: " +
- (newUrl != null ? "to same url" : "filtered"));
- }
- return null;
- }
- }
-
- private void logError(Text url, String message) {
- if (LOG.isInfoEnabled()) {
- LOG.info("fetch of " + url + " failed with: " + message);
- }
- errors.incrementAndGet();
- }
-
- private ParseStatus output(Text key, CrawlDatum datum,
- Content content, ProtocolStatus pstatus, int status) {
-
- datum.setStatus(status);
- datum.setFetchTime(System.currentTimeMillis());
- if (pstatus != null) datum.getMetaData().put(Nutch.WRITABLE_PROTO_STATUS_KEY, pstatus);
-
- ParseResult parseResult = null;
- if (content != null) {
- Metadata metadata = content.getMetadata();
- // add segment to metadata
- metadata.set(Nutch.SEGMENT_NAME_KEY, segmentName);
- // add score to content metadata so that ParseSegment can pick it up.
- try {
- scfilters.passScoreBeforeParsing(key, datum, content);
- } catch (Exception e) {
- if (LOG.isWarnEnabled()) {
- e.printStackTrace(LogUtil.getWarnStream(LOG));
- LOG.warn("Couldn't pass score, url " + key + " (" + e + ")");
- }
- }
- /* Note: Fetcher will only follow meta-redirects coming from the
- * original URL. */
- if (parsing && status == CrawlDatum.STATUS_FETCH_SUCCESS) {
- try {
- parseResult = this.parseUtil.parse(content);
- } catch (Exception e) {
- LOG.warn("Error parsing: " + key + ": " + StringUtils.stringifyException(e));
- }
-
- if (parseResult == null) {
- byte[] signature =
- SignatureFactory.getSignature(getConf()).calculate(content,
- new ParseStatus().getEmptyParse(conf));
- datum.setSignature(signature);
- }
- }
-
- /* Store status code in content So we can read this value during
- * parsing (as a separate job) and decide to parse or not.
- */
- content.getMetadata().add(Nutch.FETCH_STATUS_KEY, Integer.toString(status));
- }
-
- try {
- output.collect(key, new NutchWritable(datum));
- if (content != null && storingContent)
- output.collect(key, new NutchWritable(content));
- if (parseResult != null) {
- for (Entry<Text, Parse> entry : parseResult) {
- Text url = entry.getKey();
- Parse parse = entry.getValue();
- ParseStatus parseStatus = parse.getData().getStatus();
-
- if (!parseStatus.isSuccess()) {
- LOG.warn("Error parsing: " + key + ": " + parseStatus);
- parse = parseStatus.getEmptyParse(getConf());
- }
-
- // Calculate page signature. For non-parsing fetchers this will
- // be done in ParseSegment
- byte[] signature =
- SignatureFactory.getSignature(getConf()).calculate(content, parse);
- // Ensure segment name and score are in parseData metadata
- parse.getData().getContentMeta().set(Nutch.SEGMENT_NAME_KEY,
- segmentName);
- parse.getData().getContentMeta().set(Nutch.SIGNATURE_KEY,
- StringUtil.toHexString(signature));
- // Pass fetch time to content meta
- parse.getData().getContentMeta().set(Nutch.FETCH_TIME_KEY,
- Long.toString(datum.getFetchTime()));
- if (url.equals(key))
- datum.setSignature(signature);
- try {
- scfilters.passScoreAfterParsing(url, content, parse);
- } catch (Exception e) {
- if (LOG.isWarnEnabled()) {
- e.printStackTrace(LogUtil.getWarnStream(LOG));
- LOG.warn("Couldn't pass score, url " + key + " (" + e + ")");
- }
- }
- output.collect(url, new NutchWritable(
- new ParseImpl(new ParseText(parse.getText()),
- parse.getData(), parse.isCanonical())));
- }
- }
- } catch (IOException e) {
- if (LOG.isFatalEnabled()) {
- e.printStackTrace(LogUtil.getFatalStream(LOG));
- LOG.fatal("fetcher caught:"+e.toString());
- }
- }
-
- // return parse status if it exits
- if (parseResult != null && !parseResult.isEmpty()) {
- Parse p = parseResult.get(content.getUrl());
- if (p != null) {
- return p.getData().getStatus();
- }
- }
- return null;
- }
-
- }
-
- public Fetcher2() { super(null); }
-
- public Fetcher2(Configuration conf) { super(conf); }
-
- private void updateStatus(int bytesInPage) throws IOException {
- pages.incrementAndGet();
- bytes.addAndGet(bytesInPage);
- }
-
-
- private void reportStatus() throws IOException {
- String status;
- long elapsed = (System.currentTimeMillis() - start)/1000;
- status = activeThreads + " threads, " +
- pages+" pages, "+errors+" errors, "
- + Math.round(((float)pages.get()*10)/elapsed)/10.0+" pages/s, "
- + Math.round(((((float)bytes.get())*8)/1024)/elapsed)+" kb/s, ";
- reporter.setStatus(status);
- }
-
- public void configure(JobConf job) {
- setConf(job);
-
- this.segmentName = job.get(Nutch.SEGMENT_NAME_KEY);
- this.storingContent = isStoringContent(job);
- this.parsing = isParsing(job);
-
-// if (job.getBoolean("fetcher.verbose", false)) {
-// LOG.setLevel(Level.FINE);
-// }
- }
-
- public void close() {}
-
- public static boolean isParsing(Configuration conf) {
- return conf.getBoolean("fetcher.parse", true);
- }
-
- public static boolean isStoringContent(Configuration conf) {
- return conf.getBoolean("fetcher.store.content", true);
- }
-
- public void run(RecordReader<Text, CrawlDatum> input,
- OutputCollector<Text, NutchWritable> output,
- Reporter reporter) throws IOException {
-
- this.output = output;
- this.reporter = reporter;
- this.fetchQueues = new FetchItemQueues(getConf());
-
- int threadCount = getConf().getInt("fetcher.threads.fetch", 10);
- if (LOG.isInfoEnabled()) { LOG.info("Fetcher: threads: " + threadCount); }
-
- feeder = new QueueFeeder(input, fetchQueues, threadCount * 50);
- //feeder.setPriority((Thread.MAX_PRIORITY + Thread.NORM_PRIORITY) / 2);
- feeder.start();
-
- // set non-blocking & no-robots mode for HTTP protocol plugins.
- getConf().setBoolean(Protocol.CHECK_BLOCKING, false);
- getConf().setBoolean(Protocol.CHECK_ROBOTS, false);
-
- for (int i = 0; i < threadCount; i++) { // spawn threads
- new FetcherThread(getConf()).start();
- }
-
- // select a timeout that avoids a task timeout
- long timeout = getConf().getInt("mapred.task.timeout", 10*60*1000)/2;
-
- do { // wait for threads to exit
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {}
-
- reportStatus();
- LOG.info("-activeThreads=" + activeThreads + ", spinWaiting=" + spinWaiting.get()
- + ", fetchQueues.totalSize=" + fetchQueues.getTotalSize());
-
- if (!feeder.isAlive() && fetchQueues.getTotalSize() < 5) {
- fetchQueues.dump();
- }
- // some requests seem to hang, despite all intentions
- if ((System.currentTimeMillis() - lastRequestStart.get()) > timeout) {
- if (LOG.isWarnEnabled()) {
- LOG.warn("Aborting with "+activeThreads+" hung threads.");
- }
- return;
- }
-
- } while (activeThreads.get() > 0);
- LOG.info("-activeThreads=" + activeThreads);
-
- }
-
- public void fetch(Path segment, int threads, boolean parsing)
- throws IOException {
-
- checkConfiguration();
-
- if (LOG.isInfoEnabled()) {
- LOG.info("Fetcher: starting");
- LOG.info("Fetcher: segment: " + segment);
- }
-
- JobConf job = new NutchJob(getConf());
- job.setJobName("fetch " + segment);
-
- job.setInt("fetcher.threads.fetch", threads);
- job.set(Nutch.SEGMENT_NAME_KEY, segment.getName());
- job.setBoolean("fetcher.parse", parsing);
-
- // for politeness, don't permit parallel execution of a single task
- job.setSpeculativeExecution(false);
-
- FileInputFormat.addInputPath(job, new Path(segment, CrawlDatum.GENERATE_DIR_NAME));
- job.setInputFormat(InputFormat.class);
-
- job.setMapRunnerClass(Fetcher2.class);
-
- FileOutputFormat.setOutputPath(job, segment);
- job.setOutputFormat(FetcherOutputFormat.class);
- job.setOutputKeyClass(Text.class);
- job.setOutputValueClass(NutchWritable.class);
-
- JobClient.runJob(job);
- if (LOG.isInfoEnabled()) { LOG.info("Fetcher: done"); }
- }
-
-
- /** Run the fetcher. */
- public static void main(String[] args) throws Exception {
-
- String usage = "Usage: Fetcher <segment> [-threads n] [-noParsing]";
-
- if (args.length < 1) {
- System.err.println(usage);
- System.exit(-1);
- }
-
- Path segment = new Path(args[0]);
-
- Configuration conf = NutchConfiguration.create();
-
- int threads = conf.getInt("fetcher.threads.fetch", 10);
- boolean parsing = true;
-
- for (int i = 1; i < args.length; i++) { // parse command line
- if (args[i].equals("-threads")) { // found -threads option
- threads = Integer.parseInt(args[++i]);
- } else if (args[i].equals("-noParsing")) parsing = false;
- }
-
- conf.setInt("fetcher.threads.fetch", threads);
- if (!parsing) {
- conf.setBoolean("fetcher.parse", parsing);
- }
- Fetcher2 fetcher = new Fetcher2(conf); // make a Fetcher
-
- fetcher.fetch(segment, threads, parsing); // run the Fetcher
-
- }
-
- private void checkConfiguration() {
-
- // ensure that a value has been set for the agent name and that that
- // agent name is the first value in the agents we advertise for robot
- // rules parsing
- String agentName = getConf().get("http.agent.name");
- if (agentName == null || agentName.trim().length() == 0) {
- String message = "Fetcher: No agents listed in 'http.agent.name'"
- + " property.";
- if (LOG.isFatalEnabled()) {
- LOG.fatal(message);
- }
- throw new IllegalArgumentException(message);
- } else {
-
- // get all of the agents that we advertise
- String agentNames = getConf().get("http.robots.agents");
- StringTokenizer tok = new StringTokenizer(agentNames, ",");
- ArrayList<String> agents = new ArrayList<String>();
- while (tok.hasMoreTokens()) {
- agents.add(tok.nextToken().trim());
- }
-
- // if the first one is not equal to our agent name, log fatal and throw
- // an exception
- if (!(agents.get(0)).equalsIgnoreCase(agentName)) {
- String message = "Fetcher: Your 'http.agent.name' value should be "
- + "listed first in 'http.robots.agents' property.";
- if (LOG.isWarnEnabled()) {
- LOG.warn(message);
- }
- }
- }
- }
-
-}
diff --git a/src/test/org/apache/nutch/fetcher/TestFetcher.java b/src/test/org/apache/nutch/fetcher/TestFetcher.java
index 804593a87..f9cb7970e 100644
--- a/src/test/org/apache/nutch/fetcher/TestFetcher.java
+++ b/src/test/org/apache/nutch/fetcher/TestFetcher.java
@@ -97,9 +97,8 @@ public class TestFetcher extends TestCase {
long time=System.currentTimeMillis();
//fetch
- conf.setBoolean("fetcher.parse", true);
Fetcher fetcher=new Fetcher(conf);
- fetcher.fetch(generatedSegment, 1);
+ fetcher.fetch(generatedSegment, 1, true);
time=System.currentTimeMillis()-time;
@@ -175,7 +174,7 @@ public class TestFetcher extends TestCase {
try {
conf.setBoolean("fetcher.parse", true);
- Fetcher2 fetcher = new Fetcher2(conf);
+ Fetcher fetcher = new Fetcher(conf);
fetcher.fetch(null, 1, false);
} catch (IllegalArgumentException iae) {
String message = iae.getMessage();