Search in sources :

Example 1 with Message

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

the class BoundStatementSimulacronIT method should_use_consistencies_from_simple_statement.

@Test
public void should_use_consistencies_from_simple_statement() {
    try (CqlSession session = SessionUtils.newSession(SIMULACRON_RULE)) {
        SimpleStatement st = SimpleStatement.builder("SELECT * FROM test where k = ?").setConsistencyLevel(DefaultConsistencyLevel.TWO).setSerialConsistencyLevel(DefaultConsistencyLevel.LOCAL_SERIAL).build();
        PreparedStatement prepared = session.prepare(st);
        SIMULACRON_RULE.cluster().clearLogs();
        // since query is unprimed, we use a text value for bind parameter as this is
        // what simulacron expects for unprimed statements.
        session.execute(prepared.bind("0"));
        List<QueryLog> logs = SIMULACRON_RULE.cluster().getLogs().getQueryLogs();
        assertThat(logs).hasSize(1);
        QueryLog log = logs.get(0);
        Message message = log.getFrame().message;
        assertThat(message).isInstanceOf(Execute.class);
        Execute execute = (Execute) message;
        assertThat(execute.options.consistency).isEqualTo(DefaultConsistencyLevel.TWO.getProtocolCode());
        assertThat(execute.options.serialConsistency).isEqualTo(DefaultConsistencyLevel.LOCAL_SERIAL.getProtocolCode());
    }
}
Also used : Message(com.datastax.oss.protocol.internal.Message) Execute(com.datastax.oss.protocol.internal.request.Execute) SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) PreparedStatement(com.datastax.oss.driver.api.core.cql.PreparedStatement) QueryLog(com.datastax.oss.simulacron.common.cluster.QueryLog) CqlSession(com.datastax.oss.driver.api.core.CqlSession) Test(org.junit.Test)

Example 2 with Message

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

the class BoundStatementSimulacronIT method should_use_consistencies.

@Test
public void should_use_consistencies() {
    try (CqlSession session = SessionUtils.newSession(SIMULACRON_RULE)) {
        // set consistencies on simple statement, but they will be unused since
        // overridden by bound statement.
        SimpleStatement st = SimpleStatement.builder("SELECT * FROM test where k = ?").setConsistencyLevel(DefaultConsistencyLevel.TWO).setSerialConsistencyLevel(DefaultConsistencyLevel.LOCAL_SERIAL).build();
        PreparedStatement prepared = session.prepare(st);
        SIMULACRON_RULE.cluster().clearLogs();
        // since query is unprimed, we use a text value for bind parameter as this is
        // what simulacron expects for unprimed statements.
        session.execute(prepared.boundStatementBuilder("0").setConsistencyLevel(DefaultConsistencyLevel.THREE).setSerialConsistencyLevel(DefaultConsistencyLevel.SERIAL).build());
        List<QueryLog> logs = SIMULACRON_RULE.cluster().getLogs().getQueryLogs();
        assertThat(logs).hasSize(1);
        QueryLog log = logs.get(0);
        Message message = log.getFrame().message;
        assertThat(message).isInstanceOf(Execute.class);
        Execute execute = (Execute) message;
        assertThat(execute.options.consistency).isEqualTo(DefaultConsistencyLevel.THREE.getProtocolCode());
        assertThat(execute.options.serialConsistency).isEqualTo(DefaultConsistencyLevel.SERIAL.getProtocolCode());
    }
}
Also used : Message(com.datastax.oss.protocol.internal.Message) Execute(com.datastax.oss.protocol.internal.request.Execute) SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) PreparedStatement(com.datastax.oss.driver.api.core.cql.PreparedStatement) QueryLog(com.datastax.oss.simulacron.common.cluster.QueryLog) CqlSession(com.datastax.oss.driver.api.core.CqlSession) Test(org.junit.Test)

Example 3 with Message

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

the class CqlRequestHandler method sendRequest.

