Skip to content

Distributed Peer-to-Peer Web Search Engine and Intranet Search Appliance

performance hack and ensuring termination in serverAccessTracker. cause: "Session_:53600#0_POST /yacy/hello.html HTTP/1.1" prio=10 tid=0x2322b000 nid=0x3ba7 runnable [0x03d3e000] java.lang.Thread.State: RUNNABLE at java.lang.Long.valueOf(Long.java:557) at de.anomic.server.serverAccessTracker.clearTooOldAccess(serverAccessTracker.java:113) at de.anomic.server.serverAccessTracker.cleanupAccessTracker(serverAccessTracker.java:75) - locked <0x3bda2ae8> (a de.anomic.server.serverAccessTracker) at de.anomic.server.serverAccessTracker.track(serverAccessTracker.java:125) at de.anomic.server.serverSwitch.track(serverSwitch.java:542) at de.anomic.http.server.HTTPDemon.parseRequestLine(HTTPDemon.java:641) at de.anomic.http.server.HTTPDemon.POST(HTTPDemon.java:491) at sun.reflect.GeneratedMethodAccessor13.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at de.anomic.server.serverCore$Session.listen(serverCore.java:757) at de.anomic.server.serverCore$Session.run(serverCore.java:651)

orbitere3fc1efbef3f
diff --git a/source/de/anomic/server/serverAccessTracker.java b/source/de/anomic/server/serverAccessTracker.java
index a5cd24f80..8c37c3236 100644
--- a/source/de/anomic/server/serverAccessTracker.java
+++ b/source/de/anomic/server/serverAccessTracker.java
@@ -30,17 +30,17 @@ import java.util.concurrent.ConcurrentLinkedQueue;
public class serverAccessTracker {
private static final long cleanupCycle = 60000; // 1 minute
-
+
private final long maxTrackingTime;
private final int maxTrackingCount;
private final int maxHostCount;
private final ConcurrentHashMap<String, ConcurrentLinkedQueue<Track>> accessTracker; // mappings from requesting host to an ArrayList of serverTrack-entries
private long lastCleanup;
-
+
public static class Track {
- private long time;
- private String path;
- public Track(long time, String path) {
+ private final long time;
+ private final String path;
+ public Track(final long time, final String path) {
this.time = time;
this.path = path;
}
@@ -51,14 +51,14 @@ public class serverAccessTracker {
return this.path;
}
}
-
- public serverAccessTracker(long maxTrackingTime, int maxTrackingCount, int maxTrackingHostCount) {
+
+ public serverAccessTracker(final long maxTrackingTime, final int maxTrackingCount, final int maxTrackingHostCount) {
this.maxTrackingTime = maxTrackingTime;
this.maxTrackingCount = maxTrackingCount;
this.maxHostCount = maxTrackingHostCount;
this.accessTracker = new ConcurrentHashMap<String, ConcurrentLinkedQueue<Track>>();
}
-
+
/*
* remove all entries from the access tracker where the age of the last access is greater than the given timeout
*/
@@ -66,9 +66,9 @@ public class serverAccessTracker {
if (System.currentTimeMillis() - this.lastCleanup < cleanupCycle) return; // avoid too many scans of the queues
this.lastCleanup = System.currentTimeMillis();
-
+
// clear entries which had no entry for the maxTrackingTime time
- final Iterator<Map.Entry<String, ConcurrentLinkedQueue<Track>>> i = accessTracker.entrySet().iterator();
+ final Iterator<Map.Entry<String, ConcurrentLinkedQueue<Track>>> i = this.accessTracker.entrySet().iterator();
ConcurrentLinkedQueue<Track> track;
while (i.hasNext()) {
track = i.next().getValue();
@@ -81,16 +81,16 @@ public class serverAccessTracker {
while (track.size() > this.maxTrackingCount) try {
// delete the oldest entries
track.remove();
- } catch (NoSuchElementException e) { break; } // concurrency may cause that the track is already empty
+ } catch (final NoSuchElementException e) { break; } // concurrency may cause that the track is already empty
}
}
-
+
// if there are more entries left than maxTrackingCount, delete some.
- while (accessTracker.size() > this.maxHostCount) {
+ while (this.accessTracker.size() > this.maxHostCount) {
// delete just any
- String key = accessTracker.keys().nextElement();
+ final String key = this.accessTracker.keys().nextElement();
if (key == null) break; // may occur because of concurrency effects
- accessTracker.remove(key);
+ this.accessTracker.remove(key);
}
}
@@ -100,62 +100,63 @@ public class serverAccessTracker {
* @param delta the time delta from now to the past where the access times shall be computed
* @return the number of accesses to the host in the given time span
*/
- public int latestAccessCount(final String host, long delta) {
- Collection<Track> timeList = accessTrack(host);
+ public int latestAccessCount(final String host, final long delta) {
+ final Collection<Track> timeList = accessTrack(host);
if (timeList == null) return 0;
- long time = System.currentTimeMillis() - delta;
+ final long time = System.currentTimeMillis() - delta;
int c = 0;
- for (Track l: timeList) if ( l != null && l.getTime() > time) c++;
+ for (final Track l: timeList) if ( l != null && l.getTime() > time) c++;
return c;
}
-
+
private void clearTooOldAccess(final ConcurrentLinkedQueue<Track> access) {
- Long time = Long.valueOf(System.currentTimeMillis() - maxTrackingTime);
- Iterator<Track> e = access.iterator();
+ final long time = System.currentTimeMillis() - this.maxTrackingTime;
+ final Iterator<Track> e = access.iterator();
Track l;
- while (e.hasNext()) {
+ int max = access.size(); // ensure termination
+ while (e.hasNext() && max-- > 0) {
l = e.next();
if (l.getTime() <= time) e.remove();
}
}
-
+
public void track(final String host, String accessPath) {
// check storage size
if (System.currentTimeMillis() - this.lastCleanup > cleanupCycle) {
cleanupAccessTracker();
}
-
+
// learn that a specific host has accessed a specific path
if (accessPath == null) accessPath="NULL";
- ConcurrentLinkedQueue<Track> track = accessTracker.get(host);
+ ConcurrentLinkedQueue<Track> track = this.accessTracker.get(host);
if (track == null) {
track = new ConcurrentLinkedQueue<Track>();
track.add(new Track(System.currentTimeMillis(), accessPath));
// add to tracker
- accessTracker.put(host, track);
+ this.accessTracker.put(host, track);
} else {
track.add(new Track(System.currentTimeMillis(), accessPath));
clearTooOldAccess(track);
}
}
-
+
public Collection<Track> accessTrack(final String host) {
// returns mapping from Long(accesstime) to path
-
- ConcurrentLinkedQueue<Track> access = accessTracker.get(host);
+
+ final ConcurrentLinkedQueue<Track> access = this.accessTracker.get(host);
if (access == null) return null;
// clear too old entries
clearTooOldAccess(access);
if (access.isEmpty()) {
- accessTracker.remove(host);
+ this.accessTracker.remove(host);
}
return access;
}
-
+
public Iterator<String> accessHosts() {
// returns an iterator of hosts in tracker (String)
final Map<String, ConcurrentLinkedQueue<Track>> accessTrackerClone = new ConcurrentHashMap<String, ConcurrentLinkedQueue<Track>>();
- accessTrackerClone.putAll(accessTracker);
+ accessTrackerClone.putAll(this.accessTracker);
return accessTrackerClone.keySet().iterator();
}
}