Search in sources :

Example 1 with MicroserviceInstancePing

use of org.apache.servicecomb.registry.consumer.MicroserviceInstancePing 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 2 with MicroserviceInstancePing

use of org.apache.servicecomb.registry.consumer.MicroserviceInstancePing in project java-chassis by ServiceComb.

the class RefreshableMicroserviceCacheTest method refresh_empty_instance_protection_enabled.

@Test
public void refresh_empty_instance_protection_enabled() {
    microserviceCache.setEmptyInstanceProtectionEnabled(true);
    microserviceCache.instancePing = new MicroserviceInstancePing() {

        @Override
        public int getOrder() {
            return 0;
        }

        @Override
        public boolean ping(MicroserviceInstance instance) {
            return true;
        }
    };
    microserviceCache.instances = new ArrayList<>();
    MicroserviceInstance instance0 = new MicroserviceInstance();
    instance0.setInstanceId("instanceId0");
    microserviceCache.instances.add(instance0);
    pulledInstances = new ArrayList<>();
    microserviceCache.refresh();
    Assert.assertEquals(MicroserviceCacheStatus.REFRESHED, microserviceCache.getStatus());
    Assert.assertEquals(1, microserviceCache.getInstances().size());
    Assert.assertEquals("instanceId0", microserviceCache.getInstances().get(0).getInstanceId());
}
Also used : MicroserviceInstancePing(org.apache.servicecomb.registry.consumer.MicroserviceInstancePing) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) Test(org.junit.Test)

Example 3 with MicroserviceInstancePing

use of org.apache.servicecomb.registry.consumer.MicroserviceInstancePing 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)

Example 4 with MicroserviceInstancePing

use of org.apache.servicecomb.registry.consumer.MicroserviceInstancePing in project incubator-servicecomb-java-chassis by apache.

the class RefreshableMicroserviceCacheTest method refresh_empty_instance_protection_enabled.

@Test
public void refresh_empty_instance_protection_enabled() {
    microserviceCache.setEmptyInstanceProtectionEnabled(true);
    microserviceCache.instancePing = new MicroserviceInstancePing() {

        @Override
        public int getOrder() {
            return 0;
        }

        @Override
        public boolean ping(MicroserviceInstance instance) {
            return true;
        }
    };
    microserviceCache.instances = new ArrayList<>();
    MicroserviceInstance instance0 = new MicroserviceInstance();
    instance0.setInstanceId("instanceId0");
    microserviceCache.instances.add(instance0);
    pulledInstances = new ArrayList<>();
    microserviceCache.refresh();
    Assert.assertEquals(MicroserviceCacheStatus.REFRESHED, microserviceCache.getStatus());
    Assert.assertEquals(1, microserviceCache.getInstances().size());
    Assert.assertEquals("instanceId0", microserviceCache.getInstances().get(0).getInstanceId());
}
Also used : MicroserviceInstancePing(org.apache.servicecomb.registry.consumer.MicroserviceInstancePing) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) Test(org.junit.Test)

Aggregations

MicroserviceInstance (org.apache.servicecomb.registry.api.registry.MicroserviceInstance)4 MicroserviceInstancePing (org.apache.servicecomb.registry.consumer.MicroserviceInstancePing)4 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 CacheBuilder (com.google.common.cache.CacheBuilder)2 CacheLoader (com.google.common.cache.CacheLoader)2 LoadingCache (com.google.common.cache.LoadingCache)2 RemovalListener (com.google.common.cache.RemovalListener)2 DynamicPropertyFactory (com.netflix.config.DynamicPropertyFactory)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 TimeUnit (java.util.concurrent.TimeUnit)2 SPIServiceUtils (org.apache.servicecomb.foundation.common.utils.SPIServiceUtils)2 Test (org.junit.Test)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2