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
2011-08-03 20:47:43 +02: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 ;
2011-08-03 20:47:43 +02:00
2010-04-14 18:18:04 +02:00
public static class Track {
2011-08-03 20:47:43 +02:00
private final long time ;
private final String path ;
public Track ( final long time , final String path ) {
2010-04-14 18:18:04 +02:00
this . time = time ;
this . path = path ;
}
public long getTime ( ) {
return this . time ;
}
public String getPath ( ) {
return this . path ;
}
}
2011-08-03 20:47:43 +02:00
public serverAccessTracker ( final long maxTrackingTime , final int maxTrackingCount , final int maxTrackingHostCount ) {
2009-02-19 17:24:46 +01:00
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
}
2011-08-03 20:47:43 +02:00
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
* /
2011-09-15 01:34:05 +02:00
private void cleanupAccessTracker ( ) {
2009-02-19 17:24:46 +01:00
2011-09-15 01:34:05 +02:00
synchronized ( this ) {
if ( System . currentTimeMillis ( ) - this . lastCleanup < cleanupCycle ) return ; // avoid too many scans of the queues
this . lastCleanup = System . currentTimeMillis ( ) ;
}
2011-08-03 20:47:43 +02:00
2009-02-19 17:24:46 +01:00
// clear entries which had no entry for the maxTrackingTime time
2011-08-03 20:47:43 +02:00
final Iterator < Map . Entry < String , ConcurrentLinkedQueue < Track > > > i = this . accessTracker . entrySet ( ) . iterator ( ) ;
2010-09-20 01:00:24 +02:00
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 ( ) ;
2011-08-03 20:47:43 +02:00
} catch ( final NoSuchElementException e ) { break ; } // concurrency may cause that the track is already empty
2009-02-19 17:24:46 +01:00
}
}
2011-08-03 20:47:43 +02:00
2009-02-19 17:24:46 +01:00
// if there are more entries left than maxTrackingCount, delete some.
2011-08-03 20:47:43 +02:00
while ( this . accessTracker . size ( ) > this . maxHostCount ) {
2009-02-19 17:24:46 +01:00
// delete just any
2011-08-03 20:47:43 +02:00
final String key = this . accessTracker . keys ( ) . nextElement ( ) ;
2010-09-20 01:00:24 +02:00
if ( key = = null ) break ; // may occur because of concurrency effects
2011-08-03 20:47:43 +02:00
this . 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
* /
2011-08-03 20:47:43 +02:00
public int latestAccessCount ( final String host , final long delta ) {
final Collection < Track > timeList = accessTrack ( host ) ;
2010-09-25 23:10:50 +02:00
if ( timeList = = null ) return 0 ;
2011-08-03 20:47:43 +02:00
final long time = System . currentTimeMillis ( ) - delta ;
2010-09-20 01:00:24 +02:00
int c = 0 ;
2011-08-03 20:47:43 +02:00
for ( final Track l : timeList ) if ( l ! = null & & l . getTime ( ) > time ) c + + ;
2010-09-20 01:00:24 +02:00
return c ;
2009-02-19 17:24:46 +01:00
}
2011-08-03 20:47:43 +02:00
2010-09-20 01:00:24 +02:00
private void clearTooOldAccess ( final ConcurrentLinkedQueue < Track > access ) {
2011-08-03 20:47:43 +02:00
final long time = System . currentTimeMillis ( ) - this . maxTrackingTime ;
final Iterator < Track > e = access . iterator ( ) ;
2010-09-20 01:00:24 +02:00
Track l ;
2011-08-03 20:47:43 +02:00
int max = access . size ( ) ; // ensure termination
while ( e . hasNext ( ) & & max - - > 0 ) {
2010-09-20 01:00:24 +02:00
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
}
2011-08-03 20:47:43 +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
}
2011-08-03 20:47:43 +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 " ;
2011-08-03 20:47:43 +02:00
ConcurrentLinkedQueue < Track > track = this . accessTracker . get ( host ) ;
2010-09-20 01:00:24 +02:00
if ( track = = null ) {
track = new ConcurrentLinkedQueue < Track > ( ) ;
track . add ( new Track ( System . currentTimeMillis ( ) , accessPath ) ) ;
// add to tracker
2011-08-03 20:47:43 +02:00
this . accessTracker . put ( host , track ) ;
2010-09-20 01:00:24 +02:00
} else {
track . add ( new Track ( System . currentTimeMillis ( ) , accessPath ) ) ;
clearTooOldAccess ( track ) ;
}
2009-02-19 17:24:46 +01:00
}
2011-08-03 20:47:43 +02: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
2011-08-03 20:47:43 +02:00
final ConcurrentLinkedQueue < Track > access = this . 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 ( ) ) {
2011-08-03 20:47:43 +02:00
this . accessTracker . remove ( host ) ;
2009-02-19 17:24:46 +01:00
}
return access ;
}
2011-08-03 20:47:43 +02:00
2009-02-19 17:24:46 +01:00
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 > > ( ) ;
2011-08-03 20:47:43 +02:00
accessTrackerClone . putAll ( this . accessTracker ) ;
2009-02-19 17:24:46 +01:00
return accessTrackerClone . keySet ( ) . iterator ( ) ;
}
}