Search in sources :

Example 6 with CacheLoader

use of com.google.common.cache.CacheLoader in project opennms by OpenNMS.

the class NodeCacheImpl method init.

public void init() {
    if (cache == null) {
        LOG.info("initializing node data cache (archiveAssetData=" + archiveAssetData + ", TTL=" + MAX_TTL + "m, MAX_SIZE=" + MAX_SIZE + ")");
        CacheBuilder cacheBuilder = CacheBuilder.newBuilder();
        if (MAX_TTL > 0) {
            cacheBuilder.expireAfterWrite(MAX_TTL, TimeUnit.MINUTES);
        }
        if (MAX_SIZE > 0) {
            cacheBuilder.maximumSize(MAX_SIZE);
        }
        cache = cacheBuilder.build(new CacheLoader<Long, Map<String, String>>() {

            @Override
            public Map<String, String> load(Long key) throws Exception {
                return getNodeAndCategoryInfo(key);
            }
        });
    }
}
Also used : CacheBuilder(com.google.common.cache.CacheBuilder) CacheLoader(com.google.common.cache.CacheLoader)

Example 7 with CacheLoader

use of com.google.common.cache.CacheLoader in project gerrit by GerritCodeReview.

the class CacheModule method bindCacheLoader.

<K, V> Provider<CacheLoader<K, V>> bindCacheLoader(CacheProvider<K, V> m, Class<? extends CacheLoader<K, V>> impl) {
    Type type = Types.newParameterizedType(Cache.class, m.keyType().getType(), m.valueType().getType());
    Type loadingType = Types.newParameterizedType(LoadingCache.class, m.keyType().getType(), m.valueType().getType());
    Type loaderType = Types.newParameterizedType(CacheLoader.class, m.keyType().getType(), m.valueType().getType());
    @SuppressWarnings("unchecked") Key<LoadingCache<K, V>> key = (Key<LoadingCache<K, V>>) Key.get(type, Names.named(m.name));
    @SuppressWarnings("unchecked") Key<LoadingCache<K, V>> loadingKey = (Key<LoadingCache<K, V>>) Key.get(loadingType, Names.named(m.name));
    @SuppressWarnings("unchecked") Key<CacheLoader<K, V>> loaderKey = (Key<CacheLoader<K, V>>) Key.get(loaderType, Names.named(m.name));
    bind(loaderKey).to(impl).in(Scopes.SINGLETON);
    bind(loadingKey).to(key);
    return getProvider(loaderKey);
}
Also used : Type(java.lang.reflect.Type) LoadingCache(com.google.common.cache.LoadingCache) CacheLoader(com.google.common.cache.CacheLoader) Key(com.google.inject.Key)

Example 8 with CacheLoader

use of com.google.common.cache.CacheLoader in project jackrabbit-oak by apache.

the class AbstractSharedCachingDataStore method init.

public void init(String homeDir) throws DataStoreException {
    if (path == null) {
        path = homeDir + "/repository/datastore";
    }
    path = FilenameUtils.normalizeNoEndSeparator(new File(path).getAbsolutePath());
    checkArgument(stagingSplitPercentage >= 0 && stagingSplitPercentage <= 50, "Staging percentage cache should be between 0 and 50");
    this.rootDirectory = new File(path);
    this.tmp = new File(rootDirectory, "tmp");
    LOG.trace("Temporary file created [{}]", tmp.mkdirs());
    this.backend = createBackend();
    backend.init();
    String home = FilenameUtils.normalizeNoEndSeparator(new File(homeDir).getAbsolutePath());
    this.cache = new CompositeDataStoreCache(path, new File(home), cacheSize, stagingSplitPercentage, uploadThreads, new CacheLoader<String, InputStream>() {

        @Override
        public InputStream load(String key) throws Exception {
            return backend.read(new DataIdentifier(key));
        }
    }, new StagingUploader() {

        @Override
        public void write(String id, File file) throws DataStoreException {
            backend.write(new DataIdentifier(id), file);
        }
    }, statisticsProvider, listeningExecutor, schedulerExecutor, executor, stagingPurgeInterval, stagingRetryInterval);
}
Also used : DataIdentifier(org.apache.jackrabbit.core.data.DataIdentifier) CacheLoader(com.google.common.cache.CacheLoader) File(java.io.File)

Example 9 with CacheLoader

use of com.google.common.cache.CacheLoader in project cdap by caskdata.

the class FlowletProcessDriver method processMethodCallback.

