Search in sources :

Example 6 with AdminResult

use of com.datastax.oss.driver.internal.core.adminrequest.AdminResult in project java-driver by datastax.

the class Cassandra3SchemaQueriesTest method should_ignore_malformed_rows.

@Test
public void should_ignore_malformed_rows() {
    SchemaQueriesWithMockedChannel queries = new SchemaQueriesWithMockedChannel(driverChannel, node, config, "test");
    CompletionStage<SchemaRows> result = queries.execute();
    // Keyspace
    Call call = queries.calls.poll();
    assertThat(call.query).isEqualTo("SELECT * FROM system_schema.keyspaces");
    call.result.complete(mockResult(mockRow("keyspace_name", "ks1")));
    // No types
    call = queries.calls.poll();
    assertThat(call.query).isEqualTo("SELECT * FROM system_schema.types");
    call.result.complete(mockResult());
    // Tables
    call = queries.calls.poll();
    assertThat(call.query).isEqualTo("SELECT * FROM system_schema.tables");
    call.result.complete(mockResult(mockRow("keyspace_name", "ks", "table_name", "foo"), // Missing keyspace name:
    mockRow("table_name", "foo")));
    // Columns
    call = queries.calls.poll();
    call.result.complete(mockResult(mockRow("keyspace_name", "ks", "table_name", "foo", "column_name", "k"), // Missing keyspace name:
    mockRow("table_name", "foo", "column_name", "k"), // Missing table name:
    mockRow("keyspace_name", "ks", "column_name", "k")));
    AdminResult page2 = mockResult(mockRow("keyspace_name", "ks1", "table_name", "foo", "column_name", "v"));
    AdminResult page1 = mockResult(page2, mockRow("keyspace_name", "ks1", "table_name", "foo", "column_name", "k"));
    call.result.complete(page1);
    // No indexes
    call = queries.calls.poll();
    assertThat(call.query).isEqualTo("SELECT * FROM system_schema.indexes");
    call.result.complete(mockResult());
    // No views
    call = queries.calls.poll();
    assertThat(call.query).isEqualTo("SELECT * FROM system_schema.views");
    call.result.complete(mockResult());
    // No functions
    call = queries.calls.poll();
    assertThat(call.query).isEqualTo("SELECT * FROM system_schema.functions");
    call.result.complete(mockResult());
    // No aggregates
    call = queries.calls.poll();
    assertThat(call.query).isEqualTo("SELECT * FROM system_schema.aggregates");
    call.result.complete(mockResult());
    channel.runPendingTasks();
    assertThatStage(result).isSuccess(rows -> {
        assertThat(rows.tables().keySet()).containsOnly(KS_ID);
        assertThat(rows.tables().get(KS_ID)).hasSize(1);
        assertThat(rows.tables().get(KS_ID).iterator().next().getString("table_name")).isEqualTo("foo");
        assertThat(rows.columns().keySet()).containsOnly(KS_ID);
        assertThat(rows.columns().get(KS_ID).keySet()).containsOnly(FOO_ID);
        assertThat(rows.columns().get(KS_ID).get(FOO_ID).iterator().next().getString("column_name")).isEqualTo("k");
    });
}
Also used : AdminResult(com.datastax.oss.driver.internal.core.adminrequest.AdminResult) Test(org.junit.Test)

Example 7 with AdminResult

use of com.datastax.oss.driver.internal.core.adminrequest.AdminResult in project java-driver by datastax.

the class DefaultTopologyMonitor method refreshNode.

