Search in sources :

Example 1 with Prepare

use of com.datastax.oss.protocol.internal.request.Prepare in project java-driver by datastax.

the class CqlPrepareHandler method sendRequest.

private void sendRequest(PrepareRequest request, Node node, int retryCount) {
    if (result.isDone()) {
        return;
    }
    DriverChannel channel = null;
    if (node == null || (channel = session.getChannel(node, logPrefix)) == null) {
        while (!result.isDone() && (node = queryPlan.poll()) != null) {
            channel = session.getChannel(node, logPrefix);
            if (channel != null) {
                break;
            } else {
                recordError(node, new NodeUnavailableException(node));
            }
        }
    }
    if (channel == null) {
        setFinalError(AllNodesFailedException.fromErrors(this.errors));
    } else {
        InitialPrepareCallback initialPrepareCallback = new InitialPrepareCallback(request, node, channel, retryCount);
        Prepare message = toPrepareMessage(request);
        channel.write(message, false, request.getCustomPayload(), initialPrepareCallback).addListener(initialPrepareCallback);
    }
}
Also used : DriverChannel(com.datastax.oss.driver.internal.core.channel.DriverChannel) NodeUnavailableException(com.datastax.oss.driver.api.core.NodeUnavailableException) Prepare(com.datastax.oss.protocol.internal.request.Prepare)

Example 2 with Prepare

use of com.datastax.oss.protocol.internal.request.Prepare in project java-driver by datastax.

the class ReprepareOnUpTest method should_reprepare_all_if_system_table_empty.

@Test
public void should_reprepare_all_if_system_table_empty() {
    MockReprepareOnUp reprepareOnUp = new MockReprepareOnUp("test", pool, ImmediateEventExecutor.INSTANCE, getMockPayloads('a', 'b', 'c', 'd', 'e', 'f'), context, whenPrepared);
    reprepareOnUp.start();
    MockAdminQuery adminQuery = reprepareOnUp.queries.poll();
    assertThat(adminQuery).isNotNull();
    assertThat(adminQuery.request).isInstanceOf(Query.class);
    assertThat(((Query) adminQuery.request).query).isEqualTo("SELECT prepared_id FROM system.prepared_statements");
    // server knows no ids:
    adminQuery.resultFuture.complete(new AdminResult(preparedIdRows(), null, DefaultProtocolVersion.DEFAULT));
    for (char c = 'a'; c <= 'f'; c++) {
        adminQuery = reprepareOnUp.queries.poll();
        assertThat(adminQuery).isNotNull();
        assertThat(adminQuery.request).isInstanceOf(Prepare.class);
        assertThat(((Prepare) adminQuery.request).cqlQuery).isEqualTo("mock query " + c);
        adminQuery.resultFuture.complete(null);
    }
    assertThatStage(done).isSuccess(v -> assertThat(reprepareOnUp.queries).isEmpty());
}
Also used : Query(com.datastax.oss.protocol.internal.request.Query) Prepare(com.datastax.oss.protocol.internal.request.Prepare) AdminResult(com.datastax.oss.driver.internal.core.adminrequest.AdminResult) Test(org.junit.Test)

Example 3 with Prepare

use of com.datastax.oss.protocol.internal.request.Prepare in project java-driver by datastax.

the class ReprepareOnUpTest method should_limit_number_of_statements_reprepared_in_parallel.

@Test
public void should_limit_number_of_statements_reprepared_in_parallel() {
    when(defaultProfile.getInt(DefaultDriverOption.REPREPARE_MAX_PARALLELISM)).thenReturn(3);
    MockReprepareOnUp reprepareOnUp = new MockReprepareOnUp("test", pool, ImmediateEventExecutor.INSTANCE, getMockPayloads('a', 'b', 'c', 'd', 'e', 'f'), context, whenPrepared);
    reprepareOnUp.start();
    MockAdminQuery adminQuery = reprepareOnUp.queries.poll();
    assertThat(adminQuery).isNotNull();
    assertThat(adminQuery.request).isInstanceOf(Query.class);
    assertThat(((Query) adminQuery.request).query).isEqualTo("SELECT prepared_id FROM system.prepared_statements");
    // server knows no ids => will reprepare all 6:
    adminQuery.resultFuture.complete(new AdminResult(preparedIdRows(), null, DefaultProtocolVersion.DEFAULT));
    // 3 statements have enqueued, we've not completed the queries yet so no more should be sent:
    assertThat(reprepareOnUp.queries.size()).isEqualTo(3);
    // As we complete each statement, another one should enqueue:
    for (char c = 'a'; c <= 'c'; c++) {
        adminQuery = reprepareOnUp.queries.poll();
        assertThat(adminQuery).isNotNull();
        assertThat(adminQuery.request).isInstanceOf(Prepare.class);
        assertThat(((Prepare) adminQuery.request).cqlQuery).isEqualTo("mock query " + c);
        adminQuery.resultFuture.complete(null);
        assertThat(reprepareOnUp.queries.size()).isEqualTo(3);
    }
    // Complete the last 3:
    for (char c = 'd'; c <= 'f'; c++) {
        adminQuery = reprepareOnUp.queries.poll();
        assertThat(adminQuery).isNotNull();
        assertThat(adminQuery.request).isInstanceOf(Prepare.class);
        assertThat(((Prepare) adminQuery.request).cqlQuery).isEqualTo("mock query " + c);
        adminQuery.resultFuture.complete(null);
    }
    assertThatStage(done).isSuccess(v -> assertThat(reprepareOnUp.queries).isEmpty());
}
Also used : Query(com.datastax.oss.protocol.internal.request.Query) Prepare(com.datastax.oss.protocol.internal.request.Prepare) AdminResult(com.datastax.oss.driver.internal.core.adminrequest.AdminResult) Test(org.junit.Test)

