Search in sources :

Example 1 with RedisPubSubConnection

use of org.redisson.client.RedisPubSubConnection in project redisson by redisson.

the class MasterSlaveEntry method slaveDown.

private boolean slaveDown(ClientConnectionsEntry entry, boolean temporaryDown) {
    // add master as slave if no more slaves available
    if (config.getReadMode() == ReadMode.SLAVE && slaveBalancer.getAvailableClients() == 0) {
        InetSocketAddress addr = masterEntry.getClient().getAddr();
        if (slaveUp(addr.getHostName(), addr.getPort(), FreezeReason.SYSTEM)) {
            log.info("master {}:{} used as slave", addr.getHostName(), addr.getPort());
        }
    }
    // close all connections
    while (true) {
        final RedisConnection connection = entry.pollConnection();
        if (connection == null) {
            break;
        }
        connection.closeAsync().addListener(new ChannelFutureListener() {

            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                reattachBlockingQueue(connection);
            }
        });
    }
    // close all pub/sub connections
    while (true) {
        RedisPubSubConnection connection = entry.pollSubscribeConnection();
        if (connection == null) {
            break;
        }
        connection.closeAsync();
    }
    for (RedisPubSubConnection connection : entry.getAllSubscribeConnections()) {
        reattachPubSub(connection, temporaryDown);
    }
    entry.getAllSubscribeConnections().clear();
    return true;
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) RedisPubSubConnection(org.redisson.client.RedisPubSubConnection) InetSocketAddress(java.net.InetSocketAddress) ChannelFutureListener(io.netty.channel.ChannelFutureListener) RedisConnection(org.redisson.client.RedisConnection)

Example 2 with RedisPubSubConnection

use of org.redisson.client.RedisPubSubConnection in project redisson by redisson.

the class CommandDecoder method handlePublishSubscribe.

private void handlePublishSubscribe(CommandData<Object, Object> data, List<Object> parts, Channel channel, final Object result) {
    if (result instanceof PubSubStatusMessage) {
        String channelName = ((PubSubStatusMessage) result).getChannel();
        String operation = ((PubSubStatusMessage) result).getType().name().toLowerCase();
        PubSubKey key = new PubSubKey(channelName, operation);
        CommandData<Object, Object> d = pubSubChannels.get(key);
        if (Arrays.asList(RedisCommands.PSUBSCRIBE.getName(), RedisCommands.SUBSCRIBE.getName()).contains(d.getCommand().getName())) {
            pubSubChannels.remove(key);
            pubSubMessageDecoders.put(channelName, d.getMessageDecoder());
        }
        if (Arrays.asList(RedisCommands.PUNSUBSCRIBE.getName(), RedisCommands.UNSUBSCRIBE.getName()).contains(d.getCommand().getName())) {
            pubSubChannels.remove(key);
            pubSubMessageDecoders.remove(channelName);
        }
    }
    final RedisPubSubConnection pubSubConnection = RedisPubSubConnection.getFrom(channel);
    executor.execute(new Runnable() {

        @Override
        public void run() {
            if (result instanceof PubSubStatusMessage) {
                pubSubConnection.onMessage((PubSubStatusMessage) result);
            } else if (result instanceof PubSubMessage) {
                pubSubConnection.onMessage((PubSubMessage) result);
            } else {
                pubSubConnection.onMessage((PubSubPatternMessage) result);
            }
        }
    });
}
Also used : RedisPubSubConnection(org.redisson.client.RedisPubSubConnection) PubSubStatusMessage(org.redisson.client.protocol.pubsub.PubSubStatusMessage) PubSubMessage(org.redisson.client.protocol.pubsub.PubSubMessage)

Example 3 with RedisPubSubConnection

use of org.redisson.client.RedisPubSubConnection in project redisson by redisson.

the class SentinelConnectionManager method registerSentinel.

