Search in sources :

Example 56 with Partitions

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

the class RedisClusterSetupTest method expireStaleHostAndPortConnections.

@Test
public void expireStaleHostAndPortConnections() throws Exception {
    clusterClient.setOptions(ClusterClientOptions.builder().build());
    RedisAdvancedClusterAsyncCommands<String, String> clusterConnection = clusterClient.connect().async();
    ClusterSetup.setup2Masters(clusterHelper);
    final PooledClusterConnectionProvider<String, String> clusterConnectionProvider = getPooledClusterConnectionProvider(clusterConnection);
    assertThat(clusterConnectionProvider.getConnectionCount()).isEqualTo(0);
    assertRoutedExecution(clusterConnection);
    assertThat(clusterConnectionProvider.getConnectionCount()).isEqualTo(2);
    for (RedisClusterNode redisClusterNode : clusterClient.getPartitions()) {
        clusterConnection.getConnection(redisClusterNode.getUri().getHost(), redisClusterNode.getUri().getPort());
        clusterConnection.getConnection(redisClusterNode.getNodeId());
    }
    assertThat(clusterConnectionProvider.getConnectionCount()).isEqualTo(4);
    Partitions partitions = ClusterPartitionParser.parse(redis1.clusterNodes());
    for (RedisClusterNode redisClusterNode : partitions.getPartitions()) {
        if (!redisClusterNode.getFlags().contains(RedisClusterNode.NodeFlag.MYSELF)) {
            redis1.clusterForget(redisClusterNode.getNodeId());
        }
    }
    partitions = ClusterPartitionParser.parse(redis2.clusterNodes());
    for (RedisClusterNode redisClusterNode : partitions.getPartitions()) {
        if (!redisClusterNode.getFlags().contains(RedisClusterNode.NodeFlag.MYSELF)) {
            redis2.clusterForget(redisClusterNode.getNodeId());
        }
    }
    clusterClient.reloadPartitions();
    Wait.untilEquals(1, () -> clusterClient.getPartitions().size()).waitOrTimeout();
    Wait.untilEquals(2L, () -> clusterConnectionProvider.getConnectionCount()).waitOrTimeout();
    clusterConnection.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 57 with Partitions

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

the class RedisClusterSetupTest method slotMigrationShouldUseAsking.

@Test
public void slotMigrationShouldUseAsking() {
    ClusterSetup.setup2Masters(clusterHelper);
    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);
    redis1.clusterSetSlotMigrating(3300, redis2.clusterMyId());
    redis2.clusterSetSlotImporting(3300, redis1.clusterMyId());
    assertThat(sync.get("b")).isNull();
    async.getStatefulConnection().close();
}
Also used : Partitions(io.lettuce.core.cluster.models.partitions.Partitions) Test(org.junit.jupiter.api.Test)

Example 58 with Partitions

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

the class ClusterTestHelper method isStable.

/**
 * @return true if the cluster state is {@code ok} and there are no failing nodes
 */
public boolean isStable() {
    for (RedisAsyncCommands<String, String> commands : connectionCache.values()) {
        try {
            RedisCommands<String, String> sync = commands.getStatefulConnection().sync();
            String info = sync.clusterInfo();
            if (info != null && info.contains("cluster_state:ok")) {
                String s = sync.clusterNodes();
                Partitions parse = ClusterPartitionParser.parse(s);
                for (RedisClusterNode redisClusterNode : parse) {
                    if (redisClusterNode.getFlags().contains(RedisClusterNode.NodeFlag.FAIL) || redisClusterNode.getFlags().contains(RedisClusterNode.NodeFlag.EVENTUAL_FAIL) || redisClusterNode.getFlags().contains(RedisClusterNode.NodeFlag.HANDSHAKE)) {
                        return false;
                    }
                }
            } else {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        // nothing to do
        }
    }
    return true;
}
Also used : Partitions(io.lettuce.core.cluster.models.partitions.Partitions) RedisClusterNode(io.lettuce.core.cluster.models.partitions.RedisClusterNode) TimeoutException(java.util.concurrent.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException)

Example 59 with Partitions

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

the class ClusterTopologyRefreshSchedulerUnitTests method shouldRetrievePartitionsViaAdaptiveRefreshTriggeredEvent.

@Test
void shouldRetrievePartitionsViaAdaptiveRefreshTriggeredEvent() {
    ClusterClientOptions clusterClientOptions = ClusterClientOptions.builder().topologyRefreshOptions(immediateRefresh).build();
    when(clusterClient.getClusterClientOptions()).thenReturn(clusterClientOptions);
    sut.onMovedRedirection();
    ArgumentCaptor<AdaptiveRefreshTriggeredEvent> captor = ArgumentCaptor.forClass(AdaptiveRefreshTriggeredEvent.class);
    verify(eventBus).publish(captor.capture());
    AdaptiveRefreshTriggeredEvent capture = captor.getValue();
    Partitions partitions = new Partitions();
    when(clusterClient.getPartitions()).thenReturn(partitions);
    assertThat(capture.getPartitions()).isSameAs(partitions);
}
Also used : Partitions(io.lettuce.core.cluster.models.partitions.Partitions) AdaptiveRefreshTriggeredEvent(io.lettuce.core.event.cluster.AdaptiveRefreshTriggeredEvent) Test(org.junit.jupiter.api.Test)

Example 60 with Partitions

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

the class HealthyMajorityPartitionsConsensusUnitTests method splitUnhealthyNodeViewShouldDecideForHealthyNodes.

@Test
void splitUnhealthyNodeViewShouldDecideForHealthyNodes() {
    Partitions partitions1 = createPartitions(node1, node2);
    Partitions partitions2 = createPartitions(node2, node3);
    Partitions partitions3 = createPartitions(node3, node4, node5);
    Map<RedisURI, Partitions> map = createMap(partitions1, partitions2, partitions3);
    node2.setFlags(Collections.singleton(RedisClusterNode.NodeFlag.FAIL));
    node3.setFlags(Collections.singleton(RedisClusterNode.NodeFlag.FAIL));
    node4.setFlags(Collections.singleton(RedisClusterNode.NodeFlag.FAIL));
    Partitions result = PartitionsConsensus.HEALTHY_MAJORITY.getPartitions(null, map);
    assertThat(Arrays.asList(partitions1, partitions3)).contains(result);
}
Also used : PartitionsConsensusTestSupport.createPartitions(io.lettuce.core.cluster.PartitionsConsensusTestSupport.createPartitions) Partitions(io.lettuce.core.cluster.models.partitions.Partitions) RedisURI(io.lettuce.core.RedisURI) Test(org.junit.jupiter.api.Test)

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