Search in sources :

Example 1 with TimeoutMap

use of com.zimbra.common.util.TimeoutMap in project zm-mailbox by Zimbra.

the class ZimbraLmtpBackend method checkDedupeCacheSize.

/**
     * If the configured Message-ID cache size has changed, create a new cache and copy
     * values from the old one.
     */
private void checkDedupeCacheSize() {
    try {
        Config config = Provisioning.getInstance().getConfig();
        int cacheSize = config.getMessageIdDedupeCacheSize();
        long entryTimeout = config.getMessageIdDedupeCacheTimeout();
        synchronized (ZimbraLmtpBackend.class) {
            Map<String, Set<Integer>> newMap = null;
            if (receivedMessageIDs == null) {
                // if non-zero entry timeout is specified then use a timeout map, else use an lru map
                receivedMessageIDs = entryTimeout == 0 ? new LruMap<String, Set<Integer>>(cacheSize) : new TimeoutMap<String, Set<Integer>>(entryTimeout);
            } else if (receivedMessageIDs instanceof LruMap) {
                if (entryTimeout != 0) {
                    // change to a timeout map
                    newMap = MapUtil.newTimeoutMap(entryTimeout);
                } else if (((LruMap) receivedMessageIDs).getMaxSize() != cacheSize) {
                    // adjust lru map size
                    newMap = MapUtil.newLruMap(cacheSize);
                }
            } else if (receivedMessageIDs instanceof TimeoutMap) {
                if (entryTimeout == 0) {
                    // change to a lru map
                    newMap = MapUtil.newLruMap(cacheSize);
                } else {
                    ((TimeoutMap) receivedMessageIDs).setTimeout(entryTimeout);
                }
            }
            if (newMap != null) {
                // Copy entries from the old map to the new one.  The old map
                // is iterated in order from least-recently accessed to last accessed.
                // If the new map size is smaller, we'll get the latest entries.
                newMap.putAll(receivedMessageIDs);
                receivedMessageIDs = newMap;
            }
        }
    } catch (ServiceException e) {
        ZimbraLog.lmtp.warn("Unable to update dedupe cache size.", e);
        // create an empty lru map if it doesn't exist already.
        synchronized (ZimbraLmtpBackend.class) {
            if (receivedMessageIDs == null) {
                receivedMessageIDs = new LruMap<String, Set<Integer>>(0);
            }
        }
    }
}
Also used : LruMap(com.zimbra.common.util.LruMap) Set(java.util.Set) HashSet(java.util.HashSet) ServiceException(com.zimbra.common.service.ServiceException) DeliveryServiceException(com.zimbra.common.service.DeliveryServiceException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) Config(com.zimbra.cs.account.Config) DebugConfig(com.zimbra.common.localconfig.DebugConfig) TimeoutMap(com.zimbra.common.util.TimeoutMap)

Aggregations

DebugConfig (com.zimbra.common.localconfig.DebugConfig)1 DeliveryServiceException (com.zimbra.common.service.DeliveryServiceException)1 ServiceException (com.zimbra.common.service.ServiceException)1 LruMap (com.zimbra.common.util.LruMap)1 TimeoutMap (com.zimbra.common.util.TimeoutMap)1 Config (com.zimbra.cs.account.Config)1 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1