Example usage for com.google.common.cache RemovalNotification getCause

List of usage examples for com.google.common.cache RemovalNotification getCause

Introduction

In this page you can find the example usage for com.google.common.cache RemovalNotification getCause.

Prototype

public RemovalCause getCause() 

Source Link

Document

Returns the cause for which the entry was removed.

Usage

From source file:rapture.kernel.cache.KernelCaches.java

private static Cache<RaptureURI, Optional<String>> setupObjectStorageCache() {
    return CacheBuilder.newBuilder().expireAfterAccess(1, TimeUnit.MINUTES)
            .removalListener(new RemovalListener<RaptureURI, Optional<String>>() {
                @Override/*from w w  w .  j  a  v a  2s  .  c om*/
                public void onRemoval(RemovalNotification<RaptureURI, Optional<String>> notification) {
                    if (notification.getCause() != RemovalCause.REPLACED) {
                        if (log.isTraceEnabled())
                            log.trace("Removed " + notification.getKey() + " from local cache because "
                                    + notification.getCause());
                    }
                }
            }).build();
}

From source file:com.qubole.rubix.bookkeeper.BookKeeper.java

private static synchronized void initializeCache(final Configuration conf) {
    long avail = 0;
    for (int d = 0; d < CacheConfig.numDisks(conf); d++) {
        avail += new File(CacheConfig.getDirPath(conf, d)).getUsableSpace();
    }/*ww w .  j a v  a  2  s  .c  o  m*/
    avail = avail / 1024 / 1024;
    final long total = avail;
    log.info("total free space " + avail + "MB");
    fileMetadataCache = CacheBuilder.newBuilder().weigher(new Weigher<String, FileMetadata>() {
        @Override
        public int weigh(String key, FileMetadata md) {
            // weights are in MB to avoid overflowing due to large files
            // This is not accurate, we are placing weight as whole filesize
            // Rather it should be dynamic and should be equal to size of file data cached
            // But guava needs weight fixed at init
            // TODO: find a way to set weight accurately and get away from current workaround
            int weight = (int) (md.getOccupiedSize() / 1024 / 1024);
            log.info("weighing key " + key + " as " + weight);
            return weight;
        }
    }).maximumWeight((long) (avail * 1.0 * CacheConfig.getCacheDataFullnessPercentage(conf) / 100.0))
            .expireAfterWrite(CacheConfig.getCacheDataExpirationAfterWrite(conf), TimeUnit.SECONDS)
            .removalListener(new RemovalListener<String, FileMetadata>() {
                public void onRemoval(RemovalNotification<String, FileMetadata> notification) {
                    try {
                        FileMetadata md = notification.getValue();
                        if (notification.getCause() == RemovalCause.EXPIRED) {
                            // This is to workaround the static weighing of Guava Cache, logic goes like this:
                            // We evict aggressively but do not delete backing data unless running out of space
                            // On next get() on cache, fileMetadata.getOccupiedSize will return size occupied on disk
                            md.close();
                            log.info("Evicting " + md.getRemotePath().toString() + " due to "
                                    + notification.getCause());
                            return;
                        }

                        if (notification.getCause() == RemovalCause.SIZE) {
                            // Here also we wont delete unless very close to disk full
                            long free = 0;
                            for (int d = 0; d < CacheConfig.numDisks(conf); d++) {
                                free += new File(CacheConfig.getDirPath(conf, d)).getUsableSpace();
                            }
                            if (free > total * 1.0
                                    * (100.0 - CacheConfig.getCacheDataFullnessPercentage(conf) / 100)) {
                                // still havent utilized the allowed space so do not delete the backing file
                                md.close();
                                log.warn("Evicting " + md.getRemotePath().toString() + " due to "
                                        + notification.getCause());
                                return;
                            }
                        }
                        //if file has been modified in cloud, its entry will be deleted due to "EXPLICIT"
                        log.warn("deleting entry for" + md.getRemotePath().toString() + " due to "
                                + notification.getCause());
                        md.closeAndCleanup();
                    } catch (IOException e) {
                        throw Throwables.propagate(e);
                    }
                }
            }).build();
}

From source file:com.codefollower.lealone.omid.tso.GuavaCache.java

@Override
public void onRemoval(RemovalNotification<Long, Long> notification) {
    if (notification.getCause() == RemovalCause.REPLACED) {
        return;/*from w w w. j  ava2  s  .co  m*/
    }
    //        LOG.warn("Removing " + notification);
    //        new Exception().printStackTrace();
    removed = Math.max(removed, notification.getValue());
}