/**
 * Sends the request to the next available node.
 *
 * @param statement The statement to execute.
 * @param retriedNode if not null, it will be attempted first before the rest of the query plan.
 * @param queryPlan the list of nodes to try (shared with all other executions)
 * @param currentExecutionIndex 0 for the initial execution, 1 for the first speculative one, etc.
 * @param retryCount the number of times that the retry policy was invoked for this execution
 *     already (note that some internal retries don't go through the policy, and therefore don't
 *     increment this counter)
 * @param scheduleNextExecution whether to schedule the next speculative execution
 */
private void sendRequest(Statement<?> statement, Node retriedNode, Queue<Node> queryPlan, int currentExecutionIndex, int retryCount, boolean scheduleNextExecution) {
    if (result.isDone()) {
        return;
    }
    Node node = retriedNode;
    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) {
        // We've reached the end of the query plan without finding any node to write to
        if (!result.isDone() && activeExecutionsCount.decrementAndGet() == 0) {
            // We're the last execution so fail the result
            setFinalError(statement, AllNodesFailedException.fromErrors(this.errors), null, -1);
        }
    } else {
        NodeResponseCallback nodeResponseCallback = new NodeResponseCallback(statement, node, queryPlan, channel, currentExecutionIndex, retryCount, scheduleNextExecution, logPrefix);
        DriverExecutionProfile executionProfile = Conversions.resolveExecutionProfile(statement, context);
        Message message = Conversions.toMessage(statement, executionProfile, context);
        channel.write(message, statement.isTracing(), statement.getCustomPayload(), nodeResponseCallback).addListener(nodeResponseCallback);
    }
}
Also used : DriverChannel(com.datastax.oss.driver.internal.core.channel.DriverChannel) Message(com.datastax.oss.protocol.internal.Message) DriverExecutionProfile(com.datastax.oss.driver.api.core.config.DriverExecutionProfile) DefaultNode(com.datastax.oss.driver.internal.core.metadata.DefaultNode) Node(com.datastax.oss.driver.api.core.metadata.Node) NodeUnavailableException(com.datastax.oss.driver.api.core.NodeUnavailableException)

Example 4 with Message

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

the class AdminRequestHandler method onResponse.

@Override
public void onResponse(Frame responseFrame) {
    if (timeoutFuture != null) {
        timeoutFuture.cancel(true);
    }
    Message message = responseFrame.message;
    LOG.debug("[{}] Got response {}", logPrefix, responseFrame.message);
    if (!expectedResponseType.isInstance(message)) {
        // Note that this also covers error responses, no need to get too fancy here
        setFinalError(new UnexpectedResponseException(debugString, message));
    } else if (expectedResponseType == Rows.class) {
        Rows rows = (Rows) message;
        ByteBuffer pagingState = rows.getMetadata().pagingState;
        AdminRequestHandler nextHandler = (pagingState == null) ? null : this.copy(pagingState);
        // The public factory methods guarantee that expectedResponseType and ResultT always match:
        @SuppressWarnings("unchecked") ResultT result = (ResultT) new AdminResult(rows, nextHandler, channel.protocolVersion());
        setFinalResult(result);
    } else if (expectedResponseType == Prepared.class) {
        Prepared prepared = (Prepared) message;
        @SuppressWarnings("unchecked") ResultT result = (ResultT) ByteBuffer.wrap(prepared.preparedQueryId);
        setFinalResult(result);
    } else if (expectedResponseType == com.datastax.oss.protocol.internal.response.result.Void.class) {
        setFinalResult(null);
    } else {
        setFinalError(new AssertionError("Unhandled response type" + expectedResponseType));
    }
}
Also used : Message(com.datastax.oss.protocol.internal.Message) Prepared(com.datastax.oss.protocol.internal.response.result.Prepared) ByteBuffer(java.nio.ByteBuffer) Rows(com.datastax.oss.protocol.internal.response.result.Rows)

Example 5 with Message

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

the class GraphRequestHandlerTest method should_create_query_message_from_batch_statement.

