Search in sources :

Example 11 with RedisException

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

the class RedissonBloomFilter method add.

@Override
public boolean add(T object) {
    long[] hashes = hash(object);
    while (true) {
        if (size == 0) {
            readConfig();
        }
        int hashIterations = this.hashIterations;
        long size = this.size;
        long[] indexes = hash(hashes[0], hashes[1], hashIterations, size);
        CommandBatchService executorService = new CommandBatchService(commandExecutor);
        addConfigCheck(hashIterations, size, executorService);
        RBitSetAsync bs = createBitSet(executorService);
        for (int i = 0; i < indexes.length; i++) {
            bs.setAsync(indexes[i]);
        }
        try {
            List<Boolean> result = (List<Boolean>) executorService.execute().getResponses();
            for (Boolean val : result.subList(1, result.size() - 1)) {
                if (!val) {
                    return true;
                }
            }
            return false;
        } catch (RedisException e) {
            if (e.getMessage() == null || !e.getMessage().contains("Bloom filter config has been changed")) {
                throw e;
            }
        }
    }
}
Also used : RBitSetAsync(org.redisson.api.RBitSetAsync) CommandBatchService(org.redisson.command.CommandBatchService) RedisException(org.redisson.client.RedisException) List(java.util.List)

Example 12 with RedisException

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

the class AsyncRemoteProxy method create.

public <T> T create(Class<T> remoteInterface, RemoteInvocationOptions options, Class<?> syncInterface) {
    for (Method m : remoteInterface.getMethods()) {
        try {
            syncInterface.getMethod(m.getName(), m.getParameterTypes());
        } catch (NoSuchMethodException e) {
            throw new IllegalArgumentException("Method '" + m.getName() + "' with params '" + Arrays.toString(m.getParameterTypes()) + "' isn't defined in " + syncInterface);
        } catch (SecurityException e) {
            throw new IllegalArgumentException(e);
        }
        boolean permitted = false;
        for (Class<?> clazz : permittedClasses()) {
            if (clazz.isAssignableFrom(m.getReturnType())) {
                permitted = true;
                break;
            }
        }
        if (!permitted) {
            throw new IllegalArgumentException(m.getReturnType().getClass() + " isn't allowed as return type");
        }
    }
    // local copy of the options, to prevent mutation
    RemoteInvocationOptions optionsCopy = new RemoteInvocationOptions(options);
    InvocationHandler handler = new InvocationHandler() {

        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            RequestId requestId = remoteService.generateRequestId();
            if (method.getName().equals("toString")) {
                return getClass().getSimpleName() + "-" + remoteInterface.getSimpleName() + "-proxy-" + requestId;
            } else if (method.getName().equals("equals")) {
                return proxy == args[0];
            } else if (method.getName().equals("hashCode")) {
                return (getClass().getSimpleName() + "-" + remoteInterface.getSimpleName() + "-proxy-" + requestId).hashCode();
            }
            if (!optionsCopy.isResultExpected() && !(method.getReturnType().equals(Void.class) || method.getReturnType().equals(Void.TYPE) || method.getReturnType().equals(RFuture.class))) {
                throw new IllegalArgumentException("The noResult option only supports void return value");
            }
            String requestQueueName = getRequestQueueName(syncInterface);
            Long ackTimeout = optionsCopy.getAckTimeoutInMillis();
            RemoteServiceRequest request = new RemoteServiceRequest(executorId, requestId.toString(), method.getName(), remoteService.getMethodSignature(method), args, optionsCopy, System.currentTimeMillis());
            CompletableFuture<RemoteServiceAck> ackFuture;
            if (optionsCopy.isAckExpected()) {
                ackFuture = pollResponse(optionsCopy.getAckTimeoutInMillis(), requestId, false);
            } else {
                ackFuture = null;
            }
            CompletableFuture<RRemoteServiceResponse> responseFuture;
            if (optionsCopy.isResultExpected()) {
                long timeout = remoteService.getTimeout(optionsCopy.getExecutionTimeoutInMillis(), request);
                responseFuture = pollResponse(timeout, requestId, false);
            } else {
                responseFuture = null;
            }
            RemotePromise<Object> result = createResultPromise(optionsCopy, requestId, requestQueueName, ackTimeout);
            CompletableFuture<Boolean> addFuture = remoteService.addAsync(requestQueueName, request, result);
            addFuture.whenComplete((res, e) -> {
                if (e != null) {
                    if (responseFuture != null) {
                        responseFuture.cancel(false);
                    }
                    if (ackFuture != null) {
                        ackFuture.cancel(false);
                    }
                    result.completeExceptionally(e);
                    return;
                }
                if (!res) {
                    result.completeExceptionally(new RedisException("Task hasn't been added"));
                    if (responseFuture != null) {
                        responseFuture.cancel(false);
                    }
                    if (ackFuture != null) {
                        ackFuture.cancel(false);
                    }
                    return;
                }
                if (optionsCopy.isAckExpected()) {
                    ackFuture.whenComplete((ack, ex) -> {
                        if (ex != null) {
                            if (responseFuture != null) {
                                responseFuture.cancel(false);
                            }
                            result.completeExceptionally(ex);
                            return;
                        }
                        if (ack == null) {
                            String ackName = remoteService.getAckName(requestId);
                            CompletionStage<RemoteServiceAck> ackFutureAttempt = tryPollAckAgainAsync(optionsCopy, ackName, requestId);
                            ackFutureAttempt.whenComplete((re, ex2) -> {
                                if (ex2 != null) {
                                    result.completeExceptionally(ex2);
                                    return;
                                }
                                if (re == null) {
                                    Exception exc = new RemoteServiceAckTimeoutException("No ACK response after " + optionsCopy.getAckTimeoutInMillis() + "ms for request: " + requestId);
                                    result.completeExceptionally(exc);
                                    return;
                                }
                                awaitResultAsync(optionsCopy, result, ackName, responseFuture);
                            });
                        } else {
                            awaitResultAsync(optionsCopy, result, responseFuture);
                        }
                    });
                } else {
                    awaitResultAsync(optionsCopy, result, responseFuture);
                }
            });
            return convertResult(result, method.getReturnType());
        }
    };
    return (T) Proxy.newProxyInstance(remoteInterface.getClassLoader(), new Class[] { remoteInterface }, handler);
}
Also used : RedisException(org.redisson.client.RedisException) Method(java.lang.reflect.Method) InvocationHandler(java.lang.reflect.InvocationHandler) RedisException(org.redisson.client.RedisException) RemoteInvocationOptions(org.redisson.api.RemoteInvocationOptions)

