Search in sources :

Example 1 with TokenMap

use of com.datastax.oss.driver.api.core.metadata.TokenMap in project java-driver by datastax.

the class AllLoadBalancingPoliciesSimulacronIT method replicasInDc.

private List<Node> replicasInDc(CqlSession session, String dcName) {
    assertThat(session.getMetadata().getTokenMap()).isPresent();
    TokenMap tokenMap = session.getMetadata().getTokenMap().get();
    return tokenMap.getReplicas("test", ROUTING_KEY).stream().filter(n -> Objects.equals(n.getDatacenter(), dcName)).collect(Collectors.toList());
}
Also used : Node(com.datastax.oss.driver.api.core.metadata.Node) ClusterSpec(com.datastax.oss.simulacron.common.cluster.ClusterSpec) BoundCluster(com.datastax.oss.simulacron.server.BoundCluster) Arrays(java.util.Arrays) SocketAddress(java.net.SocketAddress) DefaultConsistencyLevel(com.datastax.oss.driver.api.core.DefaultConsistencyLevel) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TokenMap(com.datastax.oss.driver.api.core.metadata.TokenMap) RunWith(org.junit.runner.RunWith) SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) DataProviderRunner(com.tngtech.java.junit.dataprovider.DataProviderRunner) ParallelizableTests(com.datastax.oss.driver.categories.ParallelizableTests) ByteBuffer(java.nio.ByteBuffer) ImmutableMap(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMap) DefaultDriverOption(com.datastax.oss.driver.api.core.config.DefaultDriverOption) CqlSession(com.datastax.oss.driver.api.core.CqlSession) SimulacronRule(com.datastax.oss.driver.api.testinfra.simulacron.SimulacronRule) BoundTopic(com.datastax.oss.simulacron.server.BoundTopic) NoNodeAvailableException(com.datastax.oss.driver.api.core.NoNodeAvailableException) Duration(java.time.Duration) ConsistencyLevel(com.datastax.oss.driver.api.core.ConsistencyLevel) Assertions.catchThrowable(org.assertj.core.api.Assertions.catchThrowable) RowBuilder(com.datastax.oss.simulacron.common.stubbing.PrimeDsl.RowBuilder) ClassRule(org.junit.ClassRule) Before(org.junit.Before) Awaitility.await(org.awaitility.Awaitility.await) QueryLog(com.datastax.oss.simulacron.common.cluster.QueryLog) NodeState(com.datastax.oss.driver.api.core.metadata.NodeState) Predicate(java.util.function.Predicate) SessionUtils(com.datastax.oss.driver.api.testinfra.session.SessionUtils) Test(org.junit.Test) Category(org.junit.experimental.categories.Category) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) Query(com.datastax.oss.protocol.internal.request.Query) Stream(java.util.stream.Stream) DriverConfigLoader(com.datastax.oss.driver.api.core.config.DriverConfigLoader) PrimeDsl(com.datastax.oss.simulacron.common.stubbing.PrimeDsl) BoundNode(com.datastax.oss.simulacron.server.BoundNode) TokenMap(com.datastax.oss.driver.api.core.metadata.TokenMap)

Example 2 with TokenMap

use of com.datastax.oss.driver.api.core.metadata.TokenMap in project java-driver by datastax.

the class DefaultLoadBalancingPolicyIT method should_use_round_robin_on_local_dc_when_not_enough_routing_information.

@Test
public void should_use_round_robin_on_local_dc_when_not_enough_routing_information() {
    ByteBuffer routingKey = TypeCodecs.INT.encodePrimitive(1, ProtocolVersion.DEFAULT);
    TokenMap tokenMap = SESSION_RULE.session().getMetadata().getTokenMap().get();
    // TODO add statements with setKeyspace when that is supported
    List<Statement> statements = ImmutableList.of(// No information at all
    SimpleStatement.newInstance("SELECT * FROM test.foo WHERE k = 1"), // Keyspace present, missing routing key
    SimpleStatement.newInstance("SELECT * FROM test.foo WHERE k = 1").setRoutingKeyspace(CqlIdentifier.fromCql("test")), // Routing key present, missing keyspace
    SimpleStatement.newInstance("SELECT * FROM test.foo WHERE k = 1").setRoutingKey(routingKey), // Routing token present, missing keyspace
    SimpleStatement.newInstance("SELECT * FROM test.foo WHERE k = 1").setRoutingToken(tokenMap.newToken(routingKey)));
    for (Statement statement : statements) {
        List<Node> coordinators = new ArrayList<>();
        for (int i = 0; i < 12; i++) {
            ResultSet rs = SESSION_RULE.session().execute(statement);
            Node coordinator = rs.getExecutionInfo().getCoordinator();
            assertThat(coordinator.getDatacenter()).isEqualTo(LOCAL_DC);
            coordinators.add(coordinator);
        }
        for (int i = 0; i < 4; i++) {
            assertThat(coordinators.get(i)).isEqualTo(coordinators.get(4 + i)).isEqualTo(coordinators.get(8 + i));
        }
    }
}
Also used : SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) Statement(com.datastax.oss.driver.api.core.cql.Statement) Node(com.datastax.oss.driver.api.core.metadata.Node) ArrayList(java.util.ArrayList) ResultSet(com.datastax.oss.driver.api.core.cql.ResultSet) TokenMap(com.datastax.oss.driver.api.core.metadata.TokenMap) ByteBuffer(java.nio.ByteBuffer) EndPoint(com.datastax.oss.driver.api.core.metadata.EndPoint) Test(org.junit.Test)

