Search in sources :

Example 1 with RedisChannelHandler

use of io.lettuce.core.RedisChannelHandler in project lettuce-core by lettuce-io.

the class RedisClusterClientIntegrationTests method getButNoPartitionForSlothash.

@Test
void getButNoPartitionForSlothash() {
    for (RedisClusterNode redisClusterNode : clusterClient.getPartitions()) {
        redisClusterNode.setSlots(new ArrayList<>());
    }
    RedisChannelHandler rch = (RedisChannelHandler) connection;
    ClusterDistributionChannelWriter writer = (ClusterDistributionChannelWriter) rch.getChannelWriter();
    writer.setPartitions(clusterClient.getPartitions());
    clusterClient.getPartitions().reload(clusterClient.getPartitions().getPartitions());
    assertThatThrownBy(() -> sync.get(key)).isInstanceOf(RedisException.class);
}
Also used : RedisClusterNode(io.lettuce.core.cluster.models.partitions.RedisClusterNode) RedisChannelHandler(io.lettuce.core.RedisChannelHandler) Test(org.junit.jupiter.api.Test)

Example 2 with RedisChannelHandler

use of io.lettuce.core.RedisChannelHandler in project lettuce-core by lettuce-io.

the class ClusterDistributionChannelWriter method write.

@SuppressWarnings("unchecked")
@Override
public <K, V> Collection<RedisCommand<K, V, ?>> write(Collection<? extends RedisCommand<K, V, ?>> commands) {
    LettuceAssert.notNull(commands, "Commands must not be null");
    if (closed) {
        commands.forEach(it -> it.completeExceptionally(new RedisException("Connection is closed")));
        return (Collection<RedisCommand<K, V, ?>>) commands;
    }
    List<ClusterCommand<K, V, ?>> clusterCommands = new ArrayList<>(commands.size());
    List<ClusterCommand<K, V, ?>> defaultCommands = new ArrayList<>(commands.size());
    Map<SlotIntent, List<ClusterCommand<K, V, ?>>> partitions = new HashMap<>();
    // TODO: Retain order or retain Intent preference?
    // Currently: Retain order
    Intent intent = getIntent(commands);
    for (RedisCommand<K, V, ?> cmd : commands) {
        if (cmd instanceof ClusterCommand) {
            clusterCommands.add((ClusterCommand) cmd);
            continue;
        }
        CommandArgs<K, V> args = cmd.getArgs();
        ByteBuffer firstEncodedKey = args != null ? args.getFirstEncodedKey() : null;
        if (firstEncodedKey == null) {
            defaultCommands.add(new ClusterCommand<>(cmd, this, executionLimit));
            continue;
        }
        int hash = getSlot(args.getFirstEncodedKey());
        List<ClusterCommand<K, V, ?>> commandPartition = partitions.computeIfAbsent(SlotIntent.of(intent, hash), slotIntent -> new ArrayList<>());
        commandPartition.add(new ClusterCommand<>(cmd, this, executionLimit));
    }
    for (Map.Entry<SlotIntent, List<ClusterCommand<K, V, ?>>> entry : partitions.entrySet()) {
        SlotIntent slotIntent = entry.getKey();
        RedisChannelHandler<K, V> connection = (RedisChannelHandler<K, V>) clusterConnectionProvider.getConnection(slotIntent.intent, slotIntent.slotHash);
        RedisChannelWriter channelWriter = connection.getChannelWriter();
        if (channelWriter instanceof ClusterDistributionChannelWriter) {
            ClusterDistributionChannelWriter writer = (ClusterDistributionChannelWriter) channelWriter;
            channelWriter = writer.defaultWriter;
        }
        if (channelWriter != null && channelWriter != this && channelWriter != defaultWriter) {
            channelWriter.write(entry.getValue());
        }
    }
    clusterCommands.forEach(this::write);
    defaultCommands.forEach(defaultWriter::write);
    return (Collection) commands;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) RedisChannelHandler(io.lettuce.core.RedisChannelHandler) RedisException(io.lettuce.core.RedisException) ArrayList(java.util.ArrayList) List(java.util.List) RedisChannelWriter(io.lettuce.core.RedisChannelWriter) Intent(io.lettuce.core.cluster.ClusterConnectionProvider.Intent) ByteBuffer(java.nio.ByteBuffer) DefaultEndpoint(io.lettuce.core.protocol.DefaultEndpoint) Collection(java.util.Collection) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

RedisChannelHandler (io.lettuce.core.RedisChannelHandler)2 RedisChannelWriter (io.lettuce.core.RedisChannelWriter)1 RedisException (io.lettuce.core.RedisException)1 Intent (io.lettuce.core.cluster.ClusterConnectionProvider.Intent)1 RedisClusterNode (io.lettuce.core.cluster.models.partitions.RedisClusterNode)1 DefaultEndpoint (io.lettuce.core.protocol.DefaultEndpoint)1 ByteBuffer (java.nio.ByteBuffer)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Test (org.junit.jupiter.api.Test)1