2009-02-19 17:24:46 +01:00
// serverAccessTracker.java
// -------------------------------------
// (C) 2009 by Michael Peter Christen; mc@yacy.net
// first published on http://yacy.net
// Frankfurt, Germany, 20.02.2009
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package de.anomic.server ;
2010-09-17 01:00:07 +02:00
import java.util.Collection ;
2009-02-19 17:24:46 +01:00
import java.util.Iterator ;
import java.util.Map ;
2010-09-20 01:00:24 +02:00
import java.util.NoSuchElementException ;
2009-02-19 17:24:46 +01:00
import java.util.concurrent.ConcurrentHashMap ;
2010-09-17 01:00:07 +02:00
import java.util.concurrent.ConcurrentLinkedQueue ;
2009-02-19 17:24:46 +01:00
public class serverAccessTracker {
2010-01-11 00:09:48 +01:00
private static final long cleanupCycle = 60000 ; // 1 minute
2009-02-19 17:24:46 +01:00
2010-01-11 00:09:48 +01:00
private final long maxTrackingTime ;
private final int maxTrackingCount ;
private final int maxHostCount ;
2010-09-20 01:00:24 +02:00
private final ConcurrentHashMap < String , ConcurrentLinkedQueue < Track > > accessTracker ; // mappings from requesting host to an ArrayList of serverTrack-entries
2009-02-19 17:24:46 +01:00
private long lastCleanup ;
2010-04-14 18:18:04 +02:00
public static class Track {
private long time ;
private String path ;
public Track ( long time , String path ) {
this . time = time ;
this . path = path ;
}
public long getTime ( ) {
return this . time ;
}
public String getPath ( ) {
return this . path ;
}
}
2009-02-19 17:24:46 +01:00
public serverAccessTracker ( long maxTrackingTime , int maxTrackingCount , int maxTrackingHostCount ) {
this . maxTrackingTime = maxTrackingTime ;
this . maxTrackingCount = maxTrackingCount ;
this . maxHostCount = maxTrackingHostCount ;
2010-09-20 01:00:24 +02:00
this . accessTracker = new ConcurrentHashMap < String , ConcurrentLinkedQueue < Track > > ( ) ;
2009-02-19 17:24:46 +01:00
}
/ *
* remove all entries from the access tracker where the age of the last access is greater than the given timeout
* /
private synchronized void cleanupAccessTracker ( ) {
2010-09-20 01:00:24 +02:00
if ( System . currentTimeMillis ( ) - this . lastCleanup < cleanupCycle ) return ; // avoid too many scans of the queues
2010-09-19 22:41:44 +02:00
this . lastCleanup = System . currentTimeMillis ( ) ;
2009-02-19 17:24:46 +01:00
// clear entries which had no entry for the maxTrackingTime time
2010-09-20 01:00:24 +02:00
final Iterator < Map . Entry < String , ConcurrentLinkedQueue < Track > > > i = accessTracker . entrySet ( ) . iterator ( ) ;
ConcurrentLinkedQueue < Track > track ;
2009-02-19 17:24:46 +01:00
while ( i . hasNext ( ) ) {
track = i . next ( ) . getValue ( ) ;
2010-09-20 01:00:24 +02:00
clearTooOldAccess ( track ) ;
if ( track . isEmpty ( ) ) {
2009-02-19 17:24:46 +01:00
// all entries are too old. delete the whole track
i . remove ( ) ;
} else {
// check if the maxTrackingCount is exceeded
2010-09-20 01:00:24 +02:00
while ( track . size ( ) > this . maxTrackingCount ) try {
2009-02-19 17:24:46 +01:00
// delete the oldest entries
2010-09-20 01:00:24 +02:00
track . remove ( ) ;
} catch ( NoSuchElementException e ) { break ; } // concurrency may cause that the track is already empty
2009-02-19 17:24:46 +01:00
}
}
// if there are more entries left than maxTrackingCount, delete some.
while ( accessTracker . size ( ) > this . maxHostCount ) {
// delete just any
2010-09-20 01:00:24 +02:00
String key = accessTracker . keys ( ) . nextElement ( ) ;
if ( key = = null ) break ; // may occur because of concurrency effects
accessTracker . remove ( key ) ;
2009-02-19 17:24:46 +01:00
}
2010-09-20 01:00:24 +02:00
}
2009-02-19 17:24:46 +01:00
2010-09-20 01:00:24 +02:00
/ * *
* compute the number of accesses to a given host in the latest time
* @param host the host that was accessed
* @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 ) ;
long time = System . currentTimeMillis ( ) - delta ;
int c = 0 ;
for ( Track l : timeList ) if ( l . getTime ( ) > time ) c + + ;
return c ;
2009-02-19 17:24:46 +01:00
}
2010-04-14 18:18:04 +02:00
2010-09-20 01:00:24 +02:00
private void clearTooOldAccess ( final ConcurrentLinkedQueue < Track > access ) {
Long time = Long . valueOf ( System . currentTimeMillis ( ) - maxTrackingTime ) ;
Iterator < Track > e = access . iterator ( ) ;
Track l ;
while ( e . hasNext ( ) ) {
l = e . next ( ) ;
if ( l . getTime ( ) < = time ) e . remove ( ) ;
2009-05-26 17:03:50 +02:00
}
2009-02-19 17:24:46 +01:00
}
public void track ( final String host , String accessPath ) {
// check storage size
2010-09-19 22:57:25 +02:00
if ( System . currentTimeMillis ( ) - this . lastCleanup > cleanupCycle ) {
2010-09-17 01:00:07 +02:00
cleanupAccessTracker ( ) ;
2010-09-19 22:57:25 +02:00
}
2009-02-19 17:24:46 +01:00
// learn that a specific host has accessed a specific path
if ( accessPath = = null ) accessPath = " NULL " ;
2010-09-20 01:00:24 +02:00
ConcurrentLinkedQueue < Track > track = accessTracker . get ( host ) ;
if ( track = = null ) {
track = new ConcurrentLinkedQueue < Track > ( ) ;
track . add ( new Track ( System . currentTimeMillis ( ) , accessPath ) ) ;
// add to tracker
accessTracker . put ( host , track ) ;
} else {
track . add ( new Track ( System . currentTimeMillis ( ) , accessPath ) ) ;
clearTooOldAccess ( track ) ;
}
2009-02-19 17:24:46 +01:00
}
2010-09-17 01:00:07 +02:00
public Collection < Track > accessTrack ( final String host ) {
2009-02-19 17:24:46 +01:00
// returns mapping from Long(accesstime) to path
2010-09-20 01:00:24 +02:00
ConcurrentLinkedQueue < Track > access = accessTracker . get ( host ) ;
2009-02-19 17:24:46 +01:00
if ( access = = null ) return null ;
// clear too old entries
2010-09-20 01:00:24 +02:00
clearTooOldAccess ( access ) ;
if ( access . isEmpty ( ) ) {
accessTracker . remove ( host ) ;
2009-02-19 17:24:46 +01:00
}
return access ;
}
public Iterator < String > accessHosts ( ) {
// returns an iterator of hosts in tracker (String)
2010-09-20 01:00:24 +02:00
final Map < String , ConcurrentLinkedQueue < Track > > accessTrackerClone = new ConcurrentHashMap < String , ConcurrentLinkedQueue < Track > > ( ) ;
2009-02-19 17:24:46 +01:00
accessTrackerClone . putAll ( accessTracker ) ;
return accessTrackerClone . keySet ( ) . iterator ( ) ;
}
}