@Test public void doSelect() throws Exception { ConsistentHashLoadBalancer loadBalancer = new ConsistentHashLoadBalancer(null); Map<Integer, Integer> cnt = new HashMap<Integer, Integer>(); int size = 20; int total = 100000; SofaRequest request = new SofaRequest(); request.setInterfaceName(ConsistentHashLoadBalancerTest.class.getName()); request.setMethod(ConsistentHashLoadBalancerTest.class.getMethod("doSelect")); for (int i = 0; i < size; i++) { cnt.put(9000 + i, 0); } List<ProviderInfo> providers = buildSameWeightProviderList(size); long start = System.currentTimeMillis(); for (int i = 0; i < total; i++) { ProviderInfo provider = loadBalancer.doSelect(request, providers); int port = provider.getPort(); cnt.put(port, cnt.get(port) + 1); } long end = System.currentTimeMillis(); LOGGER.info("elapsed" + (end - start) + "ms"); LOGGER.info("avg " + (end - start) * 1000 * 1000 / total + "ns"); int count = 0; for (int i = 0; i < size; i++) { if (cnt.get(9000 + i) > 0) { count++; } } Assert.assertTrue(count == 1); }
@Override public ProviderInfo doSelect(SofaRequest request, List<ProviderInfo> providerInfos) { String interfaceId = request.getInterfaceName(); String method = request.getMethodName(); String key = interfaceId + "#" + method; int hashcode = providerInfos.hashCode(); Selector selector = selectorCache.get(key); if (selector == null || selector.getHashCode() != hashcode) { selector = new Selector(interfaceId, method, providerInfos, hashcode); selectorCache.put(key, selector); } return selector.select(request); }
ConsistentHashLoadBalancer extends AbstractLoadBalancer { @Override public ProviderInfo doSelect(SofaRequest request, List<ProviderInfo> providerInfos) { String interfaceId = request.getInterfaceName(); String method = request.getMethodName(); String key = interfaceId + "#" + method; int hashcode = providerInfos.hashCode(); Selector selector = selectorCache.get(key); if (selector == null || selector.getHashCode() != hashcode) { selector = new Selector(interfaceId, method, providerInfos, hashcode); selectorCache.put(key, selector); } return selector.select(request); } }
ConsistentHashLoadBalancer extends AbstractLoadBalancer { @Override public ProviderInfo doSelect(SofaRequest request, List<ProviderInfo> providerInfos) { String interfaceId = request.getInterfaceName(); String method = request.getMethodName(); String key = interfaceId + "#" + method; int hashcode = providerInfos.hashCode(); Selector selector = selectorCache.get(key); if (selector == null || selector.getHashCode() != hashcode) { selector = new Selector(interfaceId, method, providerInfos, hashcode); selectorCache.put(key, selector); } return selector.select(request); } ConsistentHashLoadBalancer(ConsumerBootstrap consumerBootstrap); }
ConsistentHashLoadBalancer extends AbstractLoadBalancer { @Override public ProviderInfo doSelect(SofaRequest request, List<ProviderInfo> providerInfos) { String interfaceId = request.getInterfaceName(); String method = request.getMethodName(); String key = interfaceId + "#" + method; int hashcode = providerInfos.hashCode(); Selector selector = selectorCache.get(key); if (selector == null || selector.getHashCode() != hashcode) { selector = new Selector(interfaceId, method, providerInfos, hashcode); selectorCache.put(key, selector); } return selector.select(request); } ConsistentHashLoadBalancer(ConsumerBootstrap consumerBootstrap); @Override ProviderInfo doSelect(SofaRequest request, List<ProviderInfo> providerInfos); }
ConsistentHashLoadBalancer extends AbstractLoadBalancer { @Override public ProviderInfo doSelect(SofaRequest request, List<ProviderInfo> providerInfos) { String interfaceId = request.getInterfaceName(); String method = request.getMethodName(); String key = interfaceId + "#" + method; int hashcode = providerInfos.hashCode(); Selector selector = selectorCache.get(key); if (selector == null || selector.getHashCode() != hashcode) { selector = new Selector(interfaceId, method, providerInfos, hashcode); selectorCache.put(key, selector); } return selector.select(request); } ConsistentHashLoadBalancer(ConsumerBootstrap consumerBootstrap); @Override ProviderInfo doSelect(SofaRequest request, List<ProviderInfo> providerInfos);  }