Example 13 with RedisException

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

the class LiveObjectInterceptor method intercept.

@RuntimeType
public Object intercept(@Origin Method method, @AllArguments Object[] args, @This Object me, @FieldValue("liveObjectId") Object id, @FieldProxy("liveObjectId") Setter idSetter, @FieldProxy("liveObjectId") Getter idGetter, @FieldValue("liveObjectLiveMap") RMap<String, ?> map, @FieldProxy("liveObjectLiveMap") Setter mapSetter, @FieldProxy("liveObjectLiveMap") Getter mapGetter) throws Throwable {
    if ("setLiveObjectId".equals(method.getName())) {
        if (args[0].getClass().isArray()) {
            throw new UnsupportedOperationException("RId value cannot be an array.");
        }
        // TODO: distributed locking maybe required.
        String idKey = getMapKey(args[0]);
        if (map != null) {
            if (((RedissonObject) map).getRawName().equals(idKey)) {
                return null;
            }
            try {
                map.rename(getMapKey(args[0]));
            } catch (RedisException e) {
                if (e.getMessage() == null || !e.getMessage().startsWith("ERR no such key")) {
                    throw e;
                }
            // key may already renamed by others.
            }
        }
        RMap<Object, Object> liveMap = new RedissonMap<Object, Object>(namingScheme.getCodec(), commandExecutor, idKey, null, null, null);
        mapSetter.setValue(liveMap);
        return null;
    }
    if ("getLiveObjectId".equals(method.getName())) {
        if (map == null) {
            return null;
        }
        return namingScheme.resolveId(((RedissonObject) map).getRawName());
    }
    if ("delete".equals(method.getName())) {
        CommandBatchService ce;
        if (commandExecutor instanceof CommandBatchService) {
            ce = (CommandBatchService) commandExecutor;
        } else {
            ce = new CommandBatchService(commandExecutor);
        }
        Object idd = ((RLiveObject) me).getLiveObjectId();
        RFuture<Long> deleteFuture = service.delete(idd, me.getClass().getSuperclass(), namingScheme, ce);
        ce.execute();
        return deleteFuture.getNow() > 0;
    }
    try {
        return method.invoke(map, args);
    } catch (InvocationTargetException e) {
        throw e.getCause();
    }
}
Also used : RLiveObject(org.redisson.api.RLiveObject) RedisException(org.redisson.client.RedisException) CommandBatchService(org.redisson.command.CommandBatchService) RedissonObject(org.redisson.RedissonObject) RLiveObject(org.redisson.api.RLiveObject) InvocationTargetException(java.lang.reflect.InvocationTargetException) RedissonMap(org.redisson.RedissonMap)

Aggregations

RedisException (org.redisson.client.RedisException)13 CommandBatchService (org.redisson.command.CommandBatchService)5 RFuture (org.redisson.api.RFuture)3 RedisClient (org.redisson.client.RedisClient)3 Future (io.netty.util.concurrent.Future)2 FutureListener (io.netty.util.concurrent.FutureListener)2 ScheduledFuture (io.netty.util.concurrent.ScheduledFuture)2 InvocationHandler (java.lang.reflect.InvocationHandler)2 Method (java.lang.reflect.Method)2 List (java.util.List)2 RBitSetAsync (org.redisson.api.RBitSetAsync)2 RemoteInvocationOptions (org.redisson.api.RemoteInvocationOptions)2 RedisConnection (org.redisson.client.RedisConnection)2 RedisConnectionException (org.redisson.client.RedisConnectionException)2 RPromise (org.redisson.misc.RPromise)2 ReqlError (com.rethinkdb.gen.exc.ReqlError)1 ByteBuf (io.netty.buffer.ByteBuf)1 ChannelFuture (io.netty.channel.ChannelFuture)1 ChannelFutureListener (io.netty.channel.ChannelFutureListener)1 ReplayingDecoder (io.netty.handler.codec.ReplayingDecoder)1