use methods to specify which report rather than string constants

* MultiReporter
    deleted. direct method calls are a better way to specify the kind of report desired
    getReports was never used to choose from the array of strings. no use case for MultiReporter.
* ArchiveUtils
    writeReportToString was not used, and it used the now deleted MultiReporter#reportTo
* DevUtils, Frontier, ToePool, AbstractFrontier, WorkQueueFrontier
    MultiReporter -> Reporter and make methods that implemented named multireport public
* FrontierNonemptyReport, FrontierSummaryReport
    call the appropriate report method instead of passing a string constant
This commit is contained in:
Travis Wellman
2012-06-01 17:57:25 -07:00
parent 493e2423f6
commit 07a2fc65ea
10 changed files with 147 additions and 272 deletions
@@ -28,7 +28,7 @@ import org.archive.modules.CrawlURI;
import org.archive.modules.deciderules.DecideRule;
import org.archive.modules.fetcher.FetchStats;
import org.archive.util.IdentityCacheable;
import org.archive.util.MultiReporter;
import org.archive.util.Reporter;
import org.json.JSONException;
import org.springframework.context.Lifecycle;
@@ -101,7 +101,7 @@ import org.springframework.context.Lifecycle;
* @see org.archive.crawler.framework.ToeThread
* @see org.archive.crawler.settings.ModuleType
*/
public interface Frontier extends Lifecycle, MultiReporter {
public interface Frontier extends Lifecycle, Reporter {
/**
* Get the next URI that should be processed. If no URI becomes availible
@@ -23,14 +23,14 @@ import java.io.PrintWriter;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeSet;
import java.util.Map.Entry;
import java.util.TreeSet;
import org.apache.commons.lang.StringUtils;
import org.archive.crawler.reporting.AlertThreadGroup;
import org.archive.util.ArchiveUtils;
import org.archive.util.Histotable;
import org.archive.util.MultiReporter;
import org.archive.util.Reporter;
/**
* A collection of ToeThreads. The class manages the ToeThreads currently
@@ -43,7 +43,7 @@ import org.archive.util.MultiReporter;
*
* @see org.archive.crawler.framework.ToeThread
*/
public class ToePool extends ThreadGroup implements MultiReporter {
public class ToePool extends ThreadGroup implements Reporter {
/** run worker thread slightly lower than usual */
public static int DEFAULT_TOE_PRIORITY = Thread.NORM_PRIORITY - 1;
@@ -70,7 +70,6 @@ public class ToePool extends ThreadGroup implements MultiReporter {
toe.interrupt();
}
}
// this.controller = null;
}
/**
@@ -190,40 +189,15 @@ public class ToePool extends ThreadGroup implements MultiReporter {
// Reporter implementation
//
public static String STANDARD_REPORT = "standard";
public static String COMPACT_REPORT = "compact";
protected static String[] REPORTS = {STANDARD_REPORT,COMPACT_REPORT};
// TODO delete me (not called from anywhere) and don't implement multireporter
@Override
public String[] getReports() {
return REPORTS;
}
@Override
public void reportTo(String name, PrintWriter writer) {
if(COMPACT_REPORT.equals(name)) {
compactReportTo(writer);
return;
}
if(name!=null && !STANDARD_REPORT.equals(name)) {
writer.print(name);
writer.print(" not recognized: giving standard report/n");
}
standardReportTo(writer);
}
/* (non-Javadoc)
* @see org.archive.util.Reporter#reportTo(java.io.Writer)
*/
protected void standardReportTo(PrintWriter writer) {
public void reportTo(PrintWriter writer) {
writer.print("Toe threads report - "
+ ArchiveUtils.get12DigitDate() + "\n");
writer.print(" Job being crawled: "
+ this.controller.getMetadata().getJobName() + "\n");
writer.print(" Number of toe threads in pool: " + getToeCount() + " ("
+ getActiveToeCount() + " active)\n\n");
Thread[] toes = this.getToes();
synchronized (toes) {
for (int i = 0; i < toes.length; i++) {
@@ -236,12 +210,9 @@ public class ToePool extends ThreadGroup implements MultiReporter {
}
}
}
}
}
/* (non-Javadoc)
* @see org.archive.util.Reporter#reportTo(java.io.Writer)
*/
protected void compactReportTo(PrintWriter writer) {
public void compactReportTo(PrintWriter writer) {
writer.print(getToeCount() + " threads (" + getActiveToeCount()
+ " active)\n");
@@ -337,11 +308,6 @@ public class ToePool extends ThreadGroup implements MultiReporter {
return "total: mostCommonStateTotal secondMostCommonStateTotal";
}
@Override
public void reportTo(PrintWriter writer) {
standardReportTo(writer);
}
public void waitForAll() {
while (true) try {
@@ -39,7 +39,6 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.Iterator;
import java.util.concurrent.atomic.AtomicLong;
@@ -1131,10 +1130,6 @@ public abstract class AbstractFrontier
return ArchiveUtils.shortReportLine(this);
}
public void reportTo(PrintWriter writer) {
reportTo(null, writer);
}
public void onApplicationEvent(ApplicationEvent event) {
if(event instanceof CrawlStateEvent) {
CrawlStateEvent event1 = (CrawlStateEvent)event;
@@ -21,9 +21,9 @@ package org.archive.crawler.frontier;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map.Entry;
import java.util.Queue;
import java.util.SortedMap;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.concurrent.DelayQueue;
import java.util.concurrent.LinkedBlockingQueue;
@@ -1089,15 +1089,6 @@ implements Closeable,
// Reporter implementation
//
public static String STANDARD_REPORT = "standard";
public static String ALL_NONEMPTY = "nonempty";
public static String ALL_QUEUES = "all";
protected static String[] REPORTS = {STANDARD_REPORT,ALL_NONEMPTY,ALL_QUEUES};
@Override
public String[] getReports() {
return REPORTS;
}
@Override
public Map<String, Object> shortReportMap() {
@@ -1227,27 +1218,131 @@ implements Closeable,
* @param writer Where to write to.
*/
@Override
public synchronized void reportTo(String name, PrintWriter writer) {
if(ALL_NONEMPTY.equals(name)) {
allNonemptyReportTo(writer);
return;
public synchronized void reportTo(PrintWriter writer) {
int allCount = allQueues.size();
int inProcessCount = inProcessQueues.size();
int readyCount = readyClassQueues.size();
int snoozedCount = getSnoozedCount();
int activeCount = inProcessCount + readyCount + snoozedCount;
int inactiveCount = getTotalInactiveQueues();
int retiredCount = getRetiredQueues().size();
int exhaustedCount =
allCount - activeCount - inactiveCount - retiredCount;
writer.print("Frontier report - ");
writer.print(ArchiveUtils.get12DigitDate());
writer.print("\n");
writer.print(" Job being crawled: ");
writer.print(controller.getMetadata().getJobName());
writer.print("\n");
writer.print("\n -----===== STATS =====-----\n");
writer.print(" Discovered: ");
writer.print(Long.toString(discoveredUriCount()));
writer.print("\n");
writer.print(" Queued: ");
writer.print(Long.toString(queuedUriCount()));
writer.print("\n");
writer.print(" Finished: ");
writer.print(Long.toString(finishedUriCount()));
writer.print("\n");
writer.print(" Successfully: ");
writer.print(Long.toString(succeededFetchCount()));
writer.print("\n");
writer.print(" Failed: ");
writer.print(Long.toString(failedFetchCount()));
writer.print("\n");
writer.print(" Disregarded: ");
writer.print(Long.toString(disregardedUriCount()));
writer.print("\n");
writer.print("\n -----===== QUEUES =====-----\n");
writer.print(" Already included size: ");
writer.print(Long.toString(uriUniqFilter.count()));
writer.print("\n");
writer.print(" pending: ");
writer.print(Long.toString(uriUniqFilter.pending()));
writer.print("\n");
writer.print("\n All class queues map size: ");
writer.print(Long.toString(allCount));
writer.print("\n");
writer.print( " Active queues: ");
writer.print(activeCount);
writer.print("\n");
writer.print(" In-process: ");
writer.print(inProcessCount);
writer.print("\n");
writer.print(" Ready: ");
writer.print(readyCount);
writer.print("\n");
writer.print(" Snoozed: ");
writer.print(snoozedCount);
writer.print("\n");
writer.print(" Inactive queues: ");
writer.print(inactiveCount);
writer.print(" (");
Map<Integer,Queue<String>> inactives = getInactiveQueuesByPrecedence();
boolean betwixt = false;
for(Integer k : inactives.keySet()) {
if(betwixt) {
writer.print("; ");
}
writer.print("p");
writer.print(k);
writer.print(": ");
writer.print(inactives.get(k).size());
betwixt = true;
}
if(ALL_QUEUES.equals(name)) {
allQueuesReportTo(writer);
return;
writer.print(")\n");
writer.print(" Retired queues: ");
writer.print(retiredCount);
writer.print("\n");
writer.print(" Exhausted queues: ");
writer.print(exhaustedCount);
writer.print("\n");
State last = lastReachedState;
writer.print("\n Last state: "+last);
writer.print("\n -----===== MANAGER THREAD =====-----\n");
ToeThread.reportThread(managerThread, writer);
writer.print("\n -----===== "+largestQueues.size()+" LONGEST QUEUES =====-----\n");
appendQueueReports(writer, "LONGEST", largestQueues.getEntriesDescending().iterator(), largestQueues.size(), largestQueues.size());
writer.print("\n -----===== IN-PROCESS QUEUES =====-----\n");
Collection<WorkQueue> inProcess = inProcessQueues;
ArrayList<WorkQueue> copy = extractSome(inProcess, maxQueuesPerReportCategory);
appendQueueReports(writer, "IN-PROCESS", copy.iterator(), copy.size(), maxQueuesPerReportCategory);
writer.print("\n -----===== READY QUEUES =====-----\n");
appendQueueReports(writer, "READY", this.readyClassQueues.iterator(),
this.readyClassQueues.size(), maxQueuesPerReportCategory);
writer.print("\n -----===== SNOOZED QUEUES =====-----\n");
Object[] objs = snoozedClassQueues.toArray();
DelayedWorkQueue[] qs = Arrays.copyOf(objs,objs.length,DelayedWorkQueue[].class);
Arrays.sort(qs);
appendQueueReports(writer, "SNOOZED", new ObjectArrayIterator(qs), getSnoozedCount(), maxQueuesPerReportCategory);
writer.print("\n -----===== INACTIVE QUEUES =====-----\n");
SortedMap<Integer,Queue<String>> sortedInactives = getInactiveQueuesByPrecedence();
for(Integer prec : sortedInactives.keySet()) {
Queue<String> inactiveQueues = sortedInactives.get(prec);
appendQueueReports(writer, "INACTIVE-p"+prec, inactiveQueues.iterator(),
inactiveQueues.size(), maxQueuesPerReportCategory);
}
if(name!=null && !STANDARD_REPORT.equals(name)) {
writer.print(name);
writer.print(" unavailable; standard report:\n");
}
standardReportTo(writer);
}
writer.print("\n -----===== RETIRED QUEUES =====-----\n");
appendQueueReports(writer, "RETIRED", getRetiredQueues().iterator(),
getRetiredQueues().size(), maxQueuesPerReportCategory);
writer.flush();
}
/** Compact report of all nonempty queues (one queue per line)
*
* @param writer
*/
private void allNonemptyReportTo(PrintWriter writer) {
public void allNonemptyReportTo(PrintWriter writer) {
ArrayList<WorkQueue> inProcessQueuesCopy;
synchronized(this.inProcessQueues) {
// grab a copy that will be stable against mods for report duration
@@ -1277,7 +1372,7 @@ implements Closeable,
*
* @param writer
*/
private void allQueuesReportTo(PrintWriter writer) {
public void allQueuesReportTo(PrintWriter writer) {
queueSingleLinesTo(writer, allQueues.keySet().iterator());
}
@@ -1322,130 +1417,6 @@ implements Closeable,
}
}
/**
* @param w Writer to print to.
*/
private void standardReportTo(PrintWriter w) {
int allCount = allQueues.size();
int inProcessCount = inProcessQueues.size();
int readyCount = readyClassQueues.size();
int snoozedCount = getSnoozedCount();
int activeCount = inProcessCount + readyCount + snoozedCount;
int inactiveCount = getTotalInactiveQueues();
int retiredCount = getRetiredQueues().size();
int exhaustedCount =
allCount - activeCount - inactiveCount - retiredCount;
w.print("Frontier report - ");
w.print(ArchiveUtils.get12DigitDate());
w.print("\n");
w.print(" Job being crawled: ");
w.print(controller.getMetadata().getJobName());
w.print("\n");
w.print("\n -----===== STATS =====-----\n");
w.print(" Discovered: ");
w.print(Long.toString(discoveredUriCount()));
w.print("\n");
w.print(" Queued: ");
w.print(Long.toString(queuedUriCount()));
w.print("\n");
w.print(" Finished: ");
w.print(Long.toString(finishedUriCount()));
w.print("\n");
w.print(" Successfully: ");
w.print(Long.toString(succeededFetchCount()));
w.print("\n");
w.print(" Failed: ");
w.print(Long.toString(failedFetchCount()));
w.print("\n");
w.print(" Disregarded: ");
w.print(Long.toString(disregardedUriCount()));
w.print("\n");
w.print("\n -----===== QUEUES =====-----\n");
w.print(" Already included size: ");
w.print(Long.toString(uriUniqFilter.count()));
w.print("\n");
w.print(" pending: ");
w.print(Long.toString(uriUniqFilter.pending()));
w.print("\n");
w.print("\n All class queues map size: ");
w.print(Long.toString(allCount));
w.print("\n");
w.print( " Active queues: ");
w.print(activeCount);
w.print("\n");
w.print(" In-process: ");
w.print(inProcessCount);
w.print("\n");
w.print(" Ready: ");
w.print(readyCount);
w.print("\n");
w.print(" Snoozed: ");
w.print(snoozedCount);
w.print("\n");
w.print(" Inactive queues: ");
w.print(inactiveCount);
w.print(" (");
Map<Integer,Queue<String>> inactives = getInactiveQueuesByPrecedence();
boolean betwixt = false;
for(Integer k : inactives.keySet()) {
if(betwixt) {
w.print("; ");
}
w.print("p");
w.print(k);
w.print(": ");
w.print(inactives.get(k).size());
betwixt = true;
}
w.print(")\n");
w.print(" Retired queues: ");
w.print(retiredCount);
w.print("\n");
w.print(" Exhausted queues: ");
w.print(exhaustedCount);
w.print("\n");
State last = lastReachedState;
w.print("\n Last state: "+last);
w.print("\n -----===== MANAGER THREAD =====-----\n");
ToeThread.reportThread(managerThread, w);
w.print("\n -----===== "+largestQueues.size()+" LONGEST QUEUES =====-----\n");
appendQueueReports(w, "LONGEST", largestQueues.getEntriesDescending().iterator(), largestQueues.size(), largestQueues.size());
w.print("\n -----===== IN-PROCESS QUEUES =====-----\n");
Collection<WorkQueue> inProcess = inProcessQueues;
ArrayList<WorkQueue> copy = extractSome(inProcess, maxQueuesPerReportCategory);
appendQueueReports(w, "IN-PROCESS", copy.iterator(), copy.size(), maxQueuesPerReportCategory);
w.print("\n -----===== READY QUEUES =====-----\n");
appendQueueReports(w, "READY", this.readyClassQueues.iterator(),
this.readyClassQueues.size(), maxQueuesPerReportCategory);
w.print("\n -----===== SNOOZED QUEUES =====-----\n");
Object[] objs = snoozedClassQueues.toArray();
DelayedWorkQueue[] qs = Arrays.copyOf(objs,objs.length,DelayedWorkQueue[].class);
Arrays.sort(qs);
appendQueueReports(w, "SNOOZED", new ObjectArrayIterator(qs), getSnoozedCount(), maxQueuesPerReportCategory);
w.print("\n -----===== INACTIVE QUEUES =====-----\n");
SortedMap<Integer,Queue<String>> sortedInactives = getInactiveQueuesByPrecedence();
for(Integer prec : sortedInactives.keySet()) {
Queue<String> inactiveQueues = sortedInactives.get(prec);
appendQueueReports(w, "INACTIVE-p"+prec, inactiveQueues.iterator(),
inactiveQueues.size(), maxQueuesPerReportCategory);
}
w.print("\n -----===== RETIRED QUEUES =====-----\n");
appendQueueReports(w, "RETIRED", getRetiredQueues().iterator(),
getRetiredQueues().size(), maxQueuesPerReportCategory);
w.flush();
}
/**
* Extract some of the elements in the given collection to an
* ArrayList. This method synchronizes on the given collection's
@@ -19,6 +19,7 @@
package org.archive.crawler.reporting;
import java.io.IOException;
import java.io.PrintWriter;
import org.archive.crawler.frontier.WorkQueueFrontier;
@@ -37,9 +38,14 @@ public class FrontierNonemptyReport extends Report {
writer.println("frontier unstarted");
} else if (stats.controller.getFrontier().isEmpty()) {
writer.println("frontier empty");
} else if (stats.controller.getFrontier() instanceof WorkQueueFrontier) {
((WorkQueueFrontier)stats.controller.getFrontier()).allNonemptyReportTo(writer);
} else {
stats.controller.getFrontier().reportTo(
WorkQueueFrontier.ALL_NONEMPTY, writer);
try {
stats.controller.getFrontier().reportTo(writer);
} catch (IOException e) {
e.printStackTrace();
}
}
}
@@ -19,10 +19,9 @@
package org.archive.crawler.reporting;
import java.io.IOException;
import java.io.PrintWriter;
import org.archive.crawler.frontier.WorkQueueFrontier;
/**
* Frontier summary report showing a limited number of queues of each
* type -- as typically consulted during a crawl in progress.
@@ -35,11 +34,12 @@ public class FrontierSummaryReport extends Report {
public void write(PrintWriter writer, StatisticsTracker stats) {
if(!stats.controller.getFrontier().isRunning()) {
writer.println("frontier unstarted");
// } else if (stats.controller.getFrontier().isEmpty()) {
// writer.println("frontier empty");
} else {
stats.controller.getFrontier().reportTo(
WorkQueueFrontier.STANDARD_REPORT, writer);
try {
stats.controller.getFrontier().reportTo(writer);
} catch (IOException e) {
e.printStackTrace();
}
}
}