Search in sources :

Example 51 with Partitions

use of io.lettuce.core.cluster.models.partitions.Partitions in project lettuce-core by lettuce-io.

the class RedisClusterClientIntegrationTests method reloadPartitionsWithDynamicSourcesFallsBackToInitialSeedNodes.

@Test
void reloadPartitionsWithDynamicSourcesFallsBackToInitialSeedNodes() {
    client.setOptions(ClusterClientOptions.builder().topologyRefreshOptions(ClusterTopologyRefreshOptions.builder().dynamicRefreshSources(true).build()).build());
    Partitions partitions = clusterClient.getPartitions();
    partitions.clear();
    partitions.add(new RedisClusterNode(RedisURI.create("localhost", 1), "foo", false, null, 0, 0, 0, Collections.emptyList(), Collections.emptySet()));
    Partitions reloaded = clusterClient.loadPartitions();
    assertThat(reloaded).hasSize(4);
}
Also used : Partitions(io.lettuce.core.cluster.models.partitions.Partitions) RedisClusterNode(io.lettuce.core.cluster.models.partitions.RedisClusterNode) Test(org.junit.jupiter.api.Test)

Example 52 with Partitions

use of io.lettuce.core.cluster.models.partitions.Partitions in project lettuce-core by lettuce-io.

the class RedisClusterClientIntegrationTests method testClusterRedirectionLimit.

@Test
@SuppressWarnings({ "rawtypes" })
void testClusterRedirectionLimit() throws Exception {
    clusterClient.setOptions(ClusterClientOptions.builder().maxRedirects(0).build());
    RedisAdvancedClusterAsyncCommands<String, String> connection = clusterClient.connect().async();
    Partitions partitions = clusterClient.getPartitions();
    for (RedisClusterNode partition : partitions) {
        if (partition.getSlots().contains(15495)) {
            partition.setSlots(Collections.emptyList());
        } else {
            partition.setSlots(IntStream.range(0, SlotHash.SLOT_COUNT).boxed().collect(Collectors.toList()));
        }
    }
    partitions.updateCache();
    // gets redirection to node 3
    RedisFuture<String> setA = connection.set(ClusterTestSettings.KEY_A, value);
    assertThat(setA instanceof AsyncCommand).isTrue();
    setA.await(10, TimeUnit.SECONDS);
    assertThat(setA.getError()).isEqualTo("MOVED 15495 127.0.0.1:7380");
    connection.getStatefulConnection().close();
}
Also used : Partitions(io.lettuce.core.cluster.models.partitions.Partitions) RedisClusterNode(io.lettuce.core.cluster.models.partitions.RedisClusterNode) AsyncCommand(io.lettuce.core.protocol.AsyncCommand) Test(org.junit.jupiter.api.Test)

Example 53 with Partitions

use of io.lettuce.core.cluster.models.partitions.Partitions in project lettuce-core by lettuce-io.

the class RedisClusterSetupTest method clusterSetSlots.

@Test
public void clusterSetSlots() {
    ClusterSetup.setup2Masters(clusterHelper);
    redis1.clusterSetSlotNode(6, getNodeId(redis2));
    waitForSlots(redis1, 11999);
    waitForSlots(redis2, 4384);
    Partitions partitions = ClusterPartitionParser.parse(redis1.clusterNodes());
    for (RedisClusterNode redisClusterNode : partitions.getPartitions()) {
        if (redisClusterNode.getFlags().contains(RedisClusterNode.NodeFlag.MYSELF)) {
            assertThat(redisClusterNode.getSlots()).contains(1, 2, 3, 4, 5).doesNotContain(6);
        }
    }
}
Also used : Partitions(io.lettuce.core.cluster.models.partitions.Partitions) RedisClusterNode(io.lettuce.core.cluster.models.partitions.RedisClusterNode) Test(org.junit.jupiter.api.Test)

Example 54 with Partitions

use of io.lettuce.core.cluster.models.partitions.Partitions in project lettuce-core by lettuce-io.

the class RedisClusterSetupTest method changeTopologyWhileOperations.

