@Test public void updateServerList_NotPingMinAvailableServerCount_Test() { int serverCount = 10; final String lbId = Thread.currentThread().getStackTrace()[1].getMethodName(); final LoadBalancerRequestConfig loadBalancerRequestConfig = new LoadBalancerRequestConfig(lbId); List<Server> servers = new ArrayList<>(); for (int i = 0; i < serverCount; i++) { servers.add(Server.newBuilder().setServerId("server_" + i).build()); } Ping ping = new Ping() { @Override public boolean isAlive(Server server) { return false; } }; LoadBalancerManager factory = LoadBalancerManager.getManager(managerId, TestUtils.getLoadBalancerManagerConfig()); DefaultDynamicServerSource serverSource = DefaultDynamicServerSource.fromServers(lbId, servers); LoadBalancerConfig config = new LoadBalancerConfig.Builder().setPing(ping).setServerSource(serverSource).build(); final LoadBalancer loadBalancer = factory.getLoadBalancer(lbId, config); DefaultLoadBalancer defaultLoadBalancer = (DefaultLoadBalancer)loadBalancer; ServerSourceFilter serverSourceFilter = defaultLoadBalancer.getLoadBalancerContext().serverSourceFilter(); Assert.assertEquals(serverCount, serverSourceFilter.getLoadBalancerRoute(loadBalancerRequestConfig).getServers().size()); Assert.assertEquals(serverCount, serverSourceFilter.getLoadBalancerRoute(loadBalancerRequestConfig).getAvailableServers().size()); servers = new ArrayList<>(); int count = defaultLoadBalancer.getLoadBalancerContext().getMinAvailableServerCount(); Assert.assertEquals(3, count); for (int i = 0; i < count; i++) { servers.add(Server.newBuilder().setServerId("newServer" + i).build()); } serverSource.setServers(lbId, servers); Assert.assertEquals(count, serverSourceFilter.getLoadBalancerRoute(loadBalancerRequestConfig).getServers().size()); Assert.assertEquals(count, serverSourceFilter.getLoadBalancerRoute(loadBalancerRequestConfig).getAvailableServers().size()); }
@Override public LoadBalancerRoute getLoadBalancerRoute(LoadBalancerRequestConfig loadBalancerRequestConfig) { if (loadBalancerRequestConfig == null || CollectionValues.isNullOrEmpty(loadBalancerRequestConfig.getRouteChains())) return null; HashMap<String, LoadBalancerRoute> routeMap = _routeMap; TreeSet<LoadBalancerRouteConfig> routeChains = loadBalancerRequestConfig.getRouteChains(); LoadBalancerRouteConfig loadBalancerRouteConfig = routeChains.first(); while (loadBalancerRouteConfig != null) { String routeId = loadBalancerRouteConfig.getRouteId(); if (!StringValues.isNullOrWhitespace(routeId)) { LoadBalancerRoute route = routeMap.get(routeId); if (route != null) { SeekRouteEvent event = new DefaultSeekRouteEvent(route); raiseSeekRouteEvent(event); return event.getRoute(); } } if (!loadBalancerRouteConfig.isAllowFallback()) return null; loadBalancerRouteConfig = routeChains.higher(loadBalancerRouteConfig); } return null; }
DefaultServerSourceFilter implements ServerSourceFilter { @Override public LoadBalancerRoute getLoadBalancerRoute(LoadBalancerRequestConfig loadBalancerRequestConfig) { if (loadBalancerRequestConfig == null || CollectionValues.isNullOrEmpty(loadBalancerRequestConfig.getRouteChains())) return null; HashMap<String, LoadBalancerRoute> routeMap = _routeMap; TreeSet<LoadBalancerRouteConfig> routeChains = loadBalancerRequestConfig.getRouteChains(); LoadBalancerRouteConfig loadBalancerRouteConfig = routeChains.first(); while (loadBalancerRouteConfig != null) { String routeId = loadBalancerRouteConfig.getRouteId(); if (!StringValues.isNullOrWhitespace(routeId)) { LoadBalancerRoute route = routeMap.get(routeId); if (route != null) { SeekRouteEvent event = new DefaultSeekRouteEvent(route); raiseSeekRouteEvent(event); return event.getRoute(); } } if (!loadBalancerRouteConfig.isAllowFallback()) return null; loadBalancerRouteConfig = routeChains.higher(loadBalancerRouteConfig); } return null; } }
DefaultServerSourceFilter implements ServerSourceFilter { @Override public LoadBalancerRoute getLoadBalancerRoute(LoadBalancerRequestConfig loadBalancerRequestConfig) { if (loadBalancerRequestConfig == null || CollectionValues.isNullOrEmpty(loadBalancerRequestConfig.getRouteChains())) return null; HashMap<String, LoadBalancerRoute> routeMap = _routeMap; TreeSet<LoadBalancerRouteConfig> routeChains = loadBalancerRequestConfig.getRouteChains(); LoadBalancerRouteConfig loadBalancerRouteConfig = routeChains.first(); while (loadBalancerRouteConfig != null) { String routeId = loadBalancerRouteConfig.getRouteId(); if (!StringValues.isNullOrWhitespace(routeId)) { LoadBalancerRoute route = routeMap.get(routeId); if (route != null) { SeekRouteEvent event = new DefaultSeekRouteEvent(route); raiseSeekRouteEvent(event); return event.getRoute(); } } if (!loadBalancerRouteConfig.isAllowFallback()) return null; loadBalancerRouteConfig = routeChains.higher(loadBalancerRouteConfig); } return null; } DefaultServerSourceFilter(LoadBalancerContext loadBalancerContext); }
DefaultServerSourceFilter implements ServerSourceFilter { @Override public LoadBalancerRoute getLoadBalancerRoute(LoadBalancerRequestConfig loadBalancerRequestConfig) { if (loadBalancerRequestConfig == null || CollectionValues.isNullOrEmpty(loadBalancerRequestConfig.getRouteChains())) return null; HashMap<String, LoadBalancerRoute> routeMap = _routeMap; TreeSet<LoadBalancerRouteConfig> routeChains = loadBalancerRequestConfig.getRouteChains(); LoadBalancerRouteConfig loadBalancerRouteConfig = routeChains.first(); while (loadBalancerRouteConfig != null) { String routeId = loadBalancerRouteConfig.getRouteId(); if (!StringValues.isNullOrWhitespace(routeId)) { LoadBalancerRoute route = routeMap.get(routeId); if (route != null) { SeekRouteEvent event = new DefaultSeekRouteEvent(route); raiseSeekRouteEvent(event); return event.getRoute(); } } if (!loadBalancerRouteConfig.isAllowFallback()) return null; loadBalancerRouteConfig = routeChains.higher(loadBalancerRouteConfig); } return null; } DefaultServerSourceFilter(LoadBalancerContext loadBalancerContext); @Override List<LoadBalancerRoute> getLoadBalancerRoutes(); @Override synchronized void setLoadBalancerRoutes(List<LoadBalancerRoute> newRoutes); @Override LoadBalancerRoute getLoadBalancerRoute(LoadBalancerRequestConfig loadBalancerRequestConfig); @Override synchronized void refresh(); @Override void addServerContextChangeListener(ServerContextChangeListener listener); @Override void addSeekRouteListener(SeekRouteListener listener); }
DefaultServerSourceFilter implements ServerSourceFilter { @Override public LoadBalancerRoute getLoadBalancerRoute(LoadBalancerRequestConfig loadBalancerRequestConfig) { if (loadBalancerRequestConfig == null || CollectionValues.isNullOrEmpty(loadBalancerRequestConfig.getRouteChains())) return null; HashMap<String, LoadBalancerRoute> routeMap = _routeMap; TreeSet<LoadBalancerRouteConfig> routeChains = loadBalancerRequestConfig.getRouteChains(); LoadBalancerRouteConfig loadBalancerRouteConfig = routeChains.first(); while (loadBalancerRouteConfig != null) { String routeId = loadBalancerRouteConfig.getRouteId(); if (!StringValues.isNullOrWhitespace(routeId)) { LoadBalancerRoute route = routeMap.get(routeId); if (route != null) { SeekRouteEvent event = new DefaultSeekRouteEvent(route); raiseSeekRouteEvent(event); return event.getRoute(); } } if (!loadBalancerRouteConfig.isAllowFallback()) return null; loadBalancerRouteConfig = routeChains.higher(loadBalancerRouteConfig); } return null; } DefaultServerSourceFilter(LoadBalancerContext loadBalancerContext); @Override List<LoadBalancerRoute> getLoadBalancerRoutes(); @Override synchronized void setLoadBalancerRoutes(List<LoadBalancerRoute> newRoutes); @Override LoadBalancerRoute getLoadBalancerRoute(LoadBalancerRequestConfig loadBalancerRequestConfig); @Override synchronized void refresh(); @Override void addServerContextChangeListener(ServerContextChangeListener listener); @Override void addSeekRouteListener(SeekRouteListener listener);  }