Skip to content

Commit

Permalink
Moved distribution summary to EVCacheMetricsFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
smadappa committed Aug 16, 2016
1 parent ae44156 commit c5bcd87
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 32 deletions.
19 changes: 9 additions & 10 deletions evcache-client/src/main/java/com/netflix/evcache/EVCacheImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,7 @@ public <T> EVCacheLatch touch(String key, int timeToLive, Policy policy) throws
final EVCacheLatchImpl latch = new EVCacheLatchImpl(policy == null ? Policy.ALL_MINUS_1 : policy, clients.length, _appName);
touchData(canonicalKey, key, timeToLive, clients, latch);

if (touchTTLSummary == null) this.touchTTLSummary = EVCacheConfig.getInstance().getDistributionSummary(_appName + "-TouchData-TTL");
if (touchTTLSummary == null) this.touchTTLSummary = EVCacheMetricsFactory.getDistributionSummary(_appName + "-TouchData-TTL", _appName, null);
if (touchTTLSummary != null) touchTTLSummary.record(timeToLive);

if (touchCounter == null) this.touchCounter = EVCacheMetricsFactory.getCounter(_appName, _cacheName, _metricPrefix + "TouchCall", DataSourceType.COUNTER);
Expand Down Expand Up @@ -1058,11 +1058,10 @@ public <T> EVCacheLatch set(String key, T value, Transcoder<T> tc, int timeToLiv
cd = client.getTranscoder().encode(value);
}

if (setTTLSummary == null) this.setTTLSummary = EVCacheConfig.getInstance().getDistributionSummary(_appName + "-SetData-TTL");
if (setTTLSummary == null) this.setTTLSummary = EVCacheMetricsFactory.getDistributionSummary(_appName + "-SetData-TTL", _appName, null);
if (setTTLSummary != null) setTTLSummary.record(timeToLive);
if (cd != null) {
if (setDataSizeSummary == null) this.setDataSizeSummary = EVCacheConfig.getInstance()
.getDistributionSummary(_appName + "-SetData-Size");
if (setDataSizeSummary == null) this.setDataSizeSummary = EVCacheMetricsFactory.getDistributionSummary(_appName + "-SetData-Size", _appName, null);
if (setDataSizeSummary != null) this.setDataSizeSummary.record(cd.getData().length);
}
}
Expand Down Expand Up @@ -1142,7 +1141,7 @@ public <T> EVCacheFuture[] append(String key, T value, Transcoder<T> tc, int tim
futures[index++] = new EVCacheFuture(future, key, _appName, client.getServerGroup());

if (cd != null) {
if (appendDataSizeSummary == null) this.appendDataSizeSummary = EVCacheConfig.getInstance().getDistributionSummary(_appName + "-AppendData-Size");
if (appendDataSizeSummary == null) this.appendDataSizeSummary = EVCacheMetricsFactory.getDistributionSummary(_appName + "-AppendData-Size", _appName, null);
if (appendDataSizeSummary != null) this.appendDataSizeSummary.record(cd.getData().length);
}
}
Expand Down Expand Up @@ -1469,10 +1468,10 @@ public <T> EVCacheLatch replace(String key, T value, Transcoder<T> tc, int timeT
cd = client.getTranscoder().encode(value);
}

if (replaceTTLSummary == null) this.replaceTTLSummary = EVCacheConfig.getInstance().getDistributionSummary(_appName + "-ReplaceData-TTL");
if (replaceTTLSummary == null) this.replaceTTLSummary = EVCacheMetricsFactory.getDistributionSummary(_appName + "-ReplaceData-TTL", _appName, null);
if (replaceTTLSummary != null) replaceTTLSummary.record(timeToLive);
if (cd != null) {
if (replaceDataSizeSummary == null) this.replaceDataSizeSummary = EVCacheConfig.getInstance().getDistributionSummary(_appName + "-ReplaceData-Size");
if (replaceDataSizeSummary == null) this.replaceDataSizeSummary = EVCacheMetricsFactory.getDistributionSummary(_appName + "-ReplaceData-Size", _appName, null);
if (replaceDataSizeSummary != null) this.replaceDataSizeSummary.record(cd.getData().length);
}
}
Expand Down Expand Up @@ -1557,7 +1556,7 @@ public <T> EVCacheLatch appendOrAdd(String key, T value, Transcoder<T> tc, int t
}
}
if (cd != null) {
if (appendDataSizeSummary == null) this.appendDataSizeSummary = EVCacheConfig.getInstance().getDistributionSummary(_appName + "-AppendData-Size");
if (appendDataSizeSummary == null) this.appendDataSizeSummary = EVCacheMetricsFactory.getDistributionSummary(_appName + "-AppendData-Size", _appName, null);
if (appendDataSizeSummary != null) this.appendDataSizeSummary.record(cd.getData().length);
}
final Future<Boolean> future = client.appendOrAdd(canonicalKey, cd, timeToLive, latch);
Expand Down Expand Up @@ -1630,7 +1629,7 @@ public <T> Future<Boolean>[] appendOrAdd(String key, T value, Transcoder<T> tc,
futures[index++] = new EVCacheFuture(future, key, _appName, client.getServerGroup(), client);

if (cd != null) {
if (appendDataSizeSummary == null) this.appendDataSizeSummary = EVCacheConfig.getInstance().getDistributionSummary(_appName + "-AppendData-Size");
if (appendDataSizeSummary == null) this.appendDataSizeSummary = EVCacheMetricsFactory.getDistributionSummary(_appName + "-AppendData-Size", _appName, null);
if (appendDataSizeSummary != null) this.appendDataSizeSummary.record(cd.getData().length);
}
}
Expand Down Expand Up @@ -1714,7 +1713,7 @@ public <T> boolean add(String key, T value, Transcoder<T> tc, int timeToLive) th
futures[index++] = new EVCacheFuture(future, key, _appName, client.getServerGroup(), client);