private <T> ProcessMethodCallback processMethodCallback(final PriorityQueue<FlowletProcessEntry<?>> processQueue, final FlowletProcessEntry<T> processEntry, final InputDatum<T> input) {
    // If it is generator flowlet, processCount is 1.
    final int processedCount = processEntry.getProcessSpec().getProcessMethod().needsInput() ? input.size() : 1;
    return new ProcessMethodCallback() {

        private final LoadingCache<String, MetricsContext> queueMetricsCollectors = CacheBuilder.newBuilder().expireAfterAccess(1, TimeUnit.HOURS).build(new CacheLoader<String, MetricsContext>() {

            @Override
            public MetricsContext load(String key) throws Exception {
                return flowletContext.getProgramMetrics().childContext(Constants.Metrics.Tag.FLOWLET_QUEUE, key);
            }
        });

        @Override
        public void onSuccess(Object object, InputContext inputContext) {
            try {
                gaugeEventProcessed(input.getQueueName());
                txCallback.onSuccess(object, inputContext);
            } catch (Throwable t) {
                LOG.error("Exception on onSuccess call: {}", flowletContext, t);
            } finally {
                enqueueEntry();
            }
        }

        @Override
        public void onFailure(Object inputObject, InputContext inputContext, FailureReason reason, InputAcknowledger inputAcknowledger) {
            LOG.warn("Process failure: {}, {}, input: {}", flowletContext, reason.getMessage(), input, reason.getCause());
            FailurePolicy failurePolicy;
            try {
                flowletContext.getProgramMetrics().increment("process.errors", 1);
                failurePolicy = txCallback.onFailure(inputObject, inputContext, reason);
                if (failurePolicy == null) {
                    failurePolicy = FailurePolicy.RETRY;
                    LOG.info("Callback returns null for failure policy. Default to {}.", failurePolicy);
                }
            } catch (Throwable t) {
                LOG.error("Exception on onFailure call: {}", flowletContext, t);
                failurePolicy = FailurePolicy.RETRY;
            }
            if (input.getRetry() >= processEntry.getProcessSpec().getProcessMethod().getMaxRetries()) {
                LOG.info("Too many retries, ignores the input: {}", input);
                failurePolicy = FailurePolicy.IGNORE;
            }
            if (failurePolicy == FailurePolicy.RETRY) {
                FlowletProcessEntry retryEntry = processEntry.isRetry() ? processEntry : FlowletProcessEntry.create(processEntry.getProcessSpec(), new ProcessSpecification<>(new SingleItemQueueReader<>(input), processEntry.getProcessSpec().getProcessMethod(), null));
                processQueue.offer(retryEntry);
            } else if (failurePolicy == FailurePolicy.IGNORE) {
                try {
                    gaugeEventProcessed(input.getQueueName());
                    inputAcknowledger.ack();
                } catch (Throwable t) {
                    LOG.error("Fatal problem, fail to ack an input: {}", flowletContext, t);
                } finally {
                    enqueueEntry();
                }
            }
        }

        private void enqueueEntry() {
            processQueue.offer(processEntry.resetRetry());
        }

        private void gaugeEventProcessed(QueueName inputQueueName) {
            if (processEntry.isTick()) {
                flowletContext.getProgramMetrics().increment("process.ticks.processed", processedCount);
            } else if (inputQueueName == null) {
                flowletContext.getProgramMetrics().increment("process.events.processed", processedCount);
            } else {
                queueMetricsCollectors.getUnchecked(inputQueueName.getSimpleName()).increment("process.events.processed", processedCount);
            }
        }
    };
}
Also used : InputContext(co.cask.cdap.api.flow.flowlet.InputContext) FailureReason(co.cask.cdap.api.flow.flowlet.FailureReason) LoadingCache(com.google.common.cache.LoadingCache) CacheLoader(com.google.common.cache.CacheLoader) FailurePolicy(co.cask.cdap.api.flow.flowlet.FailurePolicy) QueueName(co.cask.cdap.common.queue.QueueName)

Aggregations

CacheLoader (com.google.common.cache.CacheLoader)9 LoadingCache (com.google.common.cache.LoadingCache)3 CacheBuilder (com.google.common.cache.CacheBuilder)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 FailurePolicy (co.cask.cdap.api.flow.flowlet.FailurePolicy)1 FailureReason (co.cask.cdap.api.flow.flowlet.FailureReason)1 InputContext (co.cask.cdap.api.flow.flowlet.InputContext)1 MapReduceSpecification (co.cask.cdap.api.mapreduce.MapReduceSpecification)1 MetricsCollectionService (co.cask.cdap.api.metrics.MetricsCollectionService)1 SecureStore (co.cask.cdap.api.security.store.SecureStore)1 SecureStoreManager (co.cask.cdap.api.security.store.SecureStoreManager)1 MapReduceMetrics (co.cask.cdap.app.metrics.MapReduceMetrics)1 DefaultProgram (co.cask.cdap.app.program.DefaultProgram)1 Program (co.cask.cdap.app.program.Program)1 CConfiguration (co.cask.cdap.common.conf.CConfiguration)1 QueueName (co.cask.cdap.common.queue.QueueName)1 ProgramContextAware (co.cask.cdap.data.ProgramContextAware)1 DatasetFramework (co.cask.cdap.data2.dataset2.DatasetFramework)1 BasicProgramContext (co.cask.cdap.internal.app.runtime.BasicProgramContext)1 NameMappedDatasetFramework (co.cask.cdap.internal.app.runtime.workflow.NameMappedDatasetFramework)1