Volatile to be used with Concurrent Collection? - java

I am developing a metrics store (Map) , which basically collects metrics about some operations such as
mix
max
counter
timeElapsed[] etc
Here Key is the name of the method and value are metrics about it.
Spring can help me create singleton object of MetricStore, i am using ConcurrentHashMap to avoid race condition when multiple REST request comes in parallel.
My query
1- Do i need to make MetricStore variable store volatile? to improve the visibility among multiple requests. 2- I am using Map as the base class and ConcurrentHashMap as Implemetnation, does it affect as Map is not ThreadSafe.
-
#Component
class MetricStore{
public Map<String, Metric> store = new ConcurrentHashMap<>();
//OR public volatile Map<String, Metric> store = new ConcurrentHashMap<>();
}
#RestController
class MetricController{
#Autowired
private MetricStore metricStore;
#PostMapping(name="put")
public void putData(String key, Metric metricData) {
if(metricStore.store.containsKey(key)) {
// udpate data
}
else {
metricStore.store.put(key, metricData);
}
}
#PostMapping(name="remove")
public void removeData(String key) {
if(metricStore.store.containsKey(key)) {
metricStore.store.remove(key);
}
}
}

Do i need to make MetricStore variable store volatile?
No, because you are not changing the value of store (i.e. store can be marked as final and the code should still compile).
I am using Map as the base class and ConcurrentHashMap as Implemetnation, does it affect as Map is not ThreadSafe
Because you're using a ConcurrentHashMap as the implementation of Map, it is thread-safe. If you want the declared type to be more specific, Map can be changed to ConcurrentMap.
The bigger issue here is that you're using containsKey before calling put and remove when you should be using compute and computeIfPresent, which are atomic operations:
#PostMapping(name="put")
public void putData(String key, Metric metricData) {
metricStore.store.compute(key, (k, v) -> {
if (v == null) {
return metricData;
}
// update data
});
}
#PostMapping(name="remove")
public void removeData(String key) {
metricStore.store.computeIfPresent(key, (k, v) -> null);
}

Related

Java Concurrency in app server

I have a small web application with CRUD operations. For the data, I can't use a db, so I must store it in a map or similar data structure. Based on the requirement, I need two maps to store and access the data, and I have to make sure they are in sync with each other.
Now, the question, what is the preferred way to make crud operations on the two maps on in a multi threaded environment making sure they are in consistent state. Is there a better approach or pattern to avoid locking and synchronization?
Thanks
I believe there has to be some synchronization involved. You can try the following approach.
First of all use a ConcurrentHashMap for both of your maps. Then encapsulate both your maps in an object that will have synchronized methods to ensure your data is in a consistent state. The following is an example:
public class ParentMap {
private ConcurrentHashMap<String, Object> map1 = new ConcurrentHashMap<>();
private ConcurrentHashMap<String, Object> map2 = new ConcurrentHashMap<>();
public synchronized void add(Object objectToAdd) {
// Add to map1
// Add to map2
}
public synchronized void update(String key, Object object) {
// Update the object based on the key on both maps (following the rules of your application)
}
public synchronized void delete(String key) {
// Delete in both maps following the rules of your application
}
public Object get(String key) {
// Use the rules of your application to read
}
}
Ensure that there is only one ParentMap object in your application. You may use the Singleton Pattern for this.

How to populate map of string and another map in a thread safe way?

