Search in sources :

Example 1 with RedissonReference

use of org.redisson.RedissonReference in project redisson by redisson.

the class CommandAsyncService method handleReference.

private <R, V> void handleReference(RPromise<R> mainPromise, R res) {
    if (res instanceof List) {
        List<Object> r = (List<Object>) res;
        for (int i = 0; i < r.size(); i++) {
            if (r.get(i) instanceof RedissonReference) {
                try {
                    r.set(i, redisson != null ? RedissonObjectFactory.fromReference(redisson, (RedissonReference) r.get(i)) : RedissonObjectFactory.fromReference(redissonReactive, (RedissonReference) r.get(i)));
                } catch (Exception exception) {
                //skip and carry on to next one.
                }
            } else if (r.get(i) instanceof ScoredEntry && ((ScoredEntry) r.get(i)).getValue() instanceof RedissonReference) {
                try {
                    ScoredEntry<?> se = ((ScoredEntry<?>) r.get(i));
                    se = new ScoredEntry(se.getScore(), redisson != null ? RedissonObjectFactory.<R>fromReference(redisson, (RedissonReference) se.getValue()) : RedissonObjectFactory.<R>fromReference(redissonReactive, (RedissonReference) se.getValue()));
                    r.set(i, se);
                } catch (Exception exception) {
                //skip and carry on to next one.
                }
            }
        }
        mainPromise.trySuccess(res);
    } else if (res instanceof ListScanResult) {
        List<ScanObjectEntry> r = ((ListScanResult) res).getValues();
        for (int i = 0; i < r.size(); i++) {
            Object obj = r.get(i);
            if (!(obj instanceof ScanObjectEntry)) {
                break;
            }
            ScanObjectEntry e = r.get(i);
            if (e.getObj() instanceof RedissonReference) {
                try {
                    r.set(i, new ScanObjectEntry(e.getBuf(), redisson != null ? RedissonObjectFactory.<R>fromReference(redisson, (RedissonReference) e.getObj()) : RedissonObjectFactory.<R>fromReference(redissonReactive, (RedissonReference) e.getObj())));
                } catch (Exception exception) {
                //skip and carry on to next one.
                }
            } else if (e.getObj() instanceof ScoredEntry && ((ScoredEntry<?>) e.getObj()).getValue() instanceof RedissonReference) {
                try {
                    ScoredEntry<?> se = ((ScoredEntry<?>) e.getObj());
                    se = new ScoredEntry(se.getScore(), redisson != null ? RedissonObjectFactory.<R>fromReference(redisson, (RedissonReference) se.getValue()) : RedissonObjectFactory.<R>fromReference(redissonReactive, (RedissonReference) se.getValue()));
                    r.set(i, new ScanObjectEntry(e.getBuf(), se));
                } catch (Exception exception) {
                //skip and carry on to next one.
                }
            }
        }
        mainPromise.trySuccess(res);
    } else if (res instanceof MapScanResult) {
        Map<ScanObjectEntry, ScanObjectEntry> map = ((MapScanResult) res).getMap();
        HashMap<ScanObjectEntry, ScanObjectEntry> toAdd = null;
        for (Map.Entry<ScanObjectEntry, ScanObjectEntry> e : map.entrySet()) {
            if (e.getValue().getObj() instanceof RedissonReference) {
                try {
                    e.setValue(new ScanObjectEntry(e.getValue().getBuf(), redisson != null ? RedissonObjectFactory.<R>fromReference(redisson, (RedissonReference) e.getValue().getObj()) : RedissonObjectFactory.<R>fromReference(redissonReactive, (RedissonReference) e.getValue().getObj())));
                } catch (Exception exception) {
                //skip and carry on to next one.
                }
            }
            if (e.getKey().getObj() instanceof RedissonReference) {
                if (toAdd == null) {
                    toAdd = new HashMap<ScanObjectEntry, ScanObjectEntry>();
                }
                toAdd.put(e.getKey(), e.getValue());
            }
        }
        if (toAdd != null) {
            for (Map.Entry<ScanObjectEntry, ScanObjectEntry> e : toAdd.entrySet()) {
                try {
                    map.put(new ScanObjectEntry(e.getValue().getBuf(), (redisson != null ? RedissonObjectFactory.<R>fromReference(redisson, (RedissonReference) e.getKey().getObj()) : RedissonObjectFactory.<R>fromReference(redissonReactive, (RedissonReference) e.getKey().getObj()))), map.remove(e.getKey()));
                } catch (Exception exception) {
                //skip and carry on to next one.
                }
            }
        }
        mainPromise.trySuccess(res);
    } else if (res instanceof RedissonReference) {
        try {
            mainPromise.trySuccess(redisson != null ? RedissonObjectFactory.<R>fromReference(redisson, (RedissonReference) res) : RedissonObjectFactory.<R>fromReference(redissonReactive, (RedissonReference) res));
        } catch (Exception exception) {
            //fallback
            mainPromise.trySuccess(res);
        }
    } else {
        mainPromise.trySuccess(res);
    }
}
Also used : ListScanResult(org.redisson.client.protocol.decoder.ListScanResult) RedissonReference(org.redisson.RedissonReference) MapScanResult(org.redisson.client.protocol.decoder.MapScanResult) ScanObjectEntry(org.redisson.client.protocol.decoder.ScanObjectEntry) RedisAskException(org.redisson.client.RedisAskException) RedisLoadingException(org.redisson.client.RedisLoadingException) RedisTimeoutException(org.redisson.client.RedisTimeoutException) RedisException(org.redisson.client.RedisException) RedisMovedException(org.redisson.client.RedisMovedException) WriteRedisConnectionException(org.redisson.client.WriteRedisConnectionException) RedisTryAgainException(org.redisson.client.RedisTryAgainException) RedissonShutdownException(org.redisson.RedissonShutdownException) ScoredEntry(org.redisson.client.protocol.ScoredEntry) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap)

