Search in sources :

Example 11 with RedisURI

use of org.redisson.misc.RedisURI in project redisson by redisson.

the class ClusterConnectionManager method addMasterEntry.

private CompletableFuture<Void> addMasterEntry(ClusterPartition partition, ClusterServersConfig cfg) {
    CompletableFuture<Void> result = new CompletableFuture<>();
    if (partition.isMasterFail()) {
        RedisException e = new RedisException("Failed to add master: " + partition.getMasterAddress() + " for slot ranges: " + partition.getSlotRanges() + ". Reason - server has FAIL flag");
        if (partition.getSlotsAmount() == 0) {
            e = new RedisException("Failed to add master: " + partition.getMasterAddress() + ". Reason - server has FAIL flag");
        }
        result.completeExceptionally(e);
        return result;
    }
    CompletionStage<RedisConnection> connectionFuture = connectToNode(cfg, partition.getMasterAddress(), configEndpointHostName);
    connectionFuture.whenComplete((connection, ex1) -> {
        if (ex1 != null) {
            log.error("Can't connect to master: {} with slot ranges: {}", partition.getMasterAddress(), partition.getSlotRanges());
            result.completeExceptionally(ex1);
            return;
        }
        MasterSlaveServersConfig config = create(cfg);
        config.setMasterAddress(partition.getMasterAddress().toString());
        MasterSlaveEntry entry;
        if (config.checkSkipSlavesInit()) {
            entry = new SingleEntry(ClusterConnectionManager.this, config);
        } else {
            Set<String> slaveAddresses = partition.getSlaveAddresses().stream().map(r -> r.toString()).collect(Collectors.toSet());
            config.setSlaveAddresses(slaveAddresses);
            entry = new MasterSlaveEntry(ClusterConnectionManager.this, config);
        }
        CompletableFuture<RedisClient> f = entry.setupMasterEntry(new RedisURI(config.getMasterAddress()), configEndpointHostName);
        f.whenComplete((masterClient, ex3) -> {
            if (ex3 != null) {
                log.error("Can't add master: " + partition.getMasterAddress() + " for slot ranges: " + partition.getSlotRanges(), ex3);
                result.completeExceptionally(ex3);
                return;
            }
            for (Integer slot : partition.getSlots()) {
                addEntry(slot, entry);
                lastPartitions.put(slot, partition);
            }
            if (!config.checkSkipSlavesInit()) {
                CompletableFuture<Void> fs = entry.initSlaveBalancer(partition.getFailedSlaveAddresses(), configEndpointHostName);
                fs.whenComplete((r, ex) -> {
                    if (ex != null) {
                        log.error("unable to add slave for: " + partition.getMasterAddress() + " slot ranges: " + partition.getSlotRanges(), ex);
                        result.completeExceptionally(ex);
                        return;
                    }
                    if (!partition.getSlaveAddresses().isEmpty()) {
                        log.info("slaves: {} added for slot ranges: {}", partition.getSlaveAddresses(), partition.getSlotRanges());
                        if (!partition.getFailedSlaveAddresses().isEmpty()) {
                            log.warn("slaves: {} are down for slot ranges: {}", partition.getFailedSlaveAddresses(), partition.getSlotRanges());
                        }
                    }
                    if (result.complete(null)) {
                        log.info("master: {} added for slot ranges: {}", partition.getMasterAddress(), partition.getSlotRanges());
                    } else {
                        log.error("unable to add master: {} for slot ranges: {}", partition.getMasterAddress(), partition.getSlotRanges());
                    }
                });
            } else {
                if (result.complete(null)) {
                    log.info("master: {} added for slot ranges: {}", partition.getMasterAddress(), partition.getSlotRanges());
                } else {
                    log.error("unable to add master: {} for slot ranges: {}", partition.getMasterAddress(), partition.getSlotRanges());
                }
            }
        });
    });
    return result;
}
Also used : java.util(java.util) NodeType(org.redisson.api.NodeType) ClusterServersConfig(org.redisson.config.ClusterServersConfig) Config(org.redisson.config.Config) ScheduledFuture(io.netty.util.concurrent.ScheduledFuture) LoggerFactory(org.slf4j.LoggerFactory) RedisURI(org.redisson.misc.RedisURI) AtomicReference(java.util.concurrent.atomic.AtomicReference) AtomicReferenceArray(java.util.concurrent.atomic.AtomicReferenceArray) RFuture(org.redisson.api.RFuture) ReadMode(org.redisson.config.ReadMode) Type(org.redisson.cluster.ClusterPartition.Type) org.redisson.connection(org.redisson.connection) FreezeReason(org.redisson.connection.ClientConnectionsEntry.FreezeReason) AddressResolver(io.netty.resolver.AddressResolver) Logger(org.slf4j.Logger) FutureListener(io.netty.util.concurrent.FutureListener) java.util.concurrent(java.util.concurrent) org.redisson.client(org.redisson.client) MasterSlaveServersConfig(org.redisson.config.MasterSlaveServersConfig) NatMapper(org.redisson.api.NatMapper) InetSocketAddress(java.net.InetSocketAddress) Collectors(java.util.stream.Collectors) RedisCommands(org.redisson.client.protocol.RedisCommands) RedisStrictCommand(org.redisson.client.protocol.RedisStrictCommand) Future(io.netty.util.concurrent.Future) Flag(org.redisson.cluster.ClusterNodeInfo.Flag) RedisURI(org.redisson.misc.RedisURI) MasterSlaveServersConfig(org.redisson.config.MasterSlaveServersConfig)