I am working on measuing my application metrics using below class in which I increment and decrement metrics.
public class AppMetrics {
private final AtomicLongMap<String> metricCounter = AtomicLongMap.create();
private static class Holder {
private static final AppMetrics INSTANCE = new AppMetrics();
}
public static AppMetrics getInstance() {
return Holder.INSTANCE;
}
private AppMetrics() {}
public void increment(String name) {
metricCounter.getAndIncrement(name);
}
public AtomicLongMap<String> getMetricCounter() {
return metricCounter;
}
}
I am calling increment method of AppMetrics class from multithreaded code to increment the metrics by passing the metric name.
Problem Statement:
Now I want to have metricCounter for each clientId which is a String. That means we can also get same clientId multiple times and sometimes it will be a new clientId, so somehow then I need to extract the metricCounter map for that clientId and increment metrics on that particular map (which is what I am not sure how to do that).
What is the right way to do that keeping in mind it has to be thread safe and have to perform atomic operations. I was thinking to make a map like that instead:
private final Map<String, AtomicLongMap<String>> clientIdMetricCounterHolder = Maps.newConcurrentMap();
Is this the right way? If yes then how can I populate this map by passing clientId as it's key and it's value will be the counter map for each metric.
I am on Java 7.
If you use a map then you'll need to synchronize on the creation of new AtomicLongMap instances. I would recommend using a LoadingCache instead. You might not end up using any of the actual "caching" features but the "loading" feature is extremely helpful as it will synchronizing creation of AtomicLongMap instances for you. e.g.:
LoadingCache<String, AtomicLongMap<String>> clientIdMetricCounterCache =
CacheBuilder.newBuilder().build(new CacheLoader<String, AtomicLongMap<String>>() {
#Override
public AtomicLongMap<String> load(String key) throws Exception {
return AtomicLongMap.create();
}
});
Now you can safely start update metric counts for any client without worrying about whether the client is new or not. e.g.
clientIdMetricCounterCache.get(clientId).incrementAndGet(metricName);
A Map<String, Map<String, T>> is just a Map<Pair<String, String>, T> in disguise. Create a MultiKey class:
class MultiKey {
public String clientId;
public String name;
// be sure to add hashCode and equals
}
Then just use an AtomicLongMap<MultiKey>.
Edited:
Provided the set of metrics is well defined, it wouldn't be too hard to use this data structure to view metrics for one client:
Set<String> possibleMetrics = // all the possible values for "name"
Map<String, Long> getMetricsForClient(String client) {
return Maps.asMap(possibleMetrics, m -> metrics.get(new MultiKey(client, m));
}
The returned map will be a live unmodifiable view. It might be a bit more verbose if you're using an older Java version, but it's still possible.

Guava CacheBuilder and store key different from lookup key

Hey All so using google's CacheBuilder / CacheLoader:
CacheBuilder.newBuilder().maximumSize(MAX_SIZE).expireAfterAccess()
.build(new CacheLoader<String, Object>() {
#Override
public Object load(String key) throws Exception {
return Query(key);
}
});
Pretty straight forward. Thing is the key is used in the query for loading when not in cache and can actually be pretty big. I'd like to actually store the hash of the key in the cache and so when a lookup in the cache is actually done it uses the hash of the key but when the load is done the key is actually the full query.
Basically I would want my program to use the cache by sending the query to get function but just before key lookup or cache storage I wonder if there is a hook function I can override to hash the key for final cache storage and lookup?
Thanks in advance
Update:
So I figured out a solution using callable version of get. If I wrap the cache in my own container:
class WrappedCache {
private Cache<String, Object> mInnerCache;
public Object get(String key) {
String hashedKey = Hash(key); // get hashed key
return mInnerCache.get(hashedKey, new Callable<Object>() {
return Query(key);
}
}
}
This way the inner cache only deals with hash values but the callable gets the original query if it needs it to perform it's actions.
If I needed a more complex solution I could have made the WrappedCache implement the Cache<K,V> interface but my usage case is a little simpler and I can get away with above.
I don't believe the Guava Cache implementation supports such a thing, but you could create a decorator to do the translation on put and get prior to storing.
Would something like this work?
public class CacheDecorator<K1, K2, V> {
private final Function<K1, K2> keyConversion;
private final LoadingCache<K2, V> cache;
public CacheDecorator(Function<K1, K2> keyConversion, LoadingCache<K2, V> cache) {
this.keyConversion = keyConversion;
this.cache = cache;
}
public V get(K1 key) throws ExecutionException {
return cache.get(keyConversion.apply(key));
}
public void put(K1 key, V value) {
cache.put(keyConversion.apply(key), value);
}
}

How can I allow concurrent writes on different parts (with different keys) of a guava multimap

I'm using google guava multimap and would like to know if it's possible to allow concurrent writes to different parts of the map or do I have to synchronize all access? When I say different parts, I mean different key-value pairs in the multimap.
Is the following sample code correct?
class Key {}
class Value {}
private Multimap<Key, Value> multimap = HashMultimap.create();
private final Striped<Key> locks = Striped.readWriteLock(100);
public void write(final Key key, final Collection<Value> values) {
Lock writeLock = locks.get(key).writeLock();
writeLock.lock();
try {
for (Value value : values) {
multimap.put(key, value);
}
} finally {
writeLock.unlock();
}
}
public Collection<Value> read(final Key key) {
Lock readLock = locks.get(key).readLock();
readLock.lock();
try {
// Collect values
} finally {
readLock.unlock();
}
}
If you need this you may be better off using a ConcurrentHashMap<K, Set<V>> and manually replicating the multimap semantics.
You might be able to create a MultimapView type that extends Multimap and provides a view into a Map<K, Collection<V>> - I'm not sure if there's a reason Guava doesn't provide that (it might not be efficient?). All I see in Multimaps is copy factories.

Guava cache - How do I loadAll on any miss?

I have a use case where the method to load my cache's data is a bulk call, but I'm never going to use getAll to get data from the cache. Is there a way to have multiple concurrent gets all block on a single loadAll? I don't want individual gets on different keys to result in multiple calls to the data source.
cache.get(key1); // missing entry, starts refresh
cache.get(key2); // separate thread, already getting reloaded due to key1 miss
I think I'll have to implement my own synchronization after looking into the LocalCache, using something like a local cache in my data accessor that only allows a call through every so many units of time. When a call does go through, update the local copy with a single assignment statement.
Am I missing something from Guava's cache library?
Edit:
I'm considering something like the following. However, it could potentially continue returning stale data while loadAll finishes. I'd prefer everything blocks at load, and only the first request causes loadAll to proceed.
public class DataCacheLoader extends CacheLoader<String, Double>
{
private final Cache<String, Double> cache;
private ConcurrentMap<String, Double> currentData;
private final AtomicBoolean isloading;
public DataCacheLoader( final Cache<String, Double> cache )
{
this.cache = cache;
isLoading = new AtomicBoolean( false );
}
#Override
public Double load( final String key ) throws Exception
{
if ( isLoading.compareAndSet( false, true ) )
{
cache.putAll( loadAll( Lists.newArrayList( key ) ) ) );
}
return currentData.get( key );
}
#Override
public Map<String, Double> loadAll(Iterable<? extends String> keys) throws Exception
{
currentData = source.getAllData();
return currentData;
}
}
Here's a solution that should do the trick. The idea is that instead of caching each individual key you cache the whole map with a single fixed key. The one drawback is that you won't be able to expire individual parts of the underlying map (at least not easily), but that may not be a requirement.
class MyCache {
private static final Object KEY = new Object();
private final LoadingCache<Object, Map<String, Double>> delegate =
new CacheBuilder()
// configure cache
.build(new CacheLoader<Object, Map<String, Double>>() {
public Map<String, Double> load(Object key) {
return source.load();
}
};
double get(String key) {
return cache.get(KEY).get(key);
}
}

Categories