Soft404Detector: keep per-server state in a State class

Move the soft 404 fields and methods from CrawlServer into
Soft404Detector.State, which CrawlServer holds and creates when first
requested. This keeps the detector's logic in one place and leaves
servers the detector never looks at without the state.
This commit is contained in:
Alex Osborne committed 2026-11-24 13:11:56 +09:00
1 parent 2e80500747
commit f9363ca0ae
4 files changed
+78 -58

No files matched your search

@@ -38,6 +38,7 @@ import org.archive.bdb.AutoKryo;
import org.archive.modules.CrawlURI;
import org.archive.modules.credential.Credential;
import org.archive.modules.fetcher.FetchStats;
import org.archive.modules.processor.Soft404Detector;
import org.archive.net.UURI;
import org.archive.net.UURIFactory;
import org.archive.util.IdentityCacheable;
@@ -81,10 +82,7 @@ public class CrawlServer implements Serializable, FetchStats.HasFetchStats, Iden
protected int http3Port;
protected long http3Expiry;
/** Soft 404 detection state, maintained by Soft404Detector. */
protected boolean soft404ProbeScheduled;
/** Non-null only if the probe found a soft 404. */
protected Long soft404Fingerprint;
protected Soft404Detector.State soft404State;
/**
* Creates a new CrawlServer object.
@@ -259,36 +257,11 @@ public class CrawlServer implements Serializable, FetchStats.HasFetchStats, Iden
}
/**
* Marks the soft 404 probe as scheduled, unless it already was.
* @return true if the caller should schedule a probe
* Soft 404 detection state, maintained by Soft404Detector. Created when first requested.
*/
public synchronized boolean startSoft404Probe() {
if (soft404ProbeScheduled) return false;
soft404ProbeScheduled = true;
return true;
}
public synchronized boolean isSoft404ProbeScheduled() {
return soft404ProbeScheduled;
}
/**
* @return the fingerprint of the page returned with a success status for a nonexistent URL,
* or null if there is none
*/
public synchronized Long getSoft404Fingerprint() {
return soft404Fingerprint;
}
/**
* Records the fingerprint of the page returned with a success status for a nonexistent URL,
* unless one was already recorded.
* @return true if the fingerprint was recorded
*/
public synchronized boolean setSoft404FingerprintIfAbsent(long fingerprint) {
if (soft404Fingerprint != null) return false;
soft404Fingerprint = fingerprint;
return true;
public synchronized Soft404Detector.State getSoft404State() {
if (soft404State == null) soft404State = new Soft404Detector.State();
return soft404State;
}
public synchronized void clearAltSvc() {
@@ -400,6 +373,7 @@ public class CrawlServer implements Serializable, FetchStats.HasFetchStats, Iden
kryo.register(CrawlServer.class);
kryo.autoregister(FetchStats.class);
kryo.autoregister(Robotstxt.class);
kryo.autoregister(Soft404Detector.State.class);
}
//
@@ -21,6 +21,7 @@ package org.archive.modules.processor;
import static java.nio.charset.StandardCharsets.UTF_8;
import java.io.IOException;
import java.io.Serializable;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Comparator;
@@ -161,7 +162,7 @@ public class Soft404Detector extends Processor {
int status = curi.getFetchStatus();
return status >= 300 && status < 400 ? ProcessResult.PROCEED : ProcessResult.FINISH;
}
Long fingerprint = server.getSoft404Fingerprint();
Long fingerprint = server.getSoft404State().getFingerprint();
if (fingerprint == null) {
scheduleProbe(curi, server);
return ProcessResult.PROCEED;
@@ -177,7 +178,7 @@ public class Soft404Detector extends Processor {
protected void scheduleProbe(CrawlURI curi, CrawlServer server) {
int status = curi.getFetchStatus();
if (status < 200 || status >= 400) return;
if (!server.startSoft404Probe()) return;
if (!server.getSoft404State().startProbe()) return;
server.makeDirty();
try {
UURI probeUri = UURIFactory.getInstance(curi.getUURI(), probePath);
@@ -192,10 +193,11 @@ public class Soft404Detector extends Processor {
protected void recordProbeResult(CrawlURI curi, CrawlServer server) {
curi.getAnnotations().add(PROBE_ANNOTATION);
State state = server.getSoft404State();
// a probe redirected from another server counts as this server's probe
if (server.startSoft404Probe()) server.makeDirty();
if (state.startProbe()) server.makeDirty();
if (curi.getFetchStatus() != 200 || curi.getContentLength() <= 0 || !isHtml(curi)) return;
if (!server.setSoft404FingerprintIfAbsent(fingerprint(curi))) return;
if (!state.setFingerprintIfAbsent(fingerprint(curi))) return;
server.makeDirty();
serversWithSoft404s.incrementAndGet();
if (logger.isLoggable(Level.FINE)) {
@@ -384,4 +386,48 @@ public class Soft404Detector extends Processor {
" Servers with soft 404s: " + serversWithSoft404s.get() + "\n" +
" Soft 404s detected: " + soft404sDetected.get() + "\n";
}
/**
* Soft 404 detection state of one server, kept in its {@link CrawlServer}.
*/
public static class State implements Serializable {
private static final long serialVersionUID = 1L;
private boolean probeScheduled;
/** Non-null only if the probe found a soft 404. */
private Long fingerprint;
/**
* Marks the probe as scheduled, unless it already was.
* @return true if the caller should schedule a probe
*/
public synchronized boolean startProbe() {
if (probeScheduled) return false;
probeScheduled = true;
return true;
}
public synchronized boolean isProbeScheduled() {
return probeScheduled;
}
/**
* @return the fingerprint of the page returned with a success status for a nonexistent
* URL, or null if there is none
*/
public synchronized Long getFingerprint() {
return fingerprint;
}
/**
* Records the fingerprint of the page returned with a success status for a nonexistent
* URL, unless one was already recorded.
* @return true if the fingerprint was recorded
*/
public synchronized boolean setFingerprintIfAbsent(long fingerprint) {
if (this.fingerprint != null) return false;
this.fingerprint = fingerprint;
return true;
}
}
}
@@ -40,18 +40,18 @@ public class CrawlServerTest {
}
@Test
public void testKryoRoundTripKeepsSoft404Fingerprint() {
public void testKryoRoundTripKeepsSoft404State() {
CrawlServer server = new CrawlServer("example.com:443");
server.startSoft404Probe();
server.setSoft404FingerprintIfAbsent(42);
server.getSoft404State().startProbe();
server.getSoft404State().setFingerprintIfAbsent(42);
KryoBinding<CrawlServer> binding = new KryoBinding<>(CrawlServer.class);
DatabaseEntry entry = new DatabaseEntry();
binding.objectToEntry(server, entry);
CrawlServer restored = binding.entryToObject(entry);
assertTrue(restored.isSoft404ProbeScheduled());
assertEquals(42L, restored.getSoft404Fingerprint());
assertTrue(restored.getSoft404State().isProbeScheduled());
assertEquals(42L, restored.getSoft404State().getFingerprint());
}
@Test
@@ -67,8 +67,8 @@ class Soft404DetectorTest {
assertEquals("https://example.com" + detector.getProbePath(), probe.getURI());
assertEquals("I", probe.getPathFromSeed());
assertEquals(SchedulingConstants.HIGH, probe.getSchedulingDirective());
assertTrue(server().isSoft404ProbeScheduled());
assertNull(server().getSoft404Fingerprint());
assertTrue(state().isProbeScheduled());
assertNull(state().getFingerprint());
CrawlURI page = curi("https://example.com/page", 200);
detector.process(page);
@@ -82,7 +82,7 @@ class Soft404DetectorTest {
detector.process(robots);
assertTrue(robots.getOutLinks().isEmpty());
assertFalse(server().isSoft404ProbeScheduled());
assertFalse(state().isProbeScheduled());
}
@Test
@@ -92,7 +92,7 @@ class Soft404DetectorTest {
recordResponse(probe, "<h1>Not Found</h1>");
detector.process(probe);
assertNull(server().getSoft404Fingerprint());
assertNull(state().getFingerprint());
assertTrue(probe.getAnnotations().contains(Soft404Detector.PROBE_ANNOTATION));
CrawlURI page = curi("https://example.com/a/b", 200);
@@ -109,7 +109,7 @@ class Soft404DetectorTest {
recordResponse(probe, spaShell(probe.getUURI().getPath(),
"v1.eyJwcm9qZWN0X2lkIjoibG92cF8xcDNh.ULHj8Snk4GlGG5Teb57oUauBA3QoDzudjWFrtVjiBUg"));
assertEquals(ProcessResult.FINISH, detector.process(probe));
assertNotNull(server().getSoft404Fingerprint());
assertNotNull(state().getFingerprint());
CrawlURI page = curi("https://example.com/_ignition/horizon/api/jobs"
+ "/_ignition/salt-edge/get-consent", 200);
@@ -161,13 +161,13 @@ class Soft404DetectorTest {
CrawlURI probe = probe();
recordResponse(probe, "<html><title>Not here</title></html>");
detector.process(probe);
Long fingerprint = server().getSoft404Fingerprint();
Long fingerprint = state().getFingerprint();
CrawlURI redirected = curi("https://example.com" + detector.getProbePath(), 200);
recordResponse(redirected, "<html><title>Something else</title></html>");
detector.process(redirected);
assertEquals(fingerprint, server().getSoft404Fingerprint());
assertEquals(fingerprint, state().getFingerprint());
assertEquals(1, detector.getServersWithSoft404s());
}
@@ -178,7 +178,7 @@ class Soft404DetectorTest {
recordResponse(probe, "{\"error\":\"not found\",\"code\":404}");
assertEquals(ProcessResult.FINISH, detector.process(probe));
assertNull(server().getSoft404Fingerprint());
assertNull(state().getFingerprint());
assertEquals(0, detector.getServersWithSoft404s());
}
@@ -189,7 +189,7 @@ class Soft404DetectorTest {
assertEquals(ProcessResult.PROCEED, detector.process(robots));
assertTrue(robots.getOutLinks().isEmpty());
assertFalse(server().isSoft404ProbeScheduled());
assertFalse(state().isProbeScheduled());
}
@ParameterizedTest
@@ -204,7 +204,7 @@ class Soft404DetectorTest {
detector.process(page);
assertEquals(1, page.getOutLinks().size());
assertTrue(server().isSoft404ProbeScheduled());
assertTrue(state().isProbeScheduled());
}
@Test
@@ -214,8 +214,8 @@ class Soft404DetectorTest {
detector.process(redirected);
CrawlServer www = serverCache.getServerFor("www.example.com:443");
assertNotNull(www.getSoft404Fingerprint());
assertTrue(www.isSoft404ProbeScheduled());
assertNotNull(www.getSoft404State().getFingerprint());
assertTrue(www.getSoft404State().isProbeScheduled());
}
@Test
@@ -296,13 +296,13 @@ class Soft404DetectorTest {
"https://example.com/kr-ko" + probe.getUURI().getPath() + "/");
// must proceed so ExtractorHTTP follows the redirect
assertEquals(ProcessResult.PROCEED, detector.process(probe));
assertNull(server().getSoft404Fingerprint());
assertNull(state().getFingerprint());
String body = "<html><title>Home</title><a href=\"premium/\">Premium</a></html>";
CrawlURI target = curi("https://example.com/kr-ko" + probe.getUURI().getPath() + "/", 200);
recordResponse(target, body);
assertEquals(ProcessResult.FINISH, detector.process(target));
assertNotNull(server().getSoft404Fingerprint());
assertNotNull(state().getFingerprint());
assertTrue(target.getAnnotations().contains(Soft404Detector.PROBE_ANNOTATION));
CrawlURI page = curi("https://example.com/uk/icantypeanythinghere/", 200);
@@ -386,8 +386,8 @@ class Soft404DetectorTest {
assertEquals(detector.getProbePath(), restored.getProbePath());
}
private CrawlServer server() {
return serverCache.getServerFor("example.com:443");
private Soft404Detector.State state() {
return serverCache.getServerFor("example.com:443").getSoft404State();
}
private CrawlURI probe() throws Exception {