Example 12 with RedisURI

use of org.redisson.misc.RedisURI in project redisson by redisson.

the class MasterSlaveEntry method initSlaveBalancer.

public CompletableFuture<Void> initSlaveBalancer(Collection<RedisURI> disconnectedNodes, String slaveSSLHostname) {
    List<CompletableFuture<Void>> result = new ArrayList<>(config.getSlaveAddresses().size());
    for (String address : config.getSlaveAddresses()) {
        RedisURI uri = new RedisURI(address);
        CompletableFuture<Void> f = addSlave(uri, disconnectedNodes.contains(uri), NodeType.SLAVE, slaveSSLHostname);
        result.add(f);
    }
    CompletableFuture<Void> future = CompletableFuture.allOf(result.toArray(new CompletableFuture[0]));
    return future.thenAccept(v -> {
        useMasterAsSlave();
    });
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) RedisURI(org.redisson.misc.RedisURI) ArrayList(java.util.ArrayList)

Example 13 with RedisURI

use of org.redisson.misc.RedisURI in project redisson by redisson.

the class RedissonRedisNodesTest method testSentinel.

@Test
public void testSentinel() throws IOException, InterruptedException {
    RedisRunner.RedisProcess master = new RedisRunner().nosave().randomDir().run();
    RedisRunner.RedisProcess slave1 = new RedisRunner().port(6380).nosave().randomDir().slaveof("127.0.0.1", 6379).run();
    RedisRunner.RedisProcess slave2 = new RedisRunner().port(6381).nosave().randomDir().slaveof("127.0.0.1", 6379).run();
    RedisRunner.RedisProcess sentinel1 = new RedisRunner().nosave().randomDir().port(26379).sentinel().sentinelMonitor("myMaster", "127.0.0.1", 6379, 2).run();
    RedisRunner.RedisProcess sentinel2 = new RedisRunner().nosave().randomDir().port(26380).sentinel().sentinelMonitor("myMaster", "127.0.0.1", 6379, 2).run();
    RedisRunner.RedisProcess sentinel3 = new RedisRunner().nosave().randomDir().port(26381).sentinel().sentinelMonitor("myMaster", "127.0.0.1", 6379, 2).run();
    Config config = new Config();
    config.useSentinelServers().setLoadBalancer(new RandomLoadBalancer()).addSentinelAddress(sentinel3.getRedisServerAddressAndPort()).setMasterName("myMaster");
    long t = System.currentTimeMillis();
    RedissonClient redisson = Redisson.create(config);
    RedisSentinelMasterSlave nodes = redisson.getRedisNodes(RedisNodes.SENTINEL_MASTER_SLAVE);
    assertThat(nodes.getSentinels()).hasSize(3);
    assertThat(nodes.getSlaves()).hasSize(2);
    assertThat(nodes.getMaster()).isNotNull();
    for (RedisSentinel sentinel : nodes.getSentinels()) {
        Assertions.assertTrue(sentinel.ping());
        RedisURI addr = sentinel.getMasterAddr("myMaster");
        assertThat(addr.getHost()).isEqualTo("127.0.0.1");
        assertThat(addr.getPort()).isEqualTo(master.getRedisServerPort());
        Map<String, String> masterMap = sentinel.getMaster("myMaster");
        assertThat(masterMap).isNotEmpty();
        List<Map<String, String>> masters = sentinel.getMasters();
        assertThat(masters).hasSize(1);
        Map<String, String> m = masters.get(0);
        assertThat(m.get("ip")).isEqualTo("127.0.0.1");
        assertThat(Integer.valueOf(m.get("port"))).isEqualTo(master.getRedisServerPort());
        List<Map<String, String>> slaves = sentinel.getSlaves("myMaster");
        assertThat(slaves).hasSize(2);
    }
    nodes.getSlaves().forEach((node) -> {
        Assertions.assertTrue(node.ping());
    });
    redisson.shutdown();
    sentinel1.stop();
    sentinel2.stop();
    sentinel3.stop();
    master.stop();
    slave1.stop();
    slave2.stop();
}
Also used : Config(org.redisson.config.Config) RedisURI(org.redisson.misc.RedisURI) RedissonClient(org.redisson.api.RedissonClient) RandomLoadBalancer(org.redisson.connection.balancer.RandomLoadBalancer) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 14 with RedisURI

use of org.redisson.misc.RedisURI in project redisson by redisson.

the class RedisClusterNodeDecoder method decode.

