From cb9434d637a4316caf088c5ea49d0463c5a53d2b Mon Sep 17 00:00:00 2001 From: David Gillingham Date: Wed, 19 Sep 2012 10:35:13 -0500 Subject: [PATCH] Issue #1190: Cache hostnames for WsId to speed up logging. Change-Id: I3e8e5ad046853d8543eba861c4a7dff36dd0281d Former-commit-id: 1b9000ca72e73590cd008e366aa0f00cfc56d544 [formerly c0641faefe6432aed0a35214f0511cb3180b49bf] [formerly 3ada98ba44cffca898c055b025af88496f7d91b0 [formerly e036304b9ba7f93c6e836529f60a63c9b4c1c353]] Former-commit-id: 3ada98ba44cffca898c055b025af88496f7d91b0 Former-commit-id: 6a0eac44b273f11f38770566928bebd2ad419342 --- .../com/raytheon/uf/common/message/WsId.java | 52 +++++++++++++++++-- 1 file changed, 47 insertions(+), 5 deletions(-) diff --git a/edexOsgi/com.raytheon.uf.common.message/src/com/raytheon/uf/common/message/WsId.java b/edexOsgi/com.raytheon.uf.common.message/src/com/raytheon/uf/common/message/WsId.java index 94087dd4bb..e0893706ed 100644 --- a/edexOsgi/com.raytheon.uf.common.message/src/com/raytheon/uf/common/message/WsId.java +++ b/edexOsgi/com.raytheon.uf.common.message/src/com/raytheon/uf/common/message/WsId.java @@ -23,6 +23,10 @@ import java.io.Serializable; import java.lang.management.ManagementFactory; import java.net.InetAddress; import java.net.UnknownHostException; +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Map.Entry; import com.raytheon.uf.common.message.adapter.WsIdAdapter; import com.raytheon.uf.common.serialization.ISerializableObject; @@ -39,6 +43,8 @@ import com.raytheon.uf.common.serialization.annotations.DynamicSerializeTypeAdap * ------------ ---------- ----------- -------------------------- * Jun 10, 2009 randerso Initial creation * Apr 25, 2012 545 randerso Repurposed the lockKey field as threadId + * Sep 19, 2012 #1190 dgilling Cache host names so toPrettyString() doesn't + * get delayed behind DNS requests. * * * @@ -49,8 +55,33 @@ import com.raytheon.uf.common.serialization.annotations.DynamicSerializeTypeAdap @DynamicSerialize @DynamicSerializeTypeAdapter(factory = WsIdAdapter.class) public class WsId implements Serializable, ISerializableObject { + + private static class BoundedMap extends LinkedHashMap { + private static final long serialVersionUID = 1L; + + private final int maxSize; + + public BoundedMap(int maxSize) { + super(16, 0.75f, true); + this.maxSize = maxSize; + } + + /* + * (non-Javadoc) + * + * @see java.util.LinkedHashMap#removeEldestEntry(java.util.Map.Entry) + */ + @Override + protected boolean removeEldestEntry(Entry eldest) { + return size() > maxSize; + } + } + private static final long serialVersionUID = 1L; + private static final Map hostNameCache = Collections + .synchronizedMap(new BoundedMap(100)); + private final InetAddress networkId; private final String userName; @@ -61,7 +92,7 @@ public class WsId implements Serializable, ISerializableObject { // replaced A1 lockKey with threadId for A2. // lockKey was not used in A2. This allows A2 to retain - // compatibility with A1 for ISC purposes by not chaning + // compatibility with A1 for ISC purposes by not changing // the format of the WsId private final long threadId; @@ -157,7 +188,6 @@ public class WsId implements Serializable, ISerializableObject { } this.networkId = addr; } - } /* @@ -188,14 +218,26 @@ public class WsId implements Serializable, ISerializableObject { * @return WsId as pretty string */ public String toPrettyString() { + String hostname = retrieveFromHostCache(networkId); + StringBuilder o = new StringBuilder(); - o.append(userName).append('@').append(networkId.getHostName()) - .append(':').append(progName).append(':').append(pid) - .append(':').append(threadId); + o.append(userName).append('@').append(hostname).append(':') + .append(progName).append(':').append(pid).append(':') + .append(threadId); return o.toString(); } + private String retrieveFromHostCache(InetAddress address) { + String hostName = hostNameCache.get(address); + if (hostName == null) { + hostName = address.getHostName(); + hostNameCache.put(address, hostName); + } + + return hostName; + } + /** * @return the _networkId */