logging (reduced temporary object creation by _log.shouldLog)

This commit is contained in:
jrandom
2004-04-27 08:26:23 +00:00
committed by zzz
parent 5bf1658d9a
commit d0f6d47b14
2 changed files with 44 additions and 26 deletions

View File

@ -36,8 +36,7 @@ import net.i2p.I2PAppContext;
* *
*/ */
public class PersistentSessionKeyManager extends TransientSessionKeyManager { public class PersistentSessionKeyManager extends TransientSessionKeyManager {
private final static Log _log = new Log(PersistentSessionKeyManager.class); private Log _log;
private Object _yk = YKGenerator.class; private Object _yk = YKGenerator.class;
@ -49,9 +48,10 @@ public class PersistentSessionKeyManager extends TransientSessionKeyManager {
*/ */
public PersistentSessionKeyManager(I2PAppContext context) { public PersistentSessionKeyManager(I2PAppContext context) {
super(context); super(context);
_log = context.logManager().getLog(PersistentSessionKeyManager.class);
} }
private PersistentSessionKeyManager() { private PersistentSessionKeyManager() {
super(null); this(null);
} }
/** /**
* Write the session key data to the given stream * Write the session key data to the given stream
@ -60,8 +60,9 @@ public class PersistentSessionKeyManager extends TransientSessionKeyManager {
public void saveState(OutputStream out) throws IOException, DataFormatException { public void saveState(OutputStream out) throws IOException, DataFormatException {
Set tagSets = getInboundTagSets(); Set tagSets = getInboundTagSets();
Set sessions = getOutboundSessions(); Set sessions = getOutboundSessions();
_log.info("Saving state with " + tagSets.size() + " inbound tagSets and " + sessions.size() if (_log.shouldLog(Log.INFO))
+ " outbound sessions"); _log.info("Saving state with " + tagSets.size() + " inbound tagSets and "
+ sessions.size() + " outbound sessions");
DataHelper.writeLong(out, 4, tagSets.size()); DataHelper.writeLong(out, 4, tagSets.size());
for (Iterator iter = tagSets.iterator(); iter.hasNext();) { for (Iterator iter = tagSets.iterator(); iter.hasNext();) {
@ -93,8 +94,9 @@ public class PersistentSessionKeyManager extends TransientSessionKeyManager {
sessions.add(sess); sessions.add(sess);
} }
_log.info("Loading state with " + tagSets.size() + " inbound tagSets and " + sessions.size() if (_log.shouldLog(Log.INFO))
+ " outbound sessions"); _log.info("Loading state with " + tagSets.size() + " inbound tagSets and "
+ sessions.size() + " outbound sessions");
setData(tagSets, sessions); setData(tagSets, sessions);
} }
@ -161,6 +163,7 @@ public class PersistentSessionKeyManager extends TransientSessionKeyManager {
public static void main(String args[]) { public static void main(String args[]) {
I2PAppContext ctx = new I2PAppContext(); I2PAppContext ctx = new I2PAppContext();
Log log = ctx.logManager().getLog(PersistentSessionKeyManager.class);
PersistentSessionKeyManager mgr = (PersistentSessionKeyManager)ctx.sessionKeyManager(); PersistentSessionKeyManager mgr = (PersistentSessionKeyManager)ctx.sessionKeyManager();
try { try {
mgr.loadState(new FileInputStream("sessionKeys.dat")); mgr.loadState(new FileInputStream("sessionKeys.dat"));
@ -169,13 +172,13 @@ public class PersistentSessionKeyManager extends TransientSessionKeyManager {
fos.write(state.getBytes()); fos.write(state.getBytes());
fos.close(); fos.close();
int expired = mgr.aggressiveExpire(); int expired = mgr.aggressiveExpire();
_log.error("Expired: " + expired); log.error("Expired: " + expired);
String stateAfter = mgr.renderStatusHTML(); String stateAfter = mgr.renderStatusHTML();
FileOutputStream fos2 = new FileOutputStream("sessionKeysAfterExpire.html"); FileOutputStream fos2 = new FileOutputStream("sessionKeysAfterExpire.html");
fos2.write(stateAfter.getBytes()); fos2.write(stateAfter.getBytes());
fos2.close(); fos2.close();
} catch (Throwable t) { } catch (Throwable t) {
_log.error("Error loading/storing sessionKeys", t); log.error("Error loading/storing sessionKeys", t);
} }
try { try {
Thread.sleep(3000); Thread.sleep(3000);

View File

@ -33,7 +33,7 @@ import net.i2p.util.Log;
* *
*/ */
class TransientSessionKeyManager extends SessionKeyManager { class TransientSessionKeyManager extends SessionKeyManager {
private final static Log _log = new Log(TransientSessionKeyManager.class); private Log _log;
private Map _outboundSessions; // PublicKey --> OutboundSession private Map _outboundSessions; // PublicKey --> OutboundSession
private Map _inboundTagSets; // SessionTag --> TagSet private Map _inboundTagSets; // SessionTag --> TagSet
@ -61,10 +61,11 @@ class TransientSessionKeyManager extends SessionKeyManager {
*/ */
public TransientSessionKeyManager(I2PAppContext context) { public TransientSessionKeyManager(I2PAppContext context) {
super(context); super(context);
_log = context.logManager().getLog(TransientSessionKeyManager.class);
_outboundSessions = new HashMap(64); _outboundSessions = new HashMap(64);
_inboundTagSets = new HashMap(1024); _inboundTagSets = new HashMap(1024);
} }
private TransientSessionKeyManager() { super(null); } private TransientSessionKeyManager() { this(null); }
/** TagSet */ /** TagSet */
protected Set getInboundTagSets() { protected Set getInboundTagSets() {
@ -81,8 +82,9 @@ class TransientSessionKeyManager extends SessionKeyManager {
} }
protected void setData(Set inboundTagSets, Set outboundSessions) { protected void setData(Set inboundTagSets, Set outboundSessions) {
_log.info("Loading " + inboundTagSets.size() + " inbound tag sets, and " + outboundSessions.size() if (_log.shouldLog(Log.INFO))
+ " outbound sessions"); _log.info("Loading " + inboundTagSets.size() + " inbound tag sets, and "
+ outboundSessions.size() + " outbound sessions");
Map tagSets = new HashMap(inboundTagSets.size()); Map tagSets = new HashMap(inboundTagSets.size());
for (Iterator iter = inboundTagSets.iterator(); iter.hasNext();) { for (Iterator iter = inboundTagSets.iterator(); iter.hasNext();) {
TagSet ts = (TagSet) iter.next(); TagSet ts = (TagSet) iter.next();
@ -116,7 +118,9 @@ class TransientSessionKeyManager extends SessionKeyManager {
if (sess == null) return null; if (sess == null) return null;
long now = Clock.getInstance().now(); long now = Clock.getInstance().now();
if (sess.getEstablishedDate() < now - SESSION_LIFETIME_MAX_MS) { if (sess.getEstablishedDate() < now - SESSION_LIFETIME_MAX_MS) {
_log.info("Expiring old session key established on " + new Date(sess.getEstablishedDate()) if (_log.shouldLog(Log.INFO))
_log.info("Expiring old session key established on "
+ new Date(sess.getEstablishedDate())
+ " with target " + target); + " with target " + target);
return null; return null;
} else { } else {
@ -145,14 +149,17 @@ class TransientSessionKeyManager extends SessionKeyManager {
public SessionTag consumeNextAvailableTag(PublicKey target, SessionKey key) { public SessionTag consumeNextAvailableTag(PublicKey target, SessionKey key) {
OutboundSession sess = getSession(target); OutboundSession sess = getSession(target);
if (sess == null) { if (sess == null) {
if (_log.shouldLog(Log.DEBUG))
_log.debug("No session for " + target); _log.debug("No session for " + target);
return null; return null;
} }
if (sess.getCurrentKey().equals(key)) { if (sess.getCurrentKey().equals(key)) {
SessionTag nxt = sess.consumeNext(); SessionTag nxt = sess.consumeNext();
if (_log.shouldLog(Log.DEBUG))
_log.debug("Tag consumed: " + nxt); _log.debug("Tag consumed: " + nxt);
return nxt; return nxt;
} else { } else {
if (_log.shouldLog(Log.DEBUG))
_log.debug("Key does not match existing key, no tag"); _log.debug("Key does not match existing key, no tag");
return null; return null;
} }
@ -202,6 +209,7 @@ class TransientSessionKeyManager extends SessionKeyManager {
sess.setCurrentKey(key); sess.setCurrentKey(key);
TagSet set = new TagSet(sessionTags, key); TagSet set = new TagSet(sessionTags, key);
sess.addTags(set); sess.addTags(set);
if (_log.shouldLog(Log.DEBUG))
_log.debug("Tags delivered to set " + set + " on session " + sess); _log.debug("Tags delivered to set " + set + " on session " + sess);
} }
@ -223,6 +231,7 @@ class TransientSessionKeyManager extends SessionKeyManager {
TagSet tagSet = new TagSet(sessionTags, key); TagSet tagSet = new TagSet(sessionTags, key);
for (Iterator iter = sessionTags.iterator(); iter.hasNext();) { for (Iterator iter = sessionTags.iterator(); iter.hasNext();) {
SessionTag tag = (SessionTag) iter.next(); SessionTag tag = (SessionTag) iter.next();
if (_log.shouldLog(Log.DEBUG))
_log.debug("Receiving tag " + tag + " for key " + key); _log.debug("Receiving tag " + tag + " for key " + key);
synchronized (_inboundTagSets) { synchronized (_inboundTagSets) {
_inboundTagSets.put(tag, tagSet); _inboundTagSets.put(tag, tagSet);
@ -232,6 +241,7 @@ class TransientSessionKeyManager extends SessionKeyManager {
// todo: make this limit the tags by sessionKey and actually enforce the limit! // todo: make this limit the tags by sessionKey and actually enforce the limit!
int overage = _inboundTagSets.size() - MAX_INBOUND_SESSION_TAGS; int overage = _inboundTagSets.size() - MAX_INBOUND_SESSION_TAGS;
if (overage > 0) { if (overage > 0) {
if (_log.shouldLog(Log.ERROR))
_log.error("TOO MANY SESSION TAGS! " + (_inboundTagSets.size())); _log.error("TOO MANY SESSION TAGS! " + (_inboundTagSets.size()));
} }
} }
@ -250,12 +260,14 @@ class TransientSessionKeyManager extends SessionKeyManager {
synchronized (_inboundTagSets) { synchronized (_inboundTagSets) {
TagSet tagSet = (TagSet) _inboundTagSets.remove(tag); TagSet tagSet = (TagSet) _inboundTagSets.remove(tag);
if (tagSet == null) { if (tagSet == null) {
if (_log.shouldLog(Log.DEBUG))
_log.debug("Cannot consume tag " + tag + " as it is not known"); _log.debug("Cannot consume tag " + tag + " as it is not known");
return null; return null;
} else { } else {
tagSet.consume(tag); tagSet.consume(tag);
} }
SessionKey key = tagSet.getAssociatedKey(); SessionKey key = tagSet.getAssociatedKey();
if (_log.shouldLog(Log.DEBUG))
_log.debug("Consuming tag " + tag + " for sessionKey " + key); _log.debug("Consuming tag " + tag + " for sessionKey " + key);
return key; return key;
} }
@ -370,7 +382,7 @@ class TransientSessionKeyManager extends SessionKeyManager {
return buf.toString(); return buf.toString();
} }
static class OutboundSession { class OutboundSession {
private PublicKey _target; private PublicKey _target;
private SessionKey _currentKey; private SessionKey _currentKey;
private long _established; private long _established;
@ -414,7 +426,9 @@ class TransientSessionKeyManager extends SessionKeyManager {
TagSet set = (TagSet) sets.get(i); TagSet set = (TagSet) sets.get(i);
dropped += set.getTags().size(); dropped += set.getTags().size();
} }
_log.info("Rekeyed from " + _currentKey + " to " + key + ": dropping " + dropped + " session tags"); if (_log.shouldLog(Log.INFO))
_log.info("Rekeyed from " + _currentKey + " to " + key
+ ": dropping " + dropped + " session tags");
} }
} }
_currentKey = key; _currentKey = key;
@ -456,6 +470,7 @@ class TransientSessionKeyManager extends SessionKeyManager {
SessionTag tag = set.consumeNext(); SessionTag tag = set.consumeNext();
if (tag != null) return tag; if (tag != null) return tag;
} else { } else {
if (_log.shouldLog(Log.INFO))
_log.info("TagSet from " + new Date(set.getDate()) + " expired"); _log.info("TagSet from " + new Date(set.getDate()) + " expired");
} }
_tagSets.remove(0); _tagSets.remove(0);