@Test
public void changeTopologyWhileOperations() throws Exception {
    ClusterSetup.setup2Masters(clusterHelper);
    ClusterTopologyRefreshOptions clusterTopologyRefreshOptions = ClusterTopologyRefreshOptions.builder().enableAllAdaptiveRefreshTriggers().build();
    clusterClient.setOptions(ClusterClientOptions.builder().topologyRefreshOptions(clusterTopologyRefreshOptions).build());
    StatefulRedisClusterConnection<String, String> connection = clusterClient.connect();
    RedisAdvancedClusterCommands<String, String> sync = connection.sync();
    RedisAdvancedClusterAsyncCommands<String, String> async = connection.async();
    Partitions partitions = connection.getPartitions();
    assertThat(partitions.getPartitionBySlot(0).getSlots().size()).isEqualTo(12000);
    assertThat(partitions.getPartitionBySlot(16380).getSlots().size()).isEqualTo(4384);
    assertRoutedExecution(async);
    sync.del("A");
    sync.del("t");
    sync.del("p");
    shiftAllSlotsToNode1();
    assertRoutedExecution(async);
    Wait.untilTrue(() -> {
        if (clusterClient.getPartitions().size() == 2) {
            for (RedisClusterNode redisClusterNode : clusterClient.getPartitions()) {
                if (redisClusterNode.getSlots().size() > 16380) {
                    return true;
                }
            }
        }
        return false;
    }).waitOrTimeout();
    assertThat(partitions.getPartitionBySlot(0).getSlots().size()).isEqualTo(16384);
    assertThat(sync.get("A")).isEqualTo("value");
    assertThat(sync.get("t")).isEqualTo("value");
    assertThat(sync.get("p")).isEqualTo("value");
    async.getStatefulConnection().close();
}
Also used : Partitions(io.lettuce.core.cluster.models.partitions.Partitions) RedisClusterNode(io.lettuce.core.cluster.models.partitions.RedisClusterNode) Test(org.junit.jupiter.api.Test)

Example 55 with Partitions

use of io.lettuce.core.cluster.models.partitions.Partitions in project lettuce-core by lettuce-io.

the class RedisClusterSetupTest method shiftAllSlotsToNode1.

private void shiftAllSlotsToNode1() {
    redis1.clusterDelSlots(createSlots(12000, 16384));
    redis2.clusterDelSlots(createSlots(12000, 16384));
    waitForSlots(redis2, 0);
    RedisClusterNode redis2Partition = getOwnPartition(redis2);
    Wait.untilTrue(new Supplier<Boolean>() {

        @Override
        public Boolean get() {
            Partitions partitions = ClusterPartitionParser.parse(redis1.clusterNodes());
            RedisClusterNode partition = partitions.getPartitionByNodeId(redis2Partition.getNodeId());
            if (!partition.getSlots().isEmpty()) {
                removeRemaining(partition);
            }
            return partition.getSlots().size() == 0;
        }

        private void removeRemaining(RedisClusterNode partition) {
            try {
                redis1.clusterDelSlots(toIntArray(partition.getSlots()));
            } catch (Exception o_O) {
            // ignore
            }
        }
    }).waitOrTimeout();
    redis1.clusterAddSlots(createSlots(12000, 16384));
    waitForSlots(redis1, 16384);
    Wait.untilTrue(clusterHelper::isStable).waitOrTimeout();
}
Also used : Partitions(io.lettuce.core.cluster.models.partitions.Partitions) RedisClusterNode(io.lettuce.core.cluster.models.partitions.RedisClusterNode) Supplier(java.util.function.Supplier) RedisException(io.lettuce.core.RedisException)

Aggregations

Partitions (io.lettuce.core.cluster.models.partitions.Partitions)69 Test (org.junit.jupiter.api.Test)50 RedisClusterNode (io.lettuce.core.cluster.models.partitions.RedisClusterNode)36 RedisURI (io.lettuce.core.RedisURI)20 ArrayList (java.util.ArrayList)13 StatefulRedisConnection (io.lettuce.core.api.StatefulRedisConnection)10 PartitionsConsensusTestSupport.createPartitions (io.lettuce.core.cluster.PartitionsConsensusTestSupport.createPartitions)9 RedisCodec (io.lettuce.core.codec.RedisCodec)9 CompletableFuture (java.util.concurrent.CompletableFuture)8 Exceptions (io.lettuce.core.internal.Exceptions)6 Futures (io.lettuce.core.internal.Futures)6 InternalLogger (io.netty.util.internal.logging.InternalLogger)6 InternalLoggerFactory (io.netty.util.internal.logging.InternalLoggerFactory)6 List (java.util.List)6 Function (java.util.function.Function)6 LettuceAssert (io.lettuce.core.internal.LettuceAssert)5 Collection (java.util.Collection)5 Iterator (java.util.Iterator)5 Map (java.util.Map)5 CompletionStage (java.util.concurrent.CompletionStage)5