Skip to content

armgit / nutch

Public Imported from GitHub
0

Apache Nutch is an extensible and scalable web crawler

NUTCH-738 Close SegmentUpdater when FetchedSegments is closed.

Andrzej Bialecki30181a0e822f
diff --git a/CHANGES.txt b/CHANGES.txt
index 994f7d46e..9d119412a 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -2,6 +2,9 @@ Nutch Change Log
Unreleased Changes
+* NUTCH-738 Close SegmentUpdater when FetchedSegments is closed
+ (Martina Kich, Kirby Bohling via ab)
+
* NUTCH-746 NutchBeanConstructor does not close NutchBean upon contextDestroyed,
causing resource leak in the container. (Kirby Bohling via ab)
diff --git a/src/java/org/apache/nutch/searcher/FetchedSegments.java b/src/java/org/apache/nutch/searcher/FetchedSegments.java
index 4e5d515d4..7ad1ff707 100644
--- a/src/java/org/apache/nutch/searcher/FetchedSegments.java
+++ b/src/java/org/apache/nutch/searcher/FetchedSegments.java
@@ -66,9 +66,19 @@ public class FetchedSegments implements RPCSegmentBean {
private class SegmentUpdater extends Thread {
+ private volatile boolean stopRequested = false;
+
+ @Override
+ public void interrupt() {
+ super.interrupt();
+ stopRequested = true;
+ }
+
+
@Override
public void run() {
- while (true) {
+
+ while (!stopRequested && !Thread.currentThread().isInterrupted()) {
try {
final FileStatus[] fstats = fs.listStatus(segmentsDir,
HadoopFSUtil.getPassDirectoriesFilter(fs));
@@ -194,7 +204,9 @@ public class FetchedSegments implements RPCSegmentBean {
private final FileSystem fs;
private final Configuration conf;
private final Path segmentsDir;
- private final SegmentUpdater segUpdater;
+
+ // This must be nullable upon close, so do not declare final.
+ private SegmentUpdater segUpdater;
private final Summarizer summarizer;
/** Construct given a directory containing fetcher output. */
@@ -303,6 +315,13 @@ public class FetchedSegments implements RPCSegmentBean {
}
public void close() throws IOException {
+ // Interrupt that thread to convince it to stop running.
+ segUpdater.interrupt();
+
+ // Break reference cycle, otherwise this points to segUpdater, and
+ // segUpdater.$0 points to this. It appeared to keep the thread from
+ // being GC'ed/reaped.
+ segUpdater = null;
final Iterator<Segment> iterator = segments.values().iterator();
while (iterator.hasNext()) {
iterator.next().close();