private RFuture<RedisPubSubConnection> registerSentinel(final SentinelServersConfig cfg, final URL addr, final MasterSlaveServersConfig c) {
    RedisClient client = createClient(addr.getHost(), addr.getPort(), c.getConnectTimeout(), c.getRetryInterval() * c.getRetryAttempts());
    RedisClient oldClient = sentinels.putIfAbsent(addr.getHost() + ":" + addr.getPort(), client);
    if (oldClient != null) {
        return newSucceededFuture(null);
    }
    RFuture<RedisPubSubConnection> pubsubFuture = client.connectPubSubAsync();
    pubsubFuture.addListener(new FutureListener<RedisPubSubConnection>() {

        @Override
        public void operationComplete(Future<RedisPubSubConnection> future) throws Exception {
            if (!future.isSuccess()) {
                log.warn("Can't connect to sentinel: {}:{}", addr.getHost(), addr.getPort());
                return;
            }
            RedisPubSubConnection pubsub = future.getNow();
            pubsub.addListener(new BaseRedisPubSubListener() {

                @Override
                public void onMessage(String channel, Object msg) {
                    if ("+sentinel".equals(channel)) {
                        onSentinelAdded(cfg, (String) msg, c);
                    }
                    if ("+slave".equals(channel)) {
                        onSlaveAdded(addr, (String) msg);
                    }
                    if ("+sdown".equals(channel)) {
                        onNodeDown(addr, (String) msg);
                    }
                    if ("-sdown".equals(channel)) {
                        onNodeUp(addr, (String) msg);
                    }
                    if ("+switch-master".equals(channel)) {
                        onMasterChange(cfg, addr, (String) msg);
                    }
                }

                @Override
                public boolean onStatus(PubSubType type, String channel) {
                    if (type == PubSubType.SUBSCRIBE) {
                        log.debug("subscribed to channel: {} from Sentinel {}:{}", channel, addr.getHost(), addr.getPort());
                    }
                    return true;
                }
            });
            pubsub.subscribe(StringCodec.INSTANCE, "+switch-master", "+sdown", "-sdown", "+slave", "+sentinel");
            log.info("sentinel: {}:{} added", addr.getHost(), addr.getPort());
        }
    });
    return pubsubFuture;
}
Also used : RedisClient(org.redisson.client.RedisClient) RedisPubSubConnection(org.redisson.client.RedisPubSubConnection) BaseRedisPubSubListener(org.redisson.client.BaseRedisPubSubListener) PubSubType(org.redisson.client.protocol.pubsub.PubSubType) RedisConnectionException(org.redisson.client.RedisConnectionException)

Example 4 with RedisPubSubConnection

use of org.redisson.client.RedisPubSubConnection in project redisson by redisson.

the class CommandPubSubDecoder method decodeResult.