if (cd != null) {
if (addDataSizeSummary == null) this.addDataSizeSummary = EVCacheConfig.getInstance().getDistributionSummary(_appName + "-AddData-Size");
if (addDataSizeSummary == null) this.addDataSizeSummary = EVCacheMetricsFactory.getDistributionSummary(_appName + "-AddData-Size", _appName, null);
if (addDataSizeSummary != null) this.addDataSizeSummary.record(cd.getData().length);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.netflix.servo.monitor.LongGauge;
import com.netflix.servo.monitor.Monitor;
import com.netflix.servo.monitor.MonitorConfig;
import com.netflix.servo.monitor.Monitors;
import com.netflix.servo.monitor.MonitorConfig.Builder;
import com.netflix.servo.monitor.StatsTimer;
import com.netflix.servo.monitor.StepCounter;
Expand All @@ -26,13 +27,18 @@
import com.netflix.servo.tag.BasicTagList;
import com.netflix.servo.tag.Tag;
import com.netflix.servo.tag.TagList;
import com.netflix.spectator.api.DistributionSummary;
import com.netflix.spectator.api.Id;
import com.netflix.spectator.api.Registry;
import com.netflix.spectator.api.Spectator;
import com.netflix.servo.tag.BasicTag;

@edu.umd.cs.findbugs.annotations.SuppressFBWarnings(value = { "NF_LOCAL_FAST_PROPERTY",
"PMB_POSSIBLE_MEMORY_BLOAT" }, justification = "Creates only when needed")
public final class EVCacheMetricsFactory {
private static final Map<String, Stats> statsMap = new ConcurrentHashMap<String, Stats>();
private static final Map<String, Monitor<?>> monitorMap = new ConcurrentHashMap<String, Monitor<?>>();
private static final Map<String, DistributionSummary> distributionSummaryMap = new ConcurrentHashMap<String, DistributionSummary>();
private static final Lock writeLock = (new ReentrantReadWriteLock()).writeLock();
private static final Map<String, Timer> timerMap = new HashMap<String, Timer>();
private static final DynamicIntProperty sampleSize = EVCacheConfig.getInstance().getDynamicIntProperty("EVCache.metrics.sample.size", 100);
Expand Down Expand Up @@ -75,6 +81,11 @@ public static Map<String, Stats> getAllMetrics() {
public static Map<String, Monitor<?>> getAllMonitor() {
return monitorMap;
}

public static Map<String, DistributionSummary> getAllDistributionSummaryMap() {
return distributionSummaryMap;
}


public static LongGauge getLongGauge(String name) {
LongGauge gauge = (LongGauge) monitorMap.get(name);
Expand Down Expand Up @@ -297,8 +308,7 @@ public static MonitorConfig getMonitorConfig(String name, String appName, String
return builder.build();
}

public static MonitorConfig getMonitorConfig(String name, String appName, String cacheName, String serverGroup,
String metric) {
public static MonitorConfig getMonitorConfig(String name, String appName, String cacheName, String serverGroup, String metric) {
Builder builder = MonitorConfig.builder(name).withTag("APP", appName).withTag("METRIC", metric);
if (cacheName != null && cacheName.length() > 0) {
builder = builder.withTag("CACHE", cacheName);
Expand Down Expand Up @@ -331,4 +341,21 @@ public static Timer getStatsTimer(String name) {
}
}

public static DistributionSummary getDistributionSummary(String name, String appName, String serverGroup) {
final String metricName = getMetricName(appName, serverGroup, name);
final DistributionSummary _ds = distributionSummaryMap.get(metricName);
if(_ds != null) return _ds;
final Registry registry = Spectator.globalRegistry(); //_poolManager.getRegistry();
if (registry != null) {
Id id = registry.createId(name);
id = id.withTag("APP", appName);
if(serverGroup != null) id = id.withTag("ServerGroup", serverGroup);
final DistributionSummary ds = registry.distributionSummary(id);
if (!Monitors.isObjectRegistered(ds)) Monitors.registerObject(ds);
distributionSummaryMap.put(metricName, ds);
return ds;
}
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.netflix.discovery.shared.Application;
import com.netflix.evcache.metrics.EVCacheMetricsFactory;
import com.netflix.evcache.util.EVCacheConfig;
import com.netflix.servo.tag.BasicTagList;

public class DiscoveryNodeListProvider implements EVCacheNodeList {
public static final String DEFAULT_PORT = "11211";
Expand Down Expand Up @@ -106,17 +107,17 @@ public Map<ServerGroup, EVCacheServerGroupConfig> discoverInstances() throws IOE
}
final int port = rendPort == 0 ? evcachePort : ((useBatchPort.get().booleanValue()) ? rendBatchPort : rendPort);

final ServerGroup rSet = new ServerGroup(zone, asgName);
final ServerGroup serverGroup = new ServerGroup(zone, asgName);
final Set<InetSocketAddress> instances;
final EVCacheServerGroupConfig config;
if (instancesSpecific.containsKey(rSet)) {
config = instancesSpecific.get(rSet);
if (instancesSpecific.containsKey(serverGroup)) {
config = instancesSpecific.get(serverGroup);
instances = config.getInetSocketAddress();
} else {
instances = new HashSet<InetSocketAddress>();
config = new EVCacheServerGroupConfig(rSet, instances, rendPort, udsproxyMemcachedPort, udsproxyMementoPort);
instancesSpecific.put(rSet, config);
EVCacheMetricsFactory.getLongGauge("EVCacheClient-"+_appName + "-" + asgName +"-port").set(Long.valueOf(port));
config = new EVCacheServerGroupConfig(serverGroup, instances, rendPort, udsproxyMemcachedPort, udsproxyMementoPort);
instancesSpecific.put(serverGroup, config);
EVCacheMetricsFactory.getLongGauge(_appName + "-port", BasicTagList.of("ServerGroup", asgName, "APP", _appName)).set(Long.valueOf(port));
}

/* Don't try to use downed instances */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -701,8 +701,8 @@ private CachedData[] createChunks(CachedData cd, String key) {
srcPos += lengthOfArray;
chunkData[i] = decodingTranscoder.encode(dest);
}
EVCacheConfig.getInstance().getDistributionSummary(appName + "-ChunkData-NumberOfChunks").record(numOfChunks);
EVCacheConfig.getInstance().getDistributionSummary(appName + "-ChunkData-TotalSize").record(len);
EVCacheMetricsFactory.getDistributionSummary(appName + "-ChunkData-NumberOfChunks", appName, serverGroup.getName()).record(numOfChunks);
EVCacheMetricsFactory.getDistributionSummary(appName + "-ChunkData-TotalSize", appName, serverGroup.getName()).record(len);

return chunkData;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,14 @@ public MonitorConfig getMonitorConfig(final String metricName, final Tag tag, fi
return mc;
}

public DistributionSummary getDistributionSummary(String name) {
final Registry registry = Spectator.globalRegistry(); //_poolManager.getRegistry();
if (registry != null) {
final DistributionSummary ds = registry.distributionSummary(name);
if (!Monitors.isObjectRegistered(ds)) Monitors.registerObject(ds);
return ds;
}
return null;
}
// public DistributionSummary getDistributionSummary(String name) {
// final Registry registry = Spectator.globalRegistry(); //_poolManager.getRegistry();
// if (registry != null) {
// final DistributionSummary ds = registry.distributionSummary(name);
// if (!Monitors.isObjectRegistered(ds)) Monitors.registerObject(ds);
// return ds;
// }
// return null;
// }

}
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public void receivedStatus(OperationStatus status) {
public void gotData(String k, int flags, byte[] data) {

if (data != null) {
if(getDataSize == null) getDataSize = EVCacheConfig.getInstance().getDistributionSummary(appName + "-GetData-Size");
if(getDataSize == null) getDataSize = EVCacheMetricsFactory.getDistributionSummary(appName + "-GetData-Size", appName, serverGroup.getName());
if (getDataSize != null) getDataSize.record(data.length);
}
if (!key.equals(k)) log.warn("Wrong key returned. Key - " + key + "; Returned Key " + k);
Expand Down Expand Up @@ -176,7 +176,7 @@ public void receivedStatus(OperationStatus status) {
@Override
public void gotData(String k, int flags, byte[] data) {
if (data != null) {
if(bulkDataSize == null) bulkDataSize = EVCacheConfig.getInstance().getDistributionSummary(appName + "-BulkData-Size");
if(bulkDataSize == null) bulkDataSize = EVCacheMetricsFactory.getDistributionSummary(appName + "-BulkData-Size", appName, serverGroup.getName());
if (bulkDataSize != null) bulkDataSize.record(data.length);
}

Expand Down Expand Up @@ -225,7 +225,7 @@ public void complete() {
public void gotData(String k, int flags, long cas, byte[] data) {
if (!key.equals(k)) log.warn("Wrong key returned. Key - " + key + "; Returned Key " + k);
if (data != null) {
if(getAndTouchDataSize == null) getAndTouchDataSize = EVCacheConfig.getInstance().getDistributionSummary(appName + "-GATData-Size");
if(getAndTouchDataSize == null) getAndTouchDataSize = EVCacheMetricsFactory.getDistributionSummary(appName + "-GATData-Size", appName, serverGroup.getName());
if (getAndTouchDataSize != null) getAndTouchDataSize.record(data.length);
}

Expand Down

0 comments on commit c5bcd87

Please sign in to comment.