Search in sources :

Example 6 with RemovalListener

use of org.apache.flink.shaded.guava30.com.google.common.cache.RemovalListener in project kylo by Teradata.

the class JobTrackerService method createCache.

/**
 * Creates a job cache.
 */
@Nonnull
private Cache<String, Job<?>> createCache() {
    final Cache<String, Job<?>> cache = CacheBuilder.newBuilder().expireAfterWrite(1, TimeUnit.HOURS).removalListener(new RemovalListener<String, Job<?>>() {

        @Override
        public void onRemoval(@Nonnull final RemovalNotification<String, Job<?>> notification) {
            // noinspection ConstantConditions
            final Optional<Integer> jobId = notification.getValue().getJobId();
            if (jobId.isPresent()) {
                jobs.remove(jobId.get());
            }
            for (final StageInfo stage : notification.getValue().getStages()) {
                stages.remove(stage.stageId());
            }
        // TODO remove temp table
        }
    }).build();
    // Schedule clean-up of groups
    executor.scheduleAtFixedRate(new Runnable() {

        @Override
        public void run() {
            groups.cleanUp();
        }
    }, 1, 1, TimeUnit.HOURS);
    return cache;
}
Also used : Nonnull(javax.annotation.Nonnull) StageInfo(org.apache.spark.scheduler.StageInfo) RemovalNotification(com.google.common.cache.RemovalNotification) SaveJob(com.thinkbiganalytics.spark.metadata.SaveJob) Job(com.thinkbiganalytics.spark.metadata.Job) SparkJob(com.thinkbiganalytics.spark.metadata.SparkJob) TransformJob(com.thinkbiganalytics.spark.metadata.TransformJob) RemovalListener(com.google.common.cache.RemovalListener) Nonnull(javax.annotation.Nonnull)

Example 7 with RemovalListener

use of org.apache.flink.shaded.guava30.com.google.common.cache.RemovalListener in project java-chassis by ServiceComb.

the class ServiceCombLoadBalancerStats method init.

void init() {
    // for testing
    if (timer != null) {
        timer.cancel();
    }
    if (serverStatsCache != null) {
        serverStatsCache.cleanUp();
    }
    pingView.clear();
    serverStatsCache = CacheBuilder.newBuilder().expireAfterAccess(serverExpireInSeconds, TimeUnit.SECONDS).removalListener((RemovalListener<ServiceCombServer, ServiceCombServerStats>) notification -> {
        ServiceCombServer server = notification.getKey();
        LOGGER.info("stats of instance {} removed, host is {}", server.getInstance().getInstanceId(), server.getHost());
        pingView.remove(notification.getKey());
        serviceCombServers.remove(notification.getKey());
    }).build(new CacheLoader<ServiceCombServer, ServiceCombServerStats>() {

        public ServiceCombServerStats load(ServiceCombServer server) {
            ServiceCombServerStats stats = new ServiceCombServerStats(server.getMicroserviceName());
            pingView.put(server, stats);
            serviceCombServers.put(server.getInstance().getInstanceId(), server);
            return stats;
        }
    });
    timer = new Timer("LoadBalancerStatsTimer", true);
    timer.schedule(new TimerTask() {

        private MicroserviceInstancePing ping = SPIServiceUtils.getPriorityHighestService(MicroserviceInstancePing.class);

        @Override
        public void run() {
            try {
                Map<ServiceCombServer, ServiceCombServerStats> allServers = pingView;
                allServers.entrySet().forEach(serviceCombServerServiceCombServerStatsEntry -> {
                    ServiceCombServer server = serviceCombServerServiceCombServerStatsEntry.getKey();
                    ServiceCombServerStats stats = serviceCombServerServiceCombServerStatsEntry.getValue();
                    if ((System.currentTimeMillis() - stats.getLastVisitTime() > timerIntervalInMillis) && !ping.ping(server.getInstance())) {
                        LOGGER.info("ping mark server {} failure.", server.getInstance().getInstanceId());
                        stats.markFailure();
                    }
                });
                serverStatsCache.cleanUp();
            } catch (Throwable e) {
                LOGGER.warn("LoadBalancerStatsTimer error.", e);
            }
        }
    }, timerIntervalInMillis, timerIntervalInMillis);
}
Also used : MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) Logger(org.slf4j.Logger) LoadingCache(com.google.common.cache.LoadingCache) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) LoggerFactory(org.slf4j.LoggerFactory) SPIServiceUtils(org.apache.servicecomb.foundation.common.utils.SPIServiceUtils) Timer(java.util.Timer) DynamicPropertyFactory(com.netflix.config.DynamicPropertyFactory) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) CacheLoader(com.google.common.cache.CacheLoader) Map(java.util.Map) RemovalListener(com.google.common.cache.RemovalListener) VisibleForTesting(com.google.common.annotations.VisibleForTesting) CacheBuilder(com.google.common.cache.CacheBuilder) TimerTask(java.util.TimerTask) MicroserviceInstancePing(org.apache.servicecomb.registry.consumer.MicroserviceInstancePing) Timer(java.util.Timer) TimerTask(java.util.TimerTask) MicroserviceInstancePing(org.apache.servicecomb.registry.consumer.MicroserviceInstancePing) CacheLoader(com.google.common.cache.CacheLoader) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Map(java.util.Map)