@Override
public CompletionStage<Optional<NodeInfo>> refreshNode(Node node) {
    if (closeFuture.isDone()) {
        return CompletableFutures.failedFuture(new IllegalStateException("closed"));
    }
    LOG.debug("[{}] Refreshing info for {}", logPrefix, node);
    DriverChannel channel = controlConnection.channel();
    EndPoint localEndPoint = channel.getEndPoint();
    if (node.getEndPoint().equals(channel.getEndPoint())) {
        // refreshNode is called for nodes that just came up. If the control node just came up, it
        // means the control connection just reconnected, which means we did a full node refresh. So
        // we don't need to process this call.
        LOG.debug("[{}] Ignoring refresh of control node", logPrefix);
        return CompletableFuture.completedFuture(Optional.empty());
    } else if (node.getBroadcastAddress().isPresent()) {
        CompletionStage<AdminResult> query;
        if (isSchemaV2) {
            query = query(channel, "SELECT * FROM " + getPeerTableName() + " WHERE peer = :address and peer_port = :port", ImmutableMap.of("address", node.getBroadcastAddress().get().getAddress(), "port", node.getBroadcastAddress().get().getPort()));
        } else {
            query = query(channel, "SELECT * FROM " + getPeerTableName() + " WHERE peer = :address", ImmutableMap.of("address", node.getBroadcastAddress().get().getAddress()));
        }
        return query.thenApply(result -> firstPeerRowAsNodeInfo(result, localEndPoint));
    } else {
        return query(channel, "SELECT * FROM " + getPeerTableName()).thenApply(result -> findInPeers(result, node.getHostId(), localEndPoint));
    }
}
Also used : Node(com.datastax.oss.driver.api.core.metadata.Node) SocketAddress(java.net.SocketAddress) ImmutableSet(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableSet) LoggerFactory(org.slf4j.LoggerFactory) AdminRow(com.datastax.oss.driver.internal.core.adminrequest.AdminRow) ThreadSafe(net.jcip.annotations.ThreadSafe) InternalDriverContext(com.datastax.oss.driver.internal.core.context.InternalDriverContext) CompletableFuture(java.util.concurrent.CompletableFuture) ControlConnection(com.datastax.oss.driver.internal.core.control.ControlConnection) AdminResult(com.datastax.oss.driver.internal.core.adminrequest.AdminResult) ImmutableMap(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMap) ArrayList(java.util.ArrayList) InetAddress(java.net.InetAddress) Version(com.datastax.oss.driver.api.core.Version) UnexpectedResponseException(com.datastax.oss.driver.internal.core.adminrequest.UnexpectedResponseException) DefaultDriverOption(com.datastax.oss.driver.api.core.config.DefaultDriverOption) Error(com.datastax.oss.protocol.internal.response.Error) AdminRequestHandler(com.datastax.oss.driver.internal.core.adminrequest.AdminRequestHandler) NonNull(edu.umd.cs.findbugs.annotations.NonNull) Duration(java.time.Duration) Map(java.util.Map) DriverExecutionProfile(com.datastax.oss.driver.api.core.config.DriverExecutionProfile) VisibleForTesting(com.datastax.oss.driver.shaded.guava.common.annotations.VisibleForTesting) CompletableFutures(com.datastax.oss.driver.internal.core.util.concurrent.CompletableFutures) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) DseNodeProperties(com.datastax.dse.driver.api.core.metadata.DseNodeProperties) Set(java.util.Set) UUID(java.util.UUID) InetSocketAddress(java.net.InetSocketAddress) ProtocolConstants(com.datastax.oss.protocol.internal.ProtocolConstants) Objects(java.util.Objects) DriverChannel(com.datastax.oss.driver.internal.core.channel.DriverChannel) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) EndPoint(com.datastax.oss.driver.api.core.metadata.EndPoint) Nullable(edu.umd.cs.findbugs.annotations.Nullable) Optional(java.util.Optional) Collections(java.util.Collections) DriverChannel(com.datastax.oss.driver.internal.core.channel.DriverChannel) EndPoint(com.datastax.oss.driver.api.core.metadata.EndPoint) CompletionStage(java.util.concurrent.CompletionStage)

Example 8 with AdminResult

use of com.datastax.oss.driver.internal.core.adminrequest.AdminResult in project java-driver by datastax.

the class Cassandra3SchemaQueriesTest method should_query_with_paging.

