Search in sources :

Example 1 with MessageCache

use of co.cask.cdap.messaging.cache.MessageCache in project cdap by caskdata.

the class DefaultMessageTableCacheProvider method getMessageCache.

@Nullable
@Override
public MessageCache<MessageTable.Entry> getMessageCache(TopicId topicId) {
    if (!initialized) {
        synchronized (this) {
            if (!initialized) {
                Map<TopicId, MessageCache<MessageTable.Entry>> caches = new HashMap<>();
                long cacheSize = cConf.getInt(Constants.MessagingSystem.CACHE_SIZE_MB) * 1024 * 1024;
                Set<TopicId> systemTopics = MessagingServiceUtils.getSystemTopics(cConf, true);
                if (cacheSize > 0 && !systemTopics.isEmpty()) {
                    MessageTableEntryWeigher weigher = new MessageTableEntryWeigher();
                    MessageTableEntryComparator comparator = new MessageTableEntryComparator();
                    // Just evenly distributed the cache among all system topics.
                    // More sophisticated logic can be employed at runtime to monitor the metrics from MessageCache
                    // for each topic and adjust the soft/hard limit accordingly to maximize efficiency in
                    // memory usage and performance
                    long hardLimit = cacheSize / systemTopics.size();
                    if (hardLimit > 0) {
                        // Have reduce trigger as 70% of the hard limit and min retain as 50% of the hard limit
                        // In future, it can be adjusted dynamically based on metrics
                        MessageCache.Limits limits = new MessageCache.Limits(hardLimit / 2, hardLimit * 7 / 10, hardLimit);
                        for (TopicId topic : systemTopics) {
                            caches.put(topic, new MessageCache<>(comparator, weigher, limits, createMetricsContext(cConf, topic, metricsCollectionService)));
                        }
                    }
                }
                topicMessageCaches = caches;
                initialized = true;
            }
        }
    }
    return topicMessageCaches.get(topicId);
}
Also used : MessageCache(co.cask.cdap.messaging.cache.MessageCache) HashMap(java.util.HashMap) MessageTable(co.cask.cdap.messaging.store.MessageTable) TopicId(co.cask.cdap.proto.id.TopicId) Nullable(javax.annotation.Nullable)

Aggregations

MessageCache (co.cask.cdap.messaging.cache.MessageCache)1 MessageTable (co.cask.cdap.messaging.store.MessageTable)1 TopicId (co.cask.cdap.proto.id.TopicId)1 HashMap (java.util.HashMap)1 Nullable (javax.annotation.Nullable)1