@Override public Iterator<Host> newQueryPlan(String loggedKeyspace, Statement statement) { if (statement instanceof BatchStatement) { return m_writePolicy.newQueryPlan(loggedKeyspace, statement); } else { return m_queryPolicy.newQueryPlan(loggedKeyspace, statement); } }
private void addBoundStatement(BoundStatement boundStatement) { Iterator<Host> hosts = m_loadBalancingPolicy.newQueryPlan(m_clusterConnection.getKeyspace(), boundStatement); if (hosts.hasNext()) { Host hostKey = hosts.next(); BatchStatement batchStatement = m_batchMap.get(hostKey); if (batchStatement == null) { batchStatement = new BatchStatement(BatchStatement.Type.UNLOGGED); m_batchMap.put(hostKey, batchStatement); } batchStatement.add(boundStatement); } else { dataPointBatch.add(boundStatement); } }
/** * {@inheritDoc} * * <p>It is guaranteed that only hosts matching the predicate will be returned. */ @Override public Iterator<Host> newQueryPlan(String loggedKeyspace, Statement statement) { // Just delegate to the child policy, since we filter the hosts not white // listed upfront, the child policy will never see a host that is not white // listed and thus can't return one. return childPolicy.newQueryPlan(loggedKeyspace, statement); }
@Override public Iterator<Host> newQueryPlan(String loggedKeyspace, Statement statement) { final Iterator<Host> childQueryPlan = childPolicy.newQueryPlan(loggedKeyspace, statement); return new AbstractIterator<Host>() { @Override protected Host computeNext() { while (childQueryPlan.hasNext()) { Host host = childQueryPlan.next(); if (!errorTracker.isExcluded(host)) { return host; } } return endOfData(); } }; }
final Iterator<Host> childIter = childPolicy.newQueryPlan(loggedKeyspace, statement); return new AbstractIterator<Host>() {
@Override public Iterator<Host> newQueryPlan(String loggedKeyspace, Statement statement) { // Since we only add chosen nodes to the child policy, its query plan will only contain chosen // nodes return delegate.newQueryPlan(loggedKeyspace, statement); }
public Iterator<Host> newQueryPlan(String loggedKeyspace, Statement statement) { return delegate.newQueryPlan(loggedKeyspace, statement); }
private Iterator<Host> queryPlan() { return cluster.loadBalancingPolicy().newQueryPlan(null, Statement.DEFAULT); }
private void verifyNoLbpInteractions() { // load balancing policy should have been skipped completely as host was set. Mockito.verify(lbSpy, Mockito.times(0)) .newQueryPlan(Mockito.any(String.class), Mockito.any(Statement.class)); }
@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(); } };
return childPolicy.newQueryPlan(keyspace, statement); if (replicas.isEmpty()) return childPolicy.newQueryPlan(loggedKeyspace, statement); final Iterator<Host> childIterator = childPolicy.newQueryPlan(keyspace, statement);
@Test(groups = "unit") public void should_return_query_plan_of_wrapped_policy() { when(wrappedPolicy.newQueryPlan(any(String.class), any(Statement.class))) .thenReturn(Iterators.forArray(host1, host2, host3)); HostFilterPolicy policy = new HostFilterPolicy(wrappedPolicy, null); assertThat(policy.newQueryPlan("keyspace", mock(Statement.class))) .containsExactly(host1, host2, host3); }
@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); }
public RequestHandler(SessionManager manager, Callback callback, Statement statement) { this.id = Long.toString(System.identityHashCode(this)); if (logger.isTraceEnabled()) logger.trace("[{}] {}", id, statement); this.manager = manager; this.callback = callback; this.scheduler = manager.cluster.manager.connectionFactory.timer; callback.register(this); // If host is explicitly set on statement, bypass load balancing policy. if (statement.getHost() != null) { this.queryPlan = new QueryPlan(Iterators.singletonIterator(statement.getHost())); } else { this.queryPlan = new QueryPlan( manager.loadBalancingPolicy().newQueryPlan(manager.poolsState.keyspace, statement)); } this.speculativeExecutionPlan = manager.speculativeExecutionPolicy().newPlan(manager.poolsState.keyspace, statement); this.allowSpeculativeExecutions = statement != Statement.DEFAULT && statement.isIdempotentWithDefault(manager.configuration().getQueryOptions()); this.statement = statement; this.timerContext = metricsEnabled() ? metrics().getRequestsTimer().time() : null; this.startTime = System.nanoTime(); }
/** * {@inheritDoc} * <p/> * It is guaranteed that only hosts matching the predicate will be returned. */ @Override public Iterator<Host> newQueryPlan(String loggedKeyspace, Statement statement) { // Just delegate to the child policy, since we filter the hosts not white // listed upfront, the child policy will never see a host that is not white // listed and thus can't return one. return childPolicy.newQueryPlan(loggedKeyspace, statement); }
/** * {@inheritDoc} * <p/> * It is guaranteed that only hosts matching the predicate will be returned. */ @Override public Iterator<Host> newQueryPlan(String loggedKeyspace, Statement statement) { // Just delegate to the child policy, since we filter the hosts not white // listed upfront, the child policy will never see a host that is not white // listed and thus can't return one. return childPolicy.newQueryPlan(loggedKeyspace, statement); }
public RequestHandler(SessionManager manager, Callback callback, Statement statement) { this.manager = manager; this.callback = callback; callback.register(this); this.queryPlan = manager.loadBalancingPolicy().newQueryPlan(manager.poolsState.keyspace, statement); this.statement = statement; this.timerContext = metricsEnabled() ? metrics().getRequestsTimer().time() : null; this.startTime = System.nanoTime(); }
private void setChildPlan(Host... hosts) { when(childPolicy.newQueryPlan(anyString(), any(Statement.class))) .thenReturn(Iterators.forArray(hosts)); } }
statement.setKeyspace("keyspace"); List<Host> queryPlan = Lists.newArrayList(loadBalancingPolicy.newQueryPlan(null, statement)); assertThat(queryPlan).containsOnlyElementsOf(cluster.getMetadata().getAllHosts());
@Test(groups = "unit") public void should_return_query_plan_of_wrapped_policy() { when(wrappedPolicy.newQueryPlan(any(String.class), any(Statement.class))) .thenReturn(Iterators.forArray(host1, host2, host3)); HostFilterPolicy policy = new HostFilterPolicy(wrappedPolicy, null); assertThat(policy.newQueryPlan("keyspace", mock(Statement.class))) .containsExactly(host1, host2, host3); }