Apache Nutch is an extensible and scalable web crawler
NUTCH-1771 Indexer fails if a segment is corrupted or incomplete
Sebastian Nageld4c552220be2
diff --git a/CHANGES.txt b/CHANGES.txtindex 216979c1f..748b52d2f 100644--- a/CHANGES.txt+++ b/CHANGES.txt@@ -2,6 +2,8 @@ Nutch Change LogNutch Current Development 1.10-SNAPSHOT+* NUTCH-1771 Indexer fails if a segment is corrupted or incomplete (Diaa, Chong Li via snagel)+* NUTCH-1975 New configuration for CommonCrawlDataDumper tool (Giuseppe Totaro via mattmann)* NUTCH-1979 CrawlDbReader to implement Tool (markus)diff --git a/conf/log4j.properties b/conf/log4j.propertiesindex 37284a72c..a18934100 100644--- a/conf/log4j.properties+++ b/conf/log4j.properties@@ -32,6 +32,7 @@ log4j.logger.org.apache.nutch.parse.ParseSegment=INFO,cmdstdoutlog4j.logger.org.apache.nutch.crawl.CrawlDbReader=INFO,cmdstdoutlog4j.logger.org.apache.nutch.crawl.CrawlDbMerger=INFO,cmdstdoutlog4j.logger.org.apache.nutch.crawl.LinkDbReader=INFO,cmdstdout+log4j.logger.org.apache.nutch.segment.SegmentChecker=INFO,cmdstdoutlog4j.logger.org.apache.nutch.segment.SegmentReader=INFO,cmdstdoutlog4j.logger.org.apache.nutch.segment.SegmentMerger=INFO,cmdstdoutlog4j.logger.org.apache.nutch.crawl.CrawlDb=INFO,cmdstdoutdiff --git a/src/java/org/apache/nutch/indexer/IndexingJob.java b/src/java/org/apache/nutch/indexer/IndexingJob.javaindex 208641898..1cfb1de16 100644--- a/src/java/org/apache/nutch/indexer/IndexingJob.java+++ b/src/java/org/apache/nutch/indexer/IndexingJob.java@@ -22,6 +22,7 @@ import java.util.ArrayList;import java.util.List;import java.util.Random;+import org.apache.nutch.segment.SegmentChecker;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.conf.Configured;import org.apache.hadoop.fs.FileStatus;@@ -153,7 +154,9 @@ public class IndexingJob extends Configured implements Tool {HadoopFSUtil.getPassDirectoriesFilter(fs));Path[] files = HadoopFSUtil.getPaths(fstats);for (Path p : files) {- segments.add(p);+ if (SegmentChecker.isIndexable(p,fs)) {+ segments.add(p);+ }}} else if (args[i].equals("-noCommit")) {noCommit = true;diff --git a/src/java/org/apache/nutch/segment/SegmentChecker.java b/src/java/org/apache/nutch/segment/SegmentChecker.javanew file mode 100644index 000000000..1f570c178--- /dev/null+++ b/src/java/org/apache/nutch/segment/SegmentChecker.java@@ -0,0 +1,118 @@+/**+ * 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.+ * The ASF licenses this file to You 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.segment;++import java.io.IOException;++import org.apache.nutch.crawl.CrawlDatum;+import org.apache.nutch.parse.ParseText;+import org.apache.nutch.parse.ParseData;+import org.apache.nutch.util.HadoopFSUtil;+import org.apache.hadoop.fs.FileStatus;+import org.apache.hadoop.fs.FileSystem;+import org.apache.hadoop.fs.Path;+import org.slf4j.Logger;+import org.slf4j.LoggerFactory;++/**+ * Checks whether a segment is valid, or has a certain status (generated,+ * fetched, parsed), or can be used safely for a certain processing step+ * (e.g., indexing).+ */+public class SegmentChecker {++ public static final Logger LOG = LoggerFactory+ .getLogger(SegmentChecker.class);++ /**+ * Check if the segment is indexable. May add new check methods here.+ */+ public static boolean isIndexable(Path segmentPath, FileSystem fs)+ throws IOException {+ if (segmentPath == null || fs == null) {+ LOG.info("No segment path or filesystem set.");+ return false;+ }++ boolean checkResult = true;+ checkResult &= checkSegmentDir(segmentPath, fs);+ // Add new check methods here++ if (checkResult) {+ return true;+ } else {+ return false;+ }+ }++ /**+ * Check the segment to see if it is valid based on the sub directories.+ */+ public static boolean checkSegmentDir(Path segmentPath, FileSystem fs)+ throws IOException {++ FileStatus[] fstats_segment = fs.listStatus(segmentPath,+ HadoopFSUtil.getPassDirectoriesFilter(fs));+ Path[] segment_files = HadoopFSUtil.getPaths(fstats_segment);++ boolean crawlFetchExists = false;+ boolean crawlParseExists = false;+ boolean parseDataExists = false;+ boolean parseTextExists = false;++ for (Path path : segment_files) {+ String pathName = path.getName();+ crawlFetchExists |= pathName.equals(CrawlDatum.FETCH_DIR_NAME);+ crawlParseExists |= pathName.equals(CrawlDatum.PARSE_DIR_NAME);+ parseDataExists |= pathName.equals(ParseData.DIR_NAME);+ parseTextExists |= pathName.equals(ParseText.DIR_NAME);+ }++ if (parseTextExists && crawlParseExists && crawlFetchExists+ && parseDataExists) {++ // No segment dir missing+ LOG.info("Segment dir is complete: " + segmentPath.toString() + ".");++ return true;+ } else {++ // log the missing dir+ StringBuilder missingDir = new StringBuilder("");+ if (parseDataExists == false) {+ missingDir.append(ParseData.DIR_NAME + ", ");+ }+ if (parseTextExists == false) {+ missingDir.append(ParseText.DIR_NAME + ", ");+ }+ if (crawlParseExists == false) {+ missingDir.append(CrawlDatum.PARSE_DIR_NAME + ", ");+ }+ if (crawlFetchExists == false) {+ missingDir.append(CrawlDatum.FETCH_DIR_NAME + ", ");+ }++ String missingDirString = missingDir.toString();+ LOG.warn("Skipping segment: " + segmentPath.toString()+ + ". Missing sub directories: "+ + missingDirString.substring(0, missingDirString.length() - 2));++ return false;+ }+ }++}\ No newline at end of file