@Test
@UseDataProvider(location = DseTestDataProviders.class, value = "supportedGraphProtocols")
public void should_create_query_message_from_batch_statement(GraphProtocol graphProtocol) throws IOException {
    // initialization
    GraphRequestHandlerTestHarness harness = GraphRequestHandlerTestHarness.builder().build();
    @SuppressWarnings("rawtypes") List<GraphTraversal> traversalsTest = ImmutableList.of(// GraphDataTypesTest
    DseGraph.g.addV("person").property("p1", 2.3f).property("p2", LocalDateTime.now(ZoneOffset.UTC)), DseGraph.g.addV("software").property("p3", new BigInteger("123456789123456789123456789123456789")).property("p4", ImmutableList.of(Point.fromCoordinates(30.4, 25.63746284))));
    GraphStatement<?> graphStatement = BatchGraphStatement.builder().addTraversals(traversalsTest).build();
    GraphBinaryModule module = createGraphBinaryModule(harness.getContext());
    // when
    DriverExecutionProfile executionProfile = Conversions.resolveExecutionProfile(graphStatement, harness.getContext());
    Message m = GraphConversions.createMessageFromGraphStatement(graphStatement, graphProtocol, executionProfile, harness.getContext(), module);
    Map<String, ByteBuffer> createdCustomPayload = GraphConversions.createCustomPayload(graphStatement, graphProtocol, executionProfile, harness.getContext(), module);
    // checks
    assertThat(m).isInstanceOf(RawBytesQuery.class);
    testQueryRequestAndPayloadContents(((RawBytesQuery) m), createdCustomPayload, GraphConversions.bytecodeToSerialize(graphStatement), graphProtocol, module);
}
Also used : RawBytesQuery(com.datastax.dse.protocol.internal.request.RawBytesQuery) Message(com.datastax.oss.protocol.internal.Message) DriverExecutionProfile(com.datastax.oss.driver.api.core.config.DriverExecutionProfile) GraphTestUtils.createGraphBinaryModule(com.datastax.dse.driver.internal.core.graph.GraphTestUtils.createGraphBinaryModule) GraphBinaryModule(com.datastax.dse.driver.internal.core.graph.binary.GraphBinaryModule) ByteBuffer(java.nio.ByteBuffer) GraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal) BigInteger(java.math.BigInteger) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Aggregations

Message (com.datastax.oss.protocol.internal.Message)14 Test (org.junit.Test)8 DriverExecutionProfile (com.datastax.oss.driver.api.core.config.DriverExecutionProfile)7 GraphTestUtils.createGraphBinaryModule (com.datastax.dse.driver.internal.core.graph.GraphTestUtils.createGraphBinaryModule)5 GraphBinaryModule (com.datastax.dse.driver.internal.core.graph.binary.GraphBinaryModule)5 RawBytesQuery (com.datastax.dse.protocol.internal.request.RawBytesQuery)5 Execute (com.datastax.oss.protocol.internal.request.Execute)4 Query (com.datastax.oss.protocol.internal.request.Query)4 QueryLog (com.datastax.oss.simulacron.common.cluster.QueryLog)4 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)4 ByteBuffer (java.nio.ByteBuffer)4 SimpleStatement (com.datastax.oss.driver.api.core.cql.SimpleStatement)3 ScriptGraphStatement (com.datastax.dse.driver.api.core.graph.ScriptGraphStatement)2 CqlSession (com.datastax.oss.driver.api.core.CqlSession)2 NodeUnavailableException (com.datastax.oss.driver.api.core.NodeUnavailableException)2 PreparedStatement (com.datastax.oss.driver.api.core.cql.PreparedStatement)2 Node (com.datastax.oss.driver.api.core.metadata.Node)2 DriverChannel (com.datastax.oss.driver.internal.core.channel.DriverChannel)2 DefaultNode (com.datastax.oss.driver.internal.core.metadata.DefaultNode)2 GraphNode (com.datastax.dse.driver.api.core.graph.GraphNode)1