Example 2 with RedissonReference

use of org.redisson.RedissonReference in project redisson by redisson.

the class RedissonObjectBuilder method store.

public void store(RObject ar, String fieldName, RMap<String, Object> liveMap) {
    Codec codec = ar.getCodec();
    codecProvider.registerCodec((Class) codec.getClass(), ar, codec);
    liveMap.fastPut(fieldName, new RedissonReference(ar.getClass(), ar.getName(), codec));
}
Also used : Codec(org.redisson.client.codec.Codec) RedissonReference(org.redisson.RedissonReference)

Example 3 with RedissonReference

use of org.redisson.RedissonReference in project redisson by redisson.

the class RedissonObjectFactory method toReference.

public static RedissonReference toReference(RedissonClient redisson, Object object) {
    if (object != null && object.getClass().isAnnotationPresent(REntity.class)) {
        throw new IllegalArgumentException("REntity should be attached to Redisson before save");
    }
    if (object instanceof RObject && !(object instanceof RLiveObject)) {
        RObject rObject = ((RObject) object);
        redisson.getCodecProvider().registerCodec((Class) rObject.getCodec().getClass(), (Class) rObject.getClass(), rObject.getName(), rObject.getCodec());
        return new RedissonReference(object.getClass(), ((RObject) object).getName(), ((RObject) object).getCodec());
    }
    try {
        if (object instanceof RLiveObject) {
            Class<? extends Object> rEntity = object.getClass().getSuperclass();
            REntity anno = rEntity.getAnnotation(REntity.class);
            NamingScheme ns = anno.namingScheme().getDeclaredConstructor(Codec.class).newInstance(redisson.getCodecProvider().getCodec(anno, (Class) rEntity));
            String name = Introspectior.getFieldsWithAnnotation(rEntity, RId.class).getOnly().getName();
            Class<?> type = rEntity.getDeclaredField(name).getType();
            return new RedissonReference(rEntity, ns.getName(rEntity, type, name, ((RLiveObject) object).getLiveObjectId()));
        }
    } catch (Exception e) {
        throw new IllegalArgumentException(e);
    }
    return null;
}
Also used : Codec(org.redisson.client.codec.Codec) REntity(org.redisson.api.annotation.REntity) RObject(org.redisson.api.RObject) RedissonReference(org.redisson.RedissonReference) NamingScheme(org.redisson.liveobject.resolver.NamingScheme) RLiveObject(org.redisson.api.RLiveObject)

Example 4 with RedissonReference

use of org.redisson.RedissonReference in project redisson by redisson.

the class RedissonObjectFactory method toReference.