@Override
protected void decodeResult(CommandData<Object, Object> data, List<Object> parts, Channel channel, Object result) throws IOException {
    try {
        if (config.getExecutor().isShutdown()) {
            return;
        }
    } catch (IllegalStateException e) {
    // arise in JBOSS. skipped
    }
    if (result instanceof Message) {
        checkpoint();
        RedisPubSubConnection pubSubConnection = RedisPubSubConnection.getFrom(channel);
        ChannelName channelName = ((Message) result).getChannel();
        if (result instanceof PubSubStatusMessage) {
            String operation = ((PubSubStatusMessage) result).getType().name().toLowerCase();
            PubSubKey key = new PubSubKey(channelName, operation);
            CommandData<Object, Object> d = commands.get(key);
            if (Arrays.asList(RedisCommands.PSUBSCRIBE.getName(), RedisCommands.SUBSCRIBE.getName()).contains(d.getCommand().getName())) {
                commands.remove(key);
                entries.put(channelName, new PubSubEntry(d.getMessageDecoder()));
            }
            if (Arrays.asList(RedisCommands.PUNSUBSCRIBE.getName(), RedisCommands.UNSUBSCRIBE.getName()).contains(d.getCommand().getName())) {
                commands.remove(key);
                if (result instanceof PubSubPatternMessage) {
                    channelName = ((PubSubPatternMessage) result).getPattern();
                }
                PubSubEntry entry = entries.remove(channelName);
                if (config.isKeepAlive()) {
                    enqueueMessage(result, pubSubConnection, entry);
                }
            }
        }
        if (config.isKeepAlive()) {
            if (result instanceof PubSubPatternMessage) {
                channelName = ((PubSubPatternMessage) result).getPattern();
            }
            PubSubEntry entry = entries.get(channelName);
            if (entry != null) {
                enqueueMessage(result, pubSubConnection, entry);
            }
        } else {
            config.getExecutor().execute(new Runnable() {

                @Override
                public void run() {
                    if (result instanceof PubSubStatusMessage) {
                        pubSubConnection.onMessage((PubSubStatusMessage) result);
                    } else if (result instanceof PubSubMessage) {
                        pubSubConnection.onMessage((PubSubMessage) result);
                    } else if (result instanceof PubSubPatternMessage) {
                        pubSubConnection.onMessage((PubSubPatternMessage) result);
                    }
                }
            });
        }
    } else {
        if (data != null && data.getCommand().getName().equals("PING")) {
            super.decodeResult(data, parts, channel, result);
        }
    }
}
Also used : RedisPubSubConnection(org.redisson.client.RedisPubSubConnection) PubSubStatusMessage(org.redisson.client.protocol.pubsub.PubSubStatusMessage) PubSubPatternMessage(org.redisson.client.protocol.pubsub.PubSubPatternMessage) Message(org.redisson.client.protocol.pubsub.Message) PubSubMessage(org.redisson.client.protocol.pubsub.PubSubMessage) PubSubPatternMessage(org.redisson.client.protocol.pubsub.PubSubPatternMessage) ChannelName(org.redisson.client.ChannelName) PubSubStatusMessage(org.redisson.client.protocol.pubsub.PubSubStatusMessage) PubSubMessage(org.redisson.client.protocol.pubsub.PubSubMessage)

Example 5 with RedisPubSubConnection

use of org.redisson.client.RedisPubSubConnection in project redisson by redisson.

the class MasterSlaveEntry method nodeDown.

public boolean nodeDown(ClientConnectionsEntry entry) {
    entry.reset();
    for (RedisConnection connection : entry.getAllConnections()) {
        connection.closeAsync();
        reattachBlockingQueue(connection.getCurrentCommand());
    }
    while (true) {
        RedisConnection connection = entry.pollConnection(null);
        if (connection == null) {
            break;
        }
    }
    entry.getAllConnections().clear();
    for (RedisPubSubConnection connection : entry.getAllSubscribeConnections()) {
        connection.closeAsync();
        connectionManager.getSubscribeService().reattachPubSub(connection);
    }
    while (true) {
        RedisConnection connection = entry.pollSubscribeConnection();
        if (connection == null) {
            break;
        }
    }
    entry.getAllSubscribeConnections().clear();
    return true;
}
Also used : RedisPubSubConnection(org.redisson.client.RedisPubSubConnection) RedisConnection(org.redisson.client.RedisConnection)

Aggregations

RedisPubSubConnection (org.redisson.client.RedisPubSubConnection)6 RedisClient (org.redisson.client.RedisClient)2 RedisConnection (org.redisson.client.RedisConnection)2 PubSubMessage (org.redisson.client.protocol.pubsub.PubSubMessage)2 PubSubStatusMessage (org.redisson.client.protocol.pubsub.PubSubStatusMessage)2 PubSubType (org.redisson.client.protocol.pubsub.PubSubType)2 ChannelFuture (io.netty.channel.ChannelFuture)1 ChannelFutureListener (io.netty.channel.ChannelFutureListener)1 InetSocketAddress (java.net.InetSocketAddress)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Test (org.junit.Test)1 BaseRedisPubSubListener (org.redisson.client.BaseRedisPubSubListener)1 ChannelName (org.redisson.client.ChannelName)1 RedisConnectionException (org.redisson.client.RedisConnectionException)1 Message (org.redisson.client.protocol.pubsub.Message)1 PubSubPatternMessage (org.redisson.client.protocol.pubsub.PubSubPatternMessage)1