Example 4 with Prepare

use of com.datastax.oss.protocol.internal.request.Prepare in project java-driver by datastax.

the class CqlPrepareHandler method toPrepareMessage.

@NonNull
private Prepare toPrepareMessage(PrepareRequest request) {
    ProtocolVersion protocolVersion = context.getProtocolVersion();
    ProtocolVersionRegistry registry = context.getProtocolVersionRegistry();
    CqlIdentifier keyspace = request.getKeyspace();
    if (keyspace != null && !registry.supports(protocolVersion, DefaultProtocolFeature.PER_REQUEST_KEYSPACE)) {
        throw new IllegalArgumentException("Can't use per-request keyspace with protocol " + protocolVersion);
    }
    return new Prepare(request.getQuery(), (keyspace == null) ? null : keyspace.asInternal());
}
Also used : Prepare(com.datastax.oss.protocol.internal.request.Prepare) ProtocolVersion(com.datastax.oss.driver.api.core.ProtocolVersion) ProtocolVersionRegistry(com.datastax.oss.driver.internal.core.ProtocolVersionRegistry) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) NonNull(edu.umd.cs.findbugs.annotations.NonNull)

Example 5 with Prepare

use of com.datastax.oss.protocol.internal.request.Prepare in project java-driver by datastax.

the class ReprepareOnUpTest method should_limit_number_of_statements_to_reprepare.

@Test
public void should_limit_number_of_statements_to_reprepare() {
    when(defaultProfile.getInt(DefaultDriverOption.REPREPARE_MAX_STATEMENTS)).thenReturn(3);
    MockReprepareOnUp reprepareOnUp = new MockReprepareOnUp("test", pool, ImmediateEventExecutor.INSTANCE, getMockPayloads('a', 'b', 'c', 'd', 'e', 'f'), context, whenPrepared);
    reprepareOnUp.start();
    MockAdminQuery adminQuery = reprepareOnUp.queries.poll();
    assertThat(adminQuery).isNotNull();
    assertThat(adminQuery.request).isInstanceOf(Query.class);
    assertThat(((Query) adminQuery.request).query).isEqualTo("SELECT prepared_id FROM system.prepared_statements");
    // server knows no ids:
    adminQuery.resultFuture.complete(new AdminResult(preparedIdRows(), null, DefaultProtocolVersion.DEFAULT));
    for (char c = 'a'; c <= 'c'; c++) {
        adminQuery = reprepareOnUp.queries.poll();
        assertThat(adminQuery).isNotNull();
        assertThat(adminQuery.request).isInstanceOf(Prepare.class);
        assertThat(((Prepare) adminQuery.request).cqlQuery).isEqualTo("mock query " + c);
        adminQuery.resultFuture.complete(null);
    }
    assertThatStage(done).isSuccess(v -> assertThat(reprepareOnUp.queries).isEmpty());
}
Also used : Query(com.datastax.oss.protocol.internal.request.Query) Prepare(com.datastax.oss.protocol.internal.request.Prepare) AdminResult(com.datastax.oss.driver.internal.core.adminrequest.AdminResult) Test(org.junit.Test)

Aggregations

Prepare (com.datastax.oss.protocol.internal.request.Prepare)6 AdminResult (com.datastax.oss.driver.internal.core.adminrequest.AdminResult)4 Query (com.datastax.oss.protocol.internal.request.Query)4 Test (org.junit.Test)4 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)1 NodeUnavailableException (com.datastax.oss.driver.api.core.NodeUnavailableException)1 ProtocolVersion (com.datastax.oss.driver.api.core.ProtocolVersion)1 ProtocolVersionRegistry (com.datastax.oss.driver.internal.core.ProtocolVersionRegistry)1 DriverChannel (com.datastax.oss.driver.internal.core.channel.DriverChannel)1 NonNull (edu.umd.cs.findbugs.annotations.NonNull)1