@Override public HostDistance distance(Host host) { return m_writePolicy.distance(host); }
/** * Returns the HostDistance for the provided host. * * @param host the host of which to return the distance of. * @return the HostDistance to {@code host} as returned by the wrapped policy. */ @Override public HostDistance distance(Host host) { return childPolicy.distance(host); }
/** * {@inheritDoc} * * <p>This implementation always returns distances as reported by the wrapped policy. */ @Override public HostDistance distance(Host host) { return childPolicy.distance(host); }
@Override public HostDistance distance(Host host) { return childPolicy.distance(host); }
/** * {@inheritDoc} * * @return {@link HostDistance#IGNORED} if {@code host} is not matching the predicate, the * HostDistance as returned by the wrapped policy otherwise. */ @Override public HostDistance distance(Host host) { return predicate.apply(host) ? childPolicy.distance(host) : HostDistance.IGNORED; }
public HostDistance distance(Host host) { return delegate.distance(host); }
@Override protected Host computeNext() { while (replicasIterator.hasNext()) { Host host = replicasIterator.next(); if (host.isUp() && childPolicy.distance(host) == HostDistance.LOCAL) return host; } if (childIterator == null) childIterator = childPolicy.newQueryPlan(loggedKeyspace, statement); while (childIterator.hasNext()) { Host host = childIterator.next(); // Skip it if it was already a local replica if (!replicas.contains(host) || childPolicy.distance(host) != HostDistance.LOCAL) return host; } return endOfData(); } };
@Override public HostDistance distance(Host host) { if (chosenHosts.contains(host)) return delegate.distance(host); else return HostDistance.IGNORED; }
@Override protected Host computeNext() { while (childIterator.hasNext()) { Host host = childIterator.next(); if (host.isUp() && replicas.contains(host) && childPolicy.distance(host) == HostDistance.LOCAL) { // UP replicas should be prioritized, retaining order from childPolicy return host; } else { // save for later if (nonReplicas == null) nonReplicas = new ArrayList<Host>(); nonReplicas.add(host); } } // This should only engage if all local replicas are DOWN if (nonReplicas != null) { if (nonReplicasIterator == null) nonReplicasIterator = nonReplicas.iterator(); if (nonReplicasIterator.hasNext()) return nonReplicasIterator.next(); } return endOfData(); } };
void updateCreatedPools(Host h) { HostDistance dist = loadBalancingPolicy().distance(h); HostConnectionPool pool = pools.get(h); try { if (pool == null) { if (dist != HostDistance.IGNORED && h.state == Host.State.UP) maybeAddPool(h, null).get(); } else if (dist != pool.hostDistance) { if (dist == HostDistance.IGNORED) { removePool(h).get(); } else { pool.hostDistance = dist; pool.ensureCoreConnections(); } } } catch (InterruptedException e) { Thread.currentThread().interrupt(); } catch (ExecutionException e) { Throwable cause = e.getCause(); logger.error("Unexpected error while refreshing connection pools", cause); if (cause instanceof Error) throw ((Error) cause); } }
public HostAssert isAtDistance(HostDistance expected) { LoadBalancingPolicy loadBalancingPolicy = cluster.manager.loadBalancingPolicy(); assertThat(loadBalancingPolicy.distance(actual)).isEqualTo(expected); return this; }
@BeforeMethod(groups = "unit") public void setup() { MockitoAnnotations.initMocks(this); when(host1.getSocketAddress()).thenReturn(address1); when(host2.getSocketAddress()).thenReturn(address2); when(host3.getSocketAddress()).thenReturn(address3); when(wrappedPolicy.distance(any(Host.class))).thenReturn(HostDistance.LOCAL); }
ListenableFuture<Boolean> forceRenewPool(final Host host, Connection reusedConnection) { final HostDistance distance = cluster.manager.loadBalancingPolicy().distance(host); if (distance == HostDistance.IGNORED) return Futures.immediateFuture(true);
HostDistance dist = loadBalancingPolicy().distance(h); HostConnectionPool pool = pools.get(h);
ListenableFuture<Boolean> maybeAddPool(final Host host, Connection reusedConnection) { final HostDistance distance = cluster.manager.loadBalancingPolicy().distance(host); if (distance == HostDistance.IGNORED) return Futures.immediateFuture(true);
void refreshConnectedHost(Host host) { // Deal with the control connection if it was using this host Host ccHost = controlConnection.connectedHost(); if (ccHost == null || ccHost.equals(host) && loadBalancingPolicy().distance(ccHost) != HostDistance.LOCAL) controlConnection.triggerReconnect(); for (SessionManager s : sessions) s.updateCreatedPools(host); }
private void poolingMonitoring(PoolingOptions poolingOptions) { final LoadBalancingPolicy loadBalancingPolicy = cluster.getConfiguration().getPolicies().getLoadBalancingPolicy(); ScheduledExecutorService scheduled = Executors.newScheduledThreadPool(1); scheduled.scheduleAtFixedRate((Runnable) () -> { Session.State state = session.getState(); for (Host host : state.getConnectedHosts()) { HostDistance distance = loadBalancingPolicy.distance(host); int connections = state.getOpenConnections(host); int inFlightQueries = state.getInFlightQueries(host); LOGGER.info("{} connections={}, current load={}, max load={}", host, connections, inFlightQueries, connections * poolingOptions.getMaxRequestsPerConnection(distance)); } }, 5, 5, TimeUnit.SECONDS); } }
HostDistance distance = loadBalancingPolicy().distance(host);
void refreshConnectedHosts() { // Deal first with the control connection: if it's connected to a node that is not LOCAL, try // reconnecting (thus letting the loadBalancingPolicy pick a better node) Host ccHost = controlConnection.connectedHost(); if (ccHost == null || loadBalancingPolicy().distance(ccHost) != HostDistance.LOCAL) controlConnection.triggerReconnect(); try { for (SessionManager s : sessions) Uninterruptibles.getUninterruptibly(s.updateCreatedPools()); } catch (ExecutionException e) { throw DriverThrowables.propagateCause(e); } }
@BeforeMethod(groups = "unit") public void initMocks() { CodecRegistry codecRegistry = new CodecRegistry(); cluster = mock(Cluster.class); Configuration configuration = mock(Configuration.class); ProtocolOptions protocolOptions = mock(ProtocolOptions.class); Metadata metadata = mock(Metadata.class); childPolicy = mock(LoadBalancingPolicy.class); when(cluster.getConfiguration()).thenReturn(configuration); when(configuration.getCodecRegistry()).thenReturn(codecRegistry); when(configuration.getProtocolOptions()).thenReturn(protocolOptions); when(protocolOptions.getProtocolVersion()).thenReturn(ProtocolVersion.NEWEST_SUPPORTED); when(cluster.getMetadata()).thenReturn(metadata); when(metadata.getReplicas(Metadata.quote("keyspace"), routingKey)) .thenReturn(Sets.newLinkedHashSet(host1, host2)); when(childPolicy.newQueryPlan("keyspace", statement)) .thenReturn(Sets.newLinkedHashSet(host4, host3, host2, host1).iterator()); when(childPolicy.distance(any(Host.class))).thenReturn(HostDistance.LOCAL); when(host1.isUp()).thenReturn(true); when(host2.isUp()).thenReturn(true); when(host3.isUp()).thenReturn(true); when(host4.isUp()).thenReturn(true); }