diff --git a/src/main/java/com/cloudius/urchin/api/APIClient.java b/src/main/java/com/cloudius/urchin/api/APIClient.java index 1cab7d3..e2d6ecd 100644 --- a/src/main/java/com/cloudius/urchin/api/APIClient.java +++ b/src/main/java/com/cloudius/urchin/api/APIClient.java @@ -22,6 +22,7 @@ import javax.json.JsonReaderFactory; import javax.json.JsonString; import javax.management.openmbean.TabularData; import javax.management.openmbean.TabularDataSupport; +import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.UriBuilder; @@ -33,9 +34,29 @@ import com.sun.jersey.api.client.config.ClientConfig; import com.sun.jersey.api.client.config.DefaultClientConfig; import com.yammer.metrics.core.HistogramValues; -import javax.ws.rs.core.MediaType; - public class APIClient { + Map cache = new HashMap(); + String getCacheKey(String key, MultivaluedMap param, long duration) { + if (duration <= 0) { + return null; + } + if (param != null) { + StringBuilder sb = new StringBuilder(key); + sb.append("?"); + for (String k : param.keySet()) { + sb.append(k).append('=').append(param.get(k)).append('&'); + } + return sb.toString(); + } + return key; + } + String getFromCache(String key, long duration) { + if (key == null) { + return null; + } + CacheEntry value = cache.get(key); + return (value!= null && value.valid(duration))? value.value : null; + } JsonReaderFactory factory = Json.createReaderFactory(null); private static final java.util.logging.Logger logger = java.util.logging.Logger .getLogger(APIClient.class.getName()); @@ -105,15 +126,34 @@ public class APIClient { } public String getRawValue(String string, - MultivaluedMap queryParams) { - if (!string.equals("")) { - return get(string, queryParams).get(String.class); + MultivaluedMap queryParams, long duration) { + if (string.equals("")) { + return ""; } - return ""; + String key = getCacheKey(string, queryParams, duration); + String res = getFromCache(key, duration); + if (res != null) { + return res; + } + + res = get(string, queryParams).get(String.class); + if (duration > 0) { + cache.put(key, new CacheEntry(res)); + } + return res; + } + + public String getRawValue(String string, + MultivaluedMap queryParams) { + return getRawValue(string, queryParams, 0); + } + + public String getRawValue(String string, long duration) { + return getRawValue(string, null, duration); } public String getRawValue(String string) { - return getRawValue(string, null); + return getRawValue(string, null, 0); } public String getStringValue(String string, MultivaluedMap queryParams) { @@ -121,6 +161,11 @@ public class APIClient { queryParams).replaceAll("^\"|\"$", ""); } + public String getStringValue(String string, MultivaluedMap queryParams, long duration) { + return getRawValue(string, + queryParams, duration).replaceAll("^\"|\"$", ""); + } + public String getStringValue(String string) { return getStringValue(string, null); } diff --git a/src/main/java/com/cloudius/urchin/api/CacheEntry.java b/src/main/java/com/cloudius/urchin/api/CacheEntry.java new file mode 100644 index 0000000..e89b834 --- /dev/null +++ b/src/main/java/com/cloudius/urchin/api/CacheEntry.java @@ -0,0 +1,20 @@ +/* + * Copyright 2015 Cloudius Systems + */ + +package com.cloudius.urchin.api; + +public class CacheEntry { + long time; + public String value; + + CacheEntry(String value) { + time = System.currentTimeMillis(); + this.value = value; + } + + public boolean valid(long duration) { + return (System.currentTimeMillis() - time) < duration; + } + +} diff --git a/src/main/java/org/apache/cassandra/locator/EndpointSnitchInfo.java b/src/main/java/org/apache/cassandra/locator/EndpointSnitchInfo.java index b34250e..b199c46 100644 --- a/src/main/java/org/apache/cassandra/locator/EndpointSnitchInfo.java +++ b/src/main/java/org/apache/cassandra/locator/EndpointSnitchInfo.java @@ -57,10 +57,11 @@ public class EndpointSnitchInfo implements EndpointSnitchInfoMBean { /** * Provides the Rack name depending on the respective snitch used, given the * host name/ip - * + * * @param host * @throws UnknownHostException */ + @Override public String getRack(String host) throws UnknownHostException { log("getRack(String host) throws UnknownHostException"); MultivaluedMap queryParams = new MultivaluedMapImpl(); @@ -68,16 +69,17 @@ public class EndpointSnitchInfo implements EndpointSnitchInfoMBean { host = InetAddress.getLoopbackAddress().getHostAddress(); } queryParams.add("host", host); - return c.getStringValue("/snitch/rack", queryParams); + return c.getStringValue("/snitch/rack", queryParams, 10000); } /** * Provides the Datacenter name depending on the respective snitch used, * given the hostname/ip - * + * * @param host * @throws UnknownHostException */ + @Override public String getDatacenter(String host) throws UnknownHostException { log(" getDatacenter(String host) throws UnknownHostException"); MultivaluedMap queryParams = new MultivaluedMapImpl(); @@ -85,14 +87,15 @@ public class EndpointSnitchInfo implements EndpointSnitchInfoMBean { host = InetAddress.getLoopbackAddress().getHostAddress(); } queryParams.add("host", host); - return c.getStringValue("/snitch/datacenter", queryParams); + return c.getStringValue("/snitch/datacenter", queryParams, 10000); } /** * Provides the snitch name of the cluster - * + * * @return Snitch name */ + @Override public String getSnitchName() { log(" getSnitchName()"); return c.getStringValue("/snitch/name");