org.infinispan.extendedstats.topK.StreamSummaryContainer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of infinispan-extended-statistics Show documentation
Show all versions of infinispan-extended-statistics Show documentation
Infinispan Extended Statistics module
package org.infinispan.extendedstats.topK;
import java.util.ArrayList;
import java.util.EnumMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicBoolean;
import org.infinispan.Cache;
import org.infinispan.factories.ComponentRegistry;
import org.infinispan.util.logging.Log;
import org.infinispan.util.logging.LogFactory;
import com.clearspring.analytics.stream.Counter;
import com.clearspring.analytics.stream.StreamSummary;
/**
* This contains all the stream lib top keys. Stream lib is a space efficient technique to obtains the top-most
* counters.
*
* @author Pedro Ruivo
* @since 6.0
*/
public class StreamSummaryContainer {
private static final int MAX_CAPACITY = 100000;
private static final Log log = LogFactory.getLog(StreamSummaryContainer.class);
private final String cacheName;
private final String address;
private final AtomicBoolean flushing;
private final EnumMap topKeyWrapper;
private volatile int capacity = 1000;
private volatile boolean enabled = false;
private volatile boolean reset = false;
public StreamSummaryContainer(String cacheName, String address) {
this.cacheName = cacheName;
this.address = address;
flushing = new AtomicBoolean(false);
topKeyWrapper = new EnumMap<>(Stat.class);
for (Stat stat : Stat.values()) {
topKeyWrapper.put(stat, new TopKeyWrapper());
}
resetAll();
}
public static StreamSummaryContainer getOrCreateStreamLibContainer(Cache cache) {
ComponentRegistry componentRegistry = cache.getAdvancedCache().getComponentRegistry();
StreamSummaryContainer streamLibContainer = componentRegistry.getComponent(StreamSummaryContainer.class);
if (streamLibContainer == null) {
String cacheName = cache.getName();
String address = String.valueOf(cache.getCacheManager().getAddress());
componentRegistry.registerComponent(new StreamSummaryContainer(cacheName, address), StreamSummaryContainer.class);
}
return componentRegistry.getComponent(StreamSummaryContainer.class);
}
/**
* @return {@code true} if the top-key collection is enabled, {@code false} otherwise.
*/
public boolean isEnabled() {
return enabled;
}
/**
* Enables or disables the top-key collection
*/
public void setEnabled(boolean enabled) {
if (!this.enabled && enabled) {
resetAll();
} else if (!enabled) {
resetAll();
}
this.enabled = enabled;
}
public int getCapacity() {
return capacity;
}
/**
* Sets the capacity of the top-key. The capacity defines the maximum number of keys that are tracked. Remember that
* top-key is a probabilistic counter so the higher the number of keys, the more precise will be the counters
*/
public void setCapacity(int capacity) {
if (capacity <= 0) {
this.capacity = 1;
} else {
this.capacity = Math.min(capacity, MAX_CAPACITY);
}
}
/**
* Adds the key to the read top-key.
*
* @param remote {@code true} if the key is remote, {@code false} otherwise.
*/
public void addGet(Object key, boolean remote) {
if (!isEnabled()) {
return;
}
syncOffer(remote ? Stat.REMOTE_GET : Stat.LOCAL_GET, key);
}
/**
* Adds the key to the put top-key.
*
* @param remote {@code true} if the key is remote, {@code false} otherwise.
*/
public void addPut(Object key, boolean remote) {
if (!isEnabled()) {
return;
}
syncOffer(remote ? Stat.REMOTE_PUT : Stat.LOCAL_PUT, key);
}
/**
* Adds the lock information about the key, namely if the key suffer some contention and if the keys was locked or
* not.
*
* @param contention {@code true} if the key was contented.
* @param failLock {@code true} if the key was not locked.
*/
public void addLockInformation(Object key, boolean contention, boolean failLock) {
if (!isEnabled()) {
return;
}
syncOffer(Stat.MOST_LOCKED_KEYS, key);
if (contention) {
syncOffer(Stat.MOST_CONTENDED_KEYS, key);
}
if (failLock) {
syncOffer(Stat.MOST_FAILED_KEYS, key);
}
}
/**
* Adds the key to the write skew failed top-key.
*/
public void addWriteSkewFailed(Object key) {
syncOffer(Stat.MOST_WRITE_SKEW_FAILED_KEYS, key);
}
/**
* See {@link #getTopKFrom(StreamSummaryContainer.Stat, int)}.
*
* @return the top-key referring to the stat for all the keys.
*/
public Map