@Override
public List<RedisClusterNode> decode(ByteBuf buf, State state) throws IOException {
    String response = buf.toString(CharsetUtil.UTF_8);
    List<RedisClusterNode> nodes = new ArrayList<RedisClusterNode>();
    for (String nodeInfo : response.split("\n")) {
        String[] params = nodeInfo.split(" ");
        String nodeId = params[0];
        String flagsStr = params[2];
        Set<Flag> flags = EnumSet.noneOf(Flag.class);
        for (String flag : flagsStr.split(",")) {
            String flagValue = flag.toUpperCase().replaceAll("\\?", "");
            flags.add(Flag.valueOf(flagValue));
        }
        RedisURI address = null;
        if (!flags.contains(Flag.NOADDR)) {
            String addr = params[1].split("@")[0];
            address = new RedisURI("redis://" + addr);
        }
        String masterId = params[3];
        if ("-".equals(masterId)) {
            masterId = null;
        }
        Set<Integer> slotsCollection = new HashSet<Integer>();
        LinkState linkState = null;
        if (params.length >= 8 && params[7] != null) {
            linkState = LinkState.valueOf(params[7].toUpperCase());
        }
        if (params.length > 8) {
            for (int i = 0; i < params.length - 8; i++) {
                String slots = params[i + 8];
                if (slots.indexOf("-<-") != -1 || slots.indexOf("->-") != -1) {
                    continue;
                }
                String[] parts = slots.split("-");
                if (parts.length == 1) {
                    slotsCollection.add(Integer.valueOf(parts[0]));
                } else if (parts.length == 2) {
                    for (int j = Integer.valueOf(parts[0]); j < Integer.valueOf(parts[1]) + 1; j++) {
                        slotsCollection.add(j);
                    }
                }
            }
        }
        NodeType type = null;
        if (flags.contains(Flag.MASTER)) {
            type = NodeType.MASTER;
        } else if (flags.contains(Flag.SLAVE)) {
            type = NodeType.SLAVE;
        }
        RedisClusterNodeBuilder builder = RedisClusterNode.newRedisClusterNode().linkState(linkState).slaveOf(masterId).serving(new SlotRange(slotsCollection)).withId(nodeId).promotedAs(type).withFlags(flags);
        if (address != null) {
            builder.listeningAt(address.getHost(), address.getPort());
        }
        nodes.add(builder.build());
    }
    return nodes;
}
Also used : RedisURI(org.redisson.misc.RedisURI) ArrayList(java.util.ArrayList) Flag(org.springframework.data.redis.connection.RedisClusterNode.Flag) LinkState(org.springframework.data.redis.connection.RedisClusterNode.LinkState) RedisClusterNode(org.springframework.data.redis.connection.RedisClusterNode) NodeType(org.springframework.data.redis.connection.RedisNode.NodeType) SlotRange(org.springframework.data.redis.connection.RedisClusterNode.SlotRange) HashSet(java.util.HashSet) RedisClusterNodeBuilder(org.springframework.data.redis.connection.RedisClusterNode.RedisClusterNodeBuilder)

Example 15 with RedisURI

use of org.redisson.misc.RedisURI in project redisson by redisson.

the class RedisNodes method getNode.

@Override
public N getNode(String address) {
    Collection<MasterSlaveEntry> entries = connectionManager.getEntrySet();
    RedisURI addr = new RedisURI(address);
    for (MasterSlaveEntry masterSlaveEntry : entries) {
        if (masterSlaveEntry.getAllEntries().isEmpty() && RedisURI.compare(masterSlaveEntry.getClient().getAddr(), addr)) {
            return (N) new RedisClientEntry(masterSlaveEntry.getClient(), commandExecutor, NodeType.MASTER);
        }
        for (ClientConnectionsEntry entry : masterSlaveEntry.getAllEntries()) {
            if (RedisURI.compare(entry.getClient().getAddr(), addr) && entry.getFreezeReason() != FreezeReason.MANAGER) {
                return (N) new RedisClientEntry(entry.getClient(), commandExecutor, entry.getNodeType());
            }
        }
    }
    return null;
}
Also used : RedisURI(org.redisson.misc.RedisURI)

Aggregations

RedisURI (org.redisson.misc.RedisURI)25 InetSocketAddress (java.net.InetSocketAddress)9 AtomicReference (java.util.concurrent.atomic.AtomicReference)7 Collectors (java.util.stream.Collectors)7 NodeType (org.redisson.api.NodeType)7 Logger (org.slf4j.Logger)7 LoggerFactory (org.slf4j.LoggerFactory)7 ScheduledFuture (io.netty.util.concurrent.ScheduledFuture)6 org.redisson.config (org.redisson.config)6 AddressResolver (io.netty.resolver.AddressResolver)5 Future (io.netty.util.concurrent.Future)5 java.util (java.util)5 java.util.concurrent (java.util.concurrent)5 RFuture (org.redisson.api.RFuture)5 RedisCommands (org.redisson.client.protocol.RedisCommands)5 FreezeReason (org.redisson.connection.ClientConnectionsEntry.FreezeReason)5 FutureListener (io.netty.util.concurrent.FutureListener)4 org.redisson.client (org.redisson.client)4 RedisStrictCommand (org.redisson.client.protocol.RedisStrictCommand)4 NetUtil (io.netty.util.NetUtil)3