@Test
public void should_query_with_paging() {
    SchemaQueriesWithMockedChannel queries = new SchemaQueriesWithMockedChannel(driverChannel, node, config, "test");
    CompletionStage<SchemaRows> result = queries.execute();
    // Keyspace
    Call call = queries.calls.poll();
    assertThat(call.query).isEqualTo("SELECT * FROM system_schema.keyspaces");
    call.result.complete(mockResult(mockRow("keyspace_name", "ks1")));
    // No types
    call = queries.calls.poll();
    assertThat(call.query).isEqualTo("SELECT * FROM system_schema.types");
    call.result.complete(mockResult());
    // Tables
    call = queries.calls.poll();
    assertThat(call.query).isEqualTo("SELECT * FROM system_schema.tables");
    call.result.complete(mockResult(mockRow("keyspace_name", "ks1", "table_name", "foo")));
    // Columns: paged
    call = queries.calls.poll();
    assertThat(call.query).isEqualTo("SELECT * FROM system_schema.columns");
    AdminResult page2 = mockResult(mockRow("keyspace_name", "ks1", "table_name", "foo", "column_name", "v"));
    AdminResult page1 = mockResult(page2, mockRow("keyspace_name", "ks1", "table_name", "foo", "column_name", "k"));
    call.result.complete(page1);
    // No indexes
    call = queries.calls.poll();
    assertThat(call.query).isEqualTo("SELECT * FROM system_schema.indexes");
    call.result.complete(mockResult());
    // No views
    call = queries.calls.poll();
    assertThat(call.query).isEqualTo("SELECT * FROM system_schema.views");
    call.result.complete(mockResult());
    // No functions
    call = queries.calls.poll();
    assertThat(call.query).isEqualTo("SELECT * FROM system_schema.functions");
    call.result.complete(mockResult());
    // No aggregates
    call = queries.calls.poll();
    assertThat(call.query).isEqualTo("SELECT * FROM system_schema.aggregates");
    call.result.complete(mockResult());
    channel.runPendingTasks();
    assertThatStage(result).isSuccess(rows -> {
        assertThat(rows.columns().keySet()).containsOnly(KS1_ID);
        assertThat(rows.columns().get(KS1_ID).keySet()).containsOnly(FOO_ID);
        assertThat(rows.columns().get(KS1_ID).get(FOO_ID)).extracting(r -> r.getString("column_name")).containsExactly("k", "v");
    });
}
Also used : Node(com.datastax.oss.driver.api.core.metadata.Node) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) AdminResult(com.datastax.oss.driver.internal.core.adminrequest.AdminResult) ImmutableList(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList) Version(com.datastax.oss.driver.api.core.Version) DriverChannel(com.datastax.oss.driver.internal.core.channel.DriverChannel) CompletionStage(java.util.concurrent.CompletionStage) DefaultDriverOption(com.datastax.oss.driver.api.core.config.DefaultDriverOption) DriverExecutionProfile(com.datastax.oss.driver.api.core.config.DriverExecutionProfile) LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) Assertions.assertThat(com.datastax.oss.driver.Assertions.assertThat) Queue(java.util.Queue) Collections(java.util.Collections) Assertions.assertThatStage(com.datastax.oss.driver.Assertions.assertThatStage) Before(org.junit.Before) AdminResult(com.datastax.oss.driver.internal.core.adminrequest.AdminResult) Test(org.junit.Test)

Example 9 with AdminResult

use of com.datastax.oss.driver.internal.core.adminrequest.AdminResult in project java-driver by datastax.

the class SchemaQueriesTest method mockResult.

protected static AdminResult mockResult(AdminResult next, AdminRow... rows) {
    AdminResult result = mock(AdminResult.class);
    if (next == null) {
        when(result.hasNextPage()).thenReturn(false);
    } else {
        when(result.hasNextPage()).thenReturn(true);
        when(result.nextPage()).thenReturn(CompletableFuture.completedFuture(next));
    }
    when(result.iterator()).thenReturn(Iterators.forArray(rows));
    return result;
}
Also used : AdminResult(com.datastax.oss.driver.internal.core.adminrequest.AdminResult)

Example 10 with AdminResult

use of com.datastax.oss.driver.internal.core.adminrequest.AdminResult 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

AdminResult (com.datastax.oss.driver.internal.core.adminrequest.AdminResult)11 Test (org.junit.Test)6 Prepare (com.datastax.oss.protocol.internal.request.Prepare)4 Query (com.datastax.oss.protocol.internal.request.Query)4 DriverChannel (com.datastax.oss.driver.internal.core.channel.DriverChannel)3 Version (com.datastax.oss.driver.api.core.Version)2 DefaultDriverOption (com.datastax.oss.driver.api.core.config.DefaultDriverOption)2 DriverExecutionProfile (com.datastax.oss.driver.api.core.config.DriverExecutionProfile)2 EndPoint (com.datastax.oss.driver.api.core.metadata.EndPoint)2 Node (com.datastax.oss.driver.api.core.metadata.Node)2 AdminRow (com.datastax.oss.driver.internal.core.adminrequest.AdminRow)2 UnexpectedResponseException (com.datastax.oss.driver.internal.core.adminrequest.UnexpectedResponseException)2 Error (com.datastax.oss.protocol.internal.response.Error)2 InetSocketAddress (java.net.InetSocketAddress)2 ArrayList (java.util.ArrayList)2 Collections (java.util.Collections)2 CompletionStage (java.util.concurrent.CompletionStage)2 DseNodeProperties (com.datastax.dse.driver.api.core.metadata.DseNodeProperties)1 Assertions.assertThat (com.datastax.oss.driver.Assertions.assertThat)1 Assertions.assertThatStage (com.datastax.oss.driver.Assertions.assertThatStage)1