Example 8 with RemovalListener

use of org.apache.flink.shaded.guava30.com.google.common.cache.RemovalListener in project incubator-servicecomb-java-chassis by apache.

the class ServiceCombLoadBalancerStats method init.

void init() {
    // for testing
    if (timer != null) {
        timer.cancel();
    }
    if (serverStatsCache != null) {
        serverStatsCache.cleanUp();
    }
    pingView.clear();
    serverStatsCache = CacheBuilder.newBuilder().expireAfterAccess(serverExpireInSeconds, TimeUnit.SECONDS).removalListener((RemovalListener<ServiceCombServer, ServiceCombServerStats>) notification -> {
        ServiceCombServer server = notification.getKey();
        LOGGER.info("stats of instance {} removed, host is {}", server.getInstance().getInstanceId(), server.getHost());
        pingView.remove(notification.getKey());
        serviceCombServers.remove(notification.getKey());
    }).build(new CacheLoader<ServiceCombServer, ServiceCombServerStats>() {

        public ServiceCombServerStats load(ServiceCombServer server) {
            ServiceCombServerStats stats = new ServiceCombServerStats(server.getMicroserviceName());
            pingView.put(server, stats);
            serviceCombServers.put(server.getInstance().getInstanceId(), server);
            return stats;
        }
    });
    timer = new Timer("LoadBalancerStatsTimer", true);
    timer.schedule(new TimerTask() {

        private MicroserviceInstancePing ping = SPIServiceUtils.getPriorityHighestService(MicroserviceInstancePing.class);

        @Override
        public void run() {
            try {
                Map<ServiceCombServer, ServiceCombServerStats> allServers = pingView;
                allServers.entrySet().forEach(serviceCombServerServiceCombServerStatsEntry -> {
                    ServiceCombServer server = serviceCombServerServiceCombServerStatsEntry.getKey();
                    ServiceCombServerStats stats = serviceCombServerServiceCombServerStatsEntry.getValue();
                    if ((System.currentTimeMillis() - stats.getLastVisitTime() > timerIntervalInMillis) && !ping.ping(server.getInstance())) {
                        LOGGER.info("ping mark server {} failure.", server.getInstance().getInstanceId());
                        stats.markFailure();
                    }
                });
                serverStatsCache.cleanUp();
            } catch (Throwable e) {
                LOGGER.warn("LoadBalancerStatsTimer error.", e);
            }
        }
    }, timerIntervalInMillis, timerIntervalInMillis);
}
Also used : MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) Logger(org.slf4j.Logger) LoadingCache(com.google.common.cache.LoadingCache) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) LoggerFactory(org.slf4j.LoggerFactory) SPIServiceUtils(org.apache.servicecomb.foundation.common.utils.SPIServiceUtils) Timer(java.util.Timer) DynamicPropertyFactory(com.netflix.config.DynamicPropertyFactory) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) CacheLoader(com.google.common.cache.CacheLoader) Map(java.util.Map) RemovalListener(com.google.common.cache.RemovalListener) VisibleForTesting(com.google.common.annotations.VisibleForTesting) CacheBuilder(com.google.common.cache.CacheBuilder) TimerTask(java.util.TimerTask) MicroserviceInstancePing(org.apache.servicecomb.registry.consumer.MicroserviceInstancePing) Timer(java.util.Timer) TimerTask(java.util.TimerTask) MicroserviceInstancePing(org.apache.servicecomb.registry.consumer.MicroserviceInstancePing) CacheLoader(com.google.common.cache.CacheLoader) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Map(java.util.Map)

Aggregations

RemovalListener (com.google.common.cache.RemovalListener)8 RemovalNotification (com.google.common.cache.RemovalNotification)5 CacheBuilder (com.google.common.cache.CacheBuilder)4 TimeUnit (java.util.concurrent.TimeUnit)4 Logger (org.slf4j.Logger)4 LoggerFactory (org.slf4j.LoggerFactory)4 LoadingCache (com.google.common.cache.LoadingCache)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 CacheLoader (com.google.common.cache.CacheLoader)2 DynamicPropertyFactory (com.netflix.config.DynamicPropertyFactory)2 File (java.io.File)2 IOException (java.io.IOException)2 MappedByteBuffer (java.nio.MappedByteBuffer)2 List (java.util.List)2 Map (java.util.Map)2 Timer (java.util.Timer)2 TimerTask (java.util.TimerTask)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 ExecutionException (java.util.concurrent.ExecutionException)2 SPIServiceUtils (org.apache.servicecomb.foundation.common.utils.SPIServiceUtils)2