Apache Nutch is an extensible and scalable web crawler
NUTCH-2442 Injector to stop if job fails to avoid loss of CrawlDb
Omkar Reddy2352f9a4f476
diff --git a/src/java/org/apache/nutch/crawl/Injector.java b/src/java/org/apache/nutch/crawl/Injector.javaindex 5f5fd15ff..0603eb4d1 100644--- a/src/java/org/apache/nutch/crawl/Injector.java+++ b/src/java/org/apache/nutch/crawl/Injector.java@@ -414,7 +414,16 @@ public class Injector extends NutchTool implements Tool {try {// run the job- job.waitForCompletion(true);+ boolean success = job.waitForCompletion(true);+ if (!success) {+ String message = "Injector job did not succeed, job status: "+ + job.getStatus().getState() + ", reason: "+ + job.getStatus().getFailureInfo();+ LOG.error(message);+ cleanupAfterFailure(tempCrawlDb, lock, fs);+ // throw exception so that calling routine can exit with error+ throw new RuntimeException(message);+ }// save output and perform cleanupCrawlDb.install(job, crawlDb);@@ -452,11 +461,21 @@ public class Injector extends NutchTool implements Tool {LOG.info("Injector: finished at " + sdf.format(end) + ", elapsed: "+ TimingUtil.elapsedTime(start, end));}- } catch (IOException e) {+ } catch (IOException | InterruptedException | ClassNotFoundException e) {+ LOG.error("Injector job failed", e);+ cleanupAfterFailure(tempCrawlDb, lock, fs);+ throw e;+ }+ }++ public void cleanupAfterFailure(Path tempCrawlDb, Path lock, FileSystem fs)+ throws IOException {+ try{if (fs.exists(tempCrawlDb)) {- fs.delete(tempCrawlDb, true);+ fs.delete(tempCrawlDb, true);}- LockUtil.removeLockFile(conf, lock);+ LockUtil.removeLockFile(fs, lock);+ } catch(IOException e) {throw e;}}diff --git a/src/java/org/apache/nutch/hostdb/ReadHostDb.java b/src/java/org/apache/nutch/hostdb/ReadHostDb.javaindex 28a7eb709..257dd6a75 100644--- a/src/java/org/apache/nutch/hostdb/ReadHostDb.java+++ b/src/java/org/apache/nutch/hostdb/ReadHostDb.java@@ -202,8 +202,17 @@ public class ReadHostDb extends Configured implements Tool {job.setNumReduceTasks(0);try {- job.waitForCompletion(true);- } catch (Exception e) {+ boolean success = job.waitForCompletion(true);+ if(!success){+ String message = "ReadHostDb job did not succeed, job status: "+ + job.getStatus().getState() + ", reason: "+ + job.getStatus().getFailureInfo();+ LOG.error(message);+ // throw exception so that calling routine can exit with error+ throw new RuntimeException(message);+ }+ } catch (IOException | InterruptedException | ClassNotFoundException e) {+ LOG.error("ReadHostDb job failed", e);throw e;}diff --git a/src/java/org/apache/nutch/util/CrawlCompletionStats.java b/src/java/org/apache/nutch/util/CrawlCompletionStats.javaindex 4920fbf32..4b8e1871f 100644--- a/src/java/org/apache/nutch/util/CrawlCompletionStats.java+++ b/src/java/org/apache/nutch/util/CrawlCompletionStats.java@@ -171,8 +171,17 @@ public class CrawlCompletionStats extends Configured implements Tool {job.setNumReduceTasks(numOfReducers);try {- job.waitForCompletion(true);- } catch (Exception e) {+ boolean success = job.waitForCompletion(true);+ if(!success){+ String message = jobName + " job did not succeed, job status: "+ + job.getStatus().getState() + ", reason: "+ + job.getStatus().getFailureInfo();+ LOG.error(message);+ // throw exception so that calling routine can exit with error+ throw new RuntimeException(message);+ }+ } catch (IOException | InterruptedException | ClassNotFoundException e) {+ LOG.error(jobName + " job failed");throw e;}diff --git a/src/java/org/apache/nutch/util/ProtocolStatusStatistics.java b/src/java/org/apache/nutch/util/ProtocolStatusStatistics.javaindex a18860634..84d892251 100644--- a/src/java/org/apache/nutch/util/ProtocolStatusStatistics.java+++ b/src/java/org/apache/nutch/util/ProtocolStatusStatistics.java@@ -122,8 +122,17 @@ public class ProtocolStatusStatistics extends Configured implements Tool {job.setNumReduceTasks(numOfReducers);try {- job.waitForCompletion(true);- } catch (Exception e) {+ boolean success = job.waitForCompletion(true);+ if(!success){+ String message = jobName + " job did not succeed, job status: "+ + job.getStatus().getState() + ", reason: "+ + job.getStatus().getFailureInfo();+ LOG.error(message);+ // throw exception so that calling routine can exit with error+ throw new RuntimeException(message);+ }+ } catch (IOException | InterruptedException | ClassNotFoundException e) {+ LOG.error(jobName + " job failed", e);throw e;}diff --git a/src/java/org/apache/nutch/util/SitemapProcessor.java b/src/java/org/apache/nutch/util/SitemapProcessor.javaindex 8b1d8cccf..ed740807d 100644--- a/src/java/org/apache/nutch/util/SitemapProcessor.java+++ b/src/java/org/apache/nutch/util/SitemapProcessor.java@@ -358,7 +358,16 @@ public class SitemapProcessor extends Configured implements Tool {job.setReducerClass(SitemapReducer.class);try {- job.waitForCompletion(true);+ boolean success = job.waitForCompletion(true);+ if(!success){+ String message = "SitemapProcessor_" + crawldb.toString() + " job did not succeed, job status: "+ + job.getStatus().getState() + ", reason: "+ + job.getStatus().getFailureInfo();+ LOG.error(message);+ cleanupAfterFailure(tempCrawlDb, lock, fs);+ // throw exception so that calling routine can exit with error+ throw new RuntimeException(message);+ }boolean preserveBackup = conf.getBoolean("db.preserve.backup", true);if (!preserveBackup && fs.exists(old))@@ -385,11 +394,21 @@ public class SitemapProcessor extends Configured implements Tool {long end = System.currentTimeMillis();LOG.info("SitemapProcessor: Finished at {}, elapsed: {}", sdf.format(end), TimingUtil.elapsedTime(start, end));}- } catch (Exception e) {- if (fs.exists(tempCrawlDb))- fs.delete(tempCrawlDb, true);+ } catch (IOException | InterruptedException | ClassNotFoundException e) {+ LOG.error("SitemapProcessor_" + crawldb.toString(), e);+ cleanupAfterFailure(tempCrawlDb, lock, fs);+ throw e;+ }+ }+ public void cleanupAfterFailure(Path tempCrawlDb, Path lock, FileSystem fs)+ throws IOException {+ try{+ if (fs.exists(tempCrawlDb)) {+ fs.delete(tempCrawlDb, true);+ }LockUtil.removeLockFile(fs, lock);+ } catch(IOException e) {throw e;}}diff --git a/src/java/org/apache/nutch/util/domain/DomainStatistics.java b/src/java/org/apache/nutch/util/domain/DomainStatistics.javaindex af1c2660e..3ff766725 100644--- a/src/java/org/apache/nutch/util/domain/DomainStatistics.java+++ b/src/java/org/apache/nutch/util/domain/DomainStatistics.java@@ -140,8 +140,17 @@ public class DomainStatistics extends Configured implements Tool {job.setNumReduceTasks(numOfReducers);try {- job.waitForCompletion(true);- } catch (Exception e) {+ boolean success = job.waitForCompletion(true);+ if(!success){+ String message = "Injector job did not succeed, job status: "+ + job.getStatus().getState() + ", reason: "+ + job.getStatus().getFailureInfo();+ LOG.error(message);+ // throw exception so that calling routine can exit with error+ throw new RuntimeException(message);+ }+ } catch (IOException | InterruptedException | ClassNotFoundException e) {+ LOG.error(jobName + " job failed", e);throw e;}