Example 3 with TokenMap

use of com.datastax.oss.driver.api.core.metadata.TokenMap in project java-driver by datastax.

the class DefaultLoadBalancingPolicyIT method should_hit_non_replicas_when_routing_information_present_but_all_replicas_down.

@Test
public void should_hit_non_replicas_when_routing_information_present_but_all_replicas_down() {
    CqlIdentifier keyspace = CqlIdentifier.fromCql("test");
    ByteBuffer routingKey = TypeCodecs.INT.encodePrimitive(1, ProtocolVersion.DEFAULT);
    TokenMap tokenMap = SESSION_RULE.session().getMetadata().getTokenMap().get();
    InternalDriverContext context = (InternalDriverContext) SESSION_RULE.session().getContext();
    Set<Node> localReplicas = new HashSet<>();
    for (Node replica : tokenMap.getReplicas(keyspace, routingKey)) {
        if (replica.getDatacenter().equals(LOCAL_DC)) {
            localReplicas.add(replica);
            context.getEventBus().fire(TopologyEvent.forceDown(replica.getBroadcastRpcAddress().get()));
            await().pollInterval(500, TimeUnit.MILLISECONDS).atMost(60, TimeUnit.SECONDS).untilAsserted(() -> assertThat(replica.getOpenConnections()).isZero());
        }
    }
    assertThat(localReplicas).hasSize(2);
    // TODO add statements with setKeyspace when that is supported
    List<Statement> statements = ImmutableList.of(SimpleStatement.newInstance("SELECT * FROM test.foo WHERE k = 1").setRoutingKeyspace(keyspace).setRoutingKey(routingKey), SimpleStatement.newInstance("SELECT * FROM test.foo WHERE k = 1").setRoutingKeyspace(keyspace).setRoutingToken(tokenMap.newToken(routingKey)));
    for (Statement statement : statements) {
        List<Node> coordinators = new ArrayList<>();
        for (int i = 0; i < 6; i++) {
            ResultSet rs = SESSION_RULE.session().execute(statement);
            Node coordinator = rs.getExecutionInfo().getCoordinator();
            coordinators.add(coordinator);
            assertThat(coordinator.getDatacenter()).isEqualTo(LOCAL_DC);
            assertThat(localReplicas).doesNotContain(coordinator);
        }
        // Should round-robin on the two non-replicas
        for (int i = 0; i < 2; i++) {
            assertThat(coordinators.get(i)).isEqualTo(coordinators.get(2 + i)).isEqualTo(coordinators.get(4 + i));
        }
    }
    for (Node replica : localReplicas) {
        context.getEventBus().fire(TopologyEvent.forceUp(replica.getBroadcastRpcAddress().get()));
        await().pollInterval(500, TimeUnit.MILLISECONDS).atMost(60, TimeUnit.SECONDS).untilAsserted(() -> assertThat(replica.getOpenConnections()).isPositive());
    }
}
Also used : SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) Statement(com.datastax.oss.driver.api.core.cql.Statement) Node(com.datastax.oss.driver.api.core.metadata.Node) ArrayList(java.util.ArrayList) TokenMap(com.datastax.oss.driver.api.core.metadata.TokenMap) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) ByteBuffer(java.nio.ByteBuffer) EndPoint(com.datastax.oss.driver.api.core.metadata.EndPoint) ResultSet(com.datastax.oss.driver.api.core.cql.ResultSet) InternalDriverContext(com.datastax.oss.driver.internal.core.context.InternalDriverContext) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 4 with TokenMap

use of com.datastax.oss.driver.api.core.metadata.TokenMap in project java-driver by datastax.

the class TokenITBase method should_be_consistent_with_range_queries.

/**
 * Validates that the token metadata is consistent with server-side range queries. That is,
 * querying the data in a range does return a PK that the driver thinks is in that range.
 *
 * @test_category metadata:token
 * @expected_result token ranges are exposed and usable.
 * @jira_ticket JAVA-312
 * @since 2.0.10, 2.1.5
 */
