Search in sources :

Example 16 with Codec

use of org.redisson.client.codec.Codec in project redisson by redisson.

the class PublishSubscribeService method subscribe.

private void subscribe(Codec codec, ChannelName channelName, MasterSlaveEntry entry, CompletableFuture<PubSubConnectionEntry> promise, PubSubType type, AsyncSemaphore lock, AtomicInteger attempts, RedisPubSubListener<?>... listeners) {
    PubSubConnectionEntry connEntry = name2PubSubConnection.get(new PubSubKey(channelName, entry));
    if (connEntry != null) {
        addListeners(channelName, promise, type, lock, connEntry, listeners);
        return;
    }
    Timeout lockTimeout = connectionManager.newTimeout(timeout -> {
        promise.completeExceptionally(new RedisTimeoutException("Unable to acquire subscription lock after " + config.getTimeout() + "ms. " + "Increase 'subscriptionsPerConnection' and/or 'subscriptionConnectionPoolSize' parameters."));
    }, config.getTimeout(), TimeUnit.MILLISECONDS);
    freePubSubLock.acquire(() -> {
        if (!lockTimeout.cancel() || promise.isDone()) {
            lock.release();
            freePubSubLock.release();
            return;
        }
        MasterSlaveEntry msEntry = Optional.ofNullable(connectionManager.getEntry(entry.getClient())).orElse(entry);
        PubSubEntry freePubSubConnections = entry2PubSubConnection.getOrDefault(msEntry, new PubSubEntry());
        PubSubConnectionEntry freeEntry = freePubSubConnections.getEntries().peek();
        if (freeEntry == null) {
            freePubSubLock.release();
            CompletableFuture<RedisPubSubConnection> connectFuture = connect(codec, channelName, msEntry, promise, type, lock, listeners);
            connectionManager.newTimeout(t -> {
                if (attempts.get() == config.getRetryAttempts()) {
                    connectFuture.completeExceptionally(new RedisTimeoutException("Unable to acquire connection for subscription after " + attempts.get() + " attempts. " + "Increase 'subscriptionsPerConnection' and/or 'subscriptionConnectionPoolSize' parameters."));
                    return;
                }
                if (connectFuture.cancel(true)) {
                    subscribe(codec, channelName, entry, promise, type, lock, attempts, listeners);
                    attempts.incrementAndGet();
                }
            }, config.getRetryInterval(), TimeUnit.MILLISECONDS);
            return;
        }
        int remainFreeAmount = freeEntry.tryAcquire();
        if (remainFreeAmount == -1) {
            throw new IllegalStateException();
        }
        PubSubKey key = new PubSubKey(channelName, msEntry);
        PubSubConnectionEntry oldEntry = name2PubSubConnection.putIfAbsent(key, freeEntry);
        if (oldEntry != null) {
            freeEntry.release();
            freePubSubLock.release();
            addListeners(channelName, promise, type, lock, oldEntry, listeners);
            return;
        }
        if (remainFreeAmount == 0) {
            freePubSubConnections.getEntries().poll();
        }
        freePubSubLock.release();
        CompletableFuture<Void> subscribeFuture = addListeners(channelName, promise, type, lock, freeEntry, listeners);
        ChannelFuture future;
        if (PubSubType.PSUBSCRIBE == type) {
            future = freeEntry.psubscribe(codec, channelName);
        } else {
            future = freeEntry.subscribe(codec, channelName);
        }
        future.addListener((ChannelFutureListener) future1 -> {
            if (!future1.isSuccess()) {
                if (!promise.isDone()) {
                    subscribeFuture.cancel(false);
                }
                return;
            }
            connectionManager.newTimeout(timeout -> {
                if (subscribeFuture.completeExceptionally(new RedisTimeoutException("Subscription timeout after " + config.getTimeout() + "ms. " + "Check network and/or increase 'timeout' parameter."))) {
                    unsubscribe(channelName, type);
                }
            }, config.getTimeout(), TimeUnit.MILLISECONDS);
        });
    });
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) PubSubType(org.redisson.client.protocol.pubsub.PubSubType) Timeout(io.netty.util.Timeout) PubSubStatusMessage(org.redisson.client.protocol.pubsub.PubSubStatusMessage) java.util(java.util) Logger(org.slf4j.Logger) Codec(org.redisson.client.codec.Codec) ConnectionManager(org.redisson.connection.ConnectionManager) java.util.concurrent(java.util.concurrent) org.redisson.client(org.redisson.client) PubSubPatternStatusListener(org.redisson.PubSubPatternStatusListener) LoggerFactory(org.slf4j.LoggerFactory) MasterSlaveServersConfig(org.redisson.config.MasterSlaveServersConfig) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Collectors(java.util.stream.Collectors) ChannelFuture(io.netty.channel.ChannelFuture) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ChannelFutureListener(io.netty.channel.ChannelFutureListener) MasterSlaveEntry(org.redisson.connection.MasterSlaveEntry) Timeout(io.netty.util.Timeout) MasterSlaveEntry(org.redisson.connection.MasterSlaveEntry)