From source file:org.gradle.cache.internal.LoggingEvictionListener.java

@Override
public void onRemoval(RemovalNotification<Object, Object> notification) {
    if (notification.getCause() == RemovalCause.SIZE) {
        if (evictionCounter % logInterval == 0) {
            logger.info("Cache entries evicted. In-memory cache of {}: Size{{}} MaxSize{{}}, {} {}", cacheId,
                    cache.size(), maxSize, cache.stats(), EVICTION_MITIGATION_MESSAGE);
        }//from  w  ww.java 2s  . c  o m
        evictionCounter++;
    }
}

From source file:org.gradle.api.internal.changedetection.state.LoggingEvictionListener.java

@Override
public void onRemoval(RemovalNotification<Object, Object> notification) {
    if (notification.getCause() == RemovalCause.SIZE) {
        if (evictionCounter % logInterval == 0) {
            logger.log(LogLevel.INFO,//from   w  w  w. j  a v  a  2 s. c om
                    "Cache entries evicted. In-memory cache of {}: Size{{}} MaxSize{{}}, {} {}", cacheId,
                    cache.size(), maxSize, cache.stats(), EVICTION_MITIGATION_MESSAGE);
        }
        evictionCounter++;
    }
}

From source file:org.codice.ddf.graphql.servlet.GraphQLTransformerServlet.java

/**
 * Refreshes the schema periodically once the cache invalidates if a REFRESH_SCHEMA event was
 * added to the cache. This allows multiple threads to ask for a schema refresh while only
 * refreshing the schema once.//w  ww .  jav  a  2 s . c  o m
 *
 * @param notification
 */
private void refreshSchemaOnExpire(RemovalNotification notification) {
    if (notification.getCause() == RemovalCause.EXPIRED) {
        refreshSchema();
    }
}

From source file:ezbake.discovery.stethoscope.server.StethoscopeCacheRemovalListener.java

@Override
public void onRemoval(RemovalNotification<String, StethoscopeCacheEntry> entry) {
    if (entry.getCause() == RemovalCause.REPLACED) {
        // We don't want to do anything here since our key gets a new value
        return;//from   w w  w . jav a2  s  .  c  o  m
    }

    String appName = entry.getValue().getApplicationName();
    String serviceName = entry.getValue().getServiceName();
    if (servicesToIgnore.containsEntry(appName, serviceName)) {
        logger.info("Ignoring: {},{} and NOT removing that from zookeeper", appName, serviceName);
        return;
    }

    String endpoint = entry.getKey();

    // We want to ignore removing ourself for right now
    if (appName.equals(ServiceDiscoveryClient.COMMON_SERVICE_APP_NAME)
            && serviceName.equals(stethoscopeConstants.SERVICE_NAME)) {
        return;
    }

    if (!shouldRemoveEntriesFromZookeeper) {
        logger.info("Would have removed: {} for {} {} from zookeeper!", endpoint, appName, serviceName);
        return;
    }

    try {
        serviceDiscoveryClient.unregisterEndpoint(appName, serviceName, endpoint);
        logger.info("Removed: {} for {} {} from zookeeper!", endpoint, appName, serviceName);
    } catch (Exception e) {
        StringBuilder builder = new StringBuilder("We had an error removing /ezdiscovery/").append(appName)
                .append("/").append("/endpoints/").append(endpoint).append("from zookeeper!");

        logger.error(builder.toString(), e);
    }
}

From source file:ru.asmsoft.p2p.storage.MemoryNodesRepository.java

@Override
public void onRemoval(RemovalNotification<String, Node> removalNotification) {
    if (removalNotification.getCause() == RemovalCause.EXPIRED) {
        logger.info("Nodes: {}, Expire node: {}", nodes.size(), removalNotification);
    }/*from ww  w  .  j a v  a 2 s  . c om*/
}

From source file:org.auraframework.impl.cache.CacheEvictionListenerImpl.java

/**
 * Broken into a separate routine because RemovalNotification is a final class with no constructor.
 *//*from www.ja va 2 s .  com*/
@Override
public void onRemoval(RemovalNotification<K, T> notification) {
    onRemoval(notification.getCause() == RemovalCause.SIZE);
}

From source file:com.oneops.antenna.cache.SinkRemovalListener.java

@Override
public void onRemoval(RemovalNotification<SinkKey, List<BasicSubscriber>> notif) {
    logger.warn("Removing sink subscribers for " + notif.getKey().getNsPath() + " as it's " + notif.getCause());
}