Search in sources :

Example 6 with LoadBalancerStats

use of com.netflix.loadbalancer.LoadBalancerStats in project ribbon by Netflix.

the class DefaultNIWSServerListFilterTest method testZoneAffinityOverride.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testZoneAffinityOverride() throws Exception {
    ConfigurationManager.getConfigInstance().setProperty("DefaultNIWSServerListFilterTest3.ribbon.DeploymentContextBasedVipAddresses", "l10nservicegeneral.cloud.netflix.net:7001");
    ConfigurationManager.getConfigInstance().setProperty("DefaultNIWSServerListFilterTest3.ribbon.NFLoadBalancerClassName", DynamicServerListLoadBalancer.class.getName());
    ConfigurationManager.getConfigInstance().setProperty("DefaultNIWSServerListFilterTest3.ribbon.EnableZoneAffinity", "true");
    ConfigurationManager.getConfigInstance().setProperty("DefaultNIWSServerListFilterTest3.ribbon.NIWSServerListClassName", DiscoveryEnabledNIWSServerList.class.getName());
    ConfigurationManager.getConfigInstance().setProperty("DefaultNIWSServerListFilterTest3.ribbon.zoneAffinity.minAvailableServers", "3");
    DynamicServerListLoadBalancer lb = (DynamicServerListLoadBalancer) ClientFactory.getNamedLoadBalancer("DefaultNIWSServerListFilterTest3");
    ZoneAffinityServerListFilter filter = (ZoneAffinityServerListFilter) lb.getFilter();
    LoadBalancerStats loadBalancerStats = lb.getLoadBalancerStats();
    List<DiscoveryEnabledServer> servers = new ArrayList<DiscoveryEnabledServer>();
    servers.add(createServer(1, "a"));
    servers.add(createServer(2, "a"));
    servers.add(createServer(3, "a"));
    servers.add(createServer(4, "a"));
    servers.add(createServer(1, "b"));
    servers.add(createServer(2, "b"));
    servers.add(createServer(3, "b"));
    servers.add(createServer(1, "c"));
    servers.add(createServer(2, "c"));
    List<DiscoveryEnabledServer> filtered = filter.getFilteredListOfServers(servers);
    List<DiscoveryEnabledServer> expected = new ArrayList<DiscoveryEnabledServer>();
    /*
        expected.add(createServer(1, "c"));
        expected.add(createServer(2, "c"));
        expected.add(createServer(3, "c"));
        expected.add(createServer(4, "c"));
        expected.add(createServer(5, "c")); */
    // less than 3 servers in zone c, will not honor zone affinity
    assertEquals(servers, filtered);
    lb.setServersList(filtered);
    servers.add(createServer(3, "c"));
    filtered = filter.getFilteredListOfServers(servers);
    expected.add(createServer(1, "c"));
    expected.add(createServer(2, "c"));
    expected.add(createServer(3, "c"));
    filtered = filter.getFilteredListOfServers(servers);
    // now we have enough servers in C
    assertEquals(expected, filtered);
    // make one server black out
    for (int i = 1; i <= 3; i++) {
        loadBalancerStats.incrementSuccessiveConnectionFailureCount(createServer(1, "c"));
    }
    filtered = filter.getFilteredListOfServers(servers);
    assertEquals(servers, filtered);
    // new server added in zone c, zone c should now have enough servers
    servers.add(createServer(4, "c"));
    filtered = filter.getFilteredListOfServers(servers);
    expected.add(createServer(4, "c"));
    assertEquals(expected, filtered);
}
Also used : DynamicServerListLoadBalancer(com.netflix.loadbalancer.DynamicServerListLoadBalancer) ZoneAffinityServerListFilter(com.netflix.loadbalancer.ZoneAffinityServerListFilter) LoadBalancerStats(com.netflix.loadbalancer.LoadBalancerStats) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 7 with LoadBalancerStats

use of com.netflix.loadbalancer.LoadBalancerStats in project ribbon by Netflix.

the class ZoneAffinityServerListFilter method shouldEnableZoneAffinity.

private boolean shouldEnableZoneAffinity(List<T> filtered) {
    if (!zoneAffinity && !zoneExclusive) {
        return false;
    }
    if (zoneExclusive) {
        return true;
    }
    LoadBalancerStats stats = getLoadBalancerStats();
    if (stats == null) {
        return zoneAffinity;
    } else {
        logger.debug("Determining if zone affinity should be enabled with given server list: {}", filtered);
        ZoneSnapshot snapshot = stats.getZoneSnapshot(filtered);
        double loadPerServer = snapshot.getLoadPerServer();
        int instanceCount = snapshot.getInstanceCount();
        int circuitBreakerTrippedCount = snapshot.getCircuitTrippedCount();
        if (((double) circuitBreakerTrippedCount) / instanceCount >= blackOutServerPercentageThreshold.get() || loadPerServer >= activeReqeustsPerServerThreshold.get() || (instanceCount - circuitBreakerTrippedCount) < availableServersThreshold.get()) {
            logger.debug("zoneAffinity is overriden. blackOutServerPercentage: {}, activeReqeustsPerServer: {}, availableServers: {}", new Object[] { (double) circuitBreakerTrippedCount / instanceCount, loadPerServer, instanceCount - circuitBreakerTrippedCount });
            return false;
        } else {
            return true;
        }
    }
}
Also used : ZoneSnapshot(com.netflix.loadbalancer.ZoneSnapshot) LoadBalancerStats(com.netflix.loadbalancer.LoadBalancerStats)

Aggregations

LoadBalancerStats (com.netflix.loadbalancer.LoadBalancerStats)7 Test (org.junit.Test)4 DynamicServerListLoadBalancer (com.netflix.loadbalancer.DynamicServerListLoadBalancer)3 ZoneAffinityServerListFilter (com.netflix.loadbalancer.ZoneAffinityServerListFilter)3 ArrayList (java.util.ArrayList)3 AbstractLoadBalancer (com.netflix.loadbalancer.AbstractLoadBalancer)1 AvailabilityFilteringRule (com.netflix.loadbalancer.AvailabilityFilteringRule)1 Server (com.netflix.loadbalancer.Server)1 ServerStats (com.netflix.loadbalancer.ServerStats)1 ZoneSnapshot (com.netflix.loadbalancer.ZoneSnapshot)1 Transport (io.servicecomb.core.Transport)1 CacheEndpoint (io.servicecomb.serviceregistry.cache.CacheEndpoint)1 Before (org.junit.Before)1