@Test public void testUsableAndUnusable() { ConnectionProvider<ByteBuf, ByteBuf> cp = rule.strategy.newStrategy(rule.newHostStream(10, UNUSABLE_HOST_WEIGHT)); TestSubscriber<Connection<ByteBuf, ByteBuf>> sub = new TestSubscriber<>(); cp.newConnectionRequest().subscribe(sub); sub.awaitTerminalEvent(); sub.assertNoErrors(); Assert.assertEquals("Unexpected number of hosts in the pool.", 2, rule.strategy.hostsInPool); Assert.assertEquals("Unexpected number of Unusable hosts found count.", 0, rule.strategy.twoUnusableHosts); Assert.assertEquals("Unexpected number of no usable hosts count.", 0,rule.strategy.allUnusable); }
@Override public ConnectionProvider<W, R> newStrategy(final List<HostHolder<W, R>> hosts) { newHostsList(hosts.size()); return new ConnectionProvider<W, R>() { @Override public Observable<Connection<R, W>> newConnectionRequest() { HostHolder<W, R> selected = null; if (hosts.isEmpty()) { noUsableHostsFound(); return Observable.error(NoHostsAvailableException.EMPTY_INSTANCE); } else if (hosts.size() == 1) { HostHolder<W, R> holder = hosts.get(0); @SuppressWarnings("unchecked") L eventListener = (L) holder.getEventListener(); double weight = getWeight(eventListener); if (isUnusable(weight)) { noUsableHostsFound(); return Observable.error(new NoHostsAvailableException("No usable hosts found.")); } selected = holder; } else { ThreadLocalRandom rand = ThreadLocalRandom.current(); for (int i = 0; i < 5; i++) { int pos = rand.nextInt(hosts.size()); HostHolder<W, R> first = hosts.get(pos); int pos2 = (rand.nextInt(hosts.size() - 1) + pos + 1) % hosts.size(); HostHolder<W, R> second = hosts.get(pos2); @SuppressWarnings("unchecked") double w1 = getWeight((L) first.getEventListener()); @SuppressWarnings("unchecked") double w2 = getWeight((L) second.getEventListener()); if (w1 > w2) { selected = first; break; } else if (w1 < w2) { selected = second; break; } else if (!isUnusable(w1)) { selected = first; break; } foundTwoUnusableHosts(); } if (null == selected) { noUsableHostsFound(); return Observable.error(new NoHostsAvailableException("No usable hosts found after 5 tries.")); } } return selected.getConnector().getConnectionProvider().newConnectionRequest(); } }; }
AbstractP2CStrategy implements LoadBalancingStrategy<W, R> { @Override public ConnectionProvider<W, R> newStrategy(final List<HostHolder<W, R>> hosts) { newHostsList(hosts.size()); return new ConnectionProvider<W, R>() { @Override public Observable<Connection<R, W>> newConnectionRequest() { HostHolder<W, R> selected = null; if (hosts.isEmpty()) { noUsableHostsFound(); return Observable.error(NoHostsAvailableException.EMPTY_INSTANCE); } else if (hosts.size() == 1) { HostHolder<W, R> holder = hosts.get(0); @SuppressWarnings("unchecked") L eventListener = (L) holder.getEventListener(); double weight = getWeight(eventListener); if (isUnusable(weight)) { noUsableHostsFound(); return Observable.error(new NoHostsAvailableException("No usable hosts found.")); } selected = holder; } else { ThreadLocalRandom rand = ThreadLocalRandom.current(); for (int i = 0; i < 5; i++) { int pos = rand.nextInt(hosts.size()); HostHolder<W, R> first = hosts.get(pos); int pos2 = (rand.nextInt(hosts.size() - 1) + pos + 1) % hosts.size(); HostHolder<W, R> second = hosts.get(pos2); @SuppressWarnings("unchecked") double w1 = getWeight((L) first.getEventListener()); @SuppressWarnings("unchecked") double w2 = getWeight((L) second.getEventListener()); if (w1 > w2) { selected = first; break; } else if (w1 < w2) { selected = second; break; } else if (!isUnusable(w1)) { selected = first; break; } foundTwoUnusableHosts(); } if (null == selected) { noUsableHostsFound(); return Observable.error(new NoHostsAvailableException("No usable hosts found after 5 tries.")); } } return selected.getConnector().getConnectionProvider().newConnectionRequest(); } }; } }
AbstractP2CStrategy implements LoadBalancingStrategy<W, R> { @Override public ConnectionProvider<W, R> newStrategy(final List<HostHolder<W, R>> hosts) { newHostsList(hosts.size()); return new ConnectionProvider<W, R>() { @Override public Observable<Connection<R, W>> newConnectionRequest() { HostHolder<W, R> selected = null; if (hosts.isEmpty()) { noUsableHostsFound(); return Observable.error(NoHostsAvailableException.EMPTY_INSTANCE); } else if (hosts.size() == 1) { HostHolder<W, R> holder = hosts.get(0); @SuppressWarnings("unchecked") L eventListener = (L) holder.getEventListener(); double weight = getWeight(eventListener); if (isUnusable(weight)) { noUsableHostsFound(); return Observable.error(new NoHostsAvailableException("No usable hosts found.")); } selected = holder; } else { ThreadLocalRandom rand = ThreadLocalRandom.current(); for (int i = 0; i < 5; i++) { int pos = rand.nextInt(hosts.size()); HostHolder<W, R> first = hosts.get(pos); int pos2 = (rand.nextInt(hosts.size() - 1) + pos + 1) % hosts.size(); HostHolder<W, R> second = hosts.get(pos2); @SuppressWarnings("unchecked") double w1 = getWeight((L) first.getEventListener()); @SuppressWarnings("unchecked") double w2 = getWeight((L) second.getEventListener()); if (w1 > w2) { selected = first; break; } else if (w1 < w2) { selected = second; break; } else if (!isUnusable(w1)) { selected = first; break; } foundTwoUnusableHosts(); } if (null == selected) { noUsableHostsFound(); return Observable.error(new NoHostsAvailableException("No usable hosts found after 5 tries.")); } } return selected.getConnector().getConnectionProvider().newConnectionRequest(); } }; }  }
AbstractP2CStrategy implements LoadBalancingStrategy<W, R> { @Override public ConnectionProvider<W, R> newStrategy(final List<HostHolder<W, R>> hosts) { newHostsList(hosts.size()); return new ConnectionProvider<W, R>() { @Override public Observable<Connection<R, W>> newConnectionRequest() { HostHolder<W, R> selected = null; if (hosts.isEmpty()) { noUsableHostsFound(); return Observable.error(NoHostsAvailableException.EMPTY_INSTANCE); } else if (hosts.size() == 1) { HostHolder<W, R> holder = hosts.get(0); @SuppressWarnings("unchecked") L eventListener = (L) holder.getEventListener(); double weight = getWeight(eventListener); if (isUnusable(weight)) { noUsableHostsFound(); return Observable.error(new NoHostsAvailableException("No usable hosts found.")); } selected = holder; } else { ThreadLocalRandom rand = ThreadLocalRandom.current(); for (int i = 0; i < 5; i++) { int pos = rand.nextInt(hosts.size()); HostHolder<W, R> first = hosts.get(pos); int pos2 = (rand.nextInt(hosts.size() - 1) + pos + 1) % hosts.size(); HostHolder<W, R> second = hosts.get(pos2); @SuppressWarnings("unchecked") double w1 = getWeight((L) first.getEventListener()); @SuppressWarnings("unchecked") double w2 = getWeight((L) second.getEventListener()); if (w1 > w2) { selected = first; break; } else if (w1 < w2) { selected = second; break; } else if (!isUnusable(w1)) { selected = first; break; } foundTwoUnusableHosts(); } if (null == selected) { noUsableHostsFound(); return Observable.error(new NoHostsAvailableException("No usable hosts found after 5 tries.")); } } return selected.getConnector().getConnectionProvider().newConnectionRequest(); } }; }  @Override ConnectionProvider<W, R> newStrategy(final List<HostHolder<W, R>> hosts); @Override HostHolder<W, R> toHolder(HostConnector<W, R> connector); }
AbstractP2CStrategy implements LoadBalancingStrategy<W, R> { @Override public ConnectionProvider<W, R> newStrategy(final List<HostHolder<W, R>> hosts) { newHostsList(hosts.size()); return new ConnectionProvider<W, R>() { @Override public Observable<Connection<R, W>> newConnectionRequest() { HostHolder<W, R> selected = null; if (hosts.isEmpty()) { noUsableHostsFound(); return Observable.error(NoHostsAvailableException.EMPTY_INSTANCE); } else if (hosts.size() == 1) { HostHolder<W, R> holder = hosts.get(0); @SuppressWarnings("unchecked") L eventListener = (L) holder.getEventListener(); double weight = getWeight(eventListener); if (isUnusable(weight)) { noUsableHostsFound(); return Observable.error(new NoHostsAvailableException("No usable hosts found.")); } selected = holder; } else { ThreadLocalRandom rand = ThreadLocalRandom.current(); for (int i = 0; i < 5; i++) { int pos = rand.nextInt(hosts.size()); HostHolder<W, R> first = hosts.get(pos); int pos2 = (rand.nextInt(hosts.size() - 1) + pos + 1) % hosts.size(); HostHolder<W, R> second = hosts.get(pos2); @SuppressWarnings("unchecked") double w1 = getWeight((L) first.getEventListener()); @SuppressWarnings("unchecked") double w2 = getWeight((L) second.getEventListener()); if (w1 > w2) { selected = first; break; } else if (w1 < w2) { selected = second; break; } else if (!isUnusable(w1)) { selected = first; break; } foundTwoUnusableHosts(); } if (null == selected) { noUsableHostsFound(); return Observable.error(new NoHostsAvailableException("No usable hosts found after 5 tries.")); } } return selected.getConnector().getConnectionProvider().newConnectionRequest(); } }; }  @Override ConnectionProvider<W, R> newStrategy(final List<HostHolder<W, R>> hosts); @Override HostHolder<W, R> toHolder(HostConnector<W, R> connector);  }