Example 17 with Codec

use of org.redisson.client.codec.Codec in project redisson by redisson.

the class CollectionMapperTask method run.

@Override
public void run() {
    Codec codec;
    try {
        codec = (Codec) objectCodecClass.getConstructor().newInstance();
    } catch (Exception e) {
        throw new IllegalStateException(e);
    }
    Injector.inject(mapper, redisson);
    for (String objectName : objectNames) {
        Iterable<VIn> collection = null;
        if (RSetCache.class.isAssignableFrom(objectClass)) {
            collection = redisson.getSetCache(objectName, codec);
        } else if (RSet.class.isAssignableFrom(objectClass)) {
            collection = redisson.getSet(objectName, codec);
        } else if (RSortedSet.class.isAssignableFrom(objectClass)) {
            collection = redisson.getSortedSet(objectName, codec);
        } else if (RScoredSortedSet.class.isAssignableFrom(objectClass)) {
            collection = redisson.getScoredSortedSet(objectName, codec);
        } else if (RLexSortedSet.class.isAssignableFrom(objectClass)) {
            collection = (Iterable<VIn>) redisson.getLexSortedSet(objectName);
        } else if (RList.class.isAssignableFrom(objectClass)) {
            collection = redisson.getList(objectName, codec);
        } else {
            throw new IllegalStateException("Unable to work with " + objectClass);
        }
        RCollector<KOut, VOut> collector = new Collector<KOut, VOut>(codec, redisson, collectorMapName, workersAmount, timeout);
        for (VIn value : collection) {
            if (Thread.currentThread().isInterrupted()) {
                return;
            }
            mapper.map(value, collector);
        }
    }
}
Also used : RScoredSortedSet(org.redisson.api.RScoredSortedSet) Codec(org.redisson.client.codec.Codec) RSet(org.redisson.api.RSet) RList(org.redisson.api.RList) RCollector(org.redisson.api.mapreduce.RCollector)

Example 18 with Codec

use of org.redisson.client.codec.Codec in project redisson by redisson.

the class MapperTask method run.

@Override
public void run() {
    Codec codec;
    try {
        codec = (Codec) objectCodecClass.getConstructor().newInstance();
    } catch (Exception e) {
        throw new IllegalStateException(e);
    }
    Injector.inject(mapper, redisson);
    RCollector<KOut, VOut> collector = new Collector<KOut, VOut>(codec, redisson, collectorMapName, workersAmount, timeout);
    for (String objectName : objectNames) {
        RMap<KIn, VIn> map = null;
        if (RMapCache.class.isAssignableFrom(objectClass)) {
            map = redisson.getMapCache(objectName, codec);
        } else {
            map = redisson.getMap(objectName, codec);
        }
        for (Entry<KIn, VIn> entry : map.entrySet()) {
            if (Thread.currentThread().isInterrupted()) {
                return;
            }
            mapper.map(entry.getKey(), entry.getValue(), collector);
        }
    }
}
Also used : Codec(org.redisson.client.codec.Codec) RCollector(org.redisson.api.mapreduce.RCollector)

Example 19 with Codec

use of org.redisson.client.codec.Codec in project redisson by redisson.

the class RedissonReactiveSubscription method unsubscribe.

@Override
public Mono<Void> unsubscribe(ByteBuffer... channels) {
    monosListener.acquire();
    return Mono.defer(() -> {
        List<CompletableFuture<?>> futures = new ArrayList<>(channels.length);
        for (ByteBuffer channel : channels) {
            ChannelName cn = toChannelName(channel);
            CompletableFuture<Codec> f = subscribeService.unsubscribe(cn, PubSubType.UNSUBSCRIBE);
            f = f.whenComplete((res, e) -> {
                synchronized (RedissonReactiveSubscription.this.channels) {
                    PubSubConnectionEntry entry = RedissonReactiveSubscription.this.channels.get(cn);
                    if (!entry.hasListeners(cn)) {
                        RedissonReactiveSubscription.this.channels.remove(cn);
                    }
                }
            });
            futures.add(f);
        }
        CompletableFuture<Void> future = CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]));
        future = future.whenComplete((r, e) -> {
            monosListener.release();
        });
        return Mono.fromFuture(future);
    });
}
Also used : PubSubType(org.redisson.client.protocol.pubsub.PubSubType) PublishSubscribeService(org.redisson.pubsub.PublishSubscribeService) java.util(java.util) Disposable(reactor.core.Disposable) Codec(org.redisson.client.codec.Codec) PubSubConnectionEntry(org.redisson.pubsub.PubSubConnectionEntry) ConnectionManager(org.redisson.connection.ConnectionManager) ByteArrayCodec(org.redisson.client.codec.ByteArrayCodec) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Mono(reactor.core.publisher.Mono) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) Collectors(java.util.stream.Collectors) ByteBuffer(java.nio.ByteBuffer) StandardCharsets(java.nio.charset.StandardCharsets) RedisPubSubListener(org.redisson.client.RedisPubSubListener) SubscriptionListener(org.springframework.data.redis.connection.SubscriptionListener) BaseRedisPubSubListener(org.redisson.client.BaseRedisPubSubListener) Flux(reactor.core.publisher.Flux) ReactiveSubscription(org.springframework.data.redis.connection.ReactiveSubscription) Entry(java.util.Map.Entry) ChannelName(org.redisson.client.ChannelName) CompletableFuture(java.util.concurrent.CompletableFuture) Codec(org.redisson.client.codec.Codec) ByteArrayCodec(org.redisson.client.codec.ByteArrayCodec) PubSubConnectionEntry(org.redisson.pubsub.PubSubConnectionEntry) ChannelName(org.redisson.client.ChannelName) ByteBuffer(java.nio.ByteBuffer)

Example 20 with Codec

use of org.redisson.client.codec.Codec in project redisson by redisson.

the class MasterSlaveConnectionManager method punsubscribe.

public Codec punsubscribe(final String channelName, final AsyncSemaphore lock) {
    final PubSubConnectionEntry entry = name2PubSubConnection.remove(channelName);
    if (entry == null) {
        lock.release();
        return null;
    }
    Codec entryCodec = entry.getConnection().getPatternChannels().get(channelName);
    entry.punsubscribe(channelName, new BaseRedisPubSubListener() {

        @Override
        public boolean onStatus(PubSubType type, String channel) {
            if (type == PubSubType.PUNSUBSCRIBE && channel.equals(channelName)) {
                if (entry.release() == 1) {
                    freePubSubConnections.add(entry);
                }
                lock.release();
                return true;
            }
            return false;
        }
    });
    return entryCodec;
}
Also used : Codec(org.redisson.client.codec.Codec) BaseRedisPubSubListener(org.redisson.client.BaseRedisPubSubListener) PubSubType(org.redisson.client.protocol.pubsub.PubSubType)

Aggregations

Codec (org.redisson.client.codec.Codec)34 PubSubType (org.redisson.client.protocol.pubsub.PubSubType)11 java.util (java.util)10 Collectors (java.util.stream.Collectors)9 ConnectionManager (org.redisson.connection.ConnectionManager)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 MasterSlaveEntry (org.redisson.connection.MasterSlaveEntry)8 Logger (org.slf4j.Logger)8 LoggerFactory (org.slf4j.LoggerFactory)8 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7 ChannelFuture (io.netty.channel.ChannelFuture)6 ChannelFutureListener (io.netty.channel.ChannelFutureListener)6 Timeout (io.netty.util.Timeout)6 StringCodec (org.redisson.client.codec.StringCodec)6 ByteBuf (io.netty.buffer.ByteBuf)5 java.util.concurrent (java.util.concurrent)5 CompletableFuture (java.util.concurrent.CompletableFuture)5 PubSubPatternStatusListener (org.redisson.PubSubPatternStatusListener)5 org.redisson.client (org.redisson.client)5 PubSubStatusMessage (org.redisson.client.protocol.pubsub.PubSubStatusMessage)5