public static RedissonReference toReference(RedissonReactiveClient redissonReactive, Object object) {
    if (object instanceof RObjectReactive && !(object instanceof RLiveObject)) {
        RObjectReactive rObject = ((RObjectReactive) object);
        redissonReactive.getCodecProvider().registerCodec((Class) rObject.getCodec().getClass(), (Class) rObject.getClass(), rObject.getName(), rObject.getCodec());
        return new RedissonReference(object.getClass(), ((RObjectReactive) object).getName(), ((RObjectReactive) object).getCodec());
    }
    try {
        if (object instanceof RLiveObject) {
            Class<? extends Object> rEntity = object.getClass().getSuperclass();
            REntity anno = rEntity.getAnnotation(REntity.class);
            NamingScheme ns = anno.namingScheme().getDeclaredConstructor(Codec.class).newInstance(redissonReactive.getCodecProvider().getCodec(anno, (Class) rEntity));
            String name = Introspectior.getFieldsWithAnnotation(rEntity, RId.class).getOnly().getName();
            Class<?> type = rEntity.getDeclaredField(name).getType();
            return new RedissonReference(rEntity, ns.getName(rEntity, type, name, ((RLiveObject) object).getLiveObjectId()));
        }
    } catch (Exception e) {
        throw new IllegalArgumentException(e);
    }
    return null;
}
Also used : Codec(org.redisson.client.codec.Codec) RObjectReactive(org.redisson.api.RObjectReactive) REntity(org.redisson.api.annotation.REntity) RedissonReference(org.redisson.RedissonReference) NamingScheme(org.redisson.liveobject.resolver.NamingScheme) RLiveObject(org.redisson.api.RLiveObject)

Example 5 with RedissonReference

use of org.redisson.RedissonReference in project redisson by redisson.

the class CommandAsyncService method async.