@Test
public void should_be_consistent_with_range_queries() {
    TokenMap tokenMap = getTokenMap();
    // Find the replica for a given partition key of ks1.foo.
    int key = 1;
    ProtocolVersion protocolVersion = session().getContext().getProtocolVersion();
    ByteBuffer serializedKey = TypeCodecs.INT.encodePrimitive(key, protocolVersion);
    assertThat(serializedKey).isNotNull();
    Set<Node> replicas = tokenMap.getReplicas(KS1, serializedKey);
    assertThat(replicas).hasSize(1);
    Node replica = replicas.iterator().next();
    // Iterate the cluster's token ranges. For each one, use a range query to get all the keys of
    // ks1.foo that are in this range.
    PreparedStatement rangeStatement = session().prepare("SELECT i FROM foo WHERE token(i) > ? and token(i) <= ?");
    TokenRange foundRange = null;
    for (TokenRange range : tokenMap.getTokenRanges()) {
        List<Row> rows = rangeQuery(rangeStatement, range);
        for (Row row : rows) {
            if (row.getInt("i") == key) {
                // We should find our initial key exactly once
                assertThat(foundRange).describedAs("found the same key in two ranges: " + foundRange + " and " + range).isNull();
                foundRange = range;
                // That range should be managed by the replica
                assertThat(tokenMap.getReplicas(KS1, range)).contains(replica);
            }
        }
    }
    assertThat(foundRange).isNotNull();
}
Also used : Node(com.datastax.oss.driver.api.core.metadata.Node) PreparedStatement(com.datastax.oss.driver.api.core.cql.PreparedStatement) TokenRange(com.datastax.oss.driver.api.core.metadata.token.TokenRange) Row(com.datastax.oss.driver.api.core.cql.Row) TokenMap(com.datastax.oss.driver.api.core.metadata.TokenMap) ProtocolVersion(com.datastax.oss.driver.api.core.ProtocolVersion) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 5 with TokenMap

use of com.datastax.oss.driver.api.core.metadata.TokenMap in project java-driver by datastax.

the class TokenITBase method should_have_only_one_wrapped_range.

/**
 * Ensures that for there is at most one wrapped range in the ring, and check that unwrapping it
 * produces two ranges.
 *
 * @test_category metadata:token
 * @expected_result there is at most one wrapped range.
 * @jira_ticket JAVA-312
 * @since 2.0.10, 2.1.5
 */
@Test
public void should_have_only_one_wrapped_range() {
    TokenMap tokenMap = getTokenMap();
    TokenRange wrappedRange = null;
    for (TokenRange range : tokenMap.getTokenRanges()) {
        if (range.isWrappedAround()) {
            assertThat(wrappedRange).as("Found a wrapped around TokenRange (%s) when one already exists (%s).", range, wrappedRange).isNull();
            wrappedRange = range;
            assertThat(wrappedRange.unwrap()).hasSize(2);
        }
    }
}
Also used : TokenRange(com.datastax.oss.driver.api.core.metadata.token.TokenRange) TokenMap(com.datastax.oss.driver.api.core.metadata.TokenMap) Test(org.junit.Test)

Aggregations

TokenMap (com.datastax.oss.driver.api.core.metadata.TokenMap)12 Test (org.junit.Test)9 ByteBuffer (java.nio.ByteBuffer)7 Node (com.datastax.oss.driver.api.core.metadata.Node)6 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)4 SimpleStatement (com.datastax.oss.driver.api.core.cql.SimpleStatement)4 TokenRange (com.datastax.oss.driver.api.core.metadata.token.TokenRange)4 ResultSet (com.datastax.oss.driver.api.core.cql.ResultSet)3 Statement (com.datastax.oss.driver.api.core.cql.Statement)3 EndPoint (com.datastax.oss.driver.api.core.metadata.EndPoint)3 Token (com.datastax.oss.driver.api.core.metadata.token.Token)3 CqlSession (com.datastax.oss.driver.api.core.CqlSession)2 ProtocolVersion (com.datastax.oss.driver.api.core.ProtocolVersion)2 DriverConfigLoader (com.datastax.oss.driver.api.core.config.DriverConfigLoader)2 Row (com.datastax.oss.driver.api.core.cql.Row)2 ArrayList (java.util.ArrayList)2 ConsistencyLevel (com.datastax.oss.driver.api.core.ConsistencyLevel)1 DefaultConsistencyLevel (com.datastax.oss.driver.api.core.DefaultConsistencyLevel)1 NoNodeAvailableException (com.datastax.oss.driver.api.core.NoNodeAvailableException)1 DefaultDriverOption (com.datastax.oss.driver.api.core.config.DefaultDriverOption)1