- more logging for a shutdown: print reason and accessing IP into log git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@6520 6c8d7289-2bf4-0310-a012-ef5d649a1542pull/1/head
parent
5f0f6b71b4
commit
8281e29963
@ -0,0 +1,123 @@
|
||||
// EventTracker.java
|
||||
// (C) 2007 by Michael Peter Christen; mc@yacy.net, Frankfurt a. M., Germany
|
||||
// first published 17.11.2007 on http://yacy.net
|
||||
//
|
||||
// This is a part of YaCy, a peer-to-peer based web search engine
|
||||
//
|
||||
// $LastChangedDate: 2009-11-05 21:28:37 +0100 (Do, 05 Nov 2009) $
|
||||
// $LastChangedRevision: 6458 $
|
||||
// $LastChangedBy: orbiter $
|
||||
//
|
||||
// LICENSE
|
||||
//
|
||||
// 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 net.yacy.kelondro.util;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Queue;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
|
||||
public class EventTracker {
|
||||
|
||||
private final static Map<String, Queue<Event>> historyMaps = new ConcurrentHashMap<String, Queue<Event>>();
|
||||
private final static Map<String, Long> eventAccess = new ConcurrentHashMap<String, Long>(); // value: last time when this was accessed
|
||||
|
||||
public final static void update(
|
||||
final String eventName,
|
||||
final Object eventPayload,
|
||||
boolean useProtection,
|
||||
int maxQueueSize,
|
||||
long maxQueueAge) {
|
||||
// check protection against too heavy access
|
||||
if (useProtection) {
|
||||
Long lastAcc = eventAccess.get(eventName);
|
||||
if (lastAcc == null) {
|
||||
eventAccess.put(eventName, Long.valueOf(System.currentTimeMillis()));
|
||||
} else {
|
||||
long time = System.currentTimeMillis();
|
||||
if (time - lastAcc.longValue() < 1000) {
|
||||
return; // protect against too heavy load
|
||||
}
|
||||
eventAccess.put(eventName, Long.valueOf(time));
|
||||
}
|
||||
}
|
||||
|
||||
// get event history container
|
||||
Queue<Event> history = historyMaps.get(eventName);
|
||||
|
||||
// create history
|
||||
if (history == null) {
|
||||
history = new ConcurrentLinkedQueue<Event>();
|
||||
|
||||
// update entry
|
||||
history.offer(new Event(eventPayload));
|
||||
|
||||
// store map
|
||||
historyMaps.put(eventName, history);
|
||||
return;
|
||||
}
|
||||
|
||||
// update history
|
||||
synchronized (history) {
|
||||
|
||||
// update entry
|
||||
history.offer(new Event(eventPayload));
|
||||
|
||||
// clean up too old entries
|
||||
int tp = history.size() - maxQueueSize;
|
||||
while (tp-- > 0) history.poll();
|
||||
if (history.size() % 10 == 0) { // reduce number of System.currentTimeMillis() calls
|
||||
Event e;
|
||||
final long now = System.currentTimeMillis();
|
||||
while (history.size() > 0) {
|
||||
e = history.peek();
|
||||
if (now - e.time < maxQueueAge) break;
|
||||
history.poll();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public final static Iterator<Event> getHistory(final String eventName) {
|
||||
Queue<Event> list = historyMaps.get(eventName);
|
||||
if (list == null) return null;
|
||||
return list.iterator();
|
||||
}
|
||||
|
||||
public final static int countEvents(final String eventName, long time) {
|
||||
Iterator<Event> event = getHistory(eventName);
|
||||
if (event == null) return 0;
|
||||
long now = System.currentTimeMillis();
|
||||
int count = 0;
|
||||
while (event.hasNext()) {
|
||||
if (now - event.next().time < time) count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
public final static class Event {
|
||||
public Object payload;
|
||||
public long time;
|
||||
|
||||
public Event(final Object payload) {
|
||||
this.payload = payload;
|
||||
this.time = System.currentTimeMillis();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue