Issue #1190: Cache hostnames for WsId to speed up logging.

Change-Id: I3e8e5ad046853d8543eba861c4a7dff36dd0281d

Former-commit-id: 1b9000ca72 [formerly c0641faefe] [formerly 3ada98ba44 [formerly e036304b9ba7f93c6e836529f60a63c9b4c1c353]]
Former-commit-id: 3ada98ba44
Former-commit-id: 6a0eac44b2
This commit is contained in:
David Gillingham 2012-09-19 10:35:13 -05:00
parent 2939da32d7
commit cb9434d637

View file

@ -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.
*
* </pre>
*
@ -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<K, V> extends LinkedHashMap<K, V> {
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<K, V> eldest) {
return size() > maxSize;
}
}
private static final long serialVersionUID = 1L;
private static final Map<InetAddress, String> hostNameCache = Collections
.synchronizedMap(new BoundedMap<InetAddress, String>(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
*/