Apache Nutch is an extensible and scalable web crawler
fix for NUTCH-2911 contributed by prakharchaube
prakharchaube511e4a955d52
diff --git a/src/java/org/apache/nutch/fetcher/Fetcher.java b/src/java/org/apache/nutch/fetcher/Fetcher.javaindex 18d0fd5ae..9c299e60f 100644--- a/src/java/org/apache/nutch/fetcher/Fetcher.java+++ b/src/java/org/apache/nutch/fetcher/Fetcher.java@@ -192,236 +192,253 @@ public class Fetcher extends NutchTool implements Tool {@Overridepublic void run(Context innerContext) throws IOException {-- setup(innerContext);- Configuration conf = innerContext.getConfiguration();- LinkedList<FetcherThread> fetcherThreads = new LinkedList<>();- FetchItemQueues fetchQueues = new FetchItemQueues(conf);- QueueFeeder feeder;-- int threadCount = conf.getInt("fetcher.threads.fetch", 10);- LOG.info("Fetcher: threads: {}", threadCount);-- // NUTCH-2582: adapt Tika MIME detector pool size to thread count- MimeUtil.setPoolSize(Math.max(10, threadCount / 2));-- int timeoutDivisor = conf.getInt("fetcher.threads.timeout.divisor", 2);- LOG.info("Fetcher: time-out divisor: {}", timeoutDivisor);-- int queueDepthMuliplier = conf.getInt(- "fetcher.queue.depth.multiplier", 50);-- feeder = new QueueFeeder(innerContext, fetchQueues, threadCount- * queueDepthMuliplier);-- // the value of the time limit is either -1 or the time where it should- // finish- long timelimit = conf.getLong("fetcher.timelimit", -1);- if (timelimit != -1)- feeder.setTimeLimit(timelimit);- feeder.start();-- for (int i = 0; i < threadCount; i++) { // spawn threads- FetcherThread t = new FetcherThread(conf, getActiveThreads(), fetchQueues,- feeder, spinWaiting, lastRequestStart, innerContext, errors, segmentName,- parsing, storingContent, pages, bytes);- fetcherThreads.add(t);- t.start();- }-- // select a timeout that avoids a task timeout- long timeout = conf.getInt("mapreduce.task.timeout", 10 * 60 * 1000)- / timeoutDivisor;-- // Used for threshold check, holds pages and bytes processed in the last- // second- int pagesLastSec;- int bytesLastSec;-- int throughputThresholdNumRetries = 0;- int throughputThresholdPages = conf- .getInt("fetcher.throughput.threshold.pages", -1);- LOG.info("Fetcher: throughput threshold: {}", throughputThresholdPages);-- int throughputThresholdMaxRetries = conf- .getInt("fetcher.throughput.threshold.retries", 5);- LOG.info("Fetcher: throughput threshold retries: {}",- throughputThresholdMaxRetries);-- long throughputThresholdTimeLimit = conf.getLong(- "fetcher.throughput.threshold.check.after", -1);+ setup(innerContext);+ try {+ Configuration conf = innerContext.getConfiguration();+ LinkedList<FetcherThread> fetcherThreads = new LinkedList<>();+ FetchItemQueues fetchQueues = new FetchItemQueues(conf);+ QueueFeeder feeder;++ int threadCount = conf.getInt("fetcher.threads.fetch", 10);+ LOG.info("Fetcher: threads: {}", threadCount);++ // NUTCH-2582: adapt Tika MIME detector pool size to thread count+ MimeUtil.setPoolSize(Math.max(10, threadCount / 2));++ int timeoutDivisor = conf.getInt("fetcher.threads.timeout.divisor", 2);+ LOG.info("Fetcher: time-out divisor: {}", timeoutDivisor);++ int queueDepthMuliplier = conf.getInt("fetcher.queue.depth.multiplier",+ 50);++ feeder = new QueueFeeder(innerContext, fetchQueues,+ threadCount * queueDepthMuliplier);++ // the value of the time limit is either -1 or the time where it should+ // finish+ long timelimit = conf.getLong("fetcher.timelimit", -1);+ if (timelimit != -1)+ feeder.setTimeLimit(timelimit);+ feeder.start();++ for (int i = 0; i < threadCount; i++) { // spawn threads+ FetcherThread t = new FetcherThread(conf, getActiveThreads(),+ fetchQueues, feeder, spinWaiting, lastRequestStart, innerContext,+ errors, segmentName, parsing, storingContent, pages, bytes);+ fetcherThreads.add(t);+ t.start();+ }- int targetBandwidth = conf.getInt("fetcher.bandwidth.target", -1) * 1000;- int maxNumThreads = conf.getInt("fetcher.maxNum.threads", threadCount);- if (maxNumThreads < threadCount) {- LOG.info("fetcher.maxNum.threads can't be < than {} : using {} instead",- threadCount, threadCount);- maxNumThreads = threadCount;- }- int bandwidthTargetCheckEveryNSecs = conf.getInt(- "fetcher.bandwidth.target.check.everyNSecs", 30);- if (bandwidthTargetCheckEveryNSecs < 1) {- LOG.info("fetcher.bandwidth.target.check.everyNSecs can't be < to 1 : using 1 instead");- bandwidthTargetCheckEveryNSecs = 1;- }+ // select a timeout that avoids a task timeout+ long timeout = conf.getInt("mapreduce.task.timeout", 10 * 60 * 1000)+ / timeoutDivisor;++ // Used for threshold check, holds pages and bytes processed in the last+ // second+ int pagesLastSec;+ int bytesLastSec;++ int throughputThresholdNumRetries = 0;++ int throughputThresholdPages = conf+ .getInt("fetcher.throughput.threshold.pages", -1);+ LOG.info("Fetcher: throughput threshold: {}", throughputThresholdPages);++ int throughputThresholdMaxRetries = conf+ .getInt("fetcher.throughput.threshold.retries", 5);+ LOG.info("Fetcher: throughput threshold retries: {}",+ throughputThresholdMaxRetries);++ long throughputThresholdTimeLimit = conf+ .getLong("fetcher.throughput.threshold.check.after", -1);++ int targetBandwidth = conf.getInt("fetcher.bandwidth.target", -1)+ * 1000;+ int maxNumThreads = conf.getInt("fetcher.maxNum.threads", threadCount);+ if (maxNumThreads < threadCount) {+ LOG.info(+ "fetcher.maxNum.threads can't be < than {} : using {} instead",+ threadCount, threadCount);+ maxNumThreads = threadCount;+ }+ int bandwidthTargetCheckEveryNSecs = conf+ .getInt("fetcher.bandwidth.target.check.everyNSecs", 30);+ if (bandwidthTargetCheckEveryNSecs < 1) {+ LOG.info(+ "fetcher.bandwidth.target.check.everyNSecs can't be < to 1 : using 1 instead");+ bandwidthTargetCheckEveryNSecs = 1;+ }- int maxThreadsPerQueue = conf.getInt("fetcher.threads.per.queue", 1);+ int maxThreadsPerQueue = conf.getInt("fetcher.threads.per.queue", 1);- int bandwidthTargetCheckCounter = 0;- long bytesAtLastBWTCheck = 0l;+ int bandwidthTargetCheckCounter = 0;+ long bytesAtLastBWTCheck = 0l;- do { // wait for threads to exit- pagesLastSec = pages.get();- bytesLastSec = (int) bytes.get();+ do { // wait for threads to exit+ pagesLastSec = pages.get();+ bytesLastSec = (int) bytes.get();- try {- Thread.sleep(1000);- } catch (InterruptedException e) {- }+ try {+ Thread.sleep(1000);+ } catch (InterruptedException e) {+ }- pagesLastSec = pages.get() - pagesLastSec;- bytesLastSec = (int) bytes.get() - bytesLastSec;+ pagesLastSec = pages.get() - pagesLastSec;+ bytesLastSec = (int) bytes.get() - bytesLastSec;- innerContext.getCounter("FetcherStatus", "bytes_downloaded").increment(bytesLastSec);+ innerContext.getCounter("FetcherStatus", "bytes_downloaded")+ .increment(bytesLastSec);- reportStatus(innerContext, fetchQueues, pagesLastSec, bytesLastSec);+ reportStatus(innerContext, fetchQueues, pagesLastSec, bytesLastSec);- LOG.info("-activeThreads=" + activeThreads + ", spinWaiting="- + spinWaiting.get() + ", fetchQueues.totalSize="- + fetchQueues.getTotalSize() + ", fetchQueues.getQueueCount="- + fetchQueues.getQueueCount());+ LOG.info("-activeThreads=" + activeThreads + ", spinWaiting="+ + spinWaiting.get() + ", fetchQueues.totalSize="+ + fetchQueues.getTotalSize() + ", fetchQueues.getQueueCount="+ + fetchQueues.getQueueCount());- if (!feeder.isAlive() && fetchQueues.getTotalSize() < 5) {- fetchQueues.dump();- }+ if (!feeder.isAlive() && fetchQueues.getTotalSize() < 5) {+ fetchQueues.dump();+ }- // if throughput threshold is enabled- if (throughputThresholdTimeLimit < System.currentTimeMillis()- && throughputThresholdPages != -1) {- // Check if we're dropping below the threshold- if (pagesLastSec < throughputThresholdPages) {- throughputThresholdNumRetries++;- LOG.warn(- "{}: dropping below configured threshold of {} pages per second",- throughputThresholdNumRetries, throughputThresholdPages);-- // Quit if we dropped below threshold too many times- if (throughputThresholdNumRetries == throughputThresholdMaxRetries) {- LOG.warn("Dropped below threshold too many times, killing!");-- // Disable the threshold checker- throughputThresholdPages = -1;-- // Empty the queues cleanly and get number of items that were- // dropped- int hitByThrougputThreshold = fetchQueues.emptyQueues();-- if (hitByThrougputThreshold != 0)- innerContext.getCounter("FetcherStatus", "hitByThrougputThreshold").increment(- hitByThrougputThreshold);+ // if throughput threshold is enabled+ if (throughputThresholdTimeLimit < System.currentTimeMillis()+ && throughputThresholdPages != -1) {+ // Check if we're dropping below the threshold+ if (pagesLastSec < throughputThresholdPages) {+ throughputThresholdNumRetries++;+ LOG.warn(+ "{}: dropping below configured threshold of {} pages per second",+ throughputThresholdNumRetries, throughputThresholdPages);++ // Quit if we dropped below threshold too many times+ if (throughputThresholdNumRetries == throughputThresholdMaxRetries) {+ LOG.warn("Dropped below threshold too many times, killing!");++ // Disable the threshold checker+ throughputThresholdPages = -1;++ // Empty the queues cleanly and get number of items that were+ // dropped+ int hitByThrougputThreshold = fetchQueues.emptyQueues();++ if (hitByThrougputThreshold != 0)+ innerContext+ .getCounter("FetcherStatus", "hitByThrougputThreshold")+ .increment(hitByThrougputThreshold);+ }}}- }- // adjust the number of threads if a target bandwidth has been set- if (targetBandwidth > 0) {- if (bandwidthTargetCheckCounter < bandwidthTargetCheckEveryNSecs)- bandwidthTargetCheckCounter++;- else if (bandwidthTargetCheckCounter == bandwidthTargetCheckEveryNSecs) {- long bpsSinceLastCheck = ((bytes.get() - bytesAtLastBWTCheck) * 8)- / bandwidthTargetCheckEveryNSecs;-- bytesAtLastBWTCheck = bytes.get();- bandwidthTargetCheckCounter = 0;-- int averageBdwPerThread = 0;- if (activeThreads.get() > 0)- averageBdwPerThread = (int) (bpsSinceLastCheck- / activeThreads.get());-- LOG.info("averageBdwPerThread : {} kbps", (averageBdwPerThread / 1000));-- if (bpsSinceLastCheck < targetBandwidth && averageBdwPerThread > 0) {- // check whether it is worth doing e.g. more queues than threads-- if ((fetchQueues.getQueueCount() * maxThreadsPerQueue) > activeThreads- .get()) {-- long remainingBdw = targetBandwidth - bpsSinceLastCheck;- int additionalThreads = Math.round(remainingBdw- / averageBdwPerThread);- int availableThreads = maxNumThreads - activeThreads.get();-- // determine the number of available threads (min between- // availableThreads and additionalThreads)- additionalThreads = (availableThreads < additionalThreads ? availableThreads- : additionalThreads);- LOG.info("Has space for more threads ({} vs {} kbps) \t=> adding {} new threads",- new Object[]{(bpsSinceLastCheck / 1000), (targetBandwidth / 1000), additionalThreads});- // activate new threads- for (int i = 0; i < additionalThreads; i++) {- FetcherThread thread = new FetcherThread(conf, getActiveThreads(), fetchQueues,- feeder, spinWaiting, lastRequestStart, innerContext, errors, segmentName, parsing,- storingContent, pages, bytes);- fetcherThreads.add(thread);- thread.start();+ // adjust the number of threads if a target bandwidth has been set+ if (targetBandwidth > 0) {+ if (bandwidthTargetCheckCounter < bandwidthTargetCheckEveryNSecs)+ bandwidthTargetCheckCounter++;+ else if (bandwidthTargetCheckCounter == bandwidthTargetCheckEveryNSecs) {+ long bpsSinceLastCheck = ((bytes.get() - bytesAtLastBWTCheck) * 8)+ / bandwidthTargetCheckEveryNSecs;++ bytesAtLastBWTCheck = bytes.get();+ bandwidthTargetCheckCounter = 0;++ int averageBdwPerThread = 0;+ if (activeThreads.get() > 0)+ averageBdwPerThread = (int) (bpsSinceLastCheck+ / activeThreads.get());++ LOG.info("averageBdwPerThread : {} kbps",+ (averageBdwPerThread / 1000));++ if (bpsSinceLastCheck < targetBandwidth+ && averageBdwPerThread > 0) {+ // check whether it is worth doing e.g. more queues than threads++ if ((fetchQueues.getQueueCount()+ * maxThreadsPerQueue) > activeThreads.get()) {++ long remainingBdw = targetBandwidth - bpsSinceLastCheck;+ int additionalThreads = Math+ .round(remainingBdw / averageBdwPerThread);+ int availableThreads = maxNumThreads - activeThreads.get();++ // determine the number of available threads (min between+ // availableThreads and additionalThreads)+ additionalThreads = (availableThreads < additionalThreads+ ? availableThreads+ : additionalThreads);+ LOG.info(+ "Has space for more threads ({} vs {} kbps) \t=> adding {} new threads",+ new Object[] { (bpsSinceLastCheck / 1000),+ (targetBandwidth / 1000), additionalThreads });+ // activate new threads+ for (int i = 0; i < additionalThreads; i++) {+ FetcherThread thread = new FetcherThread(conf,+ getActiveThreads(), fetchQueues, feeder, spinWaiting,+ lastRequestStart, innerContext, errors, segmentName,+ parsing, storingContent, pages, bytes);+ fetcherThreads.add(thread);+ thread.start();+ }+ }+ } else if (bpsSinceLastCheck > targetBandwidth+ && averageBdwPerThread > 0) {+ // if the bandwidth we're using is greater then the expected+ // bandwidth, we have to stop some threads+ long excessBdw = bpsSinceLastCheck - targetBandwidth;+ int excessThreads = Math.round(excessBdw / averageBdwPerThread);+ LOG.info(+ "Exceeding target bandwidth ({} vs {} kbps). \t=> excessThreads = {}",+ new Object[] { bpsSinceLastCheck / 1000,+ (targetBandwidth / 1000), excessThreads });+ // keep at least one+ if (excessThreads >= fetcherThreads.size())+ excessThreads = 0;+ // de-activates threads+ for (int i = 0; i < excessThreads; i++) {+ FetcherThread thread = fetcherThreads.removeLast();+ thread.setHalted(true);}- }- } else if (bpsSinceLastCheck > targetBandwidth- && averageBdwPerThread > 0) {- // if the bandwidth we're using is greater then the expected- // bandwidth, we have to stop some threads- long excessBdw = bpsSinceLastCheck - targetBandwidth;- int excessThreads = Math.round(excessBdw / averageBdwPerThread);- LOG.info("Exceeding target bandwidth ({} vs {} kbps). \t=> excessThreads = {}",- new Object[]{bpsSinceLastCheck / 1000, (targetBandwidth / 1000), excessThreads});- // keep at least one- if (excessThreads >= fetcherThreads.size())- excessThreads = 0;- // de-activates threads- for (int i = 0; i < excessThreads; i++) {- FetcherThread thread = fetcherThreads.removeLast();- thread.setHalted(true);}}}- }- // check timelimit- if (!feeder.isAlive()) {- int hitByTimeLimit = fetchQueues.checkTimelimit();- if (hitByTimeLimit != 0)- innerContext.getCounter("FetcherStatus", "hitByTimeLimit").increment(- hitByTimeLimit);- }+ // check timelimit+ if (!feeder.isAlive()) {+ int hitByTimeLimit = fetchQueues.checkTimelimit();+ if (hitByTimeLimit != 0)+ innerContext.getCounter("FetcherStatus", "hitByTimeLimit")+ .increment(hitByTimeLimit);+ }- // some requests seem to hang, despite all intentions- if ((System.currentTimeMillis() - lastRequestStart.get()) > timeout) {- if (LOG.isWarnEnabled()) {- LOG.warn("Aborting with {} hung threads.", activeThreads);- for (int i = 0; i < fetcherThreads.size(); i++) {- FetcherThread thread = fetcherThreads.get(i);- if (thread.isAlive()) {- LOG.warn("Thread #{} hung while processing {}", i, thread.getReprUrl());- if (LOG.isDebugEnabled()) {- StackTraceElement[] stack = thread.getStackTrace();- StringBuilder sb = new StringBuilder();- sb.append("Stack of thread #").append(i).append(":\n");- for (StackTraceElement s : stack) {- sb.append(s.toString()).append('\n');+ // some requests seem to hang, despite all intentions+ if ((System.currentTimeMillis() - lastRequestStart.get()) > timeout) {+ if (LOG.isWarnEnabled()) {+ LOG.warn("Aborting with {} hung threads.", activeThreads);+ for (int i = 0; i < fetcherThreads.size(); i++) {+ FetcherThread thread = fetcherThreads.get(i);+ if (thread.isAlive()) {+ LOG.warn("Thread #{} hung while processing {}", i,+ thread.getReprUrl());+ if (LOG.isDebugEnabled()) {+ StackTraceElement[] stack = thread.getStackTrace();+ StringBuilder sb = new StringBuilder();+ sb.append("Stack of thread #").append(i).append(":\n");+ for (StackTraceElement s : stack) {+ sb.append(s.toString()).append('\n');+ }+ LOG.debug(sb.toString());}- LOG.debug(sb.toString());}}}+ return;}- return;- }-- } while (activeThreads.get() > 0);- LOG.info("-activeThreads={}", activeThreads);+ } while (activeThreads.get() > 0);+ LOG.info("-activeThreads={}", activeThreads);+ } finally {+ cleanup(innerContext);+ }}}