protected <V, R> void async(final boolean readOnlyMode, final NodeSource source, final Codec codec, final RedisCommand<V> command, final Object[] params, final RPromise<R> mainPromise, final int attempt) {
    if (mainPromise.isCancelled()) {
        return;
    }
    if (!connectionManager.getShutdownLatch().acquire()) {
        mainPromise.tryFailure(new RedissonShutdownException("Redisson is shutdown"));
        return;
    }
    final AsyncDetails<V, R> details = AsyncDetails.acquire();
    if (isRedissonReferenceSupportEnabled()) {
        try {
            for (int i = 0; i < params.length; i++) {
                RedissonReference reference = redisson != null ? RedissonObjectFactory.toReference(redisson, params[i]) : RedissonObjectFactory.toReference(redissonReactive, params[i]);
                params[i] = reference == null ? params[i] : reference;
            }
        } catch (Exception e) {
            connectionManager.getShutdownLatch().release();
            mainPromise.tryFailure(e);
            return;
        }
    }
    final RFuture<RedisConnection> connectionFuture;
    if (readOnlyMode) {
        connectionFuture = connectionManager.connectionReadOp(source, command);
    } else {
        connectionFuture = connectionManager.connectionWriteOp(source, command);
    }
    final RPromise<R> attemptPromise = connectionManager.newPromise();
    details.init(connectionFuture, attemptPromise, readOnlyMode, source, codec, command, params, mainPromise, attempt);
    final TimerTask retryTimerTask = new TimerTask() {

        @Override
        public void run(Timeout t) throws Exception {
            if (details.getAttemptPromise().isDone()) {
                return;
            }
            if (details.getConnectionFuture().cancel(false)) {
                connectionManager.getShutdownLatch().release();
            } else {
                if (details.getConnectionFuture().isSuccess()) {
                    ChannelFuture writeFuture = details.getWriteFuture();
                    if (writeFuture != null && !writeFuture.cancel(false) && writeFuture.isSuccess()) {
                        return;
                    }
                }
            }
            if (details.getMainPromise().isCancelled()) {
                if (details.getAttemptPromise().cancel(false)) {
                    AsyncDetails.release(details);
                }
                return;
            }
            if (details.getAttempt() == connectionManager.getConfig().getRetryAttempts()) {
                if (details.getException() == null) {
                    details.setException(new RedisTimeoutException("Command execution timeout for command: " + command + " with params: " + LogHelper.toString(details.getParams())));
                }
                details.getAttemptPromise().tryFailure(details.getException());
                return;
            }
            if (!details.getAttemptPromise().cancel(false)) {
                return;
            }
            int count = details.getAttempt() + 1;
            if (log.isDebugEnabled()) {
                log.debug("attempt {} for command {} and params {}", count, details.getCommand(), Arrays.toString(details.getParams()));
            }
            async(details.isReadOnlyMode(), details.getSource(), details.getCodec(), details.getCommand(), details.getParams(), details.getMainPromise(), count);
            AsyncDetails.release(details);
        }
    };
    Timeout timeout = connectionManager.newTimeout(retryTimerTask, connectionManager.getConfig().getRetryInterval(), TimeUnit.MILLISECONDS);
    details.setTimeout(timeout);
    connectionFuture.addListener(new FutureListener<RedisConnection>() {

        @Override
        public void operationComplete(Future<RedisConnection> connFuture) throws Exception {
            if (connFuture.isCancelled()) {
                return;
            }
            if (!connFuture.isSuccess()) {
                connectionManager.getShutdownLatch().release();
                details.setException(convertException(connectionFuture));
                return;
            }
            if (details.getAttemptPromise().isDone() || details.getMainPromise().isDone()) {
                releaseConnection(source, connectionFuture, details.isReadOnlyMode(), details.getAttemptPromise(), details);
                return;
            }
            final RedisConnection connection = connFuture.getNow();
            if (details.getSource().getRedirect() == Redirect.ASK) {
                List<CommandData<?, ?>> list = new ArrayList<CommandData<?, ?>>(2);
                RPromise<Void> promise = connectionManager.newPromise();
                list.add(new CommandData<Void, Void>(promise, details.getCodec(), RedisCommands.ASKING, new Object[] {}));
                list.add(new CommandData<V, R>(details.getAttemptPromise(), details.getCodec(), details.getCommand(), details.getParams()));
                RPromise<Void> main = connectionManager.newPromise();
                ChannelFuture future = connection.send(new CommandsData(main, list));
                details.setWriteFuture(future);
            } else {
                if (log.isDebugEnabled()) {
                    log.debug("aquired connection for command {} and params {} from slot {} using node {}", details.getCommand(), Arrays.toString(details.getParams()), details.getSource(), connection.getRedisClient().getAddr());
                }
                ChannelFuture future = connection.send(new CommandData<V, R>(details.getAttemptPromise(), details.getCodec(), details.getCommand(), details.getParams()));
                details.setWriteFuture(future);
            }
            details.getWriteFuture().addListener(new ChannelFutureListener() {

                @Override
                public void operationComplete(ChannelFuture future) throws Exception {
                    checkWriteFuture(details, connection);
                }
            });
            releaseConnection(source, connectionFuture, details.isReadOnlyMode(), details.getAttemptPromise(), details);
        }
    });
    attemptPromise.addListener(new FutureListener<R>() {

        @Override
        public void operationComplete(Future<R> future) throws Exception {
            checkAttemptFuture(source, details, future);
        }
    });
}
Also used : RPromise(org.redisson.misc.RPromise) TimerTask(io.netty.util.TimerTask) List(java.util.List) ArrayList(java.util.ArrayList) CommandsData(org.redisson.client.protocol.CommandsData) ChannelFuture(io.netty.channel.ChannelFuture) RedissonReference(org.redisson.RedissonReference) Timeout(io.netty.util.Timeout) ChannelFutureListener(io.netty.channel.ChannelFutureListener) RedisAskException(org.redisson.client.RedisAskException) RedisLoadingException(org.redisson.client.RedisLoadingException) RedisTimeoutException(org.redisson.client.RedisTimeoutException) RedisException(org.redisson.client.RedisException) RedisMovedException(org.redisson.client.RedisMovedException) WriteRedisConnectionException(org.redisson.client.WriteRedisConnectionException) RedisTryAgainException(org.redisson.client.RedisTryAgainException) RedissonShutdownException(org.redisson.RedissonShutdownException) RedisTimeoutException(org.redisson.client.RedisTimeoutException) RedissonShutdownException(org.redisson.RedissonShutdownException) CommandData(org.redisson.client.protocol.CommandData) RedisConnection(org.redisson.client.RedisConnection)

Aggregations

RedissonReference (org.redisson.RedissonReference)8 Codec (org.redisson.client.codec.Codec)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 RLiveObject (org.redisson.api.RLiveObject)3 REntity (org.redisson.api.annotation.REntity)3 RedisAskException (org.redisson.client.RedisAskException)3 RedisLoadingException (org.redisson.client.RedisLoadingException)3 RedisMovedException (org.redisson.client.RedisMovedException)3 RedisTimeoutException (org.redisson.client.RedisTimeoutException)3 RedisTryAgainException (org.redisson.client.RedisTryAgainException)3 WriteRedisConnectionException (org.redisson.client.WriteRedisConnectionException)3 NamingScheme (org.redisson.liveobject.resolver.NamingScheme)3 Map (java.util.Map)2 RedissonShutdownException (org.redisson.RedissonShutdownException)2 RObject (org.redisson.api.RObject)2 RedisException (org.redisson.client.RedisException)2 BatchCommandData (org.redisson.client.protocol.BatchCommandData)2 MasterSlaveEntry (org.redisson.connection.MasterSlaveEntry)2 ChannelFuture (io.netty.channel.ChannelFuture)1